Skip to content

Commit 14f3499

Browse files
authored
feat: add new parser events and rename 'onTagName' to 'onOpenTagName'. (#114)
1 parent b79698c commit 14f3499

File tree

238 files changed

+3707
-3016
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

238 files changed

+3707
-3016
lines changed

.changeset/two-points-guess.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"htmljs-parser": major
3+
---
4+
5+
Rename `onTagName` to `onOpenTagName`.
6+
Add a new `onOpenTagStart` event (before `onOpenTagName`).
7+
Split the `onCloseTag` event into three new events: `onClosetTagStart`, `onCloseTagName` & `onCloseTagEnd`).

README.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,29 @@ const parser = createParser({
155155
range.value; // Another range that includes only the value itself without the leading $ or surrounding braces (if applicable).
156156
},
157157

158+
/**
159+
* Called when we're about to begin an HTML open tag (before the tag name).
160+
* Note: This is only called for HTML mode tags and can be used to track if you are in concise mode.
161+
*
162+
* @example
163+
* 1╭─ <div>Hi</div>
164+
* ╰─ ╰─ openTagStart
165+
*/
166+
onOpenTagStart(range) {}
167+
158168
/**
159169
* Called when a tag name, which can include placeholders, has been parsed.
160170
*
161171
* @example
162172
* 1╭─ <div/>
163-
* ╰─ ╰─ tagName "div"
173+
* ╰─ ╰─ openTagName "div"
164174
* 2╭─ <hello-${test}-again/>
165-
* │ │ │ ╰─ tagName.quasis[1] "-again"
166-
* │ │ ╰─ tagName.expressions[0] "${test}"
167-
* │ ├─ tagName.quasis[0] "hello-"
168-
* ╰─ ╰─ tagName "hello-${test}-again"
175+
* │ │ │ ╰─ openTagName.quasis[1] "-again"
176+
* │ │ ╰─ openTagName.expressions[0] "${test}"
177+
* │ ├─ openTagName.quasis[0] "hello-"
178+
* ╰─ ╰─ openTagName "hello-${test}-again"
169179
*/
170-
onTagName(range) {
171-
range.concise; // true if this tag is a concise mode tag.
180+
onOpenTagName(range) {
172181
range.quasis; // An array of ranges that indicate the string literal parts of the tag name.
173182
range.expressions; // A list of placeholder ranges (similar to whats emitted via onPlaceholder).
174183

@@ -353,21 +362,38 @@ const parser = createParser({
353362
* ╰─ ╰─ openTagEnd ">"
354363
*/
355364
onOpenTagEnd(range) {
356-
range.selfClosed; // true if this tag was self closed (onCloseTag would not be called if so).
365+
range.selfClosed; // true if this tag was self closed (the onCloseTag* handlers will not be called if so).
357366
},
358367

359368
/**
360-
* Called once the closing tag (or in concise mode an outdent or eof) is parsed.
369+
* Called when we start parsing and html closing tag.
370+
* Note this is not emitted for concise, selfClosed, void or statement tags.
371+
*
372+
* @example
373+
* 1╭─ <div><span/></div>
374+
* ╰─ ╰─ closeTagStart "</"
375+
*/
376+
onCloseTagStart(range) {},
377+
378+
/**
379+
* Called after the content within the brackets of an html closing tag has been parsed.
380+
* Note this is not emitted for concise, selfClosed, void or statement tags.
381+
*
382+
* @example
383+
* 1╭─ <div><span/></div>
384+
* ╰─ ╰─ closeTagName "div"
385+
*/
386+
onCloseTagName(range) {},
387+
388+
/**
389+
* Called once the closing tag has finished parsing, or in concise mode we hit an outdent or eof.
361390
* Note this is not called for selfClosed, void or statement tags.
362391
*
363392
* @example
364393
* 1╭─ <div><span/></div>
365-
* │ │ ╰─ closeTag(div).value "div"
366-
* ╰─ ╰─ closeTag(div) "</div>"
394+
* ╰─ ╰─ closeTagEnd ">"
367395
*/
368-
onCloseTag(range) {
369-
range.value; // The raw content of the closing tag (undefined in concise mode).
370-
},
396+
onCloseTagEnd(range) {},
371397
});
372398
```
373399

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,50 @@
11
1╭─ <${x}(y)/>
2-
│ │ ││ ╰─ openTagEnd:selfClosed "/>"
3-
│ │ │╰─ tagArgs.value
4-
│ │ ├─ tagArgs "(y)"
5-
│ │ ╰─ tagName.quasis[1]
6-
│ ├─ tagName.expressions[0] "${x}"
7-
│ ├─ tagName.quasis[0]
8-
╰─ ╰─ tagName "${x}"
2+
│ ││ ││ ╰─ openTagEnd:selfClosed "/>"
3+
│ ││ │╰─ tagArgs.value
4+
│ ││ ├─ tagArgs "(y)"
5+
│ ││ ╰─ tagName.quasis[1]
6+
│ │├─ tagName.expressions[0] "${x}"
7+
│ │├─ tagName.quasis[0]
8+
│ │╰─ tagName "${x}"
9+
╰─ ╰─ openTagStart
910
2╭─ <${x}|y|/>
10-
│ │ ││ ╰─ openTagEnd:selfClosed "/>"
11-
│ │ │╰─ tagParams.value
12-
│ │ ├─ tagParams "|y|"
13-
│ │ ╰─ tagName.quasis[1]
14-
│ ├─ tagName.expressions[0] "${x}"
15-
│ ├─ tagName.quasis[0]
16-
╰─ ╰─ tagName "${x}"
11+
│ ││ ││ ╰─ openTagEnd:selfClosed "/>"
12+
│ ││ │╰─ tagParams.value
13+
│ ││ ├─ tagParams "|y|"
14+
│ ││ ╰─ tagName.quasis[1]
15+
│ │├─ tagName.expressions[0] "${x}"
16+
│ │├─ tagName.quasis[0]
17+
│ │╰─ tagName "${x}"
18+
╰─ ╰─ openTagStart
1719
3╭─ <tag.x(y)/>
18-
│ │ ││││ ╰─ openTagEnd:selfClosed "/>"
19-
│ │ │││╰─ tagArgs.value
20-
│ │ ││╰─ tagArgs "(y)"
21-
│ │ │╰─ tagShorthandClass.quasis[0]
22-
│ │ ╰─ tagShorthandClass ".x"
23-
╰─ ╰─ tagName "tag"
20+
│ ││ ││││ ╰─ openTagEnd:selfClosed "/>"
21+
│ ││ │││╰─ tagArgs.value
22+
│ ││ ││╰─ tagArgs "(y)"
23+
│ ││ │╰─ tagShorthandClass.quasis[0]
24+
│ ││ ╰─ tagShorthandClass ".x"
25+
│ │╰─ tagName "tag"
26+
╰─ ╰─ openTagStart
2427
4╭─ <tag.x|y|/>
25-
│ │ ││││ ╰─ openTagEnd:selfClosed "/>"
26-
│ │ │││╰─ tagParams.value
27-
│ │ ││╰─ tagParams "|y|"
28-
│ │ │╰─ tagShorthandClass.quasis[0]
29-
│ │ ╰─ tagShorthandClass ".x"
30-
╰─ ╰─ tagName "tag"
28+
│ ││ ││││ ╰─ openTagEnd:selfClosed "/>"
29+
│ ││ │││╰─ tagParams.value
30+
│ ││ ││╰─ tagParams "|y|"
31+
│ ││ │╰─ tagShorthandClass.quasis[0]
32+
│ ││ ╰─ tagShorthandClass ".x"
33+
│ │╰─ tagName "tag"
34+
╰─ ╰─ openTagStart
3135
5╭─ <tag#x(y)/>
32-
│ │ ││││ ╰─ openTagEnd:selfClosed "/>"
33-
│ │ │││╰─ tagArgs.value
34-
│ │ ││╰─ tagArgs "(y)"
35-
│ │ │╰─ tagShorthandId.quasis[0]
36-
│ │ ╰─ tagShorthandId "#x"
37-
╰─ ╰─ tagName "tag"
36+
│ ││ ││││ ╰─ openTagEnd:selfClosed "/>"
37+
│ ││ │││╰─ tagArgs.value
38+
│ ││ ││╰─ tagArgs "(y)"
39+
│ ││ │╰─ tagShorthandId.quasis[0]
40+
│ ││ ╰─ tagShorthandId "#x"
41+
│ │╰─ tagName "tag"
42+
╰─ ╰─ openTagStart
3843
6╭─ <tag#x|y|/>
39-
│ │ ││││ ╰─ openTagEnd:selfClosed "/>"
40-
│ │ │││╰─ tagParams.value
41-
│ │ ││╰─ tagParams "|y|"
42-
│ │ │╰─ tagShorthandId.quasis[0]
43-
│ │ ╰─ tagShorthandId "#x"
44-
╰─ ╰─ tagName "tag"
44+
│ ││ ││││ ╰─ openTagEnd:selfClosed "/>"
45+
│ ││ │││╰─ tagParams.value
46+
│ ││ ││╰─ tagParams "|y|"
47+
│ ││ │╰─ tagShorthandId.quasis[0]
48+
│ ││ ╰─ tagShorthandId "#x"
49+
│ │╰─ tagName "tag"
50+
╰─ ╰─ openTagStart
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
1╭─ <div if (x > y)>
2-
│ │ │ ││ ╰─ openTagEnd
3-
│ │ │ │╰─ attrArgs.value "x > y"
4-
│ │ │ ╰─ attrArgs "(x > y)"
5-
│ │ ╰─ attrName "if"
6-
╰─ ╰─ tagName "div"
2+
│ ││ │ ││ ╰─ openTagEnd
3+
│ ││ │ │╰─ attrArgs.value "x > y"
4+
│ ││ │ ╰─ attrArgs "(x > y)"
5+
│ ││ ╰─ attrName "if"
6+
│ │╰─ tagName "div"
7+
╰─ ╰─ openTagStart
78
2╭─ </div>
8-
│ │ ╰─ closeTag(div).value "div"
9+
│ │ │ ╰─ closeTagEnd(div)
10+
│ │ ╰─ closeTagName "div"
911
│ ├─ text "\n"
10-
╰─ ╰─ closeTag(div) "</div>"
12+
╰─ ╰─ closeTagStart "</"
1113
3╰─
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
1╭─ <div for(var i = 0; i < 10; i++) (nonsense!)></div>
2-
│ │ │ ││ ╰─ error(INVALID_ATTRIBUTE_ARGUMENT:An attribute can only have one set of arguments) "nonsense!"
3-
│ │ │ │╰─ attrArgs.value "var i = 0; i < 10; i++"
4-
│ │ │ ╰─ attrArgs "(var i = 0; i < 10; i++)"
5-
│ │ ╰─ attrName "for"
6-
╰─ ╰─ tagName "div"
2+
│ ││ │ ││ ╰─ error(INVALID_ATTRIBUTE_ARGUMENT:An attribute can only have one set of arguments) "nonsense!"
3+
│ ││ │ │╰─ attrArgs.value "var i = 0; i < 10; i++"
4+
│ ││ │ ╰─ attrArgs "(var i = 0; i < 10; i++)"
5+
│ ││ ╰─ attrName "for"
6+
│ │╰─ tagName "div"
7+
╰─ ╰─ openTagStart
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
1╭─ <div if(x > y)></div>
2-
│ │ │ ││ ││ ╰─ closeTag(div).value "div"
3-
│ │ │ ││ │╰─ closeTag(div) "</div>"
4-
│ │ │ ││ ╰─ openTagEnd
5-
│ │ │ │╰─ attrArgs.value "x > y"
6-
│ │ │ ╰─ attrArgs "(x > y)"
7-
│ │ ╰─ attrName "if"
8-
╰─ ╰─ tagName "div"
2+
│ ││ │ ││ ││ │ ╰─ closeTagEnd(div)
3+
│ ││ │ ││ ││ ╰─ closeTagName "div"
4+
│ ││ │ ││ │╰─ closeTagStart "</"
5+
│ ││ │ ││ ╰─ openTagEnd
6+
│ ││ │ │╰─ attrArgs.value "x > y"
7+
│ ││ │ ╰─ attrArgs "(x > y)"
8+
│ ││ ╰─ attrName "if"
9+
│ │╰─ tagName "div"
10+
╰─ ╰─ openTagStart
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
1╭─ <for(var i = 0; i < 10; i++) if(x > y)></for>
2-
│ │ ││ │ ││ ││ ╰─ closeTag(for).value "for"
3-
│ │ ││ │ ││ │╰─ closeTag(for) "</for>"
4-
│ │ ││ │ ││ ╰─ openTagEnd
5-
│ │ ││ │ │╰─ attrArgs.value "x > y"
6-
│ │ ││ │ ╰─ attrArgs "(x > y)"
7-
│ │ ││ ╰─ attrName "if"
8-
│ │ │╰─ tagArgs.value "var i = 0; i < 10; i++"
9-
│ │ ╰─ tagArgs "(var i = 0; i < 10; i++)"
10-
╰─ ╰─ tagName "for"
2+
│ ││ ││ │ ││ ││ │ ╰─ closeTagEnd(for)
3+
│ ││ ││ │ ││ ││ ╰─ closeTagName "for"
4+
│ ││ ││ │ ││ │╰─ closeTagStart "</"
5+
│ ││ ││ │ ││ ╰─ openTagEnd
6+
│ ││ ││ │ │╰─ attrArgs.value "x > y"
7+
│ ││ ││ │ ╰─ attrArgs "(x > y)"
8+
│ ││ ││ ╰─ attrName "if"
9+
│ ││ │╰─ tagArgs.value "var i = 0; i < 10; i++"
10+
│ ││ ╰─ tagArgs "(var i = 0; i < 10; i++)"
11+
│ │╰─ tagName "for"
12+
╰─ ╰─ openTagStart
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
1╭─ <for (x in ["Hello ${name}!", "(World)"])></for>
2-
│ │ ││ ││ ╰─ closeTag(for).value "for"
3-
│ │ ││ │╰─ closeTag(for) "</for>"
4-
│ │ ││ ╰─ openTagEnd
5-
│ │ │╰─ tagArgs.value "x in [\"Hello ${name}!\", \"(World)\"]"
6-
│ │ ╰─ tagArgs "(x in [\"Hello ${name}!\", \"(World)\"])"
7-
╰─ ╰─ tagName "for"
2+
│ ││ ││ ││ │ ╰─ closeTagEnd(for)
3+
│ ││ ││ ││ ╰─ closeTagName "for"
4+
│ ││ ││ │╰─ closeTagStart "</"
5+
│ ││ ││ ╰─ openTagEnd
6+
│ ││ │╰─ tagArgs.value "x in [\"Hello ${name}!\", \"(World)\"]"
7+
│ ││ ╰─ tagArgs "(x in [\"Hello ${name}!\", \"(World)\"])"
8+
│ │╰─ tagName "for"
9+
╰─ ╰─ openTagStart
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
1╭─ <for (x in y)></for>
2-
│ │ ││ ││ ╰─ closeTag(for).value "for"
3-
│ │ ││ │╰─ closeTag(for) "</for>"
4-
│ │ ││ ╰─ openTagEnd
5-
│ │ │╰─ tagArgs.value "x in y"
6-
│ │ ╰─ tagArgs "(x in y)"
7-
╰─ ╰─ tagName "for"
2+
│ ││ ││ ││ │ ╰─ closeTagEnd(for)
3+
│ ││ ││ ││ ╰─ closeTagName "for"
4+
│ ││ ││ │╰─ closeTagStart "</"
5+
│ ││ ││ ╰─ openTagEnd
6+
│ ││ │╰─ tagArgs.value "x in y"
7+
│ ││ ╰─ tagArgs "(x in y)"
8+
│ │╰─ tagName "for"
9+
╰─ ╰─ openTagStart
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
1╭─ <for(var i = 0; i < 10; i++) (nonsense!)></for>
2-
│ │ ││ ╰─ error(INVALID_TAG_ARGUMENT:A tag can only have one argument)
3-
│ │ │╰─ tagArgs.value "var i = 0; i < 10; i++"
4-
│ │ ╰─ tagArgs "(var i = 0; i < 10; i++)"
5-
╰─ ╰─ tagName "for"
2+
│ ││ ││ ╰─ error(INVALID_TAG_ARGUMENT:A tag can only have one argument)
3+
│ ││ │╰─ tagArgs.value "var i = 0; i < 10; i++"
4+
│ ││ ╰─ tagArgs "(var i = 0; i < 10; i++)"
5+
│ │╰─ tagName "for"
6+
╰─ ╰─ openTagStart

0 commit comments

Comments
 (0)