update script end tag parsing to allow whitespace after tagname

pull/5328/head
pngwn 5 years ago
parent 8148a7a334
commit e88191f285

@ -3,7 +3,7 @@ import { Parser } from '../index';
import { Script } from '../../interfaces';
import { Node, Program } from 'estree';
const script_closing_tag = '</script>';
// const script_closing_tag = '</script>';
function get_context(parser: Parser, attributes: any[], start: number): string {
const context = attributes.find(attribute => attribute.name === 'context');
@ -28,18 +28,21 @@ function get_context(parser: Parser, attributes: any[], start: number): string {
return value;
}
const RE_SCRIPT_END = /<\/script\s*>/;
export default function read_script(parser: Parser, start: number, attributes: Node[]): Script {
const script_start = parser.index;
const script_end = parser.template.indexOf(script_closing_tag, script_start);
const script_end = RE_SCRIPT_END.exec(parser.template.slice(script_start));
if (script_end === -1) parser.error({
if (!script_end) parser.error({
code: `unclosed-script`,
message: `<script> must have a closing tag`
});
const source = parser.template.slice(0, script_start).replace(/[^\n]/g, ' ') +
parser.template.slice(script_start, script_end);
parser.index = script_end + script_closing_tag.length;
parser.template.slice(script_start, script_end.index + script_start);
parser.index = script_end.index + script_end[0].length + script_start ;
let ast: Program;

@ -0,0 +1,10 @@
<script>
let name = 'world';
</script
>
<h1>Hello {name}!</h1>

@ -0,0 +1,150 @@
{
"html": {
"start": 51,
"end": 73,
"type": "Fragment",
"children": [
{
"start": 49,
"end": 51,
"type": "Text",
"raw": "\n\n",
"data": "\n\n"
},
{
"start": 51,
"end": 73,
"type": "Element",
"name": "h1",
"attributes": [],
"children": [
{
"start": 55,
"end": 61,
"type": "Text",
"raw": "Hello ",
"data": "Hello "
},
{
"start": 61,
"end": 67,
"type": "MustacheTag",
"expression": {
"type": "Identifier",
"start": 62,
"end": 66,
"loc": {
"start": {
"line": 10,
"column": 11
},
"end": {
"line": 10,
"column": 15
}
},
"name": "name"
}
},
{
"start": 67,
"end": 68,
"type": "Text",
"raw": "!",
"data": "!"
}
]
}
]
},
"instance": {
"type": "Script",
"start": 0,
"end": 49,
"context": "default",
"content": {
"type": "Program",
"start": 8,
"end": 30,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 0
}
},
"body": [
{
"type": "VariableDeclaration",
"start": 10,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 20
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 14,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 19
}
},
"id": {
"type": "Identifier",
"start": 14,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 9
}
},
"name": "name"
},
"init": {
"type": "Literal",
"start": 21,
"end": 28,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 19
}
},
"value": "world",
"raw": "'world'"
}
}
],
"kind": "let"
}
],
"sourceType": "module"
}
}
}
Loading…
Cancel
Save