DOMParser.cpp 3.8 KB

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