@sigil-dev/compiler 0.9.15 → 0.9.17
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/package.json
CHANGED
|
@@ -30,7 +30,7 @@ export function collectChildren(
|
|
|
30
30
|
|
|
31
31
|
for (const child of node.children) {
|
|
32
32
|
if (t.isJSXText(child)) {
|
|
33
|
-
const text = jsxText(child);
|
|
33
|
+
const text = jsxText(child, node.children);
|
|
34
34
|
if (text === null) continue;
|
|
35
35
|
childrenElements.push(t.stringLiteral(text));
|
|
36
36
|
continue;
|
package/src/babel/jsx/element.ts
CHANGED
|
@@ -769,7 +769,7 @@ export function processElement(
|
|
|
769
769
|
);
|
|
770
770
|
}
|
|
771
771
|
} else if (t.isJSXText(child)) {
|
|
772
|
-
const text = jsxText(child);
|
|
772
|
+
const text = jsxText(child, node.children);
|
|
773
773
|
if (text === null) continue;
|
|
774
774
|
if (hydrate) {
|
|
775
775
|
// SSR hydration: text already in DOM from SSR, do nothing.
|
|
@@ -77,7 +77,7 @@ export function processFragment(
|
|
|
77
77
|
);
|
|
78
78
|
}
|
|
79
79
|
} else if (t.isJSXText(child)) {
|
|
80
|
-
const text = jsxText(child);
|
|
80
|
+
const text = jsxText(child, node.children);
|
|
81
81
|
if (text === null) continue;
|
|
82
82
|
if (hydrate) {
|
|
83
83
|
// SSR hydration: text already in DOM from SSR, do nothing.
|
package/src/babel/jsx/ssr.ts
CHANGED
|
@@ -77,7 +77,8 @@ function childrenSSR(
|
|
|
77
77
|
const parts: Array<string | t.Expression> = [];
|
|
78
78
|
for (const child of children) {
|
|
79
79
|
if (t.isJSXText(child)) {
|
|
80
|
-
|
|
80
|
+
// raw text (<script>, <style>) has no inline neighbours
|
|
81
|
+
const text = jsxText(child, raw ? undefined : children);
|
|
81
82
|
if (text !== null) parts.push(raw ? text : escapeText(text));
|
|
82
83
|
continue;
|
|
83
84
|
}
|
package/src/babel/jsx/utils.ts
CHANGED
|
@@ -183,17 +183,61 @@ export function buildHydrationScope(
|
|
|
183
183
|
);
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
+
/** Text that takes no space before it: closing punctuation. */
|
|
187
|
+
const NO_SPACE_BEFORE =
|
|
188
|
+
/^[.,;:!?)\]}%\u2019\u201d\u00bb\u2026\u3001\u3002\u300d\u300f\uff09\uff0c\uff0e\uff1a\uff1b\uff01\uff1f]/;
|
|
189
|
+
/** Text that takes no space after it: opening brackets and quotes. */
|
|
190
|
+
const NO_SPACE_AFTER = /[(\[{\u2018\u201c\u00ab\u300c\u300e\uff08\/]$/;
|
|
191
|
+
/** Japanese and Chinese run words together, so a line break there is no space. */
|
|
192
|
+
const CJK =
|
|
193
|
+
/[\u3000-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff00-\uffef]/;
|
|
194
|
+
|
|
195
|
+
/** The sibling an inline neighbour is: an element or an expression, past comments. */
|
|
196
|
+
function neighbour(
|
|
197
|
+
siblings: readonly t.Node[],
|
|
198
|
+
from: number,
|
|
199
|
+
step: 1 | -1,
|
|
200
|
+
): t.Node | null {
|
|
201
|
+
for (let i = from + step; i >= 0 && i < siblings.length; i += step) {
|
|
202
|
+
const n = siblings[i] as t.Node;
|
|
203
|
+
if (t.isJSXExpressionContainer(n) && t.isJSXEmptyExpression(n.expression))
|
|
204
|
+
continue;
|
|
205
|
+
return n;
|
|
206
|
+
}
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
const isNode = (n: t.Node | null) =>
|
|
210
|
+
n !== null &&
|
|
211
|
+
(t.isJSXElement(n) ||
|
|
212
|
+
t.isJSXFragment(n) ||
|
|
213
|
+
t.isJSXExpressionContainer(n) ||
|
|
214
|
+
t.isJSXSpreadChild(n));
|
|
215
|
+
|
|
186
216
|
/**
|
|
187
|
-
* The text a JSXText child renders, or null when nothing is left.
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
* and the
|
|
191
|
-
*
|
|
217
|
+
* The text a JSXText child renders, or null when nothing is left. Whitespace
|
|
218
|
+
* that doesn't touch a line break is kept, so `Hello <b>big</b> world` keeps
|
|
219
|
+
* both spaces. Lines that are only whitespace vanish, the remaining lines
|
|
220
|
+
* join with one space, and the whitespace at the start and end of an
|
|
221
|
+
* element's content goes, so indentation never becomes text.
|
|
222
|
+
*
|
|
223
|
+
* Unlike React's rule, a line break between text and an element or
|
|
224
|
+
* expression next to it reads as a space, the way it does in HTML:
|
|
225
|
+
*
|
|
226
|
+
* loaded from
|
|
227
|
+
* <a href="…">sekai.best</a>.
|
|
228
|
+
*
|
|
229
|
+
* is "loaded from sekai.best." with no {" "}. No space goes before closing
|
|
230
|
+
* punctuation (</a>⏎.), after an opening bracket or quote, or next to
|
|
231
|
+
* Japanese and Chinese text. Pass the parent's children as `siblings` to get
|
|
232
|
+
* this; without them (a <script>'s text) the line breaks just go.
|
|
192
233
|
*
|
|
193
234
|
* Every mode (ssr, hydrate, dom) must go through this: if they disagree,
|
|
194
235
|
* server HTML and client-rendered pages show different text.
|
|
195
236
|
*/
|
|
196
|
-
export function jsxText(
|
|
237
|
+
export function jsxText(
|
|
238
|
+
child: t.JSXText,
|
|
239
|
+
siblings?: readonly t.Node[],
|
|
240
|
+
): string | null {
|
|
197
241
|
const lines = child.value.split(/\r\n|\n|\r/);
|
|
198
242
|
let lastNonEmpty = 0;
|
|
199
243
|
for (let i = 0; i < lines.length; i++) {
|
|
@@ -207,7 +251,24 @@ export function jsxText(child: t.JSXText): string | null {
|
|
|
207
251
|
if (!line) continue;
|
|
208
252
|
out += i === lastNonEmpty ? line : `${line} `;
|
|
209
253
|
}
|
|
210
|
-
|
|
254
|
+
if (!out) return null;
|
|
255
|
+
if (!siblings) return out;
|
|
256
|
+
|
|
257
|
+
const at = siblings.indexOf(child);
|
|
258
|
+
// a line break before the text, after an element or expression
|
|
259
|
+
if (
|
|
260
|
+
/^[ \t]*[\r\n]/.test(child.value) &&
|
|
261
|
+
isNode(neighbour(siblings, at, -1))
|
|
262
|
+
) {
|
|
263
|
+
if (!NO_SPACE_BEFORE.test(out) && !CJK.test(out[0] as string))
|
|
264
|
+
out = ` ${out}`;
|
|
265
|
+
}
|
|
266
|
+
// a line break after the text, before an element or expression
|
|
267
|
+
if (/[\r\n][ \t]*$/.test(child.value) && isNode(neighbour(siblings, at, 1))) {
|
|
268
|
+
if (!NO_SPACE_AFTER.test(out) && !CJK.test(out.at(-1) as string))
|
|
269
|
+
out = `${out} `;
|
|
270
|
+
}
|
|
271
|
+
return out;
|
|
211
272
|
}
|
|
212
273
|
|
|
213
274
|
/** JSX that runs when the expression is evaluated (not inside a function). */
|
|
@@ -72,7 +72,7 @@ describe("whitespace", () => {
|
|
|
72
72
|
);
|
|
73
73
|
});
|
|
74
74
|
|
|
75
|
-
test("
|
|
75
|
+
test("a line break between text and an element reads as a space", () => {
|
|
76
76
|
expectAll(
|
|
77
77
|
`export const App = () => (
|
|
78
78
|
<p>
|
|
@@ -80,7 +80,105 @@ describe("whitespace", () => {
|
|
|
80
80
|
<b>big</b> world
|
|
81
81
|
</p>
|
|
82
82
|
);`,
|
|
83
|
-
"<p>Hello<b>big</b> world</p>",
|
|
83
|
+
"<p>Hello <b>big</b> world</p>",
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("a link wrapped onto its own line needs no {' '}", () => {
|
|
88
|
+
expectAll(
|
|
89
|
+
`export const App = () => (
|
|
90
|
+
<li>
|
|
91
|
+
A few assets are loaded from
|
|
92
|
+
<a href="https://sekai.best">sekai.best</a>
|
|
93
|
+
.
|
|
94
|
+
</li>
|
|
95
|
+
);`,
|
|
96
|
+
'<li>A few assets are loaded from <a href="https://sekai.best">sekai.best</a>.</li>',
|
|
97
|
+
);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
test("an element then text on the next line gets a space", () => {
|
|
101
|
+
expectAll(
|
|
102
|
+
`export const App = () => (
|
|
103
|
+
<p>
|
|
104
|
+
<b>Miku</b>
|
|
105
|
+
sings
|
|
106
|
+
</p>
|
|
107
|
+
);`,
|
|
108
|
+
"<p><b>Miku</b> sings</p>",
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
test("expressions count as neighbours too", () => {
|
|
113
|
+
expectAll(
|
|
114
|
+
`const n = 3;
|
|
115
|
+
export const App = () => (
|
|
116
|
+
<p>
|
|
117
|
+
showing page
|
|
118
|
+
{n}
|
|
119
|
+
of 9
|
|
120
|
+
</p>
|
|
121
|
+
);`,
|
|
122
|
+
"<p>showing page 3 of 9</p>",
|
|
123
|
+
);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("no space before closing punctuation or after an opening bracket", () => {
|
|
127
|
+
expectAll(
|
|
128
|
+
`export const App = () => (
|
|
129
|
+
<p>
|
|
130
|
+
(
|
|
131
|
+
<b>a</b>
|
|
132
|
+
), <i>b</i>
|
|
133
|
+
!
|
|
134
|
+
</p>
|
|
135
|
+
);`,
|
|
136
|
+
"<p>(<b>a</b>), <i>b</i>!</p>",
|
|
137
|
+
);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("no space next to Japanese text", () => {
|
|
141
|
+
expectAll(
|
|
142
|
+
`export const App = () => (
|
|
143
|
+
<p>
|
|
144
|
+
これは
|
|
145
|
+
<b>テスト</b>
|
|
146
|
+
です
|
|
147
|
+
</p>
|
|
148
|
+
);`,
|
|
149
|
+
"<p>これは<b>テスト</b>です</p>",
|
|
150
|
+
);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test("lines of only whitespace between elements still vanish", () => {
|
|
154
|
+
expectAll(
|
|
155
|
+
`export const App = () => (
|
|
156
|
+
<ul>
|
|
157
|
+
<li>a</li>
|
|
158
|
+
<li>b</li>
|
|
159
|
+
</ul>
|
|
160
|
+
);`,
|
|
161
|
+
"<ul><li>a</li><li>b</li></ul>",
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
test("an explicit {' '} still works", () => {
|
|
166
|
+
expectAll(
|
|
167
|
+
`export const App = () => <p>from{" "}<a>x</a></p>;`,
|
|
168
|
+
"<p>from <a>x</a></p>",
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("comments are not neighbours", () => {
|
|
173
|
+
expectAll(
|
|
174
|
+
`export const App = () => (
|
|
175
|
+
<p>
|
|
176
|
+
{/* nothing */}
|
|
177
|
+
text
|
|
178
|
+
{/* nothing */}
|
|
179
|
+
</p>
|
|
180
|
+
);`,
|
|
181
|
+
"<p>text</p>",
|
|
84
182
|
);
|
|
85
183
|
});
|
|
86
184
|
|