Skip to content

Commit

Permalink
fix: improve missing attribute value error (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey authored Aug 12, 2022
1 parent 80834b7 commit cdbc6b2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-donuts-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": patch
---

Improve missing attribute error when the tag is immediately closed without the attribute value.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1╭─ <span class=>
│ ││ │ ╰─ error(INVALID_ATTRIBUTE_VALUE:Missing value for attribute)
│ ││ ╰─ attrName "class"
│ │╰─ tagName "span"
╰─ ╰─ openTagStart
2╰─
1 change: 1 addition & 0 deletions src/__tests__/fixtures/attr-value-missing/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span class=>
13 changes: 10 additions & 3 deletions src/states/ATTRIBUTE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,22 @@ function shouldTerminateHtmlAttrName(code: number, data: string, pos: number) {
}
}

function shouldTerminateHtmlAttrValue(code: number, data: string, pos: number) {
function shouldTerminateHtmlAttrValue(
this: STATE.ExpressionMeta,
code: number,
data: string,
pos: number
) {
switch (code) {
case CODE.COMMA:
return true;
case CODE.FORWARD_SLASH:
return data.charCodeAt(pos + 1) === CODE.CLOSE_ANGLE_BRACKET;
// Add special case for =>
case CODE.CLOSE_ANGLE_BRACKET:
return data.charCodeAt(pos - 1) !== CODE.EQUAL;
// Add special case for =>
// We only look behind to match => if we're not at the start of the expression
// otherwise this would match something like "<span class=>".
return pos === this.start || data.charCodeAt(pos - 1) !== CODE.EQUAL;
default:
return false;
}
Expand Down

0 comments on commit cdbc6b2

Please sign in to comment.