Truly independent web browser
LibWeb: Apply changed SVG `clip-path`, `mask` and pattern references
Tim Ledbetter5a0747c10895
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex c44ad046b7a..2ee0032bbbf 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -5594,8 +5594,11 @@ void Element::replace_style_record(CSS::StyleRecordID style_record_identity)CSS::record_waiting_children_may_be_revealed(*this);// The resources an SVG graphics element's `mask`, `clip-path`, `fill` and `stroke` name are published beside its// attributes for the layout tree build to resolve, so they follow every record the element takes.- if (is<SVG::SVGGraphicsElement>(*this) && !!style_record_identity)+ if (is<SVG::SVGGraphicsElement>(*this) && !!style_record_identity) {Layout::publish_svg_style_references(*this);+ if (held_before && Layout::svg_style_references_differ(held_before, m_installed_style))+ set_needs_layout_tree_rebuild(read, SetNeedsLayoutTreeUpdateReason::SVGResourceReferenceChange, CSS::LayoutTreeRebuildRoot::Self);+ }if (timeline_declarations_changed)document().did_change_timeline_declarations();}diff --git a/Libraries/LibWeb/DOM/Node.h b/Libraries/LibWeb/DOM/Node.hindex 60506085f71..04907ab7e05 100644--- a/Libraries/LibWeb/DOM/Node.h+++ b/Libraries/LibWeb/DOM/Node.h@@ -136,6 +136,7 @@ enum class SetNeedsLayoutReason {X(StyleChange) \X(SVGResourceContentChange) \X(SVGResourceElementRemoved) \+ X(SVGResourceReferenceChange) \X(TopLayerMembershipChange) \X(WritingSuggestionChange)diff --git a/Libraries/LibWeb/Layout/LayoutRustBridge.cpp b/Libraries/LibWeb/Layout/LayoutRustBridge.cppindex 174adeeae84..76c2d7cc362 100644--- a/Libraries/LibWeb/Layout/LayoutRustBridge.cpp+++ b/Libraries/LibWeb/Layout/LayoutRustBridge.cpp@@ -14,6 +14,7 @@#include <LibUnicode/CharacterTypes.h>#include <LibWeb/CSS/ComputedValues.h>#include <LibWeb/CSS/Display.h>+#include <LibWeb/CSS/InstalledStyle.h>#include <LibWeb/CSS/LengthBox.h>#include <LibWeb/CSS/Parser/Parser.h>#include <LibWeb/CSS/StyleComputer.h>@@ -143,11 +144,9 @@ static CSS::StyleAtomID svg_style_reference_fragment_atom(DOM::Element& element,// The four resources `mask`, `clip-path`, `fill` and `stroke` name. Read from the record's group payloads rather than// through a materialized view: this runs for every SVG graphics element whose style record is replaced, and pinning a// record to look at four properties is most of the cost of looking at them.-static Array<CSS::StyleAtomID, 4> svg_style_reference_atoms(DOM::Element& element)+static Array<Optional<CSS::URL>, 4> svg_style_reference_urls(void const* record_payloads){- if (!is<SVG::SVGGraphicsElement>(element))- return {};- auto const* payloads = static_cast<void const* const*>(element.style_record_payloads());+ auto const* payloads = static_cast<void const* const*>(record_payloads);if (!payloads)return {};auto const* mask_payload = static_cast<CSS::ComputedValues::MaskValues const*>(payloads[CSS::ComputedValues::MaskValues::style_group_index]);@@ -155,13 +154,46 @@ static Array<CSS::StyleAtomID, 4> svg_style_reference_atoms(DOM::Element& elemenif (!mask_payload || !svg_payload)return {};return {- svg_style_reference_fragment_atom(element, mask_payload->mask_url_value()),- svg_style_reference_fragment_atom(element, mask_payload->clip_path_value()),- svg_style_reference_fragment_atom(element, svg_payload->fill_url_value()),- svg_style_reference_fragment_atom(element, svg_payload->stroke_url_value()),+ mask_payload->mask_url_value(),+ mask_payload->clip_path_value(),+ svg_payload->fill_url_value(),+ svg_payload->stroke_url_value(),};}+static Array<CSS::StyleAtomID, 4> svg_style_reference_atoms(DOM::Element& element)+{+ if (!is<SVG::SVGGraphicsElement>(element))+ return {};+ auto urls = svg_style_reference_urls(element.style_record_payloads());+ return {+ svg_style_reference_fragment_atom(element, urls[0]),+ svg_style_reference_fragment_atom(element, urls[1]),+ svg_style_reference_fragment_atom(element, urls[2]),+ svg_style_reference_fragment_atom(element, urls[3]),+ };+}++bool svg_style_references_differ(CSS::InstalledStyle const& old_style, CSS::InstalledStyle const& new_style)+{+ auto const* old_payloads = static_cast<void const* const*>(old_style.payloads());+ auto const* new_payloads = static_cast<void const* const*>(new_style.payloads());+ if (old_payloads && new_payloads+ && old_payloads[CSS::ComputedValues::MaskValues::style_group_index] == new_payloads[CSS::ComputedValues::MaskValues::style_group_index]+ && old_payloads[CSS::ComputedValues::InheritedSVGValues::style_group_index] == new_payloads[CSS::ComputedValues::InheritedSVGValues::style_group_index])+ return false;++ auto old_urls = svg_style_reference_urls(old_payloads);+ auto new_urls = svg_style_reference_urls(new_payloads);+ for (size_t i = 0; i < old_urls.size(); ++i) {+ if (old_urls[i].has_value() != new_urls[i].has_value())+ return true;+ if (old_urls[i].has_value() && old_urls[i]->url() != new_urls[i]->url())+ return true;+ }+ return false;+}+RustFFI::FfiSvgAttributeFacts svg_geometry_facts(DOM::Element const& element){RustFFI::FfiSvgAttributeFacts facts {};diff --git a/Libraries/LibWeb/Layout/LayoutRustBridge.h b/Libraries/LibWeb/Layout/LayoutRustBridge.hindex 762dd6528a4..cfed58ffd6e 100644--- a/Libraries/LibWeb/Layout/LayoutRustBridge.h+++ b/Libraries/LibWeb/Layout/LayoutRustBridge.h@@ -22,6 +22,7 @@namespace Web::CSS {+class InstalledStyle;class LengthPercentage;class LengthPercentageOrAuto;class Size;@@ -40,6 +41,8 @@ void publish_svg_attribute_facts(DOM::Element&);void publish_math_attribute_facts(DOM::Element const&);void publish_svg_style_references(DOM::Element&);+bool svg_style_references_differ(CSS::InstalledStyle const&, CSS::InstalledStyle const&);+// Which shape an SVG geometry element draws, and the endpoints a <line> parses. The other fields keep their defaults.// Layout reads these from the published facts, and the DOM's geometry queries pass them to the same shape builders.RustFFI::FfiSvgAttributeFacts svg_geometry_facts(DOM::Element const&);diff --git a/Tests/LibWeb/Ref/input/svg/mask-and-pattern-reference-change.html b/Tests/LibWeb/Ref/input/svg/mask-and-pattern-reference-change.htmlnew file mode 100644index 00000000000..a1578f8a42e--- /dev/null+++ b/Tests/LibWeb/Ref/input/svg/mask-and-pattern-reference-change.html@@ -0,0 +1,34 @@+<!DOCTYPE html>+<html class="reftest-wait">+<link rel="match" href="../../expected/svg/green-square-100-ref.html" />+<svg width="100" height="100">+ <mask id="hide">+ <rect width="100" height="100" fill="black" />+ </mask>+ <mask id="show">+ <rect width="100" height="100" fill="white" />+ </mask>+ <pattern id="red" patternUnits="userSpaceOnUse" width="100" height="100">+ <rect width="100" height="100" fill="red" />+ </pattern>+ <pattern id="green" patternUnits="userSpaceOnUse" width="100" height="100">+ <rect width="100" height="100" fill="green" />+ </pattern>+ <rect width="100" height="100" fill="red" />+ <rect id="masked" width="100" height="33" fill="green" mask="url(#hide)" />+ <rect id="filled" y="33" width="100" height="33" fill="url(#red)" />+ <line id="stroked" x1="0" y1="83" x2="100" y2="83" stroke-width="34" stroke="url(#red)" />+</svg>+<script>+ requestAnimationFrame(() => {+ requestAnimationFrame(() => {+ document.getElementById("masked").setAttribute("mask", "url(#show)");+ document.getElementById("filled").setAttribute("fill", "url(#green)");+ document.getElementById("stroked").setAttribute("stroke", "url(#green)");+ requestAnimationFrame(() => {+ document.documentElement.classList.remove("reftest-wait");+ });+ });+ });+</script>+</html>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-masking/clip-path/clip-path-url-reference-change-from-empty.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-masking/clip-path/clip-path-url-reference-change-from-empty.htmlnew file mode 100644index 00000000000..8e5c9cb6ec6--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-masking/clip-path/clip-path-url-reference-change-from-empty.html@@ -0,0 +1,23 @@+<!DOCTYPE html>+<html class="reftest-wait">+<title>Switch from an empty to a non-empty clip-path url()</title>+<link rel="help" href="http://www.w3.org/TR/css-masking-1/#clipping-paths">+<link rel="help" href="http://www.w3.org/TR/css-masking-1/#the-clip-path">+<link rel="match" href="../../../../../expected/wpt-import/css/css-masking/clip-path/reference/green-100x100.html">+<script src="../../../common/reftest-wait.js"></script>+<svg>+ <clipPath id="empty"/>+ <clipPath id="rect">+ <rect width="100" height="100"/>+ </clipPath>+ <rect width="100" height="100" fill="red"/>+ <rect width="100" height="100" fill="green" id="target" clip-path="url(#empty)"/>+</svg>+<script>+ requestAnimationFrame(() => {+ requestAnimationFrame(() => {+ document.getElementById('target').setAttribute('clip-path', 'url(#rect)');+ takeScreenshot();+ });+ });+</script>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-masking/clip-path/clip-path-url-reference-change.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-masking/clip-path/clip-path-url-reference-change.htmlnew file mode 100644index 00000000000..8ac6be228f3--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-masking/clip-path/clip-path-url-reference-change.html@@ -0,0 +1,25 @@+<!DOCTYPE html>+<html class="reftest-wait">+<title>Switch from one clip-path url() to another with the same bounds</title>+<link rel="help" href="http://www.w3.org/TR/css-masking-1/#clipping-paths">+<link rel="help" href="http://www.w3.org/TR/css-masking-1/#the-clip-path">+<link rel="match" href="../../../../../expected/wpt-import/css/css-masking/clip-path/reference/green-100x100.html">+<script src="../../../common/reftest-wait.js"></script>+<svg>+ <clipPath id="circle">+ <circle cx="50" cy="50" r="50"/>+ </clipPath>+ <clipPath id="rect">+ <rect width="100" height="100"/>+ </clipPath>+ <rect width="100" height="100" fill="red"/>+ <rect width="100" height="100" fill="green" id="target" clip-path="url(#circle)"/>+</svg>+<script>+ requestAnimationFrame(() => {+ requestAnimationFrame(() => {+ document.getElementById('target').setAttribute('clip-path', 'url(#rect)');+ takeScreenshot();+ });+ });+</script>