DOMParser.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/DOMParserWrapper.h>
  7. #include <LibWeb/HTML/DOMParser.h>
  8. #include <LibWeb/HTML/Parser/HTMLParser.h>
  9. namespace Web::HTML {
  10. DOMParser::DOMParser()
  11. {
  12. }
  13. DOMParser::~DOMParser()
  14. {
  15. }
  16. // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
  17. NonnullRefPtr<DOM::Document> DOMParser::parse_from_string(String const& string, Bindings::DOMParserSupportedType type)
  18. {
  19. // 1. Let document be a new Document, whose content type is type and url is this's relevant global object's associated Document's URL.
  20. // FIXME: Pass in this's relevant global object's associated Document's URL.
  21. auto document = DOM::Document::create();
  22. document->set_content_type(Bindings::idl_enum_to_string(type));
  23. // 2. Switch on type:
  24. if (type == Bindings::DOMParserSupportedType::Text_Html) {
  25. // -> "text/html"
  26. // FIXME: 1. Set document's type to "html".
  27. // 2. Create an HTML parser parser, associated with document.
  28. // 3. Place string into the input stream for parser. The encoding confidence is irrelevant.
  29. // FIXME: We don't have the concept of encoding confidence yet.
  30. auto parser = HTMLParser::create(document, string, "UTF-8");
  31. // 4. Start parser and let it run until it has consumed all the characters just inserted into the input stream.
  32. // FIXME: This is to match the default URL. Instead, pass in this's relevant global object's associated Document's URL.
  33. parser->run("about:blank");
  34. } else {
  35. // -> Otherwise
  36. // FIXME: 1. Create an XML parser parse, associated with document, and with XML scripting support disabled.
  37. // 2. Parse string using parser.
  38. // 3. If the previous step resulted in an XML well-formedness or XML namespace well-formedness error, then:
  39. // 1. Assert: document has no child nodes.
  40. // 2. Let root be the result of creating an element given document, "parsererror", and "http://www.mozilla.org/newlayout/xml/parsererror.xml".
  41. // 3. Optionally, add attributes or children to root to describe the nature of the parsing error.
  42. // 4. Append root to document.
  43. dbgln("DOMParser::parse_from_string: Unimplemented parser for type: {}", Bindings::idl_enum_to_string(type));
  44. TODO();
  45. }
  46. // 3. Return document.
  47. return document;
  48. }
  49. }