Skip to content

Commit 2dd3668

Browse files
committed
chore: remove CDATA when parsing style
1 parent 42bceb1 commit 2dd3668

3 files changed

Lines changed: 12 additions & 13 deletions

File tree

@iconify/tools/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "module",
44
"description": "Collection of functions for cleaning up and parsing SVG for Iconify project",
55
"author": "Vjacheslav Trushkin",
6-
"version": "5.0.9",
6+
"version": "5.0.10",
77
"license": "MIT",
88
"bugs": "https://github.com/iconify/tools/issues",
99
"homepage": "https://github.com/iconify/tools",

@iconify/tools/src/svg/parse-style.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ interface ParseSVGStyleCallbackItemCommon {
2323
prop: string;
2424
value: string;
2525
}
26-
interface ParseSVGStyleCallbackItemInline
27-
extends ParseSVGStyleCallbackItemCommon {
26+
interface ParseSVGStyleCallbackItemInline extends ParseSVGStyleCallbackItemCommon {
2827
type: 'inline';
2928
item: ParseSVGCallbackItem;
3029
}
31-
interface ParseSVGStyleCallbackItemGlobal
32-
extends ParseSVGStyleCallbackItemCommon {
30+
interface ParseSVGStyleCallbackItemGlobal extends ParseSVGStyleCallbackItemCommon {
3331
type: 'global';
3432
token: CSSRuleToken;
3533
selectors: string[];
@@ -38,19 +36,16 @@ interface ParseSVGStyleCallbackItemGlobal
3836
nextTokens: CSSToken[];
3937
}
4038

41-
interface ParseSVGStyleCallbackItemGlobalAtRule
42-
extends ParseSVGStyleCallbackItemCommon {
39+
interface ParseSVGStyleCallbackItemGlobalAtRule extends ParseSVGStyleCallbackItemCommon {
4340
token: CSSAtRuleToken;
4441
childTokens: CSSToken[];
4542
prevTokens: (CSSToken | null)[];
4643
nextTokens: CSSToken[];
4744
}
48-
interface ParseSVGStyleCallbackItemGlobalGenericAtRule
49-
extends ParseSVGStyleCallbackItemGlobalAtRule {
45+
interface ParseSVGStyleCallbackItemGlobalGenericAtRule extends ParseSVGStyleCallbackItemGlobalAtRule {
5046
type: 'at-rule';
5147
}
52-
interface ParseSVGStyleCallbackItemGlobalKeyframesAtRule
53-
extends ParseSVGStyleCallbackItemGlobalAtRule {
48+
interface ParseSVGStyleCallbackItemGlobalKeyframesAtRule extends ParseSVGStyleCallbackItemGlobalAtRule {
5449
type: 'keyframes';
5550
from: Record<string, string>;
5651
}
@@ -96,12 +91,16 @@ export function parseSVGStyle(svg: SVG, callback: ParseSVGStyleCallback): void {
9691

9792
// Parse <style> tag
9893
function parseStyleItem() {
99-
const content = stringifyXMLContent(node.children);
94+
let content = stringifyXMLContent(node.children);
10095
if (!content) {
10196
item.removeNode = true;
10297
return;
10398
}
10499

100+
// Remove '<![CDATA['
101+
content = content.replace('<![CDATA[', '').replace(']]>', '');
102+
103+
// Parse style
105104
const tokens = getTokens(content);
106105
if (!(tokens instanceof Array)) {
107106
// Invalid style

@iconify/tools/tests/svg/cleanup-global-style-test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('Removing global style', () => {
7777

7878
test('Multiple rules', () => {
7979
const content =
80-
'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><defs><style>.cls-1,.cls-2{fill:#aecbfa;fill-rule:evenodd}.cls-2{fill:#669df6}</style></defs><g data-name="Product Icons"><path class="cls-1" d="M6.73 10.83v2.63a4.91 4.91 0 0 0 1.71 1.74v-4.37Z"/><path class="cls-2" d="M9.89 8.41v7.53A7.62 7.62 0 0 0 11 16a8 8 0 0 0 1 0V8.41Z"/><path class="cls-1" d="M13.64 11.86v3.29a5 5 0 0 0 1.7-1.82v-1.47Z"/><path d="m17.74 16.32-1.42 1.42a.42.42 0 0 0 0 .6l3.54 3.54a.42.42 0 0 0 .59 0l1.43-1.43a.42.42 0 0 0 0-.59l-3.54-3.54a.42.42 0 0 0-.6 0" style="fill:#4285f4;fill-rule:evenodd"/><path class="cls-2" d="M11 2a9 9 0 1 0 9 9 9 9 0 0 0-9-9m0 15.69A6.68 6.68 0 1 1 17.69 11 6.68 6.68 0 0 1 11 17.69"/></g></svg>';
80+
'<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><defs><style><![CDATA[ .cls-1,.cls-2{fill:#aecbfa;fill-rule:evenodd}.cls-2{fill:#669df6} ]]></style></defs><g data-name="Product Icons"><path class="cls-1" d="M6.73 10.83v2.63a4.91 4.91 0 0 0 1.71 1.74v-4.37Z"/><path class="cls-2" d="M9.89 8.41v7.53A7.62 7.62 0 0 0 11 16a8 8 0 0 0 1 0V8.41Z"/><path class="cls-1" d="M13.64 11.86v3.29a5 5 0 0 0 1.7-1.82v-1.47Z"/><path d="m17.74 16.32-1.42 1.42a.42.42 0 0 0 0 .6l3.54 3.54a.42.42 0 0 0 .59 0l1.43-1.43a.42.42 0 0 0 0-.59l-3.54-3.54a.42.42 0 0 0-.6 0" style="fill:#4285f4;fill-rule:evenodd"/><path class="cls-2" d="M11 2a9 9 0 1 0 9 9 9 9 0 0 0-9-9m0 15.69A6.68 6.68 0 1 1 17.69 11 6.68 6.68 0 0 1 11 17.69"/></g></svg>';
8181
const svg = new SVG(content);
8282
cleanupGlobalStyle(svg);
8383

0 commit comments

Comments
 (0)