olum-compiler 1.9.2 → 1.9.4
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/lib/changes.txt +8 -0
- package/lib/parser.js +2 -2
- package/lib/sfc.js +133 -8
- package/package.json +1 -1
- package/tests/compiler.test.js +101 -1
package/lib/changes.txt
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
change /tmp/olum-dev-test-dNAask/src/page.html
|
|
2
|
+
change /tmp/olum-dev-test-dNAask/public/main.css
|
|
3
|
+
addDir /tmp/olum-dev-test-dNAask/src/blog
|
|
4
|
+
add /tmp/olum-dev-test-dNAask/src/blog/page.html
|
|
5
|
+
unlink /tmp/olum-dev-test-dNAask/src/blog/page.html
|
|
6
|
+
unlinkDir /tmp/olum-dev-test-dNAask/src/blog
|
|
7
|
+
change /tmp/olum-dev-test-dNAask/src/util.js
|
|
8
|
+
change /tmp/olum-dev-test-dNAask/src/page.html
|
package/lib/parser.js
CHANGED
|
@@ -22,7 +22,7 @@ const liveProps = require("./liveProps");
|
|
|
22
22
|
const usage = require("./usage");
|
|
23
23
|
|
|
24
24
|
const parser = (str, counter, filePath) => {
|
|
25
|
-
const sfc = splitSFC(str);
|
|
25
|
+
const sfc = splitSFC(str, filePath);
|
|
26
26
|
|
|
27
27
|
let breaks = "";
|
|
28
28
|
for (let i = 0; i < sfc.scriptStartLine; i++) breaks += "//\n";
|
|
@@ -152,7 +152,7 @@ const parser = (str, counter, filePath) => {
|
|
|
152
152
|
return entries.join(",");
|
|
153
153
|
}
|
|
154
154
|
|
|
155
|
-
js = js.replace(/params\(
|
|
155
|
+
js = js.replace(/params\(/g, `params("${filePath}",location.pathname`);
|
|
156
156
|
|
|
157
157
|
js = js.replace(/props\(\s*\)/g, "props(_storeKey)");
|
|
158
158
|
html = html.replace(/props\(\s*\)/g, "props(_storeKey)");
|
package/lib/sfc.js
CHANGED
|
@@ -1,7 +1,99 @@
|
|
|
1
1
|
const parse5 = require("parse5");
|
|
2
|
+
const colors = require("./colors");
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
const RAW_TEXT_TAGS = ["script", "style", "textarea", "title"];
|
|
5
|
+
|
|
6
|
+
const TAG_BODY = "(?:\"[^\"]*\"|'[^']*'|[^>\"'])*?";
|
|
7
|
+
const SELF_CLOSING_RAW_TEXT = new RegExp(
|
|
8
|
+
"<(" + RAW_TEXT_TAGS.join("|") + ")(" + TAG_BODY + ")\\/\\s*>",
|
|
9
|
+
"gi",
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
const expandSelfClosingRawText = (source) =>
|
|
13
|
+
source.replace(
|
|
14
|
+
SELF_CLOSING_RAW_TEXT,
|
|
15
|
+
(_m, name, body) => "<" + name + body + "></" + name + ">",
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
function findTagEnd(text, from) {
|
|
19
|
+
let quote = null;
|
|
20
|
+
for (let i = from; i < text.length; i++) {
|
|
21
|
+
const char = text[i];
|
|
22
|
+
if (quote) {
|
|
23
|
+
if (char === quote) quote = null;
|
|
24
|
+
} else if (char === '"' || char === "'") quote = char;
|
|
25
|
+
else if (char === ">") return i;
|
|
26
|
+
}
|
|
27
|
+
return -1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function scanRawTextBlocks(text, names) {
|
|
31
|
+
const blocks = [];
|
|
32
|
+
const open = new RegExp("<(" + names.join("|") + ")(?=[\\s/>])", "gi");
|
|
33
|
+
let match;
|
|
34
|
+
while ((match = open.exec(text)) !== null) {
|
|
35
|
+
const start = match.index;
|
|
36
|
+
const tagEnd = findTagEnd(text, start);
|
|
37
|
+
if (tagEnd === -1) break;
|
|
38
|
+
const innerStart = tagEnd + 1;
|
|
39
|
+
const close = new RegExp("<\\/" + match[1] + "\\s*>", "i");
|
|
40
|
+
const closeMatch = text.slice(innerStart).match(close);
|
|
41
|
+
const innerEnd = closeMatch ? innerStart + closeMatch.index : text.length;
|
|
42
|
+
const inner = text.slice(innerStart, innerEnd);
|
|
43
|
+
|
|
44
|
+
if (!closeMatch && new RegExp(open.source, "i").test(inner)) {
|
|
45
|
+
open.lastIndex = innerStart;
|
|
46
|
+
blocks.push({
|
|
47
|
+
name: match[1].toLowerCase(),
|
|
48
|
+
inner: "",
|
|
49
|
+
start,
|
|
50
|
+
end: innerStart,
|
|
51
|
+
spurious: true,
|
|
52
|
+
});
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
const end = closeMatch ? innerEnd + closeMatch[0].length : text.length;
|
|
56
|
+
blocks.push({ name: match[1].toLowerCase(), inner, start, end });
|
|
57
|
+
open.lastIndex = end;
|
|
58
|
+
}
|
|
59
|
+
return blocks;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function neutralizeStrayRawText(source) {
|
|
63
|
+
const stray = scanRawTextBlocks(source, ["script", "style"]).filter(
|
|
64
|
+
(b) => b.spurious,
|
|
65
|
+
);
|
|
66
|
+
if (!stray.length) return source;
|
|
67
|
+
let out = "";
|
|
68
|
+
let cursor = 0;
|
|
69
|
+
stray.forEach((block) => {
|
|
70
|
+
out +=
|
|
71
|
+
source.slice(cursor, block.start) +
|
|
72
|
+
"<" +
|
|
73
|
+
source.slice(block.start + 1, block.end);
|
|
74
|
+
cursor = block.end;
|
|
75
|
+
});
|
|
76
|
+
return out + source.slice(cursor);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function warn(filePath, line, msg) {
|
|
80
|
+
console.warn(
|
|
81
|
+
colors("yellow.bold", "\n<<Olum Warning>>\n") +
|
|
82
|
+
colors(
|
|
83
|
+
"yellow",
|
|
84
|
+
" File: " +
|
|
85
|
+
(filePath || "unknown") +
|
|
86
|
+
"\n Line: " +
|
|
87
|
+
line +
|
|
88
|
+
"\n " +
|
|
89
|
+
msg +
|
|
90
|
+
"\n",
|
|
91
|
+
),
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function splitSFC(source, filePath) {
|
|
96
|
+
source = neutralizeStrayRawText(expandSelfClosingRawText(String(source)));
|
|
5
97
|
const doc = parse5.parse(source, {
|
|
6
98
|
sourceCodeLocationInfo: true,
|
|
7
99
|
scriptingEnabled: false,
|
|
@@ -53,15 +145,48 @@ function splitSFC(source) {
|
|
|
53
145
|
});
|
|
54
146
|
template += source.slice(pos);
|
|
55
147
|
|
|
148
|
+
let js = script ? inner(script) : "";
|
|
149
|
+
let css = style ? inner(style) : "";
|
|
150
|
+
let scriptStartLine =
|
|
151
|
+
script && script.sourceCodeLocation
|
|
152
|
+
? script.sourceCodeLocation.startLine - 1
|
|
153
|
+
: 0;
|
|
154
|
+
|
|
155
|
+
const leaked = scanRawTextBlocks(template, ["script", "style"]);
|
|
156
|
+
if (leaked.length) {
|
|
157
|
+
const sourceLine = (block) => {
|
|
158
|
+
const at = source.indexOf(template.slice(block.start, block.end));
|
|
159
|
+
return at === -1 ? 0 : source.slice(0, at).split("\n").length;
|
|
160
|
+
};
|
|
161
|
+
warn(
|
|
162
|
+
filePath,
|
|
163
|
+
sourceLine(leaked[0]),
|
|
164
|
+
"A <" +
|
|
165
|
+
leaked[0].name +
|
|
166
|
+
"> tag stayed inside the markup, so an element above it is still open: an unterminated attribute quote, a literal <script> typed as text, or an unclosed <textarea>/<title>. The tag was pulled out anyway — fix the markup above it.",
|
|
167
|
+
);
|
|
168
|
+
let cleaned = "";
|
|
169
|
+
let cursor = 0;
|
|
170
|
+
leaked.forEach((block) => {
|
|
171
|
+
cleaned += template.slice(cursor, block.start);
|
|
172
|
+
cursor = block.end;
|
|
173
|
+
if (block.name === "style" && !css.trim()) css = block.inner;
|
|
174
|
+
if (block.name === "script" && !js.trim()) {
|
|
175
|
+
js = block.inner;
|
|
176
|
+
const line = sourceLine(block);
|
|
177
|
+
if (line) scriptStartLine = line - 1;
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
cleaned += template.slice(cursor);
|
|
181
|
+
template = cleaned;
|
|
182
|
+
}
|
|
183
|
+
|
|
56
184
|
return {
|
|
57
|
-
js
|
|
185
|
+
js,
|
|
58
186
|
jsAttrs,
|
|
59
187
|
|
|
60
|
-
scriptStartLine
|
|
61
|
-
|
|
62
|
-
? script.sourceCodeLocation.startLine - 1
|
|
63
|
-
: 0,
|
|
64
|
-
css: style ? inner(style) : "",
|
|
188
|
+
scriptStartLine,
|
|
189
|
+
css,
|
|
65
190
|
template: template.trim(),
|
|
66
191
|
};
|
|
67
192
|
}
|
package/package.json
CHANGED
package/tests/compiler.test.js
CHANGED
|
@@ -238,6 +238,41 @@ check(
|
|
|
238
238
|
(out) => /onclick\|a=[^&]*&b=/.test(tmpl(out)) && parses(out),
|
|
239
239
|
);
|
|
240
240
|
|
|
241
|
+
check(
|
|
242
|
+
"several on* attributes on ONE element all survive, joined by OLUM_EVT_SEP",
|
|
243
|
+
comp(
|
|
244
|
+
`<input oninput="draft($event)" onblur="save()" />`,
|
|
245
|
+
`const draft = (e) => 0; const save = () => 0;`,
|
|
246
|
+
),
|
|
247
|
+
(out) => {
|
|
248
|
+
const t = tmpl(out);
|
|
249
|
+
return (
|
|
250
|
+
/oninput\|draft=/.test(t) &&
|
|
251
|
+
/onblur\|save=/.test(t) &&
|
|
252
|
+
t.split("OLUM_EVT_SEP").length === 2 &&
|
|
253
|
+
parses(out)
|
|
254
|
+
);
|
|
255
|
+
},
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
check(
|
|
259
|
+
"a third event on the same element is appended too (2 separators)",
|
|
260
|
+
comp(
|
|
261
|
+
`<div onmousedown="down()" onmousemove="move()" onmouseup="up()"></div>`,
|
|
262
|
+
`const down = () => 0; const move = () => 0; const up = () => 0;`,
|
|
263
|
+
),
|
|
264
|
+
(out) => {
|
|
265
|
+
const t = tmpl(out);
|
|
266
|
+
return (
|
|
267
|
+
/onmousedown\|down=/.test(t) &&
|
|
268
|
+
/onmousemove\|move=/.test(t) &&
|
|
269
|
+
/onmouseup\|up=/.test(t) &&
|
|
270
|
+
t.split("OLUM_EVT_SEP").length === 3 &&
|
|
271
|
+
parses(out)
|
|
272
|
+
);
|
|
273
|
+
},
|
|
274
|
+
);
|
|
275
|
+
|
|
241
276
|
check(
|
|
242
277
|
"$event is forwarded to the handler",
|
|
243
278
|
comp(`<input oninput="setVal($event)" />`, `const setVal = (e) => 0;`),
|
|
@@ -1262,6 +1297,71 @@ mapOf(
|
|
|
1262
1297
|
(out) => Object.keys(out).length === 0,
|
|
1263
1298
|
);
|
|
1264
1299
|
|
|
1300
|
+
section("§26 <style> extraction vs malformed markup");
|
|
1301
|
+
|
|
1302
|
+
const CSS_BLOCK = `<style>\n main#home {\n padding: 20px;\n }\n</style>`;
|
|
1303
|
+
function styleOf(out) {
|
|
1304
|
+
const m = out.match(/__style__\(\) \{ return `([\s\S]*?)`;\}/);
|
|
1305
|
+
return m ? m[1] : "";
|
|
1306
|
+
}
|
|
1307
|
+
function styleSplit(name, template) {
|
|
1308
|
+
check(name, comp(template + "\n" + CSS_BLOCK), (out) => {
|
|
1309
|
+
const html = tmpl(out);
|
|
1310
|
+
return (
|
|
1311
|
+
!/<style/i.test(html) &&
|
|
1312
|
+
!/olum\.esc\(\s*padding/.test(html) &&
|
|
1313
|
+
/padding: 20px/.test(styleOf(out)) &&
|
|
1314
|
+
parses(out)
|
|
1315
|
+
);
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
const realWarn = console.warn;
|
|
1320
|
+
console.warn = () => {};
|
|
1321
|
+
|
|
1322
|
+
styleSplit(
|
|
1323
|
+
"keeps a plain <style> out of the markup",
|
|
1324
|
+
`<main id="home">\n</main>`,
|
|
1325
|
+
);
|
|
1326
|
+
styleSplit(
|
|
1327
|
+
"survives a self-closing <textarea />",
|
|
1328
|
+
`<main id="home">\n <textarea />\n</main>`,
|
|
1329
|
+
);
|
|
1330
|
+
styleSplit(
|
|
1331
|
+
"survives a self-closing <title />",
|
|
1332
|
+
`<main id="home">\n <title />\n</main>`,
|
|
1333
|
+
);
|
|
1334
|
+
styleSplit(
|
|
1335
|
+
"survives a self-closing <script src=... />",
|
|
1336
|
+
`<script src="a.js" />\n<main id="home"></main>`,
|
|
1337
|
+
);
|
|
1338
|
+
styleSplit(
|
|
1339
|
+
"survives an unterminated attribute quote",
|
|
1340
|
+
`<main class="home>\n</main>`,
|
|
1341
|
+
);
|
|
1342
|
+
styleSplit(
|
|
1343
|
+
"survives an unclosed <textarea>",
|
|
1344
|
+
`<main id="home">\n <textarea>hi\n</main>`,
|
|
1345
|
+
);
|
|
1346
|
+
styleSplit(
|
|
1347
|
+
"survives a literal <script> written in text",
|
|
1348
|
+
`<main id="home"><code><script></code></main>`,
|
|
1349
|
+
);
|
|
1350
|
+
|
|
1351
|
+
check(
|
|
1352
|
+
"a <style> inside a JS template literal stays in the script",
|
|
1353
|
+
"<script>\n" +
|
|
1354
|
+
STATE +
|
|
1355
|
+
'\nconst t = `<style>x</style>`;\n</script>\n<main id="home"></main>\n' +
|
|
1356
|
+
CSS_BLOCK,
|
|
1357
|
+
(out) =>
|
|
1358
|
+
/padding: 20px/.test(styleOf(out)) &&
|
|
1359
|
+
!/<style/i.test(tmpl(out)) &&
|
|
1360
|
+
parses(out),
|
|
1361
|
+
);
|
|
1362
|
+
|
|
1363
|
+
console.warn = realWarn;
|
|
1364
|
+
|
|
1265
1365
|
console.log("\n========================");
|
|
1266
1366
|
const summary = `${passed} passed, ${failed} failed`;
|
|
1267
1367
|
console.log((failed ? red(bold(summary)) : green(bold(summary))) + "\n");
|
|
@@ -1279,7 +1379,7 @@ if (failed) {
|
|
|
1279
1379
|
console.log("");
|
|
1280
1380
|
}
|
|
1281
1381
|
|
|
1282
|
-
const EXPECTED_CHECKS =
|
|
1382
|
+
const EXPECTED_CHECKS = 118;
|
|
1283
1383
|
const total = passed + failed;
|
|
1284
1384
|
if (total !== EXPECTED_CHECKS) {
|
|
1285
1385
|
console.log(
|