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: ensure unary expression continuation is a word boundary #156

Merged
merged 1 commit into from
Mar 11, 2023
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/stale-impalas-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"htmljs-parser": patch
---

Fix regression where the parser would continue unary keyword expressions even if the keyword was inside a word boundary. Eg `<div class=thing_new x>` would cause the parser to see the expression as `thing_` and `new x`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
1╭─ tag a = class b {}
│ │ │ │ ╰─ attrValue.value "class b {}"
│ │ │ ╰─ attrValue "= class b {}"
│ │ ╰─ attrName
╰─ ╰─ tagName "tag"
2╭─
╰─ ╰─ openTagEnd
3╭─ tag a = class, b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "class"
│ │ │ ╰─ attrValue "= class"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
╰─ ╰─ tagName "tag"
4╭─
╰─ ╰─ openTagEnd
5╭─ <tag a = class></tag>
│ ││ │ │ │ ││ │ ╰─ closeTagEnd(tag)
│ ││ │ │ │ ││ ╰─ closeTagName "tag"
│ ││ │ │ │ │╰─ closeTagStart "</"
│ ││ │ │ │ ╰─ openTagEnd
│ ││ │ │ ╰─ attrValue.value "class"
│ ││ │ ╰─ attrValue "= class"
│ ││ ╰─ attrName
│ │╰─ tagName "tag"
│ ├─ closeTagEnd(tag)
╰─ ╰─ openTagStart
6├─
7╭─ <tag a = class/>
│ ││ │ │ │ ╰─ openTagEnd:selfClosed "/>"
│ ││ │ │ ╰─ attrValue.value "class"
│ ││ │ ╰─ attrValue "= class"
│ ││ ╰─ attrName
│ │╰─ tagName "tag"
╰─ ╰─ openTagStart
8├─
9╭─ tag a = classthing b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "classthing"
│ │ │ ╰─ attrValue "= classthing"
│ │ ╰─ attrName
╰─ ╰─ tagName "tag"
10╭─
╰─ ╰─ openTagEnd
11╭─ tag a = testclass b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "testclass"
│ │ │ ╰─ attrValue "= testclass"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
╰─ ╰─ tagName "tag"
12╭─
╰─ ╰─ openTagEnd
13╭─ tag a = test_class b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "test_class"
│ │ │ ╰─ attrValue "= test_class"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
╰─ ╰─ tagName "tag"
14╭─
╰─ ╰─ openTagEnd
15╭─ tag a = test$class b
│ │ │ │ │ ╰─ attrName
│ │ │ │ ╰─ attrValue.value "test$class"
│ │ │ ╰─ attrValue "= test$class"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
╰─ ╰─ tagName "tag"
16╭─
╰─ ╰─ openTagEnd
17╭─ tag a = test+class b
│ │ │ │ ╰─ attrValue.value "test+class b"
│ │ │ ╰─ attrValue "= test+class b"
│ │ ╰─ attrName
│ ├─ closeTagEnd(tag)
╰─ ╰─ tagName "tag"
18╭─
│ ├─ openTagEnd
╰─ ╰─ closeTagEnd(tag)
17 changes: 17 additions & 0 deletions src/__tests__/fixtures/attr-complex-unary/input.marko
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tag a = class b {}

tag a = class, b

<tag a = class></tag>

<tag a = class/>

tag a = classthing b

tag a = testclass b

tag a = test_class b

tag a = test$class b

tag a = test+class b
3 changes: 2 additions & 1 deletion src/states/EXPRESSION.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ function lookBehindForOperator(data: string, pos: number): number {
for (const keyword of unaryKeywords) {
const keywordPos = lookBehindFor(data, curPos, keyword);
if (keywordPos !== -1) {
return data.charCodeAt(keywordPos - 1) === CODE.PERIOD
const prevCode = data.charCodeAt(keywordPos - 1);
return prevCode === CODE.PERIOD || isWordCode(prevCode)
? -1
: keywordPos;
}
Expand Down