Skip to content

Commit

Permalink
LibWeb: Make innerHTML for XML nodes actually return inner HTML
Browse files Browse the repository at this point in the history
The spec says to just call the XML serialization algorithm, but it
returns the "outer serialization", and we need the "inner" one. Let's
just concatenate serializations of children; then the result produced is
similar to one from Blink or Gecko.
  • Loading branch information
sppmacd authored and alimpfard committed Jul 14, 2024
1 parent cad3b08 commit 9eb568e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions Tests/LibWeb/Text/expected/HTML/get-innerHTML-xml.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PASS
10 changes: 10 additions & 0 deletions Tests/LibWeb/Text/input/HTML/get-innerHTML-xml.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
test(() => {
// We need an actual XML document to test this.
const svgstr = `<svg xmlns="http://www.w3.org/2000/svg">PASS</svg>`;
const svg = new DOMParser().parseFromString(svgstr, "image/svg+xml").documentElement;
println(svg.innerHTML);
});
</script>
11 changes: 11 additions & 0 deletions Userland/Libraries/LibWeb/DOM/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1490,6 +1490,17 @@ WebIDL::ExceptionOr<String> Node::serialize_fragment(DOMParsing::RequireWellForm
return HTML::HTMLParser::serialize_html_fragment(*this, HTML::HTMLParser::SerializableShadowRoots::No, {}, fragment_serialization_mode);

// 3. Return the XML serialization of node given require well-formed.
// AD-HOC: XML serialization algorithm returns the "outer" XML serialization of the node.
// For inner, concatenate the serialization of all children.
if (fragment_serialization_mode == FragmentSerializationMode::Inner) {
StringBuilder markup;
for_each_child([&markup, require_well_formed](auto& child) {
auto child_markup = DOMParsing::serialize_node_to_xml_string(child, require_well_formed).release_value_but_fixme_should_propagate_errors();
markup.append(child_markup.bytes_as_string_view());
return IterationDecision::Continue;
});
return MUST(markup.to_string());
}
return DOMParsing::serialize_node_to_xml_string(*this, require_well_formed);
}

Expand Down

0 comments on commit 9eb568e

Please sign in to comment.