DOMParser.cpp 4.0 KB

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