@markuplint/ejs-parser 3.6.0 → 3.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/parse.js +2 -2
- package/package.json +4 -4
- package/test/index.spec.js +168 -0
package/lib/parse.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.parse = void 0;
|
|
4
4
|
const html_parser_1 = require("@markuplint/html-parser");
|
|
5
5
|
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
6
|
-
const parse = rawCode => {
|
|
6
|
+
const parse = (rawCode, options) => {
|
|
7
7
|
const blocks = (0, parser_utils_1.ignoreBlock)(rawCode, [
|
|
8
8
|
{
|
|
9
9
|
type: 'ejs-whitespace-slurping',
|
|
@@ -31,7 +31,7 @@ const parse = rawCode => {
|
|
|
31
31
|
end: /%>/,
|
|
32
32
|
},
|
|
33
33
|
]);
|
|
34
|
-
const doc = (0, html_parser_1.parse)(blocks.replaced);
|
|
34
|
+
const doc = (0, html_parser_1.parse)(blocks.replaced, options);
|
|
35
35
|
doc.nodeList = (0, parser_utils_1.restoreNode)(doc.nodeList, blocks);
|
|
36
36
|
return doc;
|
|
37
37
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/ejs-parser",
|
|
3
|
-
"version": "3.6.
|
|
3
|
+
"version": "3.6.2",
|
|
4
4
|
"description": "EJS parser for markuplint",
|
|
5
5
|
"repository": "git@github.com:markuplint/markuplint.git",
|
|
6
6
|
"author": "Yusuke Hirao <yusukehirao@me.com>",
|
|
@@ -16,9 +16,9 @@
|
|
|
16
16
|
"clean": "tsc --build --clean"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@markuplint/html-parser": "3.
|
|
19
|
+
"@markuplint/html-parser": "3.7.0",
|
|
20
20
|
"@markuplint/ml-ast": "3.1.0",
|
|
21
|
-
"@markuplint/parser-utils": "3.
|
|
21
|
+
"@markuplint/parser-utils": "3.7.0"
|
|
22
22
|
},
|
|
23
|
-
"gitHead": "
|
|
23
|
+
"gitHead": "adc6e432cccba7cfad0dc8bf9f92e5aaf1107359"
|
|
24
24
|
}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
const { nodeListToDebugMaps } = require('@markuplint/parser-utils');
|
|
2
|
+
|
|
3
|
+
const { parse } = require('../lib/');
|
|
4
|
+
|
|
5
|
+
describe('Node list', () => {
|
|
6
|
+
it('a code', () => {
|
|
7
|
+
const doc = parse('<div>abc<%= efg %>hij</div>');
|
|
8
|
+
expect(nodeListToDebugMaps(doc.nodeList)).toStrictEqual([
|
|
9
|
+
'[1:1]>[1:6](0,5)div: <div>',
|
|
10
|
+
'[1:6]>[1:9](5,8)#text: abc',
|
|
11
|
+
'[1:9]>[1:19](8,18)#ps:ejs-output-value: <%=␣efg␣%>',
|
|
12
|
+
'[1:19]>[1:22](18,21)#text: hij',
|
|
13
|
+
'[1:22]>[1:28](21,27)div: </div>',
|
|
14
|
+
]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('two codes', () => {
|
|
18
|
+
const doc = parse('<div>abc<%= efg %>hij<%= klm %></div>');
|
|
19
|
+
expect(nodeListToDebugMaps(doc.nodeList)).toStrictEqual([
|
|
20
|
+
'[1:1]>[1:6](0,5)div: <div>',
|
|
21
|
+
'[1:6]>[1:9](5,8)#text: abc',
|
|
22
|
+
'[1:9]>[1:19](8,18)#ps:ejs-output-value: <%=␣efg␣%>',
|
|
23
|
+
'[1:19]>[1:22](18,21)#text: hij',
|
|
24
|
+
'[1:22]>[1:32](21,31)#ps:ejs-output-value: <%=␣klm␣%>',
|
|
25
|
+
'[1:32]>[1:38](31,37)div: </div>',
|
|
26
|
+
]);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('two codes2', () => {
|
|
30
|
+
const doc = parse('<div>abc<%= efg %>hij<%= klm %>nop</div>');
|
|
31
|
+
expect(nodeListToDebugMaps(doc.nodeList)).toStrictEqual([
|
|
32
|
+
'[1:1]>[1:6](0,5)div: <div>',
|
|
33
|
+
'[1:6]>[1:9](5,8)#text: abc',
|
|
34
|
+
'[1:9]>[1:19](8,18)#ps:ejs-output-value: <%=␣efg␣%>',
|
|
35
|
+
'[1:19]>[1:22](18,21)#text: hij',
|
|
36
|
+
'[1:22]>[1:32](21,31)#ps:ejs-output-value: <%=␣klm␣%>',
|
|
37
|
+
'[1:32]>[1:35](31,34)#text: nop',
|
|
38
|
+
'[1:35]>[1:41](34,40)div: </div>',
|
|
39
|
+
]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('two codes2 on bare', () => {
|
|
43
|
+
const doc = parse('abc<%= efg %>hij<%= klm %>nop');
|
|
44
|
+
expect(nodeListToDebugMaps(doc.nodeList)).toStrictEqual([
|
|
45
|
+
'[1:1]>[1:4](0,3)#text: abc',
|
|
46
|
+
'[1:4]>[1:14](3,13)#ps:ejs-output-value: <%=␣efg␣%>',
|
|
47
|
+
'[1:14]>[1:17](13,16)#text: hij',
|
|
48
|
+
'[1:17]>[1:27](16,26)#ps:ejs-output-value: <%=␣klm␣%>',
|
|
49
|
+
'[1:27]>[1:30](26,29)#text: nop',
|
|
50
|
+
]);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('nest block', () => {
|
|
54
|
+
const doc = parse('<% if (foo) { %>abcdef<% } %>');
|
|
55
|
+
expect(nodeListToDebugMaps(doc.nodeList)).toStrictEqual([
|
|
56
|
+
'[1:1]>[1:17](0,16)#ps:ejs-scriptlet: <%␣if␣(foo)␣{␣%>',
|
|
57
|
+
'[1:17]>[1:23](16,22)#text: abcdef',
|
|
58
|
+
'[1:23]>[1:30](22,29)#ps:ejs-scriptlet: <%␣}␣%>',
|
|
59
|
+
]);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('nest block', () => {
|
|
63
|
+
const doc = parse('<% if (foo) { %>abc<%= foo %>def<% } %>');
|
|
64
|
+
expect(nodeListToDebugMaps(doc.nodeList)).toStrictEqual([
|
|
65
|
+
'[1:1]>[1:17](0,16)#ps:ejs-scriptlet: <%␣if␣(foo)␣{␣%>',
|
|
66
|
+
'[1:17]>[1:20](16,19)#text: abc',
|
|
67
|
+
'[1:20]>[1:30](19,29)#ps:ejs-output-value: <%=␣foo␣%>',
|
|
68
|
+
'[1:30]>[1:33](29,32)#text: def',
|
|
69
|
+
'[1:33]>[1:40](32,39)#ps:ejs-scriptlet: <%␣}␣%>',
|
|
70
|
+
]);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('nest block', () => {
|
|
74
|
+
const doc = parse(`<% if (user) { %>
|
|
75
|
+
<div><%= user.name %></div>
|
|
76
|
+
<% } %>
|
|
77
|
+
`);
|
|
78
|
+
expect(nodeListToDebugMaps(doc.nodeList)).toStrictEqual([
|
|
79
|
+
'[1:1]>[1:18](0,17)#ps:ejs-scriptlet: <%␣if␣(user)␣{␣%>',
|
|
80
|
+
'[1:18]>[2:1](17,18)#text: ⏎',
|
|
81
|
+
'[2:1]>[2:6](18,23)div: <div>',
|
|
82
|
+
'[2:6]>[2:22](23,39)#ps:ejs-output-value: <%=␣user.name␣%>',
|
|
83
|
+
'[2:22]>[2:28](39,45)div: </div>',
|
|
84
|
+
'[2:28]>[3:1](45,46)#text: ⏎',
|
|
85
|
+
'[3:1]>[3:8](46,53)#ps:ejs-scriptlet: <%␣}␣%>',
|
|
86
|
+
'[3:8]>[4:1](53,54)#text: ⏎',
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
const el = doc.nodeList[3];
|
|
90
|
+
const el2 = doc.nodeList[3].parentNode.childNodes[0];
|
|
91
|
+
expect(el.nodeName).toBe(el2.nodeName);
|
|
92
|
+
expect(el.uuid).toBe(el2.uuid);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('Full HTML', () => {
|
|
96
|
+
const doc = parse(`<!DOCTYPE html>
|
|
97
|
+
<html lang="en">
|
|
98
|
+
<head>
|
|
99
|
+
<%- include('path/to') _%>
|
|
100
|
+
<meta />
|
|
101
|
+
</head>
|
|
102
|
+
<body></body>
|
|
103
|
+
</html>
|
|
104
|
+
`);
|
|
105
|
+
expect(nodeListToDebugMaps(doc.nodeList)).toStrictEqual([
|
|
106
|
+
'[1:1]>[1:16](0,15)#doctype: <!DOCTYPE␣html>',
|
|
107
|
+
'[1:16]>[2:1](15,16)#text: ⏎',
|
|
108
|
+
'[2:1]>[2:17](16,32)html: <html␣lang="en">',
|
|
109
|
+
'[2:17]>[3:2](32,34)#text: ⏎→',
|
|
110
|
+
'[3:2]>[3:8](34,40)head: <head>',
|
|
111
|
+
'[3:8]>[4:3](40,43)#text: ⏎→→',
|
|
112
|
+
"[4:3]>[4:29](43,69)#ps:ejs-output-unescaped: <%-␣include('path/to')␣_%>",
|
|
113
|
+
'[4:29]>[5:3](69,72)#text: ⏎→→',
|
|
114
|
+
'[5:3]>[5:11](72,80)meta: <meta␣/>',
|
|
115
|
+
'[5:11]>[6:2](80,82)#text: ⏎→',
|
|
116
|
+
'[6:2]>[6:9](82,89)head: </head>',
|
|
117
|
+
'[6:9]>[7:2](89,91)#text: ⏎→',
|
|
118
|
+
'[7:2]>[7:8](91,97)body: <body>',
|
|
119
|
+
'[7:8]>[7:15](97,104)body: </body>',
|
|
120
|
+
'[7:15]>[8:1](104,105)#text: ⏎',
|
|
121
|
+
'[8:1]>[8:8](105,112)html: </html>',
|
|
122
|
+
'[8:8]>[9:1](112,113)#text: ⏎',
|
|
123
|
+
]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('mutable text node', () => {
|
|
127
|
+
const doc = parse('<title><%= title %></title>');
|
|
128
|
+
expect(nodeListToDebugMaps(doc.nodeList)).toStrictEqual([
|
|
129
|
+
'[1:1]>[1:8](0,7)title: <title>',
|
|
130
|
+
'[1:8]>[1:20](7,19)#ps:ejs-output-value: <%=␣title␣%>',
|
|
131
|
+
'[1:20]>[1:28](19,27)title: </title>',
|
|
132
|
+
]);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('Tags', () => {
|
|
137
|
+
it('ejs-whitespace-slurping', () => {
|
|
138
|
+
expect(parse('<%_ any _%>').nodeList[0].nodeName).toBe('#ps:ejs-whitespace-slurping');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('ejs-output-value', () => {
|
|
142
|
+
expect(parse('<%= any %>').nodeList[0].nodeName).toBe('#ps:ejs-output-value');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('ejs-output-unescaped', () => {
|
|
146
|
+
expect(parse('<%- any -%>').nodeList[0].nodeName).toBe('#ps:ejs-output-unescaped');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('ejs-comment', () => {
|
|
150
|
+
expect(parse('<%# any %>').nodeList[0].nodeName).toBe('#ps:ejs-comment');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('ejs-scriptlet', () => {
|
|
154
|
+
expect(parse('<% any %>').nodeList[0].nodeName).toBe('#ps:ejs-scriptlet');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('text node', () => {
|
|
158
|
+
expect(parse('<%% any %>').nodeList[0].nodeName).toBe('#text');
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe('Issues', () => {
|
|
163
|
+
test('#607', () => {
|
|
164
|
+
const ast = parse('<% %><div></div>');
|
|
165
|
+
expect(ast.nodeList.length).toBe(3);
|
|
166
|
+
expect(ast.nodeList[1].prevNode?.uuid).toBe(ast.nodeList[0].uuid);
|
|
167
|
+
});
|
|
168
|
+
});
|