DOMParser.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // FIXME: 1. Let compliantString to the result of invoking the Get Trusted Type compliant string algorithm with TrustedHTML, this's relevant global object, string, "DOMParser parseFromString", and "script".
  35. // 2. Let document be a new Document, whose content type is type and url is this's relevant global object's associated Document's URL.
  36. JS::GCPtr<DOM::Document> document;
  37. // 3. Switch on type:
  38. if (type == Bindings::DOMParserSupportedType::Text_Html) {
  39. // -> "text/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. // 1. Parse HTML from a string given document and compliantString. FIXME: Use compliantString.
  43. document->parse_html_from_a_string(string);
  44. } else {
  45. // -> Otherwise
  46. document = DOM::XMLDocument::create(realm(), verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document().url());
  47. document->set_content_type(Bindings::idl_enum_to_string(type));
  48. document->set_document_type(DOM::Document::Type::XML);
  49. // 1. Create an XML parser parse, associated with document, and with XML scripting support disabled.
  50. XML::Parser parser(string, { .resolve_external_resource = resolve_xml_resource });
  51. XMLDocumentBuilder builder { *document, XMLScriptingSupport::Disabled };
  52. // 2. Parse compliantString using parser. FIXME: Use compliantString.
  53. auto result = parser.parse_with_listener(builder);
  54. // 3. If the previous step resulted in an XML well-formedness or XML namespace well-formedness error, then:
  55. if (result.is_error() || builder.has_error()) {
  56. // NOTE: The XML parsing can produce nodes before it hits an error, just remove them.
  57. // 1. Assert: document has no child nodes.
  58. document->remove_all_children(true);
  59. // 2. Let root be the result of creating an element given document, "parsererror", and "http://www.mozilla.org/newlayout/xml/parsererror.xml".
  60. 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();
  61. // FIXME: 3. Optionally, add attributes or children to root to describe the nature of the parsing error.
  62. // 4. Append root to document.
  63. MUST(document->append_child(*root));
  64. }
  65. }
  66. // 3. Return document.
  67. return *document;
  68. }
  69. }