Skip to content

Commit

Permalink
fix: <a> element not parsed as text (#1959)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkenny54 authored Feb 11, 2024
1 parent a938d5e commit e257598
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
32 changes: 32 additions & 0 deletions lib/parser.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { parseSvg } from './parser.js';
import { stringifySvg } from './stringifier.js';

const input = `<svg xmlns="http://www.w3.org/2000/svg">
<text x="10" y="35" xml:space="preserve">
<a href="x">
this is a test
</a>
</text>
<text x="10" y="35" xml:space="preserve">
<tspan>
this is a test
</tspan>
</text>
</svg>`;

const expected = `<svg xmlns="http://www.w3.org/2000/svg"><text x="10" y="35" xml:space="preserve">
<a href="x">
this is a test
</a>
</text><text x="10" y="35" xml:space="preserve">
<tspan>
this is a test
</tspan>
</text></svg>`;

test('a text preserved', () => {
const parsed = parseSvg(input);
const actual = stringifySvg(parsed, {});

expect(actual).toBe(expected);
});
1 change: 1 addition & 0 deletions plugins/_collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const elemsGroups = {
'symbol',
]),
textContent: new Set([
'a',
'altGlyph',
'altGlyphDef',
'altGlyphItem',
Expand Down
5 changes: 4 additions & 1 deletion plugins/removeScriptElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export const fn = () => {
}

const index = parentNode.children.indexOf(node);
parentNode.children.splice(index, 1, ...node.children);
const usefulChildren = node.children.filter(
(child) => !(child.type === 'text' && /\s*/.test(child.value)),
);
parentNode.children.splice(index, 1, ...usefulChildren);

// TODO remove legacy parentNode in v4
for (const child of node.children) {
Expand Down
4 changes: 2 additions & 2 deletions test/plugins/removeEmptyContainers.06.svg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ systemLanguage.
<switch>
<g requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility"/>
<a transform="translate(0,-5)" href="https://www.diagrams.net/doc/faq/svg-export-text-problems" target="_blank">
<text text-anchor="middle" font-size="10px" x="50%" y="100%">Viewer does not support full SVG 1.1</text>
</a>
<text text-anchor="middle" font-size="10px" x="50%" y="100%">Viewer does not support full SVG 1.1</text>
</a>
</switch>
</svg>
4 changes: 2 additions & 2 deletions test/plugins/removeScriptElement.03.svg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ Does not remove normal links, and does remove event attributes.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<a href="https://yewtu.be/watch?v=dQw4w9WgXcQ">
<text y="10">uwu</text>
</a>
<text y="10">uwu</text>
</a>
</svg>
7 changes: 3 additions & 4 deletions test/plugins/removeXlink.03.svg.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ to title node.
@@@

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
<a target="_blank" href="https://duckduckgo.com">
<title>DuckDuckGo Homepage</title>
<text x="0" y="10">uwu</text>
</a>
<a target="_blank" href="https://duckduckgo.com"><title>DuckDuckGo Homepage</title>
<text x="0" y="10">uwu</text>
</a>
</svg>

0 comments on commit e257598

Please sign in to comment.