Skip to content

Commit b770293

Browse files
authored
Change HTMLText token to split into blanks and words. (#54)
1 parent efb4c1b commit b770293

File tree

144 files changed

+29583
-1963
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+29583
-1963
lines changed

src/parser/converts/text.ts

+28-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,20 @@ export function convertText(
1313
parent,
1414
...ctx.getConvertLocation(node),
1515
}
16-
ctx.addToken("HTMLText", node)
16+
let start = node.start
17+
let word = false
18+
for (let index = node.start; index < node.end; index++) {
19+
if (word !== Boolean(ctx.code[index].trim())) {
20+
if (start < index) {
21+
ctx.addToken("HTMLText", { start, end: index })
22+
}
23+
word = !word
24+
start = index
25+
}
26+
}
27+
if (start < node.end) {
28+
ctx.addToken("HTMLText", { start, end: node.end })
29+
}
1730
return text
1831
}
1932

@@ -29,6 +42,19 @@ export function convertTextToLiteral(
2942
parent,
3043
...ctx.getConvertLocation(node),
3144
}
32-
ctx.addToken("HTMLText", node)
45+
let start = node.start
46+
let word = false
47+
for (let index = node.start; index < node.end; index++) {
48+
if (word !== Boolean(ctx.code[index].trim())) {
49+
if (start < index) {
50+
ctx.addToken("HTMLText", { start, end: index })
51+
}
52+
word = !word
53+
start = index
54+
}
55+
}
56+
if (start < node.end) {
57+
ctx.addToken("HTMLText", { start, end: node.end })
58+
}
3359
return text
3460
}

tests/fixtures/parser/ast/blog/write-less-code01-output.json

+76-4
Original file line numberDiff line numberDiff line change
@@ -1916,16 +1916,52 @@
19161916
},
19171917
{
19181918
"type": "HTMLText",
1919-
"value": " + ",
1919+
"value": " ",
19201920
"range": [
19211921
125,
1922-
128
1922+
126
19231923
],
19241924
"loc": {
19251925
"start": {
19261926
"line": 9,
19271927
"column": 6
19281928
},
1929+
"end": {
1930+
"line": 9,
1931+
"column": 7
1932+
}
1933+
}
1934+
},
1935+
{
1936+
"type": "HTMLText",
1937+
"value": "+",
1938+
"range": [
1939+
126,
1940+
127
1941+
],
1942+
"loc": {
1943+
"start": {
1944+
"line": 9,
1945+
"column": 7
1946+
},
1947+
"end": {
1948+
"line": 9,
1949+
"column": 8
1950+
}
1951+
}
1952+
},
1953+
{
1954+
"type": "HTMLText",
1955+
"value": " ",
1956+
"range": [
1957+
127,
1958+
128
1959+
],
1960+
"loc": {
1961+
"start": {
1962+
"line": 9,
1963+
"column": 8
1964+
},
19291965
"end": {
19301966
"line": 9,
19311967
"column": 9
@@ -1988,16 +2024,52 @@
19882024
},
19892025
{
19902026
"type": "HTMLText",
1991-
"value": " = ",
2027+
"value": " ",
19922028
"range": [
19932029
131,
1994-
134
2030+
132
19952031
],
19962032
"loc": {
19972033
"start": {
19982034
"line": 9,
19992035
"column": 12
20002036
},
2037+
"end": {
2038+
"line": 9,
2039+
"column": 13
2040+
}
2041+
}
2042+
},
2043+
{
2044+
"type": "HTMLText",
2045+
"value": "=",
2046+
"range": [
2047+
132,
2048+
133
2049+
],
2050+
"loc": {
2051+
"start": {
2052+
"line": 9,
2053+
"column": 13
2054+
},
2055+
"end": {
2056+
"line": 9,
2057+
"column": 14
2058+
}
2059+
}
2060+
},
2061+
{
2062+
"type": "HTMLText",
2063+
"value": " ",
2064+
"range": [
2065+
133,
2066+
134
2067+
],
2068+
"loc": {
2069+
"start": {
2070+
"line": 9,
2071+
"column": 14
2072+
},
20012073
"end": {
20022074
"line": 9,
20032075
"column": 15

tests/fixtures/parser/ast/components02-output.json

+20-2
Original file line numberDiff line numberDiff line change
@@ -642,16 +642,34 @@
642642
},
643643
{
644644
"type": "HTMLText",
645-
"value": "\n\tcontents",
645+
"value": "\n\t",
646646
"range": [
647647
82,
648-
92
648+
84
649649
],
650650
"loc": {
651651
"start": {
652652
"line": 5,
653653
"column": 13
654654
},
655+
"end": {
656+
"line": 6,
657+
"column": 1
658+
}
659+
}
660+
},
661+
{
662+
"type": "HTMLText",
663+
"value": "contents",
664+
"range": [
665+
84,
666+
92
667+
],
668+
"loc": {
669+
"start": {
670+
"line": 6,
671+
"column": 1
672+
},
655673
"end": {
656674
"line": 6,
657675
"column": 9

tests/fixtures/parser/ast/components03-output.json

+40-4
Original file line numberDiff line numberDiff line change
@@ -985,16 +985,34 @@
985985
},
986986
{
987987
"type": "HTMLText",
988-
"value": "\n\tcontents",
988+
"value": "\n\t",
989989
"range": [
990990
95,
991-
105
991+
97
992992
],
993993
"loc": {
994994
"start": {
995995
"line": 5,
996996
"column": 26
997997
},
998+
"end": {
999+
"line": 6,
1000+
"column": 1
1001+
}
1002+
}
1003+
},
1004+
{
1005+
"type": "HTMLText",
1006+
"value": "contents",
1007+
"range": [
1008+
97,
1009+
105
1010+
],
1011+
"loc": {
1012+
"start": {
1013+
"line": 6,
1014+
"column": 1
1015+
},
9981016
"end": {
9991017
"line": 6,
10001018
"column": 9
@@ -1327,16 +1345,34 @@
13271345
},
13281346
{
13291347
"type": "HTMLText",
1330-
"value": "\n\tcontents",
1348+
"value": "\n\t",
13311349
"range": [
13321350
177,
1333-
187
1351+
179
13341352
],
13351353
"loc": {
13361354
"start": {
13371355
"line": 8,
13381356
"column": 32
13391357
},
1358+
"end": {
1359+
"line": 9,
1360+
"column": 1
1361+
}
1362+
}
1363+
},
1364+
{
1365+
"type": "HTMLText",
1366+
"value": "contents",
1367+
"range": [
1368+
179,
1369+
187
1370+
],
1371+
"loc": {
1372+
"start": {
1373+
"line": 9,
1374+
"column": 1
1375+
},
13401376
"end": {
13411377
"line": 9,
13421378
"column": 9

0 commit comments

Comments
 (0)