Skip to content

Commit

Permalink
fix: issue with spreads, closing tags & default attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Mar 1, 2022
1 parent 3728e3e commit 89bbe69
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 17 deletions.
6 changes: 2 additions & 4 deletions src/core/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,17 +445,17 @@ export class Parser {
}

next(): Parser {
if (this.hasEvent()) return this;

let { pos } = this;
const { maxPos, data } = this;

if (pos >= maxPos) {
if (this.hasEvent()) return this;
this.done = true;
return this;
}

do {
if (this.hasEvent()) return this;
const code = data.charCodeAt(pos);

if (code === CODE.NEWLINE) {
Expand All @@ -475,8 +475,6 @@ export class Parser {
} else {
this.forward = true;
}

if (this.hasEvent()) return this;
} while ((pos = this.pos) < maxPos);

do {
Expand Down
3 changes: 2 additions & 1 deletion src/states/ATTRIBUTE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,17 @@ export const ATTRIBUTE: StateDefinition<AttrMeta> = {
(code === CODE.PERIOD && this.lookAheadFor(".."))
) {
attr.valueStart = this.pos;
ensureAttrName(this, attr);

if (code === CODE.COLON) {
ensureAttrName(this, attr);
attr.bound = true;
this.skip(2); // skip :=
this.consumeWhitespace();
} else if (code === CODE.PERIOD) {
attr.spread = true;
this.skip(3); // skip ...
} else {
ensureAttrName(this, attr);
this.skip(1); // skip =
this.consumeWhitespace();
}
Expand Down
27 changes: 16 additions & 11 deletions src/states/CLOSE_TAG.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CODE, STATE, StateDefinition, Parser, Range } from "../internal";
import { CODE, StateDefinition, Parser, Range } from "../internal";

// We enter STATE.CLOSE_TAG after we see "</"
export const CLOSE_TAG: StateDefinition = {
Expand Down Expand Up @@ -49,16 +49,16 @@ export function checkForClosingTag(parser: Parser) {
}

if (match) {
if (parser.activeState === STATE.JS_COMMENT_LINE) {
parser.endText();

if (
ensureExpectedCloseTag(parser, {
start: parser.pos,
end: parser.skip(skip),
})
) {
parser.exitState();
}

parser.endText();
ensureExpectedCloseTag(parser, {
start: parser.pos,
end: parser.skip(skip),
});
parser.forward = false;
return true;
}

Expand All @@ -71,13 +71,15 @@ function ensureExpectedCloseTag(parser: Parser, closeTag: Range) {
const closeTagNameEnd = closeTag.end - 1; // strip >

if (!activeTag) {
return parser.emitError(
parser.emitError(
closeTag!,
"EXTRA_CLOSING_TAG",
'The closing "' +
parser.read({ start: closeTagNameStart, end: closeTagNameEnd }) +
'" tag was not expected'
);

return false;
}

const closeTagNamePos = {
Expand All @@ -101,7 +103,7 @@ function ensureExpectedCloseTag(parser: Parser, closeTag: Range) {
end: activeTag.shorthandEnd,
})
) {
return parser.emitError(
parser.emitError(
closeTag,
"MISMATCHED_CLOSING_TAG",
'The closing "' +
Expand All @@ -110,9 +112,12 @@ function ensureExpectedCloseTag(parser: Parser, closeTag: Range) {
(parser.read(activeTag.tagName) || "div") +
'" tag'
);

return false;
}
}
}

parser.closeTag(closeTag.start, closeTag.end, closeTagNamePos);
return true;
}
10 changes: 10 additions & 0 deletions test/autotest/attr-spread/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<span ...(a) SELF_CLOSED>
</span>
<span a=(EMPTY) ...(b) SELF_CLOSED>
</span>
<span a=(EMPTY) ...(b) ...(c) SELF_CLOSED>
</span>
<span a=(EMPTY) ...(b) c=(EMPTY) ...(d) SELF_CLOSED>
</span>
<span a=(EMPTY) ...(b) c=(EMPTY) ...(d) e=(EMPTY) SELF_CLOSED>
</span>
5 changes: 5 additions & 0 deletions test/autotest/attr-spread/input.htmljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<span ...a/>
<span a ...b/>
<span a ...b ...c/>
<span a ...b c ...d/>
<span a ...b c ...d e/>
15 changes: 15 additions & 0 deletions test/autotest/html-comment-tag/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<div>
text:"\n "
comment:"123"
text:"\n "
<html-comment>
text:"abc"
</html-comment>
text:"\n "
comment:"[if lt IE 9]><script src=\"...\"></script><![endif]"
text:"\n "
<html-comment>
text:"[if lt IE 9]><script src=\"...\"></script><![endif]"
</html-comment>
text:"\n"
</div>
6 changes: 6 additions & 0 deletions test/autotest/html-comment-tag/input.htmljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
<!--123-->
<html-comment>abc</html-comment>
<!--[if lt IE 9]><script src="..."></script><![endif]-->
<html-comment>[if lt IE 9]><script src="..."></script><![endif]</html-comment>
</div>
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<script>
text:"\n var foo = `Text 1: ${content}`;\n // ${foo}\n /*\n ${bar}\n */\n"
</script>
text:"\n"
6 changes: 6 additions & 0 deletions test/autotest/unclosed-tag-eof/expected.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
text:"\n "
<span SELF_CLOSED>
</span>
error:"Missing ending \"div\" tag" (code: "MISSING_END_TAG")
</div>
2 changes: 2 additions & 0 deletions test/autotest/unclosed-tag-eof/input.htmljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<div>
<span/>

0 comments on commit 89bbe69

Please sign in to comment.