Skip to content

Commit a350489

Browse files
committed
fix parsing error. sveltejs/svelte#4282
1 parent d3a7c79 commit a350489

File tree

3 files changed

+65
-36
lines changed

3 files changed

+65
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ filter(path) -> Boolean
120120
Filter the path. Return `true` to transform, or return `false` to skip the path.
121121

122122

123-
### Transform css url()
123+
## Transform css url()
124124
Use https://github.com/canadaduane/snowpack-plugin-relative-css-urls
125125

126126
## License

index.js

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -108,46 +108,54 @@ module.exports = ({ rules, filter } = {}) => {
108108

109109
return {
110110
markup({ content }) {
111-
const ast = svelte.parse(content);
112-
113-
if (ast.html) {
114-
let offset = 0;
115-
116-
svelte.walk(ast.html, {
117-
enter(node) {
118-
if (node.type === 'Element') {
119-
const rulesOfTag = rules.filter(rule => rule.tag === node.name);
120-
121-
if (rulesOfTag.length) {
122-
node.attributes.forEach(attr => {
123-
const rule = rulesOfTag.find(rule => rule.attribute === attr.name);
124-
125-
if (
126-
rule &&
127-
attr.value instanceof Array &&
128-
attr.value.length === 1 &&
129-
attr.value[0].type === 'Text'
130-
) {
131-
const val = attr.value[0];
132-
133-
if (rule.type === 'src') {
134-
if (isLocalPath(val.data)) {
135-
({ content, offset } = replace(content, offset, val.start, val.end, transform(val.data)));
136-
}
137-
} else {
138-
({ content, offset } = replaceSrcset(content, offset, val.start, val.end, val.data));
139-
}
111+
const regex = new RegExp(`<(${[...new Set(rules.map(rule => rule.tag))].join('|')})\\s+[^>]+>`, 'gi');
112+
const copy = content;
113+
let match;
114+
let offset = 0;
115+
116+
while ((match = regex.exec(copy)) !== null) {
117+
const tag = match[0];
118+
const base = match.index;
119+
120+
try {
121+
const ast = svelte.parse(tag);
122+
const node = ast.html.children[0];
123+
const rulesOfTag = rules.filter(rule => rule.tag === node.name);
124+
125+
if (rulesOfTag.length) {
126+
node.attributes.forEach(attr => {
127+
const rule = rulesOfTag.find(rule => rule.attribute === attr.name);
128+
129+
if (
130+
rule &&
131+
attr.value instanceof Array &&
132+
attr.value.length === 1 &&
133+
attr.value[0].type === 'Text'
134+
) {
135+
const val = attr.value[0];
136+
137+
if (rule.type === 'src') {
138+
if (isLocalPath(val.data)) {
139+
({ content, offset } = replace(
140+
content,
141+
offset,
142+
val.start + base,
143+
val.end + base,
144+
transform(val.data))
145+
);
140146
}
141-
});
147+
} else {
148+
({ content, offset } = replaceSrcset(content, offset, val.start + base, val.end + base, val.data));
149+
}
142150
}
143-
}
151+
});
144152
}
145-
});
153+
} catch (e) {
154+
// nop
155+
}
146156
}
147157

148-
return {
149-
code: content
150-
};
158+
return { code: content };
151159
}
152160
};
153161
};

test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,27 @@ const preprocessor = require('./index.js');
33
const content = `
44
<img src="./1.png">
55
<img src="/2.png">
6+
7+
<img srcset="elva-fairy-480w.jpg 480w,
8+
elva-fairy-800w.jpg 800w"
9+
sizes="(max-width: 600px) 480px,
10+
800px"
11+
src="elva-fairy-800w.jpg"
12+
alt="Elva dressed as a fairy">
13+
14+
<picture>
15+
<source type="image/svg+xml" srcset="pyramid.svg">
16+
<source type="image/webp" srcset="pyramid.webp">
17+
<img src="pyramid.png" alt="regular pyramid built from four equilateral triangles">
18+
</picture>
19+
20+
<style>
21+
p {
22+
& b {
23+
color: red;
24+
}
25+
}
26+
</style>
627
`;
728

829
console.log(preprocessor().markup({ content }).code);

0 commit comments

Comments
 (0)