1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

LibWeb: Fully implement the fragment serializing algorithm

Rather than assuming the node's node document is an HTML document,
handle XML documents as well.
This commit is contained in:
Timothy Flynn 2022-11-02 19:14:27 -04:00 committed by Linus Groh
parent e1ac9c83b2
commit 13b8eeff54
7 changed files with 19 additions and 13 deletions

View file

@ -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; }

View file

@ -492,9 +492,9 @@ WebIDL::ExceptionOr<void> Element::set_inner_html(String const& markup)
}
// 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

View file

@ -117,7 +117,7 @@ public:
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> insert_adjacent_html(String position, String text);

View file

@ -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<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

View file

@ -13,6 +13,7 @@
#include <AK/TypeCasts.h>
#include <AK/Vector.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/DOMParsing/XMLSerializer.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
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<String> serialize_fragment(DOMParsing::RequireWellFormed) const;
void replace_all(JS::GCPtr<Node>);
void string_replace_all(String const&);

View file

@ -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<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

View file

@ -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<String> inner_html() const;
WebIDL::ExceptionOr<void> set_inner_html(String const&);
private: