DOMParser.cpp 3.7 KB

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