Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/concise-attr-group-without-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": minor
---

Read an attribute group (`[`) at the start of a concise line as following an empty tag name, as `.class` and `|params|` are, rather than reading its first attribute as the tag name.
5 changes: 5 additions & 0 deletions .changeset/concise-semicolon-line.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": minor
---

Ignore a concise line that starts with `;`, which opened a tag with no name. As after a tag's `;`, only comments may follow it.
5 changes: 5 additions & 0 deletions .changeset/concise-tag-name-hyphen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": minor
---

Read a concise line starting with `-` and a word character (eg `-if`) as a tag whose name starts with `-`, as `<-if>` already is in HTML mode. A single `-` followed by anything else is still an error, and so is any single `-` within a tag that only allows text.
5 changes: 5 additions & 0 deletions .changeset/open-tag-ended-before-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": minor
---

Name an open tag that ends before its name (eg a concise line of just `,`, or `<,>`) empty, so a tag never ends without `onOpenTagName`; the compiler and editor tooling crashed on these, and `<,>` crashed the parser at the end of the template.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
1╭─ [a=1]
│ │││╰─ attrValue.value
│ ││╰─ attrValue "=1"
│ │╰─ attrName
╰─ ╰─ tagName
2╭─ [
│ ├─ closeTagEnd()
│ ├─ openTagEnd
╰─ ╰─ tagName
3╭─ class="x"
│ │ │╰─ attrValue.value "\"x\""
│ │ ╰─ attrValue "=\"x\""
╰─ ╰─ attrName "class"
4├─ ]
5╭─ []
│ ├─ closeTagEnd()
│ ├─ openTagEnd
╰─ ╰─ tagName
6╭─
│ ├─ openTagEnd
╰─ ╰─ closeTagEnd()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[a=1]
[
class="x"
]
[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
1╭─ ;
2╭─ div
╰─ ╰─ tagName "div"
3╭─ ; // comment
│ │ │ ╰─ comment.value " comment"
│ │ ╰─ comment "// comment"
╰─ ╰─ openTagEnd
4╭─ span
╰─ ╰─ tagName "span"
5╭─ ; /* block */
│ │ │ ╰─ comment.value " block "
│ │ ╰─ comment "/* block */"
│ ├─ closeTagEnd(div)
│ ├─ closeTagEnd(span)
╰─ ╰─ openTagEnd
6╭─ ;<!-- html -->
│ │ ╰─ comment.value " html "
╰─ ╰─ comment "<!-- html -->"
7╰─
6 changes: 6 additions & 0 deletions src/__tests__/fixtures/concise-semicolon-line/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;
div
; // comment
span
; /* block */
;<!-- html -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1╭─ style
╰─ ╰─ tagName "style"
2╭─ -foo
│ │ ╰─ error(INVALID_LINE_START:A line in concise mode cannot start with a single hyphen. Use "--" instead. See: https://github.com/marko-js/htmljs-parser/issues/43)
╰─ ╰─ openTagEnd
3╰─
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
style
-foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
1╭─ -if(x)
│ │ │╰─ tagArgs.value
│ │ ╰─ tagArgs "(x)"
╰─ ╰─ tagName "-if"
2╭─ span
│ │ ╰─ tagName "span"
╰─ ╰─ openTagEnd
3╭─ -else-if(y)
│ │ │╰─ tagArgs.value
│ │ ╰─ tagArgs "(y)"
│ ├─ closeTagEnd(-if)
│ ├─ closeTagEnd(span)
│ ├─ openTagEnd
╰─ ╰─ tagName "-else-if"
4╭─ -my-tag
│ │ ╰─ tagName "-my-tag"
╰─ ╰─ openTagEnd
5╭─
│ ├─ openTagEnd
│ ├─ closeTagEnd(-else-if)
╰─ ╰─ closeTagEnd(-my-tag)
4 changes: 4 additions & 0 deletions src/__tests__/fixtures/concise-tag-name-hyphen/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-if(x)
span
-else-if(y)
-my-tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
1╭─ ,;
2╭─ <,/>
│ │ ├─ openTagEnd:selfClosed "/>"
│ │ ╰─ tagName
│ ├─ closeTagEnd()
│ ├─ openTagEnd
│ ├─ tagName
╰─ ╰─ openTagStart
3╭─ <,></>
│ │ ││ ├─ closeTagEnd()
│ │ ││ ╰─ closeTagName
│ │ │╰─ closeTagStart "</"
│ │ ├─ openTagEnd
│ │ ╰─ tagName
╰─ ╰─ openTagStart
4├─ ,
5╭─
│ ├─ closeTagEnd()
│ ├─ openTagEnd
╰─ ╰─ tagName
4 changes: 4 additions & 0 deletions src/__tests__/fixtures/open-tag-ended-before-name/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
,;
<,/>
<,></>
,
11 changes: 11 additions & 0 deletions src/states/CONCISE_HTML_CONTENT.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
htmlEOF,
isWhitespaceCode,
isWordCode,
STATE,
type StateDefinition,
} from "../internal.ts";
Expand Down Expand Up @@ -106,6 +107,11 @@ export const CONCISE_HTML_CONTENT: StateDefinition = {
}

switch (code) {
case CODE.SEMICOLON:
// An empty line, as after a tag ended by ";".
this.pos++; // skip ;
STATE.endConciseLine(this, data);
return;
case CODE.OPEN_ANGLE_BRACKET:
this.beginMixedMode = true;
this.beginHtmlBlock(undefined, false); // pos stays at <
Expand All @@ -121,6 +127,11 @@ export const CONCISE_HTML_CONTENT: StateDefinition = {
if (data.charCodeAt(this.pos + 1) === CODE.HYPHEN) {
this.enterState(STATE.BEGIN_DELIMITED_HTML_BLOCK);
return; // pos stays at the first -, BEGIN_DELIMITED_HTML_BLOCK parses it
} else if (
parentTag?.type !== TagType.text &&
isWordCode(data.charCodeAt(this.pos + 1))
) {
break; // a tag name starting with "-" (eg "-if")
} else {
this.emitError(
this.pos,
Expand Down
91 changes: 61 additions & 30 deletions src/states/OPEN_TAG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {
isIndentCode,
isLineCode,
isWhitespaceCode,
isWordCode,
matchesCloseAngleBracket,
matchesCloseParen,
matchesPipe,
type Meta,
type Parser,
type Ranges,
STATE,
type StateDefinition,
Expand Down Expand Up @@ -69,9 +71,22 @@ export const OPEN_TAG: StateDefinition<OpenTagMeta> = {

exit(tag) {
const { selfClosed } = tag;
const start = this.pos - (this.isConcise ? 0 : selfClosed ? 2 : 1);

if (!tag.tagName) {
// An open tag can end before a name (eg a concise line of just ",", or
// "<,>"), so name it empty where it ends, as one starting with "|" is.
tag.tagName = {
start,
end: start,
quasis: [{ start, end: start }],
expressions: [],
};
tag.type = this.options.onOpenTagName?.(tag.tagName) || tag.type;
}

this.options.onOpenTagEnd?.({
start: this.pos - (this.isConcise ? 0 : selfClosed ? 2 : 1),
start,
end: this.pos,
selfClosed,
});
Expand Down Expand Up @@ -150,40 +165,17 @@ export const OPEN_TAG: StateDefinition<OpenTagMeta> = {
if (code === CODE.SEMICOLON) {
this.pos++; // skip ;
this.exitState();
if (!this.consumeWhitespaceOnLine(0)) {
switch (data.charCodeAt(this.pos)) {
case CODE.FORWARD_SLASH:
switch (data.charCodeAt(this.pos + 1)) {
case CODE.FORWARD_SLASH:
this.enterState(STATE.JS_COMMENT_LINE);
this.pos += 2; // skip //
return;
case CODE.ASTERISK:
this.enterState(STATE.JS_COMMENT_BLOCK);
this.pos += 2; // skip /*
return;
}
break;
case CODE.OPEN_ANGLE_BRACKET:
if (this.lookAheadFor("!--")) {
this.enterState(STATE.HTML_COMMENT);
this.pos += 4; // skip <!--
return;
}
break;
}

this.emitError(
this.pos,
ErrorCode.INVALID_CODE_AFTER_SEMICOLON,
"A semicolon indicates the end of a line. Only comments may follow it.",
);
}
endConciseLine(this, data);
return;
}

if (code === CODE.HYPHEN) {
if (data.charCodeAt(this.pos + 1) !== CODE.HYPHEN) {
if (!tag.tagName && isWordCode(data.charCodeAt(this.pos + 1))) {
this.enterState(STATE.TAG_NAME); // eg "-if"
return;
}

this.emitError(
tag,
ErrorCode.MALFORMED_OPEN_TAG,
Expand Down Expand Up @@ -236,6 +228,12 @@ export const OPEN_TAG: StateDefinition<OpenTagMeta> = {
return;
}

if (!tag.tagName) {
// eg "[a=1]" at a line start: an empty name, as with "|x|".
this.enterState(STATE.TAG_NAME);
return; // pos stays at [, which ends the name
}

tag.stage = TAG_STAGE.ATTR_GROUP;
this.pos++;
continue;
Expand Down Expand Up @@ -527,6 +525,39 @@ export const OPEN_TAG: StateDefinition<OpenTagMeta> = {
},
};

/** Only comments may follow the ";" ending a concise line. */
export function endConciseLine(parser: Parser, data: string) {
if (parser.consumeWhitespaceOnLine(0)) return;

switch (data.charCodeAt(parser.pos)) {
case CODE.FORWARD_SLASH:
switch (data.charCodeAt(parser.pos + 1)) {
case CODE.FORWARD_SLASH:
parser.enterState(STATE.JS_COMMENT_LINE);
parser.pos += 2; // skip //
return;
case CODE.ASTERISK:
parser.enterState(STATE.JS_COMMENT_BLOCK);
parser.pos += 2; // skip /*
return;
}
break;
case CODE.OPEN_ANGLE_BRACKET:
if (parser.lookAheadFor("!--")) {
parser.enterState(STATE.HTML_COMMENT);
parser.pos += 4; // skip <!--
return;
}
break;
}

parser.emitError(
parser.pos,
ErrorCode.INVALID_CODE_AFTER_SEMICOLON,
"A semicolon indicates the end of a line. Only comments may follow it.",
);
}

function shouldTerminateConciseTagVar(
code: number,
data: string,
Expand Down
Loading