DOMParser.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <LibWeb/XML/XMLDocumentBuilder.h>
  10. namespace Web::HTML {
  11. DOMParser::DOMParser() = default;
  12. DOMParser::~DOMParser() = default;
  13. // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
  14. NonnullRefPtr<DOM::Document> DOMParser::parse_from_string(String const& string, Bindings::DOMParserSupportedType type)
  15. {
  16. // 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.
  17. // FIXME: Pass in this's relevant global object's associated Document's URL.
  18. auto document = DOM::Document::create();
  19. document->set_content_type(Bindings::idl_enum_to_string(type));
  20. // 2. Switch on type:
  21. if (type == Bindings::DOMParserSupportedType::Text_Html) {
  22. // -> "text/html"
  23. // 1. Set document's type to "html".
  24. document->set_document_type(DOM::Document::Type::HTML);
  25. // 2. Create an HTML parser parser, associated with document.
  26. // 3. Place string into the input stream for parser. The encoding confidence is irrelevant.
  27. // FIXME: We don't have the concept of encoding confidence yet.
  28. auto parser = HTMLParser::create(document, string, "UTF-8");
  29. // 4. Start parser and let it run until it has consumed all the characters just inserted into the input stream.
  30. // FIXME: This is to match the default URL. Instead, pass in this's relevant global object's associated Document's URL.
  31. parser->run("about:blank"sv);
  32. } else {
  33. // -> Otherwise
  34. // 1. Create an XML parser parse, associated with document, and with XML scripting support disabled.
  35. XML::Parser parser(string, { .resolve_external_resource = resolve_xml_resource });
  36. XMLDocumentBuilder builder { document, XMLScriptingSupport::Disabled };
  37. // 2. Parse string using parser.
  38. auto result = parser.parse_with_listener(builder);
  39. // 3. If the previous step resulted in an XML well-formedness or XML namespace well-formedness error, then:
  40. if (result.is_error() || builder.has_error()) {
  41. // NOTE: The XML parsing can produce nodes before it hits an error, just remove them.
  42. // 1. Assert: document has no child nodes.
  43. document->remove_all_children(true);
  44. // 2. Let root be the result of creating an element given document, "parsererror", and "http://www.mozilla.org/newlayout/xml/parsererror.xml".
  45. auto root = DOM::create_element(document, "parsererror", "http://www.mozilla.org/newlayout/xml/parsererror.xml");
  46. // FIXME: 3. Optionally, add attributes or children to root to describe the nature of the parsing error.
  47. // 4. Append root to document.
  48. document->append_child(root);
  49. }
  50. }
  51. // 3. Return document.
  52. return document;
  53. }
  54. }