dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / xml / xml_fragment_parser.lucb
#==============================================================================================
#
# xml_fragment_parser - XMLFragmentParser: the XML fragment parsing algorithm
#
# DESCRIPTION:
# Port of LibWeb/XML/XMLFragmentParser.cpp (lines 1-113) with XMLFragmentParser.h:
# the markup is parsed as the children of a start tag of the context element that
# declares every namespace prefix in scope there, into a new XML document, with XML
# scripting support disabled (Element::parse_fragment of an element in an XML
# document: innerHTML, outerHTML and insertAdjacentHTML).
#
# Copyright (c) 2025, mikiubo <michele.uboldi@gmail.com>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from XMLFragmentParser.cpp:18 Web::XMLFragmentParser::parse_xml_fragment.
# https://html.spec.whatwg.org/multipage/xhtml.html#parsing-xhtml-fragments
pub func xml_fragment_parser_parse_xml_fragment(context: DomElement*, input: ak.StringView) -> ak.Vector[DomNode*?]!:
# 1. Create a new XML parser.
# NB: The feed will be used to create the parser below
var feed = ak.string_builder_new()
var qualified_name_builder = ak.string_builder_new()
if let prefix = dom_element_prefix(context):
if not ak.fly_string_is_empty(&prefix):
ak.string_builder_append_string_view(&qualified_name_builder, ak.fly_string_bytes_as_string_view(&prefix))
ak.string_builder_append_u8(&qualified_name_builder, ':')
let local_name = dom_element_local_name(context)
ak.string_builder_append_string_view(&qualified_name_builder, ak.fly_string_bytes_as_string_view(&local_name))
let qualified_name = ak.string_builder_string_view(&qualified_name_builder)
# 2. Feed the parser just created the string corresponding to the start tag of context,
ak.string_builder_append_u8(&feed, '<')
ak.string_builder_append_string_view(&feed, qualified_name)
# declaring all the namespace prefixes that are in scope on that element in the DOM,
let in_scope_prefixes = dom_node_get_in_scope_prefixes(context.dom_node())
for prefix in ak.vector_span_const(&in_scope_prefixes):
# NB: Skipping the empty prefix because it is handled specially
# and the "xmlns" prefix because it is illegal to declare.
let prefix_view = ak.fly_string_bytes_as_string_view(&prefix)
if ak.fly_string_is_empty(&prefix) or ak.string_view_equals(&prefix_view, "xmlns"):
continue
let namespace_uri = dom_node_lookup_namespace_uri(context.dom_node(), ak.fly_string_to_string_const(&prefix)) else trap("VERIFY: lookup_namespace_uri(prefix).has_value()")
assert(not ak.string_is_empty(&namespace_uri))
ak.string_builder_append(&feed, " xmlns:")
ak.string_builder_append_string_view(&feed, ak.fly_string_bytes_as_string_view(&prefix))
ak.string_builder_append(&feed, "=\"")
ak.string_builder_append_string_view(&feed, ak.string_bytes_as_string_view(&namespace_uri))
ak.string_builder_append_u8(&feed, '"')
# as well as declaring the default namespace (if any) that is in scope on that element in the DOM.
if let default_namespace = dom_node_locate_a_namespace(context.dom_node(), none):
if not ak.string_is_empty(&default_namespace):
ak.string_builder_append(&feed, " xmlns=\"")
ak.string_builder_append_string_view(&feed, ak.string_bytes_as_string_view(&default_namespace))
ak.string_builder_append_u8(&feed, '"')
# A namespace prefix is in scope if the DOM lookupNamespaceURI() method on the element would return a non-null value for that prefix.
# The default namespace is the namespace for which the DOM isDefaultNamespace() method on the element would return true.
ak.string_builder_append_u8(&feed, '>')
# 3. Feed the parser just created the string input.
ak.string_builder_append_string_view(&feed, input)
# 4. Feed the parser just created the string corresponding to the end tag of context.
ak.string_builder_append(&feed, "</")
ak.string_builder_append_string_view(&feed, qualified_name)
ak.string_builder_append(&feed, ">")
let realm = dom_element_realm(context)
let document = dom_document_create(realm, web_url.about_blank())
dom_document_set_document_type(document, DocumentDomDocumentType.xml)
var parser = xml_parser_create(ak.string_builder_string_view(&feed), XmlParserOptions(resolve_named_html_entity = resolve_named_html_entity))
var builder = XmlDocumentBuilder(vtable = &xml_document_builder_vtable, m_document = document)
xml_document_builder_construct(&builder, document, XmlScriptingSupport.disabled)
let result = xml_parser_parse_with_listener(&parser, &builder)
# 5. If there is an XML well-formedness or XML namespace well-formedness error, then throw a "SyntaxError" DOMException.
if let parse_error = result:
let message = xml_parse_error_to_string(&parse_error)
try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_utf8_string(&message))))
let doc_element = dom_document_document_element(document) else trap("VERIFY: the document element")
# 6. If the document element of the resulting Document has any sibling nodes, then throw a "SyntaxError" DOMException.
if tree_node_previous_sibling(doc_element.dom_node()) != none or tree_node_next_sibling(doc_element.dom_node()) != none:
try idl_throw(.idl_dom_exception(idl_syntax_error_create(realm, ak.utf16_string_from_utf8_string_view(ak.sv("Document element has sibling nodes")))))
# 7. Return the resulting Document node's document element's children, in tree order.
var result_nodes: ak.Vector[DomNode*?]
for child in dom_node_children(doc_element.dom_node()):
ak.vector_append_t(&result_nodes, child)
return result_nodes