1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:58:11 +00:00

LibWeb: Load X(HT)ML documents and transform them into HTML DOM

This commit is contained in:
Ali Mohammad Pur 2022-03-28 16:25:17 +04:30 committed by Andreas Kling
parent c1649e3372
commit 5a0123fd2f
9 changed files with 347 additions and 29 deletions

View file

@ -7,6 +7,7 @@
#include <LibWeb/Bindings/DOMParserWrapper.h>
#include <LibWeb/HTML/DOMParser.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/XML/XMLDocumentBuilder.h>
namespace Web::HTML {
@ -36,16 +37,23 @@ NonnullRefPtr<DOM::Document> DOMParser::parse_from_string(String const& string,
parser->run("about:blank");
} else {
// -> Otherwise
// FIXME: 1. Create an XML parser parse, associated with document, and with XML scripting support disabled.
// 2. Parse string using parser.
// 3. If the previous step resulted in an XML well-formedness or XML namespace well-formedness error, then:
// 1. Assert: document has no child nodes.
// 2. Let root be the result of creating an element given document, "parsererror", and "http://www.mozilla.org/newlayout/xml/parsererror.xml".
// 3. Optionally, add attributes or children to root to describe the nature of the parsing error.
// 4. Append root to document.
dbgln("DOMParser::parse_from_string: Unimplemented parser for type: {}", Bindings::idl_enum_to_string(type));
TODO();
// 1. Create an XML parser parse, associated with document, and with XML scripting support disabled.
XML::Parser parser(string, { .resolve_external_resource = resolve_xml_resource });
XMLDocumentBuilder builder { document, XMLScriptingSupport::Disabled };
// 2. Parse string using parser.
auto result = parser.parse_with_listener(builder);
// 3. If the previous step resulted in an XML well-formedness or XML namespace well-formedness error, then:
if (result.is_error() || builder.has_error()) {
// NOTE: The XML parsing can produce nodes before it hits an error, just remove them.
// 1. Assert: document has no child nodes.
document->remove_all_children(true);
// 2. Let root be the result of creating an element given document, "parsererror", and "http://www.mozilla.org/newlayout/xml/parsererror.xml".
auto root = DOM::create_element(document, "parsererror", "http://www.mozilla.org/newlayout/xml/parsererror.xml");
// FIXME: 3. Optionally, add attributes or children to root to describe the nature of the parsing error.
// 4. Append root to document.
document->append_child(root);
}
}
// 3. Return document.