parse <script> tags

pull/31/head
Rich-Harris 9 years ago
parent a44398b0f1
commit 564ddba4bc

@ -67,7 +67,7 @@ export default function parse ( template ) {
html: { html: {
start: 0, start: 0,
end: template.length, end: null,
type: 'Fragment', type: 'Fragment',
children: [] children: []
}, },
@ -85,6 +85,9 @@ export default function parse ( template ) {
state = state( parser ) || fragment; state = state( parser ) || fragment;
} }
const lastTemplateItem = parser.html.children[ parser.html.children.length - 1 ];
parser.html.end = lastTemplateItem ? lastTemplateItem.end : 0;
return { return {
html: parser.html, html: parser.html,
css: parser.css, css: parser.css,

@ -1,25 +1,38 @@
import { parse, tokenizer, tokTypes } from 'acorn'; import { parse, tokenizer } from 'acorn';
function spaces ( i ) {
let result = '';
while ( i-- ) result += ' ';
return result;
}
export default function readScript ( parser, start, attributes ) { export default function readScript ( parser, start, attributes ) {
const scriptStart = parser.index; const scriptStart = parser.index;
let scriptEnd = null; let scriptEnd = null;
const js = {
start,
end: null,
attributes,
content: null
};
const endPattern = /\s*<\/script\>/g;
for ( const token of tokenizer( parser.remaining() ) ) { for ( const token of tokenizer( parser.remaining() ) ) {
endPattern.lastIndex = scriptStart + token.end; parser.index = scriptStart + token.end;
if ( endPattern.test( parser.template ) ) { parser.allowWhitespace();
if ( parser.eat( '</script>' ) ) {
scriptEnd = scriptStart + token.end; scriptEnd = scriptStart + token.end;
break; break;
} }
} }
js.content = parse( ) const source = spaces( scriptStart ) + parser.template.slice( scriptStart, scriptEnd );
const ast = parse( source, {
ecmaVersion: 8,
sourceType: 'module'
});
ast.start = scriptStart;
return {
start,
end: parser.index,
attributes,
content: ast
};
} }

@ -54,7 +54,7 @@ export default function tag ( parser ) {
} }
parser.eat( '>', true ); parser.eat( '>', true );
parser[ special.id ] = special.read( parser, start, attributes ); parser[ special.property ] = special.read( parser, start, attributes );
return; return;
} }

@ -39,5 +39,83 @@
] ]
}, },
"css": null, "css": null,
"js": null "js": {
"start": 26,
"end": 105,
"attributes": [],
"content": {
"type": "Program",
"start": 34,
"end": 95,
"body": [
{
"declaration": {
"start": 51,
"end": 94,
"type": "ObjectExpression",
"properties": [
{
"start": 55,
"end": 91,
"type": "Property",
"computed": false,
"key": {
"start": 55,
"end": 59,
"type": "Identifier",
"name": "data"
},
"kind": "init",
"method": false,
"shorthand": false,
"value": {
"start": 61,
"end": 91,
"type": "ArrowFunctionExpression",
"async": false,
"body": {
"start": 68,
"end": 90,
"type": "ObjectExpression",
"properties": [
{
"start": 73,
"end": 86,
"type": "Property",
"computed": false,
"key": {
"start": 73,
"end": 77,
"type": "Identifier",
"name": "name"
},
"kind": "init",
"method": false,
"shorthand": false,
"value": {
"start": 79,
"end": 86,
"type": "Literal",
"raw": "'world'",
"value": "world"
}
}
]
},
"expression": true,
"generator": false,
"id": null,
"params": []
}
}
]
},
"end": 95,
"start": 36,
"type": "ExportDefaultDeclaration"
}
],
"sourceType": "module"
}
}
} }

Loading…
Cancel
Save