Skip to content

Commit

Permalink
feat: support parsing template syntax (#24)
Browse files Browse the repository at this point in the history
* feat: add templatable, templates tokens

* refactor

* feat: templates attributes

* feat: templates data

* feat: templates comment

* feat: script content template

* feat: templates content end

* feat: add templatable, template container node

* feat: construct node

* format
  • Loading branch information
yeonjuan authored Nov 29, 2024
1 parent f3d474e commit f753390
Show file tree
Hide file tree
Showing 96 changed files with 2,877 additions and 389 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ coverage
yarn.lock
dist
website
.yarn
.yarn
__file_snapshots__
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 @@ -22,6 +22,7 @@ exports[`parse basic 1`] = `
5,
12,
],
"templates": [],
"type": "Text",
"value": "content",
},
Expand Down Expand Up @@ -167,6 +168,7 @@ exports[`parse basic 1`] = `
5,
12,
],
"templates": [],
"type": "Text",
"value": "content",
},
Expand Down Expand Up @@ -214,6 +216,7 @@ exports[`parse token adapter 1`] = `
6,
13,
],
"templates": [],
"type": "Text",
"value": "content",
},
Expand Down Expand Up @@ -359,6 +362,7 @@ exports[`parse token adapter 1`] = `
6,
13,
],
"templates": [],
"type": "Text",
"value": "content",
},
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +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 { ast } = constructTree(tokens, undefined);
const { tokens } = tokenize(html, tokenAdapter, options?.templateRanges);
const { ast } = constructTree(tokens);
return {
ast: clearParent(ast),
tokens,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div ${key}></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 class="one ${two} ${three}"></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="${id}"></div>
1 change: 1 addition & 0 deletions src/tokenizer/__tests__/__input__/templates-comment.html
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 @@
${content}
1 change: 1 addition & 0 deletions src/tokenizer/__tests__/__input__/templates-data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>${children}</div>
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>${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 @@ -45,6 +45,7 @@ export default [
column: 2,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagStart,
Expand Down Expand Up @@ -75,6 +76,7 @@ export default [
column: 13,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -120,6 +122,7 @@ export default [
column: 25,
},
},
templates: [],
},
{
type: TokenTypes.AttributeValueWrapperEnd,
Expand Down Expand Up @@ -150,6 +153,7 @@ export default [
column: 36,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -195,6 +199,7 @@ export default [
column: 48,
},
},
templates: [],
},
{
type: TokenTypes.AttributeValueWrapperEnd,
Expand Down Expand Up @@ -240,6 +245,7 @@ export default [
column: 4,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagStart,
Expand Down Expand Up @@ -285,6 +291,7 @@ export default [
column: 6,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagStart,
Expand Down Expand Up @@ -315,6 +322,7 @@ export default [
column: 19,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -360,6 +368,7 @@ export default [
column: 33,
},
},
templates: [],
},
{
type: TokenTypes.AttributeValueWrapperEnd,
Expand Down Expand Up @@ -390,6 +399,7 @@ export default [
column: 19,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -435,6 +445,7 @@ export default [
line: 10,
},
},
templates: [],
},
{
type: TokenTypes.AttributeValueWrapperEnd,
Expand Down Expand Up @@ -480,6 +491,7 @@ export default [
line: 14,
},
},
templates: [],
},
{
type: TokenTypes.CloseTag,
Expand Down Expand Up @@ -510,6 +522,7 @@ export default [
column: 4,
},
},
templates: [],
},
{
type: TokenTypes.CloseTag,
Expand Down Expand Up @@ -540,6 +553,7 @@ export default [
column: 2,
},
},
templates: [],
},
{
type: TokenTypes.CloseTag,
Expand Down Expand Up @@ -570,6 +584,7 @@ export default [
column: 0,
},
},
templates: [],
},
{
type: TokenTypes.CloseTag,
Expand Down Expand Up @@ -600,5 +615,6 @@ export default [
column: 0,
},
},
templates: [],
},
];
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TokenTypes } from "../../../constants";
import { AnyToken } from "../../../types";

export default [
const OUTPUT: AnyToken[] = [
{
type: TokenTypes.OpenTagStart,
value: "<a",
Expand Down Expand Up @@ -30,6 +31,7 @@ export default [
column: 9,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -60,6 +62,7 @@ export default [
column: 18,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagEnd,
Expand Down Expand Up @@ -92,3 +95,5 @@ export default [
},
},
];

export default OUTPUT;
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 @@ -30,6 +30,7 @@ export default [
column: 8,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -60,6 +61,7 @@ export default [
column: 16,
},
},
templates: [],
},
{
type: TokenTypes.AttributeKey,
Expand All @@ -75,6 +77,7 @@ export default [
column: 29,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -105,6 +108,7 @@ export default [
column: 37,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagEnd,
Expand Down Expand Up @@ -150,6 +154,7 @@ export default [
column: 0,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagStart,
Expand Down Expand Up @@ -180,6 +185,7 @@ export default [
column: 10,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -210,6 +216,7 @@ export default [
column: 27,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagEnd,
Expand Down Expand Up @@ -240,6 +247,7 @@ export default [
column: 2,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagStart,
Expand Down Expand Up @@ -270,6 +278,7 @@ export default [
column: 21,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -300,6 +309,7 @@ export default [
column: 26,
},
},
templates: [],
},
{
type: TokenTypes.AttributeKey,
Expand All @@ -315,6 +325,7 @@ export default [
column: 12,
},
},
templates: [],
},
{
type: TokenTypes.AttributeAssignment,
Expand Down Expand Up @@ -345,6 +356,7 @@ export default [
column: 19,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagEnd,
Expand Down Expand Up @@ -375,6 +387,7 @@ export default [
column: 2,
},
},
templates: [],
},
{
type: TokenTypes.CloseTag,
Expand Down Expand Up @@ -405,6 +418,7 @@ export default [
column: 0,
},
},
templates: [],
},
{
type: TokenTypes.CloseTag,
Expand Down Expand Up @@ -435,5 +449,6 @@ export default [
column: 0,
},
},
templates: [],
},
];
8 changes: 7 additions & 1 deletion src/tokenizer/__tests__/__output__/attributes-empty.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TokenTypes } from "../../../constants";
import { AnyToken } from "../../../types";

export default [
const OUTPUT: AnyToken[] = [
{
type: TokenTypes.OpenTagStart,
value: "<span",
Expand Down Expand Up @@ -45,6 +46,7 @@ export default [
column: 2,
},
},
templates: [],
},
{
type: TokenTypes.OpenTagStart,
Expand Down Expand Up @@ -90,6 +92,7 @@ export default [
column: 2,
},
},
templates: [],
},
{
type: TokenTypes.CloseTag,
Expand Down Expand Up @@ -120,6 +123,7 @@ export default [
column: 0,
},
},
templates: [],
},
{
type: TokenTypes.CloseTag,
Expand Down Expand Up @@ -150,5 +154,7 @@ export default [
column: 0,
},
},
templates: [],
},
];
export default OUTPUT;
Loading

0 comments on commit f753390

Please sign in to comment.