DOMParser.cpp 3.5 KB

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