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