Skip to content

Commit b67ef91

Browse files
committed
Merge branch 'master' into gh-301
2 parents 530277f + ce1916f commit b67ef91

File tree

6 files changed

+115
-17
lines changed

6 files changed

+115
-17
lines changed

src/generators/shared/processCss.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
1-
import parse from 'css-tree/lib/parser/index.js';
2-
import walk from 'css-tree/lib/utils/walk.js';
3-
41
const commentsPattern = /\/\*[\s\S]*?\*\//g;
52

63
export default function processCss ( parsed, code ) {
74
const css = parsed.css.content.styles;
85
const offset = parsed.css.content.start;
96

10-
const ast = parse( css, {
11-
positions: true
12-
});
13-
147
const attr = `[svelte-${parsed.hash}]`;
158

16-
walk.rules( ast, rule => {
17-
rule.selector.children.each( selector => {
18-
const start = selector.loc.start.offset;
19-
const end = selector.loc.end.offset;
9+
function transform ( rule ) {
10+
rule.selector.children.forEach( selector => {
11+
const start = selector.start - offset;
12+
const end = selector.end - offset;
2013

2114
const selectorString = css.slice( start, end );
2215

23-
const firstToken = selector.children.head;
16+
const firstToken = selector.children[0];
2417

2518
let transformed;
2619

27-
if ( firstToken.data.type === 'TypeSelector' ) {
28-
const insert = firstToken.data.loc.end.offset;
20+
if ( firstToken.type === 'TypeSelector' ) {
21+
const insert = firstToken.end - offset;
2922
const head = css.slice( start, insert );
3023
const tail = css.slice( insert, end );
3124

@@ -36,7 +29,19 @@ export default function processCss ( parsed, code ) {
3629

3730
code.overwrite( start + offset, end + offset, transformed );
3831
});
39-
});
32+
}
33+
34+
function walk ( node ) {
35+
if ( node.type === 'Rule' ) {
36+
transform( node );
37+
} else if ( node.children ) {
38+
node.children.forEach( walk );
39+
} else if ( node.block ) {
40+
walk( node.block );
41+
}
42+
}
43+
44+
parsed.css.children.forEach( walk );
4045

4146
// remove comments. TODO would be nice if this was exposed in css-tree
4247
let match;

src/parse/read/style.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
1+
import parse from 'css-tree/lib/parser/index.js';
2+
import walk from 'css-tree/lib/utils/walk.js';
3+
14
export default function readStyle ( parser, start, attributes ) {
25
const contentStart = parser.index;
36
const styles = parser.readUntil( /<\/style>/ );
47
const contentEnd = parser.index;
58

9+
let ast;
10+
11+
try {
12+
ast = parse( styles, {
13+
positions: true,
14+
offset: contentStart
15+
});
16+
} catch ( err ) {
17+
if ( err.name === 'CssSyntaxError' ) {
18+
parser.error( err.message, err.offset );
19+
} else {
20+
throw err;
21+
}
22+
}
23+
24+
// tidy up AST
25+
walk.all( ast, node => {
26+
if ( node.loc ) {
27+
node.start = node.loc.start.offset;
28+
node.end = node.loc.end.offset;
29+
delete node.loc;
30+
}
31+
});
32+
633
parser.eat( '</style>', true );
734
const end = parser.index;
835

936
return {
1037
start,
1138
end,
1239
attributes,
40+
children: JSON.parse( JSON.stringify( ast.children ) ),
1341
content: {
1442
start: contentStart,
1543
end: contentEnd,

test/parse.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe( 'parse', () => {
1616
const input = fs.readFileSync( `test/parser/${dir}/input.html`, 'utf-8' ).replace( /\s+$/, '' );
1717

1818
try {
19-
const actual = svelte.parse( input );
19+
const actual = JSON.parse( JSON.stringify( svelte.parse( input ) ) );
2020
const expected = require( `./parser/${dir}/output.json` );
2121

2222
assert.deepEqual( actual.html, expected.html );

test/parser/css/output.json

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,61 @@
2929
"start": 23,
3030
"end": 48,
3131
"styles": "\n\tdiv {\n\t\tcolor: red;\n\t}\n"
32-
}
32+
},
33+
"children": [
34+
{
35+
"type": "Rule",
36+
"start": 25,
37+
"end": 47,
38+
"selector": {
39+
"type": "SelectorList",
40+
"start": 25,
41+
"end": 28,
42+
"children": [
43+
{
44+
"type": "Selector",
45+
"start": 25,
46+
"end": 28,
47+
"children": [
48+
{
49+
"type": "TypeSelector",
50+
"start": 25,
51+
"end": 28,
52+
"name": "div"
53+
}
54+
]
55+
}
56+
]
57+
},
58+
"block": {
59+
"type": "Block",
60+
"start": 29,
61+
"end": 47,
62+
"children": [
63+
{
64+
"type": "Declaration",
65+
"start": 33,
66+
"end": 43,
67+
"important": false,
68+
"property": "color",
69+
"value": {
70+
"type": "Value",
71+
"start": 39,
72+
"end": 43,
73+
"children": [
74+
{
75+
"type": "Identifier",
76+
"start": 40,
77+
"end": 43,
78+
"name": "red"
79+
}
80+
]
81+
}
82+
}
83+
]
84+
}
85+
}
86+
]
3387
},
3488
"js": null
3589
}

test/parser/error-css/error.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"message": "LeftCurlyBracket is expected",
3+
"loc": {
4+
"line": 2,
5+
"column": 16
6+
},
7+
"pos": 24
8+
}

test/parser/error-css/input.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<style>
2+
this is not css
3+
</style>

0 commit comments

Comments
 (0)