Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve missing attribute value error #134

Merged
merged 1 commit into from
Aug 12, 2022
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/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