Skip to content

Truly independent web browser

LibWeb: Lay out SVG text again when a position attribute changes

sideshowbarker4c89b3b57539
diff --git a/Libraries/LibWeb/SVG/SVGTextPositioningElement.cpp b/Libraries/LibWeb/SVG/SVGTextPositioningElement.cpp
index 4f69bafa9b8..9f55c908010 100644
--- a/Libraries/LibWeb/SVG/SVGTextPositioningElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGTextPositioningElement.cpp
@@ -50,6 +50,10 @@ void SVGTextPositioningElement::attribute_changed(Utf16FlyString const& name, Op
m_dy->synchronize();
else if (name == AttributeNames::rotate && m_rotate)
m_rotate->synchronize();
+
+ // The attributes position the element's characters, so the text has to be laid out with their new values.
+ if (old_value != value && name.is_one_of(AttributeNames::x, AttributeNames::y, AttributeNames::dx, AttributeNames::dy))
+ set_needs_layout_update(DOM::SetNeedsLayoutReason::StyleChange);
}
SVGTextPositioningElement::ParsedTextPositioning SVGTextPositioningElement::parsed_text_positioning() const
diff --git a/Tests/LibWeb/Text/expected/SVG/text-positioning-attribute-changes.txt b/Tests/LibWeb/Text/expected/SVG/text-positioning-attribute-changes.txt
new file mode 100644
index 00000000000..6a04bbe22cd
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/text-positioning-attribute-changes.txt
@@ -0,0 +1,8 @@
+At first: 10,14 30,14 50,14 70,14 90,14 110,14
+<text> x="50": 50,14 70,14 90,14 110,14 130,14 150,14
+<text> y="60": 50,44 70,44 90,44 110,44 130,44 150,44
+<tspan> x="200": 50,44 70,44 200,44 220,44 240,44 260,44
+<tspan> dy="20": 50,44 70,44 200,64 220,64 240,64 260,64
+<tspan> dx="10": 50,44 70,44 210,64 230,64 250,64 270,64
+<tspan> x removed: 50,44 70,44 100,64 120,64 140,64 160,64
+<text> dx="5": 55,44 75,44 105,64 125,64 145,64 165,64
diff --git a/Tests/LibWeb/Text/input/SVG/text-positioning-attribute-changes.html b/Tests/LibWeb/Text/input/SVG/text-positioning-attribute-changes.html
new file mode 100644
index 00000000000..c6f4b069b52
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/text-positioning-attribute-changes.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @font-face {
+ font-family: Ahem;
+ src: url("../../../Ref/data/fonts/Ahem.ttf");
+ }
+ body {
+ margin: 0;
+ }
+ svg {
+ display: block;
+ font: 20px Ahem;
+ }
+</style>
+<svg width="400" height="200">
+ <text x="10" y="30">AB<tspan>CD</tspan>EF</text>
+</svg>
+<script>
+ asyncTest(async done => {
+ await document.fonts.load("20px Ahem");
+ const text = document.querySelector("text");
+ const tspan = document.querySelector("tspan");
+ const describe = () => {
+ const cells = [];
+ for (let index = 0; index < text.getNumberOfChars(); ++index) {
+ const cell = text.getExtentOfChar(index);
+ cells.push(`${cell.x},${cell.y}`);
+ }
+ return cells.join(" ");
+ };
+
+ println(`At first: ${describe()}`);
+ text.setAttribute("x", "50");
+ println(`<text> x="50": ${describe()}`);
+ text.setAttribute("y", "60");
+ println(`<text> y="60": ${describe()}`);
+ tspan.setAttribute("x", "200");
+ println(`<tspan> x="200": ${describe()}`);
+ tspan.setAttribute("dy", "20");
+ println(`<tspan> dy="20": ${describe()}`);
+ tspan.setAttribute("dx", "10");
+ println(`<tspan> dx="10": ${describe()}`);
+ tspan.removeAttribute("x");
+ println(`<tspan> x removed: ${describe()}`);
+ text.setAttribute("dx", "5");
+ println(`<text> dx="5": ${describe()}`);
+ done();
+ });
+</script>