From 13b8eeff54d275c99d9595f40a0ace178f82ece7 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Wed, 2 Nov 2022 19:14:27 -0400 Subject: [PATCH] LibWeb: Fully implement the fragment serializing algorithm Rather than assuming the node's node document is an HTML document, handle XML documents as well. --- Userland/Libraries/LibWeb/DOM/Document.h | 3 +++ Userland/Libraries/LibWeb/DOM/Element.cpp | 4 ++-- Userland/Libraries/LibWeb/DOM/Element.h | 2 +- Userland/Libraries/LibWeb/DOM/Node.cpp | 14 ++++++++------ Userland/Libraries/LibWeb/DOM/Node.h | 3 ++- Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp | 4 ++-- Userland/Libraries/LibWeb/DOM/ShadowRoot.h | 2 +- 7 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index b1b48c910c..2d0aa84c8f 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -254,6 +254,9 @@ public: Type document_type() const { return m_type; } void set_document_type(Type type) { m_type = type; } + // https://dom.spec.whatwg.org/#html-document + bool is_html_document() const { return m_type == Type::HTML; } + // https://dom.spec.whatwg.org/#xml-document bool is_xml_document() const { return m_type == Type::XML; } diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 50457f081d..d041b4c841 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -492,9 +492,9 @@ WebIDL::ExceptionOr Element::set_inner_html(String const& markup) } // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml -String Element::inner_html() const +WebIDL::ExceptionOr Element::inner_html() const { - return serialize_fragment(/* FIXME: Providing true for the require well-formed flag (which may throw) */); + return serialize_fragment(DOMParsing::RequireWellFormed::Yes); } bool Element::is_focused() const diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 2af1e0e094..e737105c6a 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -117,7 +117,7 @@ public: CSS::CSSStyleDeclaration* style_for_bindings(); - String inner_html() const; + WebIDL::ExceptionOr inner_html() const; WebIDL::ExceptionOr set_inner_html(String const&); WebIDL::ExceptionOr insert_adjacent_html(String position, String text); diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index 54ff241352..57a756a109 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -1173,15 +1173,17 @@ void Node::string_replace_all(String const& string) } // https://w3c.github.io/DOM-Parsing/#dfn-fragment-serializing-algorithm -String Node::serialize_fragment(/* FIXME: Requires well-formed flag */) const +WebIDL::ExceptionOr Node::serialize_fragment(DOMParsing::RequireWellFormed require_well_formed) const { - // FIXME: 1. Let context document be the value of node's node document. + // 1. Let context document be the value of node's node document. + auto const& context_document = document(); - // FIXME: 2. If context document is an HTML document, return an HTML serialization of node. - // (We currently always do this) - return HTML::HTMLParser::serialize_html_fragment(*this); + // 2. If context document is an HTML document, return an HTML serialization of node. + if (context_document.is_html_document()) + return HTML::HTMLParser::serialize_html_fragment(*this); - // FIXME: 3. Otherwise, context document is an XML document; return an XML serialization of node passing the flag require well-formed. + // 3. Otherwise, context document is an XML document; return an XML serialization of node passing the flag require well-formed. + return DOMParsing::serialize_node_to_xml_string(*this, require_well_formed); } // https://dom.spec.whatwg.org/#dom-node-issamenode diff --git a/Userland/Libraries/LibWeb/DOM/Node.h b/Userland/Libraries/LibWeb/DOM/Node.h index 2c5ec5e429..7e0dd8ec0b 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.h +++ b/Userland/Libraries/LibWeb/DOM/Node.h @@ -13,6 +13,7 @@ #include #include #include +#include #include namespace Web::DOM { @@ -196,7 +197,7 @@ public: i32 id() const { return m_id; } static Node* from_id(i32 node_id); - String serialize_fragment() const; + WebIDL::ExceptionOr serialize_fragment(DOMParsing::RequireWellFormed) const; void replace_all(JS::GCPtr); void string_replace_all(String const&); diff --git a/Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp b/Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp index 19ba81c54f..83d8349428 100644 --- a/Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp +++ b/Userland/Libraries/LibWeb/DOM/ShadowRoot.cpp @@ -31,9 +31,9 @@ EventTarget* ShadowRoot::get_parent(Event const& event) } // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml -String ShadowRoot::inner_html() const +WebIDL::ExceptionOr ShadowRoot::inner_html() const { - return serialize_fragment(/* FIXME: Providing true for the require well-formed flag (which may throw) */); + return serialize_fragment(DOMParsing::RequireWellFormed::Yes); } // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml diff --git a/Userland/Libraries/LibWeb/DOM/ShadowRoot.h b/Userland/Libraries/LibWeb/DOM/ShadowRoot.h index f15fe502fc..40b617dab2 100644 --- a/Userland/Libraries/LibWeb/DOM/ShadowRoot.h +++ b/Userland/Libraries/LibWeb/DOM/ShadowRoot.h @@ -28,7 +28,7 @@ public: // NOTE: This is intended for the JS bindings. String mode() const { return m_closed ? "closed" : "open"; } - String inner_html() const; + WebIDL::ExceptionOr inner_html() const; WebIDL::ExceptionOr set_inner_html(String const&); private: