Skip to content

Commit bcfd809

Browse files
authored
fix: ensure keyword operators are always followed by a space (#146)
1 parent b232aa2 commit bcfd809

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

.changeset/beige-seahorses-talk.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"htmljs-parser": patch
3+
---
4+
5+
Fixes an issue where attribute names that started with a keyword (eg: `as-thing` or `instanceof-thing`) were incorrectly treated as an expression continuation.

src/__tests__/fixtures/attr-complex-instanceof/__snapshots__/attr-complex-instanceof.expected.txt

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,34 @@
4444
╰─ ╰─ tagName "tag"
4545
7╭─
4646
╰─ ╰─ openTagEnd
47-
8╭─ tag a = 'foo' instanceof, b
48-
│ │ │ │ │ │ ╰─ attrName
49-
│ │ │ │ │ ╰─ attrName "instanceof"
47+
8╭─ tag a = 'foo' instanceof-test;
48+
│ │ │ │ │ ╰─ attrName "instanceof-test"
5049
│ │ │ │ ╰─ attrValue.value "'foo'"
5150
│ │ │ ╰─ attrValue "= 'foo'"
5251
│ │ ╰─ attrName
5352
│ ├─ closeTagEnd(tag)
5453
╰─ ╰─ tagName "tag"
55-
9╭─
56-
╰─ ╰─ openTagEnd
57-
10╭─ <tag a = 'foo' instanceof></tag>
54+
9╭─ tag a = 'foo' instanceof_test;
55+
│ │ │ │ │ ╰─ attrName "instanceof_test"
56+
│ │ │ │ ╰─ attrValue.value "'foo'"
57+
│ │ │ ╰─ attrValue "= 'foo'"
58+
│ │ ╰─ attrName
59+
│ ├─ closeTagEnd(tag)
60+
│ ├─ openTagEnd
61+
╰─ ╰─ tagName "tag"
62+
10╭─
63+
╰─ ╰─ openTagEnd
64+
11╭─ tag a = 'foo' instanceof, b
65+
│ │ │ │ │ │ ╰─ attrName
66+
│ │ │ │ │ ╰─ attrName "instanceof"
67+
│ │ │ │ ╰─ attrValue.value "'foo'"
68+
│ │ │ ╰─ attrValue "= 'foo'"
69+
│ │ ╰─ attrName
70+
│ ├─ closeTagEnd(tag)
71+
╰─ ╰─ tagName "tag"
72+
12╭─
73+
╰─ ╰─ openTagEnd
74+
13╭─ <tag a = 'foo' instanceof></tag>
5875
│ ││ │ │ │ │ ││ │ ╰─ closeTagEnd(tag)
5976
│ ││ │ │ │ │ ││ ╰─ closeTagName "tag"
6077
│ ││ │ │ │ │ │╰─ closeTagStart "</"
@@ -66,25 +83,25 @@
6683
│ │╰─ tagName "tag"
6784
│ ├─ closeTagEnd(tag)
6885
╰─ ╰─ openTagStart
69-
11╭─ <tag a = 'foo' instanceof/>
86+
14╭─ <tag a = 'foo' instanceof/>
7087
│ ││ │ │ │ │ ╰─ openTagEnd:selfClosed "/>"
7188
│ ││ │ │ │ ╰─ attrName "instanceof"
7289
│ ││ │ │ ╰─ attrValue.value "'foo'"
7390
│ ││ │ ╰─ attrValue "= 'foo'"
7491
│ ││ ╰─ attrName
7592
│ │╰─ tagName "tag"
7693
╰─ ╰─ openTagStart
77-
12├─
78-
13╭─ tag a = 'foo' instanceofthing String
94+
15├─
95+
16╭─ tag a = 'foo' instanceofthing String
7996
│ │ │ │ │ │ ╰─ attrName "String"
8097
│ │ │ │ │ ╰─ attrName "instanceofthing"
8198
│ │ │ │ ╰─ attrValue.value "'foo'"
8299
│ │ │ ╰─ attrValue "= 'foo'"
83100
│ │ ╰─ attrName
84101
╰─ ╰─ tagName "tag"
85-
14╭─
102+
17╭─
86103
╰─ ╰─ openTagEnd
87-
15╭─ tag a = 'foo' instanceof
104+
18╭─ tag a = 'foo' instanceof
88105
│ │ │ │ │ │ ├─ closeTagEnd(tag)
89106
│ │ │ │ │ │ ╰─ openTagEnd
90107
│ │ │ │ │ ╰─ attrName "instanceof"

src/__tests__/fixtures/attr-complex-instanceof/input.marko

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ tag a = 'foo' instanceof := String
55
tag a = 'foo' instanceof= String
66
tag a = 'foo' instanceof;
77

8+
tag a = 'foo' instanceof-test;
9+
tag a = 'foo' instanceof_test;
10+
811
tag a = 'foo' instanceof, b
912

1013
<tag a = 'foo' instanceof></tag>

src/states/EXPRESSION.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ function lookAheadForOperator(data: string, pos: number): number {
342342
nextPos = lookAheadWhile(isWhitespaceCode, data, nextPos + 2);
343343
if (nextPos === max) return -1;
344344
nextCode = data.charCodeAt(nextPos);
345-
} else if (isWordCode(nextCode)) {
346-
// bail if we are continuing a word, eg "**in**teger"
345+
} else {
346+
// bail if we didn't match a space keyword.
347347
return -1;
348348
}
349349

0 commit comments

Comments
 (0)