handle no closing match for script and style for eof and non-eof failures

pull/5328/head
pngwn 5 years ago
parent bb952fe0b9
commit cb24094963

@ -177,9 +177,9 @@ export class Parser {
return identifier; return identifier;
} }
read_until(pattern: RegExp) { read_until(pattern: RegExp, error?: {code: string; message: string}) {
if (this.index >= this.template.length) if (this.index >= this.template.length)
this.error({ this.error(error || {
code: `unexpected-eof`, code: `unexpected-eof`,
message: 'Unexpected end of input' message: 'Unexpected end of input'
}); });

@ -28,16 +28,15 @@ function get_context(parser: Parser, attributes: any[], start: number): string {
export default function read_script(parser: Parser, start: number, attributes: Node[]): Script { export default function read_script(parser: Parser, start: number, attributes: Node[]): Script {
const script_start = parser.index; const script_start = parser.index;
const script_end = /<\/script\s*>/.exec(parser.template.slice(script_start)); const error_message ={
if (!script_end) parser.error({
code: `unclosed-script`, code: `unclosed-script`,
message: `<script> must have a closing tag` message: `<script> must have a closing tag`
}); };
const [data, matched] = parser.read_until(/<\/script\s*>/, error_message);
const source = parser.template.slice(0, script_start).replace(/[^\n]/g, ' ') + if (!matched) parser.error(error_message);
parser.template.slice(script_start, script_end.index + script_start);
parser.index = script_end.index + script_end[0].length + script_start ; const source = parser.template.slice(0, script_start).replace(/[^\n]/g, ' ') + data;
let ast: Program; let ast: Program;
@ -50,6 +49,8 @@ export default function read_script(parser: Parser, start: number, attributes: N
// TODO is this necessary? // TODO is this necessary?
(ast as any).start = script_start; (ast as any).start = script_start;
parser.eat(matched, true);
return { return {
type: 'Script', type: 'Script',
start, start,

@ -6,9 +6,15 @@ import { Style } from '../../interfaces';
export default function read_style(parser: Parser, start: number, attributes: Node[]): Style { export default function read_style(parser: Parser, start: number, attributes: Node[]): Style {
const content_start = parser.index; const content_start = parser.index;
const [styles, matched] = parser.read_until(/<\/style\s*>/); const error_message ={
code: `unclosed-style`,
message: `<style> must have a closing tag`
};
const [styles, matched] = parser.read_until(/<\/style\s*>/, error_message);
const content_end = parser.index; const content_end = parser.index;
if (!matched) parser.error(error_message);
let ast; let ast;
try { try {
@ -70,12 +76,11 @@ export default function read_style(parser: Parser, start: number, attributes: No
}); });
parser.eat(matched, true); parser.eat(matched, true);
const end = parser.index;
return { return {
type: 'Style', type: 'Style',
start, start,
end, end: parser.index,
attributes, attributes,
children: ast.children, children: ast.children,
content: { content: {

@ -0,0 +1,10 @@
{
"code": "unclosed-style",
"message": "<style> must have a closing tag",
"start": {
"line": 3,
"column": 22,
"character": 31
},
"pos": 31
}

@ -0,0 +1,10 @@
{
"code": "unclosed-style",
"message": "<style> must have a closing tag",
"start": {
"line": 3,
"column": 7,
"character": 31
},
"pos": 31
}

@ -0,0 +1,3 @@
<h1>Hello {name}!</h1>
<style>
Loading…
Cancel
Save