Skip to content

Commit b1e68a3

Browse files
authored
fix: regex continuation issue (#121)
1 parent c91aa55 commit b1e68a3

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

.changeset/gorgeous-laws-grin.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+
Fix regression that causes incorrect expression continuations after regexps.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1╭─ <input pattern=/\s*?\S+\s*?/ name="test">
2+
│ ││ │ ││ │ ││ ╰─ openTagEnd
3+
│ ││ │ ││ │ │╰─ attrValue.value "\"test\""
4+
│ ││ │ ││ │ ╰─ attrValue "=\"test\""
5+
│ ││ │ ││ ╰─ attrName "name"
6+
│ ││ │ │╰─ attrValue.value "/\\s*?\\S+\\s*?/"
7+
│ ││ │ ╰─ attrValue "=/\\s*?\\S+\\s*?/"
8+
│ ││ ╰─ attrName "pattern"
9+
│ │╰─ tagName "input"
10+
╰─ ╰─ openTagStart
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<input pattern=/\s*?\S+\s*?/ name="test">

src/__tests__/fixtures/comments-within-open-tag/__snapshots__/comments-within-open-tag.expected.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
│ ││ │ ││ │ ││ ││ ╰─ closeTagName "div"
44
│ ││ │ ││ │ ││ │╰─ closeTagStart "</"
55
│ ││ │ ││ │ ││ ╰─ openTagEnd
6-
│ ││ │ ││ │ │╰─ attrValue.value "\"world\""
7-
│ ││ │ ││ │ ╰─ attrValue "=\"world\""
6+
│ ││ │ ││ │ │╰─ attrValue.value "\"world\" /* this is another comment */"
7+
│ ││ │ ││ │ ╰─ attrValue "=\"world\" /* this is another comment */"
88
│ ││ │ ││ ╰─ attrName "hello"
9-
│ ││ │ │╰─ attrValue.value "\"bar\""
10-
│ ││ │ ╰─ attrValue "=\"bar\""
9+
│ ││ │ │╰─ attrValue.value "\"bar\" /*this is a comment*/"
10+
│ ││ │ ╰─ attrValue "=\"bar\" /*this is a comment*/"
1111
│ ││ ╰─ attrName "foo"
1212
│ │╰─ tagName "div"
1313
╰─ ╰─ openTagStart
1414
2╭─ <div foo="bar" // this is a test
15-
│ ││ │ │╰─ attrValue.value "\"bar\""
16-
│ ││ │ ╰─ attrValue "=\"bar\""
15+
│ ││ │ │╰─ attrValue.value "\"bar\" // this is a test"
16+
│ ││ │ ╰─ attrValue "=\"bar\" // this is a test"
1717
│ ││ ╰─ attrName "foo"
1818
│ │╰─ tagName "div"
1919
╰─ ╰─ openTagStart
2020
3╭─ hello="world" // this is another test
21-
│ │ │╰─ attrValue.value "\"world\""
22-
│ │ ╰─ attrValue "=\"world\""
21+
│ │ │╰─ attrValue.value "\"world\" // this is another test"
22+
│ │ ╰─ attrValue "=\"world\" // this is another test"
2323
╰─ ╰─ attrName "hello"
2424
4╭─ ></div>
2525
│ ││ │ ╰─ closeTagEnd(div)

src/states/EXPRESSION.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ export const EXPRESSION: StateDefinition<ExpressionMeta> = {
8787
break;
8888
default: {
8989
if (
90-
!canCharCodeBeFollowedByDivision(
90+
canCharCodeBeFollowedByDivision(
9191
this.getPreviousNonWhitespaceCharCode()
9292
)
9393
) {
94+
this.pos++;
95+
this.consumeWhitespace();
96+
} else {
9497
this.enterState(STATE.REGULAR_EXPRESSION);
9598
}
9699
break;
@@ -219,7 +222,6 @@ function buildPattern(type: PatternType) {
219222
`|(?<!-)-${
220223
type === PatternType.CONCISE_ATTRS ? "" : "(?:\\s*-\\s*-)*\\s*"
221224
}(?!-)` + // In concise mode we can't match multiple hyphens otherwise we can match an even number of hyphens
222-
`|(?<![/*])/(?![/*${type === PatternType.HTML_ATTRS ? ">" : ""}])` + // We only continue after a forward slash if it isn't //, /* (or /> in html mode)
223225
`|(?<!\\.)\\.(?!\\.)` + // We only continue after a dot if it isn't a ...
224226
`|>${type === PatternType.HTML_ATTRS ? "{2,}" : "+"}` + // in html mode only consume closing angle brackets if it is >>
225227
"|[ \\t]+(?:in(?:stanceof)?|as|extends)(?=[ \\t]+[^=/,;:>])"; // We only continue after word operators (instanceof/in) when they are not followed by a terminator
@@ -233,7 +235,7 @@ function buildPattern(type: PatternType) {
233235
"|typeof" +
234236
"|void" +
235237
")\\b";
236-
const lookAheadPattern = `${space}*(?:${binary})\\s*|${space}+(?=[{(])`; // if we have spaces followed by an opening bracket, we'll consume the spaces and let the expression state handle the brackets
238+
const lookAheadPattern = `${space}*(?:${binary})\\s*|${space}+(?=[{(]|/[^>])`; // if we have spaces followed by an opening bracket, we'll consume the spaces and let the expression state handle the brackets
237239
const lookBehindPattern = `(?<=${unary}|${binary})`;
238240
return new RegExp(`${lookAheadPattern}|${lookBehindPattern}`, "ym");
239241
}

0 commit comments

Comments
 (0)