Skip to content

Commit

Permalink
feat: implement templateRanges option (#22)
Browse files Browse the repository at this point in the history
* refactor

* feat: support template syntax

* feat: template syntax attributes value quote

* feat: script start

* impl

* tokenizer

* update

* imple

* Update token-types.ts

* Update token-adapter.ts
  • Loading branch information
yeonjuan authored Nov 27, 2024
1 parent 0ffb8e0 commit 2538b1e
Show file tree
Hide file tree
Showing 97 changed files with 3,355 additions and 324 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ coverage
dist
.yarn/*
!.yarn/releases
.DS_store
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ interface ScriptTagNode extends BaseNode {
openStart: OpenScriptTagStartNode;
openEnd: OpenScriptTagEndNode;
close: CloseScriptTagNode;
value?: ScriptTagContentNode;
children: ScriptTagContentNode[];
}
```

Expand Down Expand Up @@ -529,7 +529,7 @@ interface StyleTagNode extends BaseNode {
openStart: OpenStyleTagStartNode;
openEnd: OpenStyleTagEndNode;
close: CloseStyleTagNode;
value?: StyleTagContentNode;
children: StyleTagContentNode[];
}
```

Expand Down Expand Up @@ -586,7 +586,7 @@ interface CommentNode extends BaseNode {
type: "Comment";
open: CommentOpenNode;
close: CommentCloseNode;
value: CommentContentNode;
children: CommentContentNode[];
}
```

Expand Down
4 changes: 4 additions & 0 deletions src/parser/__tests__/__snapshots__/token-adapter.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exports[`parse basic 1`] = `
"attributes": [],
"children": [
{
"isTemplate": false,
"loc": {
"end": {
"column": 12,
Expand Down Expand Up @@ -153,6 +154,7 @@ exports[`parse basic 1`] = `
"value": ">",
},
{
"isTemplate": false,
"loc": {
"end": {
"column": 12,
Expand Down Expand Up @@ -200,6 +202,7 @@ exports[`parse token adapter 1`] = `
"attributes": [],
"children": [
{
"isTemplate": false,
"loc": {
"end": {
"column": 12,
Expand Down Expand Up @@ -345,6 +348,7 @@ exports[`parse token adapter 1`] = `
"value": ">",
},
{
"isTemplate": false,
"loc": {
"end": {
"column": 12,
Expand Down
3 changes: 2 additions & 1 deletion src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { Options } from "../types/parse";

export function parse(html: string, options?: Options): ParseResult {
const tokenAdapter = (options && options.tokenAdapter) || defaultTokenAdapter;
const { tokens } = tokenize(html, tokenAdapter);
const templateRanges = (options && options.templateRanges) || [];
const { tokens } = tokenize(html, tokenAdapter, templateRanges);
const { ast } = constructTree(tokens, undefined);
return {
ast: clearParent(ast),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div ${key}=${value}></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div ${ attr }="value"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id=${id}></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="${id}"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div ${ attr }></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--${comment}-->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div> ${ data } text </div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script ${type}></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<script> ${content} </script>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<style type=${type} ></style>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<style> ${content} </style>
16 changes: 16 additions & 0 deletions src/tokenizer/__tests__/__output__/attributes-apostrophe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default [
{
type: TokenTypes.Text,
value: "\n ",
isTemplate: false,
range: [5, 8],
loc: {
start: {
Expand Down Expand Up @@ -64,6 +65,7 @@ export default [
{
type: TokenTypes.AttributeKey,
value: "class",
isTemplate: false,
range: [14, 19],
loc: {
start: {
Expand Down Expand Up @@ -108,6 +110,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "test class",
range: [21, 31],
loc: {
Expand Down Expand Up @@ -138,6 +141,7 @@ export default [
},
{
type: TokenTypes.AttributeKey,
isTemplate: false,
value: "data-test",
range: [33, 42],
loc: {
Expand Down Expand Up @@ -183,6 +187,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "data<test>",
range: [44, 54],
loc: {
Expand Down Expand Up @@ -228,6 +233,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n some text\n\n ",
range: [56, 76],
loc: {
Expand Down Expand Up @@ -273,6 +279,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n another text\n\n ",
range: [81, 108],
loc: {
Expand Down Expand Up @@ -303,6 +310,7 @@ export default [
},
{
type: TokenTypes.AttributeKey,
isTemplate: false,
value: "custom-attr",
range: [132, 143],
loc: {
Expand Down Expand Up @@ -348,6 +356,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "custom >test",
range: [145, 157],
loc: {
Expand Down Expand Up @@ -378,6 +387,7 @@ export default [
},
{
type: TokenTypes.AttributeKey,
isTemplate: false,
value: "with-spaces",
range: [167, 178],
loc: {
Expand Down Expand Up @@ -423,6 +433,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "spaces test",
range: [185, 196],
loc: {
Expand Down Expand Up @@ -468,6 +479,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n custom element\n text\n ",
range: [205, 248],
loc: {
Expand Down Expand Up @@ -498,6 +510,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n ",
range: [265, 270],
loc: {
Expand Down Expand Up @@ -528,6 +541,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n ",
range: [276, 279],
loc: {
Expand Down Expand Up @@ -559,6 +573,7 @@ export default [
{
type: TokenTypes.Text,
value: "\n",
isTemplate: false,
range: [286, 287],
loc: {
start: {
Expand Down Expand Up @@ -588,6 +603,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n",
range: [293, 294],
loc: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default [
},
{
type: TokenTypes.AttributeKey,
isTemplate: false,
value: "target",
range: [3, 9],
loc: {
Expand Down Expand Up @@ -48,6 +49,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "”_blank”",
range: [10, 18],
loc: {
Expand Down
15 changes: 15 additions & 0 deletions src/tokenizer/__tests__/__output__/attributes-bare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default [
},
{
type: TokenTypes.AttributeKey,
isTemplate: false,
value: "key",
range: [5, 8],
loc: {
Expand Down Expand Up @@ -48,6 +49,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "value",
range: [11, 16],
loc: {
Expand All @@ -63,6 +65,7 @@ export default [
},
{
type: TokenTypes.AttributeKey,
isTemplate: false,
value: "and-another",
range: [18, 29],
loc: {
Expand Down Expand Up @@ -93,6 +96,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "value",
range: [32, 37],
loc: {
Expand Down Expand Up @@ -138,6 +142,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n\n",
range: [44, 46],
loc: {
Expand Down Expand Up @@ -168,6 +173,7 @@ export default [
},
{
type: TokenTypes.AttributeKey,
isTemplate: false,
value: "some",
range: [52, 56],
loc: {
Expand Down Expand Up @@ -198,6 +204,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "custom-attribute",
range: [57, 73],
loc: {
Expand Down Expand Up @@ -228,6 +235,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n some text\n\n ",
range: [74, 90],
loc: {
Expand Down Expand Up @@ -258,6 +266,7 @@ export default [
},
{
type: TokenTypes.AttributeKey,
isTemplate: false,
value: "and",
range: [106, 109],
loc: {
Expand Down Expand Up @@ -288,6 +297,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "here",
range: [110, 114],
loc: {
Expand All @@ -303,6 +313,7 @@ export default [
},
{
type: TokenTypes.AttributeKey,
isTemplate: false,
value: "and-even",
range: [119, 127],
loc: {
Expand Down Expand Up @@ -333,6 +344,7 @@ export default [
},
{
type: TokenTypes.AttributeValue,
isTemplate: false,
value: "here",
range: [130, 134],
loc: {
Expand Down Expand Up @@ -363,6 +375,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n custom element\n text\n ",
range: [138, 169],
loc: {
Expand Down Expand Up @@ -393,6 +406,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n",
range: [186, 187],
loc: {
Expand Down Expand Up @@ -423,6 +437,7 @@ export default [
},
{
type: TokenTypes.Text,
isTemplate: false,
value: "\n",
range: [194, 195],
loc: {
Expand Down
Loading

0 comments on commit 2538b1e

Please sign in to comment.