Skip to content

Commit cdbc6b2

Browse files
authored
fix: improve missing attribute value error (#134)
1 parent 80834b7 commit cdbc6b2

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

.changeset/moody-donuts-enjoy.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmljs-parser": patch
3+
---
4+
5+
Improve missing attribute error when the tag is immediately closed without the attribute value.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1╭─ <span class=>
2+
│ ││ │ ╰─ error(INVALID_ATTRIBUTE_VALUE:Missing value for attribute)
3+
│ ││ ╰─ attrName "class"
4+
│ │╰─ tagName "span"
5+
╰─ ╰─ openTagStart
6+
2╰─
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<span class=>

src/states/ATTRIBUTE.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,22 @@ function shouldTerminateHtmlAttrName(code: number, data: string, pos: number) {
266266
}
267267
}
268268

269-
function shouldTerminateHtmlAttrValue(code: number, data: string, pos: number) {
269+
function shouldTerminateHtmlAttrValue(
270+
this: STATE.ExpressionMeta,
271+
code: number,
272+
data: string,
273+
pos: number
274+
) {
270275
switch (code) {
271276
case CODE.COMMA:
272277
return true;
273278
case CODE.FORWARD_SLASH:
274279
return data.charCodeAt(pos + 1) === CODE.CLOSE_ANGLE_BRACKET;
275-
// Add special case for =>
276280
case CODE.CLOSE_ANGLE_BRACKET:
277-
return data.charCodeAt(pos - 1) !== CODE.EQUAL;
281+
// Add special case for =>
282+
// We only look behind to match => if we're not at the start of the expression
283+
// otherwise this would match something like "<span class=>".
284+
return pos === this.start || data.charCodeAt(pos - 1) !== CODE.EQUAL;
278285
default:
279286
return false;
280287
}

0 commit comments

Comments
 (0)