Skip to content

Commit 1bfed2c

Browse files
authored
Handle none void self closing tags (#11)
1 parent bb689a7 commit 1bfed2c

File tree

6 files changed

+372
-1
lines changed

6 files changed

+372
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<svg xmlns="http://www.w3.org/2000/svg"><circle /></svg>
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import { TokenTypes } from "../../../constants";
2+
3+
export default [
4+
{
5+
type: TokenTypes.OpenTagStart,
6+
value: "<svg",
7+
range: [0, 4],
8+
loc: {
9+
start: {
10+
line: 1,
11+
column: 0,
12+
},
13+
end: {
14+
line: 1,
15+
column: 4,
16+
},
17+
},
18+
},
19+
{
20+
type: TokenTypes.AttributeKey,
21+
range: [5, 10],
22+
value: "xmlns",
23+
loc: {
24+
end: {
25+
column: 10,
26+
line: 1,
27+
},
28+
start: {
29+
column: 5,
30+
line: 1,
31+
},
32+
},
33+
},
34+
{
35+
type: TokenTypes.AttributeAssignment,
36+
value: "=",
37+
range: [10, 11],
38+
loc: {
39+
end: {
40+
column: 11,
41+
line: 1,
42+
},
43+
start: {
44+
column: 10,
45+
line: 1,
46+
},
47+
},
48+
},
49+
{
50+
type: TokenTypes.AttributeValueWrapperStart,
51+
value: '"',
52+
range: [11, 12],
53+
loc: {
54+
end: {
55+
column: 12,
56+
line: 1,
57+
},
58+
start: {
59+
column: 11,
60+
line: 1,
61+
},
62+
},
63+
},
64+
{
65+
type: TokenTypes.AttributeValue,
66+
value: "http://www.w3.org/2000/svg",
67+
range: [12, 38],
68+
loc: {
69+
end: {
70+
column: 38,
71+
line: 1,
72+
},
73+
start: {
74+
column: 12,
75+
line: 1,
76+
},
77+
},
78+
},
79+
{
80+
type: TokenTypes.AttributeValueWrapperEnd,
81+
value: '"',
82+
range: [38, 39],
83+
loc: {
84+
end: {
85+
column: 39,
86+
line: 1,
87+
},
88+
start: {
89+
column: 38,
90+
line: 1,
91+
},
92+
},
93+
},
94+
{
95+
type: TokenTypes.OpenTagEnd,
96+
value: ">",
97+
range: [39, 40],
98+
loc: {
99+
end: {
100+
column: 40,
101+
line: 1,
102+
},
103+
start: {
104+
column: 39,
105+
line: 1,
106+
},
107+
},
108+
},
109+
{
110+
type: TokenTypes.OpenTagStart,
111+
value: "<circle",
112+
range: [40, 47],
113+
loc: {
114+
end: {
115+
column: 47,
116+
line: 1,
117+
},
118+
start: {
119+
column: 40,
120+
line: 1,
121+
},
122+
},
123+
},
124+
{
125+
type: TokenTypes.OpenTagEnd,
126+
value: "/>",
127+
range: [48, 50],
128+
loc: {
129+
end: {
130+
column: 50,
131+
line: 1,
132+
},
133+
start: {
134+
column: 48,
135+
line: 1,
136+
},
137+
},
138+
},
139+
{
140+
type: TokenTypes.CloseTag,
141+
value: "</svg>",
142+
range: [50, 56],
143+
loc: {
144+
end: {
145+
column: 56,
146+
line: 1,
147+
},
148+
start: {
149+
column: 50,
150+
line: 1,
151+
},
152+
},
153+
},
154+
];

src/tokenizer/__tests__/tokenize.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import STYLE_ELEMENTS from "./__output__/style-elements";
1717
import TAGS_REGISTER from "./__output__/tags-register";
1818
import VOID_TAGS from "./__output__/void-tags";
1919
import EMPTY from "./__output__/empty";
20+
import SVG from "./__output__/svg";
2021

2122
describe("tokenize", () => {
2223
test.each(
@@ -101,6 +102,11 @@ describe("tokenize", () => {
101102
"Empty",
102103
"empty.html",
103104
EMPTY,
105+
],
106+
[
107+
"Svg",
108+
"svg.html",
109+
SVG,
104110
]
105111
]
106112
)("%s", (name, input, output) => {
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
export default {
2+
children: [
3+
{
4+
attributes: [
5+
{
6+
endWrapper: {
7+
loc: {
8+
end: {
9+
column: 39,
10+
line: 1,
11+
},
12+
start: {
13+
column: 38,
14+
line: 1,
15+
},
16+
},
17+
range: [38, 39],
18+
type: "AttributeValueWrapperEnd",
19+
value: '"',
20+
},
21+
key: {
22+
loc: {
23+
end: {
24+
column: 10,
25+
line: 1,
26+
},
27+
start: {
28+
column: 5,
29+
line: 1,
30+
},
31+
},
32+
range: [5, 10],
33+
type: "AttributeKey",
34+
value: "xmlns",
35+
},
36+
loc: {
37+
end: {
38+
column: 39,
39+
line: 1,
40+
},
41+
start: {
42+
column: 5,
43+
line: 1,
44+
},
45+
},
46+
range: [5, 39],
47+
startWrapper: {
48+
loc: {
49+
end: {
50+
column: 12,
51+
line: 1,
52+
},
53+
start: {
54+
column: 11,
55+
line: 1,
56+
},
57+
},
58+
range: [11, 12],
59+
type: "AttributeValueWrapperStart",
60+
value: '"',
61+
},
62+
type: "Attribute",
63+
value: {
64+
loc: {
65+
end: {
66+
column: 38,
67+
line: 1,
68+
},
69+
start: {
70+
column: 12,
71+
line: 1,
72+
},
73+
},
74+
range: [12, 38],
75+
type: "AttributeValue",
76+
value: "http://www.w3.org/2000/svg",
77+
},
78+
},
79+
],
80+
children: [
81+
{
82+
attributes: [],
83+
children: [],
84+
loc: {
85+
end: {
86+
column: 50,
87+
line: 1,
88+
},
89+
start: {
90+
column: 40,
91+
line: 1,
92+
},
93+
},
94+
name: "circle",
95+
openEnd: {
96+
loc: {
97+
end: {
98+
column: 50,
99+
line: 1,
100+
},
101+
start: {
102+
column: 48,
103+
line: 1,
104+
},
105+
},
106+
range: [48, 50],
107+
type: "OpenTagEnd",
108+
value: "/>",
109+
},
110+
openStart: {
111+
loc: {
112+
end: {
113+
column: 47,
114+
line: 1,
115+
},
116+
start: {
117+
column: 40,
118+
line: 1,
119+
},
120+
},
121+
range: [40, 47],
122+
type: "OpenTagStart",
123+
value: "<circle",
124+
},
125+
range: [40, 50],
126+
selfClosing: true,
127+
type: "Tag",
128+
},
129+
],
130+
loc: {
131+
end: {
132+
column: 56,
133+
line: 1,
134+
},
135+
start: {
136+
column: 0,
137+
line: 1,
138+
},
139+
},
140+
name: "svg",
141+
openEnd: {
142+
loc: {
143+
end: {
144+
column: 40,
145+
line: 1,
146+
},
147+
start: {
148+
column: 39,
149+
line: 1,
150+
},
151+
},
152+
range: [39, 40],
153+
type: "OpenTagEnd",
154+
value: ">",
155+
},
156+
openStart: {
157+
loc: {
158+
end: {
159+
column: 4,
160+
line: 1,
161+
},
162+
start: {
163+
column: 0,
164+
line: 1,
165+
},
166+
},
167+
range: [0, 4],
168+
type: "OpenTagStart",
169+
value: "<svg",
170+
},
171+
range: [0, 56],
172+
selfClosing: false,
173+
type: "Tag",
174+
close: {
175+
type: "CloseTag",
176+
range: [50, 56],
177+
value: "</svg>",
178+
loc: {
179+
start: {
180+
line: 1,
181+
column: 50,
182+
},
183+
end: {
184+
line: 1,
185+
column: 56,
186+
},
187+
},
188+
},
189+
},
190+
],
191+
loc: {
192+
end: {
193+
column: 56,
194+
line: 1,
195+
},
196+
start: {
197+
column: 0,
198+
line: 1,
199+
},
200+
},
201+
range: [0, 56],
202+
type: "Document",
203+
};

src/tree-constructor/__tests__/construct-tree.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ import SCRIPT_ELEMENTS_ATTRIBUTES_OUTPUT from "./__output__/script-elements-attr
4242
import EMPTY_INPUT from "../../tokenizer/__tests__/__output__/empty";
4343
import EMPTY_OUTPUT from "./__output__/empty";
4444

45+
import SVG_INPUT from "../../tokenizer/__tests__/__output__/svg";
46+
import SVG_OUTPUT from "./__output__/svg";
47+
4548
import { clearParent } from "../../utils";
4649

4750
describe("construct-tree", () => {
@@ -76,6 +79,7 @@ describe("construct-tree", () => {
7679
],
7780
["Tag register", TAGS_REGISTER_INPUT, TAGS_REGISTERS_OUTPUT],
7881
["Empty", EMPTY_INPUT, EMPTY_OUTPUT],
82+
["Svg", SVG_INPUT, SVG_OUTPUT],
7983
])("%s", (name: string, inputTokens: any, output: any) => {
8084
const { ast } = constructTree(inputTokens, undefined);
8185
expect(clearParent(ast)).toEqual(output);

0 commit comments

Comments
 (0)