Skip to content

Truly independent web browser

LibWeb: Render the text of a link inside SVG text

sideshowbarkerf9f6140d4d22
diff --git a/Libraries/LibWeb/SVG/SVGAElement.cpp b/Libraries/LibWeb/SVG/SVGAElement.cpp
index 7ba94491c8f..b73dfe6ccf7 100644
--- a/Libraries/LibWeb/SVG/SVGAElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGAElement.cpp
@@ -15,6 +15,7 @@
#include <LibWeb/Namespace.h>
#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/SVGAElement.h>
+#include <LibWeb/SVG/SVGTextContentElement.h>
namespace Web::SVG {
@@ -95,6 +96,12 @@ GC::Ref<DOM::DOMTokenList> SVGAElement::rel_list()
CSS::ElementBoxKind SVGAElement::box_kind() const
{
+ // https://svgwg.org/svg2-draft/text.html#TextLayoutAlgorithm
+ // The 'text' element is treated as a block element and its descendant 'tspan', 'textPath' and 'a' elements are
+ // treated as inline elements.
+ // NB: So, an <a> inside a text content element renders its text as a <tspan> does.
+ if (first_flat_tree_ancestor_of_type<SVGTextContentElement>())
+ return CSS::ElementBoxKind::SvgText;
return CSS::ElementBoxKind::SvgGraphics;
}
diff --git a/Libraries/LibWeb/SVG/SVGTextContentElement.h b/Libraries/LibWeb/SVG/SVGTextContentElement.h
index 592e22d7818..8fcf8d64157 100644
--- a/Libraries/LibWeb/SVG/SVGTextContentElement.h
+++ b/Libraries/LibWeb/SVG/SVGTextContentElement.h
@@ -45,6 +45,8 @@ protected:
SVGTextContentElement(DOM::Document&, DOM::QualifiedName);
private:
+ virtual bool is_svg_text_content_element() const final { return true; }
+
Vector<Layout::RustFFI::FfiSvgTextCharacterCell> character_cells();
};
diff --git a/Tests/LibWeb/Layout/expected/tree-builder/svg-text-tspan-textpath.txt b/Tests/LibWeb/Layout/expected/tree-builder/svg-text-tspan-textpath.txt
index f675af87e48..c63a4c76010 100644
--- a/Tests/LibWeb/Layout/expected/tree-builder/svg-text-tspan-textpath.txt
+++ b/Tests/LibWeb/Layout/expected/tree-builder/svg-text-tspan-textpath.txt
@@ -3,12 +3,12 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 103 0+0+8] children: inline
frag 0 from SVGSVGBox start: 0, length: 0, rect: [8,8 300x100] baseline: 100
SVGSVGBox <svg> at [8,8] [0+0+0 300 0+0+0] [0+0+0 100 0+0+0] viewport-transform=[1 0 0 1 0 0] [SVG] children: not-inline
- SVGTextBox <text> at [10,7.203125] [0+0+0 117.359375 0+0+0] [0+0+0 16 0+0+0] children: inline
+ SVGTextBox <text> at [10,7.203125] [0+0+0 143.484375 0+0+0] [0+0+0 16 0+0+0] children: inline
TextNode <#text> (not painted)
SVGTextBox <tspan> at [82.78125,7.203125] [0+0+0 44.578125 0+0+0] [0+0+0 16 0+0+0] children: inline
TextNode <#text> (not painted)
TextNode <#text> (not painted)
- SVGGraphicsBox <a> (not painted) children: inline
+ SVGTextBox <a> at [127.359375,7.203125] [0+0+0 26.125 0+0+0] [0+0+0 16 0+0+0] children: inline
TextNode <#text> (not painted)
TextNode <#text> (not painted)
SVGTextBox <text> at [10.921875,67.6875] [0+0+0 79.453125 0+0+0] [0+0+0 13.0625 0+0+0] children: not-inline
diff --git a/Tests/LibWeb/Text/expected/SVG/text-in-a-link.txt b/Tests/LibWeb/Text/expected/SVG/text-in-a-link.txt
new file mode 100644
index 00000000000..ec6e0c84bae
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/text-in-a-link.txt
@@ -0,0 +1,6 @@
+link-around-text: box 10,134 40x20
+text-in-link: 2 characters, length 40: 10,134 30,134; box 10,134 40x20
+link-last: 6 characters, length 120: 10,164 30,164 50,164 70,164 90,164 110,164; box 10,164 120x20
+link-last-a: box 50,164 80x20
+elementFromPoint(80, 170): link-last-a
+elementFromPoint(20, 170): link-last
diff --git a/Tests/LibWeb/Text/input/SVG/text-in-a-link.html b/Tests/LibWeb/Text/input/SVG/text-in-a-link.html
new file mode 100644
index 00000000000..64b1c745c89
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/text-in-a-link.html
@@ -0,0 +1,49 @@
+<!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="800" height="200">
+ <a id="link-around-text" href="#"><text id="text-in-link" x="10" y="150">AB</text></a>
+ <text id="link-last" x="10" y="180">S:<a id="link-last-a" href="#">LINK</a></text>
+</svg>
+<script>
+ const round = value => Math.round(value * 100) / 100;
+ const characters = element => {
+ const positions = [];
+ for (let index = 0; index < element.getNumberOfChars(); ++index) {
+ const cell = element.getExtentOfChar(index);
+ positions.push(`${round(cell.x)},${round(cell.y)}`);
+ }
+ return `${positions.length} characters, length ${round(element.getComputedTextLength())}: ${positions.join(" ")}`;
+ };
+ const box = element => {
+ const rect = element.getBBox();
+ return `${round(rect.x)},${round(rect.y)} ${round(rect.width)}x${round(rect.height)}`;
+ };
+
+ asyncTest(async done => {
+ await document.fonts.load("20px Ahem");
+ for (const element of document.querySelectorAll("svg [id]")) {
+ const queries = element.getNumberOfChars ? `${characters(element)}; ` : "";
+ println(`${element.id}: ${queries}box ${box(element)}`);
+ }
+ // The link's text is what a click on it lands on.
+ const svg = document.querySelector("svg").getBoundingClientRect();
+ for (const [x, y] of [[80, 170], [20, 170]]) {
+ const hit = document.elementFromPoint(svg.x + x, svg.y + y);
+ println(`elementFromPoint(${x}, ${y}): ${hit.id || hit.localName}`);
+ }
+ done();
+ });
+</script>