Skip to content

Commit 4d373b7

Browse files
committed
fix: handle style element separately in SVG parser
1 parent 76a0935 commit 4d373b7

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/parser/parse-svg.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,24 @@ function walk(iter, parentToken) {
132132
let tokenName = selectors.pop();
133133
let skip = isSkip(...selectors, parentToken.name, tokenName);
134134

135-
if (tokenName === 'style') {
135+
const allSelectors = [...selectors, tokenName];
136+
const styleIndex = allSelectors.indexOf('style');
137+
138+
// handle style block separately
139+
if (styleIndex >= 0) {
140+
let styleContent = '';
141+
const cssSelectors = allSelectors.slice(styleIndex + 1);
142+
if (cssSelectors.length > 0) {
143+
styleContent = cssSelectors.join(' ') + '{';
144+
}
145+
styleContent += readStyle(iter);
146+
if (cssSelectors.length > 0) {
147+
styleContent += '}';
148+
}
136149
rules.push({
137150
type: 'block',
138-
name: tokenName,
139-
value: readStyle(iter)
151+
name: 'style',
152+
value: styleContent
140153
});
141154
} else {
142155
let block = resolveId(walk(iter, splitTimes(tokenName, {

test/parse-svg.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,38 @@ test('content values', () => {
444444
});
445445
});
446446

447+
test('style with css selectors', () => {
448+
compare('style { a { fill: red } }', {
449+
type: 'block',
450+
name: 'svg',
451+
value: [{
452+
type: 'block',
453+
name: 'style',
454+
value: 'a{fill:red}'
455+
}]
456+
});
457+
458+
compare('style a { fill: red }', {
459+
type: 'block',
460+
name: 'svg',
461+
value: [{
462+
type: 'block',
463+
name: 'style',
464+
value: 'a{fill:red}'
465+
}]
466+
});
467+
468+
compare('style .cls { stroke: blue }', {
469+
type: 'block',
470+
name: 'svg',
471+
value: [{
472+
type: 'block',
473+
name: 'style',
474+
value: '.cls{stroke:blue}'
475+
}]
476+
});
477+
});
478+
447479
test('times syntax', () => {
448480
compare(`
449481
circle*10 {}

0 commit comments

Comments
 (0)