Skip to content

Commit

Permalink
better error message for unclose tag due to autoclosing
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Jun 24, 2020
1 parent 1c39f60 commit 801050d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/compiler/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import error from '../utils/error';

type ParserState = (parser: Parser) => (ParserState | void);

interface LastAutoCloseTag {
tag: string;
reason: string;
depth: number;
}

export class Parser {
readonly template: string;
readonly filename?: string;
Expand All @@ -20,6 +26,7 @@ export class Parser {
css: Style[] = [];
js: Script[] = [];
meta_tags = {};
last_auto_closed_tag?: LastAutoCloseTag;

constructor(template: string, options: ParserOptions) {
if (typeof template !== 'string') {
Expand Down
16 changes: 16 additions & 0 deletions src/compiler/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ export default function tag(parser: Parser) {

parser.eat('>', true);

if (parser.last_auto_closed_tag && parser.last_auto_closed_tag.tag === name) {
parser.error({
code: `invalid-closing-tag`,
message: `<${parser.last_auto_closed_tag.tag}> cannot have child element <${parser.last_auto_closed_tag.reason}>`,
}, start);
}

// close any elements that don't have their own closing tags, e.g. <div><p></div>
while (parent.name !== name) {
if (parent.type !== 'Element')
Expand All @@ -148,10 +155,19 @@ export default function tag(parser: Parser) {
parent.end = parser.index;
parser.stack.pop();

if (parser.last_auto_closed_tag && parser.stack.length < parser.last_auto_closed_tag.depth) {
parser.last_auto_closed_tag = null;
}

return;
} else if (closing_tag_omitted(parent.name, name)) {
parent.end = start;
parser.stack.pop();
parser.last_auto_closed_tag ={
tag: parent.name,
reason: name,
depth: parser.stack.length,
};
}

const unique_names: Set<string> = new Set();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"code": "invalid-closing-tag",
"message": "<p> cannot have child element <pre>",
"pos": 24,
"start": {
"character": 24,
"column": 0,
"line": 3
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>
<pre>pre tag</pre>
</p>

0 comments on commit 801050d

Please sign in to comment.