DOMParser.cpp 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/Bindings/DOMParserPrototype.h>
  7. #include <LibWeb/Bindings/MainThreadVM.h>
  8. #include <LibWeb/HTML/DOMParser.h>
  9. #include <LibWeb/HTML/HTMLDocument.h>
  10. #include <LibWeb/HTML/Parser/HTMLParser.h>
  11. #include <LibWeb/HTML/Scripting/Environments.h>
  12. #include <LibWeb/HTML/Window.h>
  13. #include <LibWeb/XML/XMLDocumentBuilder.h>
  14. namespace Web::HTML {
  15. WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMParser>> DOMParser::construct_impl(JS::Realm& realm)
  16. {
  17. return realm.heap().allocate<DOMParser>(realm, realm);
  18. }
  19. DOMParser::DOMParser(JS::Realm& realm)
  20. : PlatformObject(realm)
  21. {
  22. }
  23. DOMParser::~DOMParser() = default;
  24. void DOMParser::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMParserPrototype>(realm, "DOMParser"));
  28. }
  29. // https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#dom-domparser-parsefromstring
  30. JS::NonnullGCPtr<DOM::Document> DOMParser::parse_from_string(StringView string, Bindings::DOMParserSupportedType type)
  31. {
  32. // 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.
  33. JS::GCPtr<DOM::Document> document;
  34. // 2. Switch on type:
  35. if (type == Bindings::DOMParserSupportedType::Text_Html) {
  36. // -> "text/html"
  37. // 1. Set document's type to "html".
  38. document = HTML::HTMLDocument::create(realm(), verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document().url());
  39. document->set_content_type(Bindings::idl_enum_to_string(type));
  40. document->set_document_type(DOM::Document::Type::HTML);
  41. // 2. Create an HTML parser parser, associated with document.
  42. // 3. Place string into the input stream for parser. The encoding confidence is irrelevant.
  43. // FIXME: We don't have the concept of encoding confidence yet.
  44. auto parser = HTMLParser::create(*document, string, "UTF-8");
  45. // 4. Start parser and let it run until it has consumed all the characters just inserted into the input stream.
  46. // FIXME: This is to match the default URL. Instead, pass in this's relevant global object's associated Document's URL.
  47. parser->run("about:blank"sv);
  48. } else {
  49. // -> Otherwise
  50. document = DOM::Document::create(realm(), verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document().url());
  51. document->set_content_type(Bindings::idl_enum_to_string(type));
  52. // 1. Create an XML parser parse, associated with document, and with XML scripting support disabled.
  53. XML::Parser parser(string, { .resolve_external_resource = resolve_xml_resource });
  54. XMLDocumentBuilder builder { *document, XMLScriptingSupport::Disabled };
  55. // 2. Parse string using parser.
  56. auto result = parser.parse_with_listener(builder);
  57. // 3. If the previous step resulted in an XML well-formedness or XML namespace well-formedness error, then:
  58. if (result.is_error() || builder.has_error()) {
  59. // NOTE: The XML parsing can produce nodes before it hits an error, just remove them.
  60. // 1. Assert: document has no child nodes.
  61. document->remove_all_children(true);
  62. // 2. Let root be the result of creating an element given document, "parsererror", and "http://www.mozilla.org/newlayout/xml/parsererror.xml".
  63. auto root = DOM::create_element(*document, "parsererror"_fly_string, "http://www.mozilla.org/newlayout/xml/parsererror.xml").release_value_but_fixme_should_propagate_errors();
  64. // FIXME: 3. Optionally, add attributes or children to root to describe the nature of the parsing error.
  65. // 4. Append root to document.
  66. MUST(document->append_child(*root));
  67. }
  68. }
  69. // 3. Return document.
  70. return *document;
  71. }
  72. }