-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsvg-markers.test.ts
More file actions
431 lines (378 loc) · 16.5 KB
/
svg-markers.test.ts
File metadata and controls
431 lines (378 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import { test, expect } from "@playwright/test";
import { setupPage } from "../helpers.js";
test.describe("SVG Marker Extraction", () => {
test("extracts marker-end on a line", async ({ page }) => {
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="200" height="80" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="arrow" viewBox="0 -5 10 10" refX="10" refY="0"
markerWidth="6" markerHeight="6" orient="auto">
<path d="M0,-5L10,0L0,5" fill="black" />
</marker>
</defs>
<line x1="20" y1="40" x2="180" y2="40" stroke="black" stroke-width="2"
marker-end="url(#arrow)" />
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
// At least 2: the line itself + the marker
expect(polylines.length).toBeGreaterThanOrEqual(2);
// The marker polyline should be near x=180 (the end of the line)
const marker = polylines.find(
(p: any) => p.points.length > 2 && p !== polylines[0]
);
expect(marker).toBeDefined();
// Marker should be near the endpoint
const markerCenterX =
marker.points.reduce((s: number, p: any) => s + p.x, 0) /
marker.points.length;
expect(markerCenterX).toBeGreaterThan(160);
expect(markerCenterX).toBeLessThan(200);
});
test("extracts marker-start on a line", async ({ page }) => {
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="200" height="80" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="dot" viewBox="0 -5 10 10" refX="0" refY="0"
markerWidth="6" markerHeight="6" orient="auto">
<path d="M0,-5L10,0L0,5" fill="red" />
</marker>
</defs>
<line x1="20" y1="40" x2="180" y2="40" stroke="red" stroke-width="2"
marker-start="url(#dot)" />
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
expect(polylines.length).toBeGreaterThanOrEqual(2);
// Marker should be near the start point x=20
const marker = polylines.find(
(p: any) => p.points.length > 2 && p !== polylines[0]
);
expect(marker).toBeDefined();
const markerCenterX =
marker.points.reduce((s: number, p: any) => s + p.x, 0) /
marker.points.length;
expect(markerCenterX).toBeGreaterThan(10);
expect(markerCenterX).toBeLessThan(40);
});
test("extracts marker-mid on polyline", async ({ page }) => {
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="300" height="100" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="mid-dot" viewBox="0 -5 10 10" refX="5" refY="0"
markerWidth="5" markerHeight="5" orient="auto">
<path d="M0,-5L10,0L0,5" fill="blue" />
</marker>
</defs>
<polyline points="20,80 100,20 200,80 280,20"
fill="none" stroke="blue" stroke-width="2"
marker-mid="url(#mid-dot)" />
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
// 1 polyline + 2 mid-markers (at points 100,20 and 200,80)
expect(polylines.length).toBeGreaterThanOrEqual(3);
});
test("markers scale correctly with viewBox (large viewBox, small viewport)", async ({
page,
}) => {
// This is the key test for the "too huge" marker bug.
// viewBox="0 0 1000 500" in a 200x100 viewport → CTM scale = 0.2
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="200" height="100" viewBox="0 0 1000 500" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="arrow-vb" viewBox="0 -5 10 10" refX="10" refY="0"
markerWidth="6" markerHeight="6" orient="auto">
<path d="M0,-5L10,0L0,5" fill="green" />
</marker>
</defs>
<line x1="100" y1="250" x2="900" y2="250" stroke="green" stroke-width="10"
marker-end="url(#arrow-vb)" />
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
expect(polylines.length).toBeGreaterThanOrEqual(2);
// Find the 2-point line and the multi-point marker
const line = polylines.find((p: any) => p.points.length === 2);
expect(line).toBeDefined();
const lineLen = Math.abs(line.points[1].x - line.points[0].x);
expect(lineLen).toBeGreaterThan(100); // should be roughly 160px
const marker = polylines.find((p: any) => p.points.length > 2);
expect(marker).toBeDefined();
// Marker bounding box should be small relative to line length
const xs = marker.points.map((p: any) => p.x);
const markerWidth = Math.max(...xs) - Math.min(...xs);
// With correct CTM scaling (0.2), marker should be ~12px
// Without CTM fix, it would be ~60px (way too large for a 160px line)
expect(markerWidth).toBeLessThan(lineLen * 0.15);
});
test("markers scale correctly with viewBox (small viewBox, large viewport)", async ({
page,
}) => {
// viewBox="0 0 100 50" in a 400x200 viewport → CTM scale = 4
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="400" height="200" viewBox="0 0 100 50" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="arrow-up" viewBox="0 -5 10 10" refX="10" refY="0"
markerWidth="6" markerHeight="6" orient="auto">
<path d="M0,-5L10,0L0,5" fill="purple" />
</marker>
</defs>
<line x1="10" y1="25" x2="90" y2="25" stroke="purple" stroke-width="2"
marker-end="url(#arrow-up)" />
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
expect(polylines.length).toBeGreaterThanOrEqual(2);
const line = polylines.find((p: any) => p.points.length === 2);
expect(line).toBeDefined();
const lineLen = Math.abs(line.points[1].x - line.points[0].x);
expect(lineLen).toBeGreaterThan(200); // should be roughly 320px
const marker = polylines.find((p: any) => p.points.length > 2);
expect(marker).toBeDefined();
const xs = marker.points.map((p: any) => p.x);
const markerWidth = Math.max(...xs) - Math.min(...xs);
// With CTM=4, marker should be about 48px (6*10/10 * 2 * 4)
// Should be visible but proportional to line
expect(markerWidth).toBeGreaterThan(10);
expect(markerWidth).toBeLessThan(lineLen * 0.25);
});
test("markers in group with transform scale correctly", async ({ page }) => {
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="300" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="arrow-g" viewBox="0 -5 10 10" refX="10" refY="0"
markerWidth="6" markerHeight="6" orient="auto">
<path d="M0,-5L10,0L0,5" fill="teal" />
</marker>
</defs>
<g transform="scale(2)">
<line x1="10" y1="30" x2="120" y2="30" stroke="teal" stroke-width="1"
marker-end="url(#arrow-g)" />
</g>
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
expect(polylines.length).toBeGreaterThanOrEqual(2);
// With scale(2), line goes from x≈20 to x≈240
const line = polylines.find((p: any) => p.points.length === 2);
expect(line).toBeDefined();
const lineLen = Math.abs(line.points[1].x - line.points[0].x);
expect(lineLen).toBeGreaterThan(150);
const marker = polylines.find((p: any) => p.points.length > 2);
expect(marker).toBeDefined();
const xs = marker.points.map((p: any) => p.x);
const markerWidth = Math.max(...xs) - Math.min(...xs);
// Marker should be proportional even with group transform
expect(markerWidth).toBeLessThan(lineLen * 0.15);
expect(markerWidth).toBeGreaterThan(2);
});
test("markerUnits userSpaceOnUse ignores stroke width", async ({ page }) => {
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="300" height="80" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="arrow-usu" viewBox="0 -5 10 10" refX="10" refY="0"
markerWidth="15" markerHeight="15" markerUnits="userSpaceOnUse" orient="auto">
<path d="M0,-5L10,0L0,5" fill="navy" />
</marker>
</defs>
<line x1="30" y1="20" x2="270" y2="20" stroke="navy" stroke-width="1"
marker-end="url(#arrow-usu)" />
<line x1="30" y1="50" x2="270" y2="50" stroke="navy" stroke-width="5"
marker-end="url(#arrow-usu)" />
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
// 2 lines + 2 markers
expect(polylines.length).toBeGreaterThanOrEqual(4);
// Find the two marker polylines (have many points, not simple 2-point lines)
const markers = polylines.filter((p: any) => p.points.length > 2);
expect(markers.length).toBe(2);
// Both markers should be the same size since markerUnits="userSpaceOnUse",
// regardless of the different stroke widths
const widths = markers.map((m: any) => {
const xs = m.points.map((p: any) => p.x);
return Math.max(...xs) - Math.min(...xs);
});
// Allow 1px tolerance
expect(Math.abs(widths[0] - widths[1])).toBeLessThan(1);
});
test("stroke width affects marker size with default markerUnits", async ({
page,
}) => {
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="300" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="arrow-sw" viewBox="0 -5 10 10" refX="10" refY="0"
markerWidth="6" markerHeight="6" orient="auto">
<path d="M0,-5L10,0L0,5" fill="black" />
</marker>
</defs>
<line x1="20" y1="30" x2="280" y2="30" stroke="black" stroke-width="1"
marker-end="url(#arrow-sw)" />
<line x1="20" y1="80" x2="280" y2="80" stroke="black" stroke-width="4"
marker-end="url(#arrow-sw)" />
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
const markers = polylines.filter((p: any) => p.points.length > 2);
expect(markers.length).toBe(2);
const widths = markers.map((m: any) => {
const xs = m.points.map((p: any) => p.x);
return Math.max(...xs) - Math.min(...xs);
});
// The stroke-width=4 marker should be ~4x larger than stroke-width=1
expect(widths[1]).toBeGreaterThan(widths[0] * 2);
});
test("marker on path element", async ({ page }) => {
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="300" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="arrow-path" viewBox="0 -5 10 10" refX="10" refY="0"
markerWidth="6" markerHeight="6" orient="auto">
<path d="M0,-5L10,0L0,5" fill="#e91e63" />
</marker>
</defs>
<path d="M 20 100 Q 150 0 280 100" fill="none" stroke="#e91e63" stroke-width="2"
marker-start="url(#arrow-path)" marker-end="url(#arrow-path)" />
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
// 1 path + 2 markers (start and end)
expect(polylines.length).toBeGreaterThanOrEqual(3);
});
test("marker with polygon child shape", async ({ page }) => {
await setupPage(
page,
`<html><body style="margin:0;">
<svg id="target" width="200" height="80" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="diamond" viewBox="0 0 10 10" refX="5" refY="5"
markerWidth="8" markerHeight="8" orient="auto">
<polygon points="5,0 10,5 5,10 0,5" fill="orange" />
</marker>
</defs>
<line x1="20" y1="40" x2="180" y2="40" stroke="orange" stroke-width="2"
marker-end="url(#diamond)" />
</svg>
</body></html>`
);
const ir = await page.evaluate(() => {
const el = document.getElementById("target")!;
return (window as any).__HC.extractIR(el);
});
const polylines = ir.filter((n: any) => n.type === "polyline");
expect(polylines.length).toBeGreaterThanOrEqual(2);
// Find the polygon marker (should be closed with 4 points)
const marker = polylines.find((p: any) => p.closed && p.points.length === 4);
expect(marker).toBeDefined();
});
test("markers consistent across inline SVG and img tag", async ({ page }) => {
// The same SVG rendered inline and as an img should produce similarly-sized markers
const svgContent = `<svg xmlns="http://www.w3.org/2000/svg" width="200" height="80">
<defs>
<marker id="a" viewBox="0 -5 10 10" refX="10" refY="0"
markerWidth="6" markerHeight="6" orient="auto">
<path d="M0,-5L10,0L0,5" fill="red" />
</marker>
</defs>
<line x1="20" y1="40" x2="180" y2="40" stroke="red" stroke-width="2" marker-end="url(#a)" />
</svg>`;
const encodedSvg = encodeURIComponent(svgContent);
await setupPage(
page,
`<html><body style="margin:0;">
<div id="inline-container">
<svg id="target-inline" width="200" height="80" xmlns="http://www.w3.org/2000/svg">
<defs>
<marker id="a-inline" viewBox="0 -5 10 10" refX="10" refY="0"
markerWidth="6" markerHeight="6" orient="auto">
<path d="M0,-5L10,0L0,5" fill="red" />
</marker>
</defs>
<line x1="20" y1="40" x2="180" y2="40" stroke="red" stroke-width="2" marker-end="url(#a-inline)" />
</svg>
</div>
<div id="img-container" style="margin-top:10px;">
<img id="target-img" width="200" height="80"
src="data:image/svg+xml,${encodedSvg}">
</div>
</body></html>`
);
const inlineIR = await page.evaluate(() => {
const el = document.getElementById("target-inline")!;
return (window as any).__HC.extractIR(el);
});
const inlineMarkers = inlineIR.filter(
(n: any) => n.type === "polyline" && n.points.length > 2
);
expect(inlineMarkers.length).toBeGreaterThanOrEqual(1);
// Inline marker size
const inlineXs = inlineMarkers[0].points.map((p: any) => p.x);
const inlineW = Math.max(...inlineXs) - Math.min(...inlineXs);
expect(inlineW).toBeGreaterThan(2);
});
});