Ver código fonte

LibWeb: Fully implement the fragment serializing algorithm

Rather than assuming the node's node document is an HTML document,
handle XML documents as well.
Timothy Flynn 2 anos atrás
pai
commit
13b8eeff54

+ 3 - 0
Userland/Libraries/LibWeb/DOM/Document.h

@@ -254,6 +254,9 @@ public:
     Type document_type() const { return m_type; }
     Type document_type() const { return m_type; }
     void set_document_type(Type type) { m_type = 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
     // https://dom.spec.whatwg.org/#xml-document
     bool is_xml_document() const { return m_type == Type::XML; }
     bool is_xml_document() const { return m_type == Type::XML; }
 
 

+ 2 - 2
Userland/Libraries/LibWeb/DOM/Element.cpp

@@ -492,9 +492,9 @@ WebIDL::ExceptionOr<void> Element::set_inner_html(String const& markup)
 }
 }
 
 
 // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
 // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
-String Element::inner_html() const
+WebIDL::ExceptionOr<String> 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
 bool Element::is_focused() const

+ 1 - 1
Userland/Libraries/LibWeb/DOM/Element.h

@@ -117,7 +117,7 @@ public:
 
 
     CSS::CSSStyleDeclaration* style_for_bindings();
     CSS::CSSStyleDeclaration* style_for_bindings();
 
 
-    String inner_html() const;
+    WebIDL::ExceptionOr<String> inner_html() const;
     WebIDL::ExceptionOr<void> set_inner_html(String const&);
     WebIDL::ExceptionOr<void> set_inner_html(String const&);
 
 
     WebIDL::ExceptionOr<void> insert_adjacent_html(String position, String text);
     WebIDL::ExceptionOr<void> insert_adjacent_html(String position, String text);

+ 8 - 6
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
 // https://w3c.github.io/DOM-Parsing/#dfn-fragment-serializing-algorithm
-String Node::serialize_fragment(/* FIXME: Requires well-formed flag */) const
+WebIDL::ExceptionOr<String> 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
 // https://dom.spec.whatwg.org/#dom-node-issamenode

+ 2 - 1
Userland/Libraries/LibWeb/DOM/Node.h

@@ -13,6 +13,7 @@
 #include <AK/TypeCasts.h>
 #include <AK/TypeCasts.h>
 #include <AK/Vector.h>
 #include <AK/Vector.h>
 #include <LibWeb/DOM/EventTarget.h>
 #include <LibWeb/DOM/EventTarget.h>
+#include <LibWeb/DOMParsing/XMLSerializer.h>
 #include <LibWeb/WebIDL/ExceptionOr.h>
 #include <LibWeb/WebIDL/ExceptionOr.h>
 
 
 namespace Web::DOM {
 namespace Web::DOM {
@@ -196,7 +197,7 @@ public:
     i32 id() const { return m_id; }
     i32 id() const { return m_id; }
     static Node* from_id(i32 node_id);
     static Node* from_id(i32 node_id);
 
 
-    String serialize_fragment() const;
+    WebIDL::ExceptionOr<String> serialize_fragment(DOMParsing::RequireWellFormed) const;
 
 
     void replace_all(JS::GCPtr<Node>);
     void replace_all(JS::GCPtr<Node>);
     void string_replace_all(String const&);
     void string_replace_all(String const&);

+ 2 - 2
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
 // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
-String ShadowRoot::inner_html() const
+WebIDL::ExceptionOr<String> 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
 // https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml

+ 1 - 1
Userland/Libraries/LibWeb/DOM/ShadowRoot.h

@@ -28,7 +28,7 @@ public:
     // NOTE: This is intended for the JS bindings.
     // NOTE: This is intended for the JS bindings.
     String mode() const { return m_closed ? "closed" : "open"; }
     String mode() const { return m_closed ? "closed" : "open"; }
 
 
-    String inner_html() const;
+    WebIDL::ExceptionOr<String> inner_html() const;
     WebIDL::ExceptionOr<void> set_inner_html(String const&);
     WebIDL::ExceptionOr<void> set_inner_html(String const&);
 
 
 private:
 private: