@sigil-dev/compiler 0.9.17 → 0.9.18

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
@@ -2,9 +2,16 @@
2
2
  "name": "@sigil-dev/compiler",
3
3
  "module": "index.ts",
4
4
  "type": "module",
5
- "version": "0.9.17",
5
+ "version": "0.9.18",
6
6
  "private": false,
7
- "description": "Compiler for the Sigil framework",
7
+ "description": "Compiles Sigil's JSX and $state/$derived macros into direct DOM updates",
8
+ "license": "MIT",
9
+ "homepage": "https://sigil.cane1712.dev/docs/internals/compiler",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://git.cane1712.dev/sigil/sigil.git",
13
+ "directory": "packages/compiler"
14
+ },
8
15
  "peerDependencies": {
9
16
  "typescript": "^5"
10
17
  },
@@ -206,6 +206,39 @@ function neighbour(
206
206
  }
207
207
  return null;
208
208
  }
209
+ /** Elements that sit in a line of text, where HTML renders a line break between two of them as a space. */
210
+ const INLINE = new Set([
211
+ "a",
212
+ "abbr",
213
+ "b",
214
+ "bdi",
215
+ "bdo",
216
+ "cite",
217
+ "code",
218
+ "data",
219
+ "dfn",
220
+ "em",
221
+ "i",
222
+ "kbd",
223
+ "mark",
224
+ "q",
225
+ "s",
226
+ "samp",
227
+ "small",
228
+ "span",
229
+ "strong",
230
+ "sub",
231
+ "sup",
232
+ "time",
233
+ "u",
234
+ "var",
235
+ ]);
236
+ const isInline = (n: t.Node | null) =>
237
+ n !== null &&
238
+ t.isJSXElement(n) &&
239
+ t.isJSXIdentifier(n.openingElement.name) &&
240
+ INLINE.has(n.openingElement.name.name);
241
+
209
242
  const isNode = (n: t.Node | null) =>
210
243
  n !== null &&
211
244
  (t.isJSXElement(n) ||
@@ -251,7 +284,21 @@ export function jsxText(
251
284
  if (!line) continue;
252
285
  out += i === lastNonEmpty ? line : `${line} `;
253
286
  }
254
- if (!out) return null;
287
+ if (!out) {
288
+ // a line break between two inline elements is a space, as in HTML:
289
+ // <a href="…">three.js</a>
290
+ // <span>by the three.js authors</span>
291
+ // Between anything else (blocks, components) it is indentation.
292
+ if (siblings && /[\r\n]/.test(child.value)) {
293
+ const at = siblings.indexOf(child);
294
+ if (
295
+ isInline(neighbour(siblings, at, -1)) &&
296
+ isInline(neighbour(siblings, at, 1))
297
+ )
298
+ return " ";
299
+ }
300
+ return null;
301
+ }
255
302
  if (!siblings) return out;
256
303
 
257
304
  const at = siblings.indexOf(child);
@@ -109,6 +109,52 @@ describe("whitespace", () => {
109
109
  );
110
110
  });
111
111
 
112
+ test("a line break between two inline elements is a space", () => {
113
+ expectAll(
114
+ `const LIBS = [["three.js", "the three.js authors", "MIT"]];
115
+ export const App = () => (
116
+ <ul>
117
+ {LIBS.map(([what, who, licence]) => (
118
+ <li>
119
+ <a href="https://threejs.org">{what}</a>
120
+ <span>
121
+ by {who}, {licence}
122
+ </span>
123
+ </li>
124
+ ))}
125
+ </ul>
126
+ );`,
127
+ '<ul><li><a href="https://threejs.org">three.js</a> <span>by the three.js authors, MIT</span></li></ul>',
128
+ );
129
+ expectAll(
130
+ `export const App = () => (
131
+ <p>
132
+ <b>bold</b>
133
+ <i>italic</i>
134
+ <code>code</code>
135
+ </p>
136
+ );`,
137
+ "<p><b>bold</b> <i>italic</i> <code>code</code></p>",
138
+ );
139
+ });
140
+
141
+ test("indentation between blocks and components stays nothing", () => {
142
+ expectAll(
143
+ `const Item = () => <span>x</span>;
144
+ export const App = () => (
145
+ <div>
146
+ <p>one</p>
147
+ <p>two</p>
148
+ <Item />
149
+ <Item />
150
+ <span>inline</span>
151
+ <div>block</div>
152
+ </div>
153
+ );`,
154
+ "<div><p>one</p><p>two</p><span>x</span><span>x</span><span>inline</span><div>block</div></div>",
155
+ );
156
+ });
157
+
112
158
  test("expressions count as neighbours too", () => {
113
159
  expectAll(
114
160
  `const n = 3;