Skip to content

Commit 6dcc1c3

Browse files
authored
fix: update node end on attribute value handler (fixes #16) (#17)
1 parent 99ceac7 commit 6dcc1c3

File tree

6 files changed

+210
-0
lines changed

6 files changed

+210
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<a target=”_blank”></a>
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { TokenTypes } from "../../../constants";
2+
3+
export default [
4+
{
5+
type: TokenTypes.OpenTagStart,
6+
value: "<a",
7+
range: [0, 2],
8+
loc: {
9+
start: {
10+
line: 1,
11+
column: 0,
12+
},
13+
end: {
14+
line: 1,
15+
column: 2,
16+
},
17+
},
18+
},
19+
{
20+
type: TokenTypes.AttributeKey,
21+
value: "target",
22+
range: [3, 9],
23+
loc: {
24+
start: {
25+
line: 1,
26+
column: 3,
27+
},
28+
end: {
29+
line: 1,
30+
column: 9,
31+
},
32+
},
33+
},
34+
{
35+
type: TokenTypes.AttributeAssignment,
36+
value: "=",
37+
range: [9, 10],
38+
loc: {
39+
start: {
40+
line: 1,
41+
column: 9,
42+
},
43+
end: {
44+
line: 1,
45+
column: 10,
46+
},
47+
},
48+
},
49+
{
50+
type: TokenTypes.AttributeValue,
51+
value: "”_blank”",
52+
range: [10, 18],
53+
loc: {
54+
start: {
55+
line: 1,
56+
column: 10,
57+
},
58+
end: {
59+
line: 1,
60+
column: 18,
61+
},
62+
},
63+
},
64+
{
65+
type: TokenTypes.OpenTagEnd,
66+
value: ">",
67+
range: [18, 19],
68+
loc: {
69+
start: {
70+
line: 1,
71+
column: 18,
72+
},
73+
end: {
74+
line: 1,
75+
column: 19,
76+
},
77+
},
78+
},
79+
{
80+
type: TokenTypes.CloseTag,
81+
value: "</a>",
82+
range: [19, 23],
83+
loc: {
84+
start: {
85+
line: 1,
86+
column: 19,
87+
},
88+
end: {
89+
line: 1,
90+
column: 23,
91+
},
92+
},
93+
},
94+
];

src/tokenizer/__tests__/tokenize.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import VOID_TAGS from "./__output__/void-tags";
1919
import EMPTY from "./__output__/empty";
2020
import SVG from "./__output__/svg";
2121
import ATTRIBUTES_MULTILINE_CRLF from "./__output__/attributes-multiline-crlf";
22+
import ATTRIBUTES_BARE_WRONG_QUOTE from "./__output__/attributes-bare-wrong-quote";
2223

2324
describe("tokenize", () => {
2425
test.each(
@@ -34,6 +35,11 @@ describe("tokenize", () => {
3435
"attributes-bare.html",
3536
ATTRIBUTES_BARE
3637
],
38+
[
39+
"Attributes bare wrong quotes",
40+
"attributes-bare-wrong-quote.html",
41+
ATTRIBUTES_BARE_WRONG_QUOTE
42+
],
3743
[
3844
"Attributes empty",
3945
"attributes-empty.html",
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
export default {
2+
type: "Document",
3+
range: [0, 23],
4+
children: [
5+
{
6+
type: "Tag",
7+
range: [0, 23],
8+
loc: {
9+
start: {
10+
line: 1,
11+
column: 0,
12+
},
13+
end: {
14+
line: 1,
15+
column: 23,
16+
},
17+
},
18+
name: "a",
19+
children: [],
20+
openStart: {
21+
type: "OpenTagStart",
22+
value: "<a",
23+
loc: {
24+
start: {
25+
line: 1,
26+
column: 0,
27+
},
28+
end: {
29+
line: 1,
30+
column: 2,
31+
},
32+
},
33+
range: [0, 2],
34+
},
35+
openEnd: {
36+
type: "OpenTagEnd",
37+
value: ">",
38+
loc: {
39+
start: {
40+
line: 1,
41+
column: 18,
42+
},
43+
end: {
44+
line: 1,
45+
column: 19,
46+
},
47+
},
48+
range: [18, 19],
49+
},
50+
selfClosing: false,
51+
attributes: [
52+
{
53+
type: "Attribute",
54+
loc: {
55+
start: { line: 1, column: 3 },
56+
end: { line: 1, column: 18 },
57+
},
58+
range: [3, 18],
59+
key: {
60+
type: "AttributeKey",
61+
value: "target",
62+
loc: {
63+
start: { line: 1, column: 3 },
64+
end: { line: 1, column: 9 },
65+
},
66+
range: [3, 9],
67+
},
68+
value: {
69+
type: "AttributeValue",
70+
value: "”_blank”",
71+
loc: {
72+
start: { line: 1, column: 10 },
73+
end: { line: 1, column: 18 },
74+
},
75+
range: [10, 18],
76+
},
77+
},
78+
],
79+
close: {
80+
type: "CloseTag",
81+
value: "</a>",
82+
loc: {
83+
start: { line: 1, column: 19 },
84+
end: { line: 1, column: 23 },
85+
},
86+
range: [19, 23],
87+
},
88+
},
89+
],
90+
loc: {
91+
start: {
92+
line: 1,
93+
column: 0,
94+
},
95+
end: {
96+
line: 1,
97+
column: 23,
98+
},
99+
},
100+
};

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ import SVG_OUTPUT from "./__output__/svg";
4848
import ATTRIBUTES_MULTILINE_CRLF_INPUT from "../../tokenizer/__tests__/__output__/attributes-multiline-crlf";
4949
import ATTRIBUTES_MULTILINE_CRLF_OUTPUT from "./__output__/attributes-multiline-crlf";
5050

51+
import ATTRIBUTES_BARE_WRONG_QUOTE_INPUT from "../../tokenizer/__tests__/__output__/attributes-bare-wrong-quote";
52+
import ATTRIBUTES_BARE_WRONG_QUOTE_OUTPUT from "./__output__/attributes-bare-wrong-quote";
53+
5154
import { clearParent } from "../../utils";
5255

5356
describe("construct-tree", () => {
@@ -58,6 +61,11 @@ describe("construct-tree", () => {
5861
ATTRIBUTES_APOSTROPHE_OUTPUT,
5962
],
6063
["Attributes empty", ATTRIBUTES_EMPTY_INPUT, ATTRIBUTES_EMPTY_OUTPUT],
64+
[
65+
"Attributes bare wrong quote",
66+
ATTRIBUTES_BARE_WRONG_QUOTE_INPUT,
67+
ATTRIBUTES_BARE_WRONG_QUOTE_OUTPUT,
68+
],
6169
["Comments", COMMENTS_INPUT, COMMENTS_OUTPUT],
6270
[
6371
"Opening closing text",

src/tree-constructor/handlers/attribute-value.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ function handleAttributeValue(
4343
const attribute = getLastAttribute(state);
4444

4545
attribute.value = createNodeFrom(token) as AttributeValueNode;
46+
updateNodeEnd(attribute, token);
4647
state.caretPosition++;
4748
return state;
4849
}

0 commit comments

Comments
 (0)