Skip to content

Truly independent web browser

LibWeb: Give no box to a foreignObject in a never-rendered subtree

Aliaksandr Kalenik4f35f7fc387c
diff --git a/Libraries/LibWeb/CSS/StyleEngineInput.cpp b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
index 960c30aec7a..f78f119cd23 100644
--- a/Libraries/LibWeb/CSS/StyleEngineInput.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineInput.cpp
@@ -2130,12 +2130,13 @@ void record_element_moved(DOM::Element& element, DOM::Node* old_parent, DOM::Ele
// rather than computed values. Republish them for the moved subtree from its new place.
Invalidation::invalidate_style_after_language_change(element);
- // Whether an SVG element's position must become static depends on the SVG and
- // foreignObject elements above it. A preserved move changes that chain without giving
- // the moved subtree another arrival notification.
+ // Whether an SVG element's position must become static, and whether it gets a box at all, depends on the
+ // elements above it. A preserved move changes that chain without giving the moved subtree another arrival
+ // notification.
element.for_each_shadow_including_inclusive_descendant([&](auto& node) {
if (auto* descendant = as_if<DOM::Element>(node); descendant && descendant->namespace_uri() == Namespace::SVG && descendant->style_node_id() != no_style_node) {
StyleEngineFFI::style_engine_set_element_adjustment_facts(style_engine->host(), descendant->style_node_id(), element_style_adjustment_facts(*descendant));
+ StyleEngineFFI::style_engine_set_element_box_kind(style_engine->host(), descendant->style_node_id(), to_underlying(box_kind_for_layout(*descendant)));
style_engine->record_derived_element_style_input_change(descendant->style_node_id(), StyleEngine::RecomputeStyle);
}
// A table cell's hints come from the table it is now under.
diff --git a/Libraries/LibWeb/SVG/SVGElement.cpp b/Libraries/LibWeb/SVG/SVGElement.cpp
index d9df6fecb7c..037d9314c3a 100644
--- a/Libraries/LibWeb/SVG/SVGElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGElement.cpp
@@ -121,7 +121,8 @@ bool SVGElement::is_focusable() const
bool SVGElement::is_in_never_rendered_subtree() const
{
- for (auto element = this; element; element = as_if<SVGElement>(element->parent_element().ptr())) {
+ // NB: A use-element shadow tree is rendered where its host is, so the walk continues at the host.
+ for (auto element = this; element; element = as_if<SVGElement>(element->parent_or_shadow_host_element())) {
if (is_never_rendered_element(*element))
return true;
}
diff --git a/Libraries/LibWeb/SVG/SVGForeignObjectElement.cpp b/Libraries/LibWeb/SVG/SVGForeignObjectElement.cpp
index cd0f9c3be10..65aa7fccdcb 100644
--- a/Libraries/LibWeb/SVG/SVGForeignObjectElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGForeignObjectElement.cpp
@@ -29,6 +29,10 @@ void SVGForeignObjectElement::initialize_element()
CSS::ElementBoxKind SVGForeignObjectElement::box_kind() const
{
+ // NB: A mask or a pattern renders its content only where it is referenced, and foreign content is not part of
+ // that rendering. Thus a foreignObject in a never-rendered subtree gets no box.
+ if (is_in_never_rendered_subtree())
+ return CSS::ElementBoxKind::NoBox;
return CSS::ElementBoxKind::SvgForeignObject;
}
diff --git a/Tests/LibWeb/Crash/wpt-import/svg/extensibility/foreignObject/foreign-object-under-clip-path-crash.html b/Tests/LibWeb/Crash/wpt-import/svg/extensibility/foreignObject/foreign-object-under-clip-path-crash.html
new file mode 100644
index 00000000000..afc2931075a
--- /dev/null
+++ b/Tests/LibWeb/Crash/wpt-import/svg/extensibility/foreignObject/foreign-object-under-clip-path-crash.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<title>Should not crash on foreignObject under clipPath</title>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script>test(()=>{})</script>
+<svg>
+ <clipPath>
+ <foreignObject>ABC</foreignObject>
+ </clipPath>
+</svg>
diff --git a/Tests/LibWeb/Crash/wpt-import/svg/extensibility/foreignObject/foreign-object-under-defs-crash.html b/Tests/LibWeb/Crash/wpt-import/svg/extensibility/foreignObject/foreign-object-under-defs-crash.html
new file mode 100644
index 00000000000..2c2c6da77db
--- /dev/null
+++ b/Tests/LibWeb/Crash/wpt-import/svg/extensibility/foreignObject/foreign-object-under-defs-crash.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<title>Should not crash on foreignObject under defs</title>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script>test(()=>{})</script>
+<div style="will-change: opacity; mix-blend-mode: exclusion;"></div>
+<svg>
+ <defs>
+ <foreignObject width="100" height="100">
+ <div style="will-change: opacity;">FAIL</div>
+ </foreignObject>
+ </defs>
+</svg>
+<div style="will-change: opacity;"></div>
diff --git a/Tests/LibWeb/Crash/wpt-import/svg/linking/scripted/use-iframe-crash.html b/Tests/LibWeb/Crash/wpt-import/svg/linking/scripted/use-iframe-crash.html
new file mode 100644
index 00000000000..01546401adb
--- /dev/null
+++ b/Tests/LibWeb/Crash/wpt-import/svg/linking/scripted/use-iframe-crash.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<link rel="help" href="https://github.com/servo/servo/issues/46250" />
+
+<svg>
+ <foreignObject>
+ <iframe id="iframe"></iframe>
+ </foreignObject>
+ <use href="#iframe">
+</svg>
diff --git a/Tests/LibWeb/Ref/input/wpt-import/svg/struct/reftests/use-foreignObject.html b/Tests/LibWeb/Ref/input/wpt-import/svg/struct/reftests/use-foreignObject.html
new file mode 100644
index 00000000000..e7f888a0dd8
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/wpt-import/svg/struct/reftests/use-foreignObject.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>foreignObject is valid inside SVG use elements</title>
+<link rel="author" title="Psychpsyo" href="[email protected]">
+<link rel="help" href="https://w3c.github.io/svgwg/svg2-draft/struct.html#UseElement:~:text=Previous%20versions%20of%20SVG,subtree%20to%20be%20cloned.">
+<link rel="match" href="../../../../../expected/wpt-import/svg/struct/reftests/reference/green-100x100.html">
+<style>
+ svg {
+ width: 100px;
+ height: 100px;
+ background-color: red;
+ }
+ #square {
+ width: 100px;
+ height: 100px;
+ background-color: green;
+ }
+</style>
+<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
+ <symbol id="ref">
+ <foreignObject width="100" height="100">
+ <div id="square"></div>
+ </foreignObject>
+ </symbol>
+ <use href="#ref" />
+</svg>
diff --git a/Tests/LibWeb/Text/expected/SVG/foreignObject-in-never-rendered-subtree.txt b/Tests/LibWeb/Text/expected/SVG/foreignObject-in-never-rendered-subtree.txt
new file mode 100644
index 00000000000..6433672f5bf
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/foreignObject-in-never-rendered-subtree.txt
@@ -0,0 +1,6 @@
+In mask: auto
+In pattern: auto
+In clipPath: auto
+Rendered: 40px
+Moved into mask: auto
+Moved back: 40px
diff --git a/Tests/LibWeb/Text/expected/display_list/iframe-in-svg-mask-and-pattern.txt b/Tests/LibWeb/Text/expected/display_list/iframe-in-svg-mask-and-pattern.txt
new file mode 100644
index 00000000000..5fb6c8ab82f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/display_list/iframe-in-svg-mask-and-pattern.txt
@@ -0,0 +1,20 @@
+AccumulatedVisualContext Tree:
+ spatial:
+ [s0] transform=[1,0,0,1,0,0] origin=(0,0) (Viewport<#document>)
+ [s1] scroll (Viewport<#document>)
+ [s2] svg-viewport-transform=[1,0,0,1,0,0] origin=(0,0) (SVGSVGBox<svg>)
+ clips:
+ [c0 in s1] clip=[0,0 300x100] (SVGSVGBox<svg>)
+ effects:
+ [e0 in s0] effects=[]
+ [e1 in s2 out=c0] mask=[0,0 100x100] kind=luminance origin=svg-mask (SVGGeometryBox<rect>)
+ [e2 in s2 out=c0] mask=[200,0 100x100] kind=luminance origin=svg-mask (SVGGeometryBox<rect>)
+
+DisplayList:
+DeclareMaskContent@s2/c0/e1 rect=[0,0 100x100] effect=e1
+FillPath@s2/c0/e1 path_bounding_rect=[0,0 100x100]
+FillPath@s2/c0/e0 path_bounding_rect=[100,0 100x100]
+ PatternTile:
+DeclareMaskContent@s2/c0/e2 rect=[200,0 100x100] effect=e2
+FillPath@s2/c0/e2 path_bounding_rect=[200,0 100x100]
+
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/render/foreignObject-in-non-rendered-getComputedStyle.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/render/foreignObject-in-non-rendered-getComputedStyle.txt
new file mode 100644
index 00000000000..ca305f7e4d8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/render/foreignObject-in-non-rendered-getComputedStyle.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test that we don't generated boxes for foreignObject in non-rendered SVG.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/SVG/foreignObject-in-never-rendered-subtree.html b/Tests/LibWeb/Text/input/SVG/foreignObject-in-never-rendered-subtree.html
new file mode 100644
index 00000000000..c067a24f6bc
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/foreignObject-in-never-rendered-subtree.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<svg id="svg" width="100" height="100">
+ <mask id="mask">
+ <foreignObject width="40" height="40"><div id="in-mask"></div></foreignObject>
+ </mask>
+ <pattern id="pattern" width="50" height="50" patternUnits="userSpaceOnUse">
+ <foreignObject width="40" height="40"><div id="in-pattern"></div></foreignObject>
+ </pattern>
+ <clipPath id="clip">
+ <foreignObject width="40" height="40"><div id="in-clip-path"></div></foreignObject>
+ </clipPath>
+ <rect id="rect" width="100" height="100" fill="url(#pattern)" mask="url(#mask)" clip-path="url(#clip)"/>
+ <foreignObject id="movable" width="40" height="40"><div id="rendered"></div></foreignObject>
+</svg>
+<script>
+ test(() => {
+ const lines = [];
+ const report = (label, id) => lines.push(`${label}: ${getComputedStyle(document.getElementById(id)).width}`);
+ report("In mask", "in-mask");
+ report("In pattern", "in-pattern");
+ report("In clipPath", "in-clip-path");
+ report("Rendered", "rendered");
+
+ document.getElementById("mask").moveBefore(document.getElementById("movable"), null);
+ report("Moved into mask", "rendered");
+
+ document.getElementById("svg").moveBefore(document.getElementById("movable"), null);
+ report("Moved back", "rendered");
+
+ for (const line of lines)
+ println(line);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/display_list/iframe-in-svg-mask-and-pattern.html b/Tests/LibWeb/Text/input/display_list/iframe-in-svg-mask-and-pattern.html
new file mode 100644
index 00000000000..851da92e377
--- /dev/null
+++ b/Tests/LibWeb/Text/input/display_list/iframe-in-svg-mask-and-pattern.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+</style>
+<svg width="300" height="100">
+ <defs>
+ <g id="frame-group">
+ <foreignObject width="80" height="80"><iframe xmlns="http://www.w3.org/1999/xhtml" style="width: 80px; height: 80px; border: 0" srcdoc="&lt;body style='background: white'&gt;"></iframe></foreignObject>
+ </g>
+ </defs>
+ <mask id="mask" maskUnits="userSpaceOnUse" x="0" y="0" width="100" height="100">
+ <foreignObject width="80" height="80"><iframe xmlns="http://www.w3.org/1999/xhtml" style="width: 80px; height: 80px; border: 0" srcdoc="&lt;body style='background: white'&gt;"></iframe></foreignObject>
+ </mask>
+ <pattern id="pattern" width="50" height="50" patternUnits="userSpaceOnUse">
+ <foreignObject width="40" height="40"><iframe xmlns="http://www.w3.org/1999/xhtml" style="width: 40px; height: 40px; border: 0" srcdoc="&lt;body style='background: green'&gt;"></iframe></foreignObject>
+ </pattern>
+ <mask id="mask-with-use" maskUnits="userSpaceOnUse" x="200" y="0" width="100" height="100">
+ <use href="#frame-group" x="200"/>
+ </mask>
+ <rect width="100" height="100" fill="green" mask="url(#mask)"/>
+ <rect x="100" width="100" height="100" fill="url(#pattern)"/>
+ <rect x="200" width="100" height="100" fill="green" mask="url(#mask-with-use)"/>
+</svg>
+<script>
+ promiseTest(async () => {
+ if (document.readyState !== "complete")
+ await new Promise(resolve => window.addEventListener("load", resolve));
+ await animationFrame();
+ println(internals.dumpDisplayList());
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/render/foreignObject-in-non-rendered-getComputedStyle.html b/Tests/LibWeb/Text/input/wpt-import/svg/render/foreignObject-in-non-rendered-getComputedStyle.html
new file mode 100644
index 00000000000..85b2c93491e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/render/foreignObject-in-non-rendered-getComputedStyle.html
@@ -0,0 +1,20 @@
+<!doctype html>
+<title>SVG Test: Resolved width inside non-rendered foreignObject</title>
+<link rel="help" href="https://www.w3.org/TR/SVG2/render.html#Rendered-vs-NonRendered">
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<svg>
+ <radialGradient>
+ <pattern>
+ <foreignObject id="foreignObject">
+ <div id="foRoot"></div>
+ </foreignObject>
+ </pattern>
+ </radialGradient>
+</svg>
+<script>
+ test(() => {
+ assert_equals(getComputedStyle(foreignObject).width, "auto", "Non-rendered foreignObject should have width computed to 'auto'.");
+ assert_equals(getComputedStyle(foRoot).width, "auto", "Non-rendered block inside foreignObject should have width computed to 'auto'.");
+ }, "Test that we don't generated boxes for foreignObject in non-rendered SVG.");
+</script>