janja 4.1.0 → 4.3.0
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/dist/index.cjs +609 -93
- package/dist/index.d.ts +36 -10
- package/dist/index.esm.js +609 -93
- package/dist/index.js +609 -93
- package/dist/index.min.js +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -342,6 +342,9 @@ const WRITE = 'w';
|
|
|
342
342
|
const UNKNOWN_FILTER = 'u_';
|
|
343
343
|
// Holds the result of a filter call while checking whether to await it
|
|
344
344
|
const RESULT = 'r_';
|
|
345
|
+
// Top-level render context; plain include always receives this, even when
|
|
346
|
+
// nested inside a for/with or an `include … with` partial.
|
|
347
|
+
const TOP_CONTEXT = 'tc_';
|
|
345
348
|
|
|
346
349
|
/**
|
|
347
350
|
* Contains the source map information.
|
|
@@ -609,6 +612,7 @@ const expTokenTypes = {
|
|
|
609
612
|
in: 'IN',
|
|
610
613
|
ni: 'NI',
|
|
611
614
|
of: 'OF',
|
|
615
|
+
with: 'WITH',
|
|
612
616
|
'+': 'ADD',
|
|
613
617
|
'-': 'SUB',
|
|
614
618
|
'*': 'MUL',
|
|
@@ -900,6 +904,7 @@ const expTokenPrecedences = {
|
|
|
900
904
|
IN: 40,
|
|
901
905
|
NI: 40,
|
|
902
906
|
OF: 40,
|
|
907
|
+
WITH: 40,
|
|
903
908
|
ADD: 50,
|
|
904
909
|
SUB: 50,
|
|
905
910
|
MUL: 60,
|
|
@@ -1047,36 +1052,12 @@ class ExpParser {
|
|
|
1047
1052
|
}
|
|
1048
1053
|
continue;
|
|
1049
1054
|
}
|
|
1050
|
-
if (token.type
|
|
1051
|
-
token.type === 'OR' ||
|
|
1052
|
-
token.type === 'IS' ||
|
|
1053
|
-
token.type === 'EQ' ||
|
|
1054
|
-
token.type === 'NE' ||
|
|
1055
|
-
token.type === 'GT' ||
|
|
1056
|
-
token.type === 'LT' ||
|
|
1057
|
-
token.type === 'GE' ||
|
|
1058
|
-
token.type === 'LE' ||
|
|
1059
|
-
token.type === 'IN' ||
|
|
1060
|
-
token.type === 'NI' ||
|
|
1061
|
-
token.type === 'OF' ||
|
|
1062
|
-
token.type === 'ADD' ||
|
|
1063
|
-
token.type === 'SUB' ||
|
|
1064
|
-
token.type === 'MUL' ||
|
|
1065
|
-
token.type === 'DIV' ||
|
|
1066
|
-
token.type === 'MOD' ||
|
|
1067
|
-
token.type === 'ASSIGN') {
|
|
1055
|
+
if (isBinaryOp(token.type)) {
|
|
1068
1056
|
if (!left) {
|
|
1069
1057
|
throw new ExpError(`No left operand for "${token.type}"`, token.loc);
|
|
1070
1058
|
}
|
|
1071
1059
|
if (token.type === 'ASSIGN') {
|
|
1072
|
-
|
|
1073
|
-
if (left.elements.some((el) => el.type !== 'ID' || el.path || el.args)) {
|
|
1074
|
-
throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
|
|
1075
|
-
}
|
|
1076
|
-
}
|
|
1077
|
-
else if (left.type !== 'ID' || left.path || left.args) {
|
|
1078
|
-
throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
|
|
1079
|
-
}
|
|
1060
|
+
assertAssignableLeft(left, token);
|
|
1080
1061
|
}
|
|
1081
1062
|
const right = this.parseExp((t) => isHigher(token, t) ? 'BACK' : undefined);
|
|
1082
1063
|
if (!right) {
|
|
@@ -1179,12 +1160,46 @@ class ExpParser {
|
|
|
1179
1160
|
}
|
|
1180
1161
|
}
|
|
1181
1162
|
}
|
|
1163
|
+
const BINARY_OPS = new Set([
|
|
1164
|
+
'AND',
|
|
1165
|
+
'OR',
|
|
1166
|
+
'IS',
|
|
1167
|
+
'EQ',
|
|
1168
|
+
'NE',
|
|
1169
|
+
'GT',
|
|
1170
|
+
'LT',
|
|
1171
|
+
'GE',
|
|
1172
|
+
'LE',
|
|
1173
|
+
'IN',
|
|
1174
|
+
'NI',
|
|
1175
|
+
'OF',
|
|
1176
|
+
'WITH',
|
|
1177
|
+
'ADD',
|
|
1178
|
+
'SUB',
|
|
1179
|
+
'MUL',
|
|
1180
|
+
'DIV',
|
|
1181
|
+
'MOD',
|
|
1182
|
+
'ASSIGN',
|
|
1183
|
+
]);
|
|
1184
|
+
function isBinaryOp(type) {
|
|
1185
|
+
return BINARY_OPS.has(type);
|
|
1186
|
+
}
|
|
1182
1187
|
// Two operands in a row, e.g. `a b`
|
|
1183
1188
|
function assertNotOperand(token) {
|
|
1184
1189
|
if (token.type === 'ID' || token.type === 'LIT' || token.type === 'LP') {
|
|
1185
1190
|
throw new ExpError(`Unexpected "${token.raw ?? token.value}"`, token.loc);
|
|
1186
1191
|
}
|
|
1187
1192
|
}
|
|
1193
|
+
function assertAssignableLeft(left, token) {
|
|
1194
|
+
if (left.type === 'SEQ') {
|
|
1195
|
+
if (left.elements.some((el) => el.type !== 'ID' || el.path || el.args)) {
|
|
1196
|
+
throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
else if (left.type !== 'ID' || left.path || left.args) {
|
|
1200
|
+
throw new ExpError('Left operand of assignment must be an identifier or a sequence of identifiers', token.loc);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1188
1203
|
function isHigher(a, b) {
|
|
1189
1204
|
// "(" starts an operand; it is not an operator to compare against
|
|
1190
1205
|
if (b.type === 'LP') {
|
|
@@ -1852,6 +1867,20 @@ class Compiler extends Context {
|
|
|
1852
1867
|
writable: true,
|
|
1853
1868
|
value: void 0
|
|
1854
1869
|
});
|
|
1870
|
+
/** Stylesheet bodies keyed by include path (or `\0root` for the root template). */
|
|
1871
|
+
Object.defineProperty(this, "stylesheets", {
|
|
1872
|
+
enumerable: true,
|
|
1873
|
+
configurable: true,
|
|
1874
|
+
writable: true,
|
|
1875
|
+
value: void 0
|
|
1876
|
+
});
|
|
1877
|
+
/** Key used when collecting stylesheets from the template currently compiling. */
|
|
1878
|
+
Object.defineProperty(this, "stylesheetKey", {
|
|
1879
|
+
enumerable: true,
|
|
1880
|
+
configurable: true,
|
|
1881
|
+
writable: true,
|
|
1882
|
+
value: void 0
|
|
1883
|
+
});
|
|
1855
1884
|
Object.defineProperty(this, "sources", {
|
|
1856
1885
|
enumerable: true,
|
|
1857
1886
|
configurable: true,
|
|
@@ -1882,11 +1911,13 @@ class Compiler extends Context {
|
|
|
1882
1911
|
});
|
|
1883
1912
|
this.cache = cache;
|
|
1884
1913
|
}
|
|
1885
|
-
async compile(template, root = true, partials = new Map(), blocks = new Map(), sources = new Map()) {
|
|
1914
|
+
async compile(template, root = true, partials = new Map(), blocks = new Map(), sources = new Map(), stylesheets = new Map(), stylesheetKey = '\0root') {
|
|
1886
1915
|
this.template = template;
|
|
1887
1916
|
this.isRoot = root;
|
|
1888
1917
|
this.partials = partials;
|
|
1889
1918
|
this.blocks = blocks;
|
|
1919
|
+
this.stylesheets = stylesheets;
|
|
1920
|
+
this.stylesheetKey = stylesheetKey;
|
|
1890
1921
|
this.sources = sources;
|
|
1891
1922
|
// Generate cache key from template and options
|
|
1892
1923
|
const cacheKey = this.generateCacheKey(template);
|
|
@@ -1909,14 +1940,19 @@ class Compiler extends Context {
|
|
|
1909
1940
|
this.start(this.isRoot);
|
|
1910
1941
|
if (this.isRoot) {
|
|
1911
1942
|
this.pushResourceLimitCounters();
|
|
1943
|
+
this.pushRaw(null, 'let ss_u=0;');
|
|
1912
1944
|
}
|
|
1913
1945
|
await this.parseTemplates();
|
|
1914
1946
|
if (this.isRoot) {
|
|
1915
1947
|
if (this.partials.size) {
|
|
1916
1948
|
const { maxIncludeDepth } = resolveSecurityLimits(this.options.security);
|
|
1917
|
-
|
|
1949
|
+
// Alias the render data before partials shadow `c` with their argument.
|
|
1950
|
+
this.pushRaw(null, `const ${TOP_CONTEXT}=${CONTEXT};`, 'let d_=0;', 'const p={');
|
|
1918
1951
|
for (const [path, code] of this.partials) {
|
|
1919
|
-
this.pushRaw(null,
|
|
1952
|
+
this.pushRaw(null,
|
|
1953
|
+
// Partials take the context they should render with: plain include
|
|
1954
|
+
// passes the top-level `tc_`; `include … with` passes a merged scope.
|
|
1955
|
+
`${JSON.stringify(path)}:async(c)=>{`, `if(d_>=${maxIncludeDepth})throw new Error(${JSON.stringify(`Includes of ${JSON.stringify(path)} are nested too deeply`)});`, 'd_++;', 'try{return await(async()=>{', code, '})();}finally{d_--;}', '},');
|
|
1920
1956
|
}
|
|
1921
1957
|
this.pushRaw(null, '};');
|
|
1922
1958
|
}
|
|
@@ -1935,6 +1971,16 @@ class Compiler extends Context {
|
|
|
1935
1971
|
}
|
|
1936
1972
|
this.pushRaw(null, '};');
|
|
1937
1973
|
}
|
|
1974
|
+
// Always emit ss_ so `{{ stylesheets }}` can run even when empty.
|
|
1975
|
+
this.pushRaw(null, 'const ss_=[');
|
|
1976
|
+
for (const [, nodes] of this.stylesheets) {
|
|
1977
|
+
this.pushRaw(null, 'async()=>{', 'let s="";');
|
|
1978
|
+
for (const node of nodes) {
|
|
1979
|
+
await this.compileNodes(node.body);
|
|
1980
|
+
}
|
|
1981
|
+
this.pushRaw(null, 'return s;', '},');
|
|
1982
|
+
}
|
|
1983
|
+
this.pushRaw(null, '];');
|
|
1938
1984
|
}
|
|
1939
1985
|
this.flushable = this.isRoot && this.stream;
|
|
1940
1986
|
await this.compileNode(this.rootNode);
|
|
@@ -1995,7 +2041,7 @@ class Compiler extends Context {
|
|
|
1995
2041
|
return true;
|
|
1996
2042
|
}
|
|
1997
2043
|
async parseTemplates() {
|
|
1998
|
-
const { partials, blocks } = this.collectPartialsAndBlocks();
|
|
2044
|
+
const { partials, blocks, stylesheets } = this.collectPartialsAndBlocks();
|
|
1999
2045
|
for (const [path, node] of partials) {
|
|
2000
2046
|
if (!this.partials.has(path)) {
|
|
2001
2047
|
let template;
|
|
@@ -2022,10 +2068,15 @@ class Compiler extends Context {
|
|
|
2022
2068
|
}
|
|
2023
2069
|
this.blocks.get(value).push(block);
|
|
2024
2070
|
}
|
|
2071
|
+
// One entry per source file (Shopify: at most one stylesheet tag per file).
|
|
2072
|
+
if (stylesheets.length && !this.stylesheets.has(this.stylesheetKey)) {
|
|
2073
|
+
this.stylesheets.set(this.stylesheetKey, stylesheets);
|
|
2074
|
+
}
|
|
2025
2075
|
}
|
|
2026
2076
|
collectPartialsAndBlocks() {
|
|
2027
2077
|
const partials = new Map();
|
|
2028
2078
|
const blocks = [];
|
|
2079
|
+
const stylesheets = [];
|
|
2029
2080
|
const traverse = (node) => {
|
|
2030
2081
|
if (node.type === 'INCLUDE') {
|
|
2031
2082
|
if (!partials.has(node.val.value)) {
|
|
@@ -2035,12 +2086,16 @@ class Compiler extends Context {
|
|
|
2035
2086
|
if (node.type === 'BLOCK') {
|
|
2036
2087
|
blocks.push(node);
|
|
2037
2088
|
}
|
|
2089
|
+
if (node.type === 'STYLESHEET') {
|
|
2090
|
+
stylesheets.push(node);
|
|
2091
|
+
}
|
|
2038
2092
|
node.traverse(traverse);
|
|
2039
2093
|
};
|
|
2040
2094
|
traverse(this.rootNode);
|
|
2041
2095
|
return {
|
|
2042
2096
|
partials,
|
|
2043
2097
|
blocks,
|
|
2098
|
+
stylesheets,
|
|
2044
2099
|
};
|
|
2045
2100
|
}
|
|
2046
2101
|
async compileNode(node) {
|
|
@@ -2090,7 +2145,7 @@ class Compiler extends Context {
|
|
|
2090
2145
|
}
|
|
2091
2146
|
async compilePartial(path, node, template) {
|
|
2092
2147
|
try {
|
|
2093
|
-
const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks, this.sources);
|
|
2148
|
+
const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks, this.sources, this.stylesheets, path);
|
|
2094
2149
|
this.partials.set(path, code);
|
|
2095
2150
|
}
|
|
2096
2151
|
catch {
|
|
@@ -2496,7 +2551,7 @@ async function compileSuper({ loc }, compiler) {
|
|
|
2496
2551
|
// Each super() call pops the next level from the stack
|
|
2497
2552
|
compiler.pushRaw(loc, `s+=await b[${JSON.stringify(compiler.state.block)}]?.s?.pop()?.()??"";`);
|
|
2498
2553
|
}
|
|
2499
|
-
const compilers$
|
|
2554
|
+
const compilers$e = {
|
|
2500
2555
|
BLOCK: compileBlock,
|
|
2501
2556
|
SUPER: compileSuper,
|
|
2502
2557
|
};
|
|
@@ -2589,15 +2644,15 @@ function* parseSuper(token, parser) {
|
|
|
2589
2644
|
yield 'NEXT';
|
|
2590
2645
|
yield new SuperNode(token.val, token.loc, token.strip);
|
|
2591
2646
|
}
|
|
2592
|
-
const parsers$
|
|
2647
|
+
const parsers$e = {
|
|
2593
2648
|
block: parseBlock,
|
|
2594
2649
|
super: parseSuper,
|
|
2595
2650
|
endblock: 'unexpected',
|
|
2596
2651
|
};
|
|
2597
2652
|
|
|
2598
|
-
const plugin$
|
|
2599
|
-
compilers: compilers$
|
|
2600
|
-
parsers: parsers$
|
|
2653
|
+
const plugin$e = {
|
|
2654
|
+
compilers: compilers$e,
|
|
2655
|
+
parsers: parsers$e,
|
|
2601
2656
|
};
|
|
2602
2657
|
|
|
2603
2658
|
const expTokenOperators$1 = {
|
|
@@ -2614,6 +2669,7 @@ const expTokenOperators$1 = {
|
|
|
2614
2669
|
IN: ' in ',
|
|
2615
2670
|
NI: ' in ',
|
|
2616
2671
|
OF: ' of ',
|
|
2672
|
+
WITH: ' with ',
|
|
2617
2673
|
ADD: '+',
|
|
2618
2674
|
SUB: '-',
|
|
2619
2675
|
MUL: '*',
|
|
@@ -2773,7 +2829,7 @@ async function compileCall({ val, body, loc }, compiler) {
|
|
|
2773
2829
|
await compiler.compileNodes(body);
|
|
2774
2830
|
compiler.pushRaw(null, 'return s;', '})??"";');
|
|
2775
2831
|
}
|
|
2776
|
-
const compilers$
|
|
2832
|
+
const compilers$d = {
|
|
2777
2833
|
CALL: compileCall,
|
|
2778
2834
|
};
|
|
2779
2835
|
|
|
@@ -2828,14 +2884,14 @@ function* parseCall(token, parser) {
|
|
|
2828
2884
|
}
|
|
2829
2885
|
yield new CallNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
2830
2886
|
}
|
|
2831
|
-
const parsers$
|
|
2887
|
+
const parsers$d = {
|
|
2832
2888
|
call: parseCall,
|
|
2833
2889
|
endcall: 'unexpected',
|
|
2834
2890
|
};
|
|
2835
2891
|
|
|
2836
|
-
const plugin$
|
|
2837
|
-
compilers: compilers$
|
|
2838
|
-
parsers: parsers$
|
|
2892
|
+
const plugin$d = {
|
|
2893
|
+
compilers: compilers$d,
|
|
2894
|
+
parsers: parsers$d,
|
|
2839
2895
|
};
|
|
2840
2896
|
|
|
2841
2897
|
async function compileCapture({ val, body, loc }, compiler) {
|
|
@@ -2845,7 +2901,7 @@ async function compileCapture({ val, body, loc }, compiler) {
|
|
|
2845
2901
|
await compiler.compileNodes(body);
|
|
2846
2902
|
compiler.pushRaw(null, 'return s;})(""));');
|
|
2847
2903
|
}
|
|
2848
|
-
const compilers$
|
|
2904
|
+
const compilers$c = {
|
|
2849
2905
|
CAPTURE: compileCapture,
|
|
2850
2906
|
};
|
|
2851
2907
|
|
|
@@ -2900,14 +2956,14 @@ function* parseCapture(token, parser) {
|
|
|
2900
2956
|
}
|
|
2901
2957
|
yield new CaptureNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
2902
2958
|
}
|
|
2903
|
-
const parsers$
|
|
2959
|
+
const parsers$c = {
|
|
2904
2960
|
capture: parseCapture,
|
|
2905
2961
|
endcapture: 'unexpected',
|
|
2906
2962
|
};
|
|
2907
2963
|
|
|
2908
|
-
const plugin$
|
|
2909
|
-
compilers: compilers$
|
|
2910
|
-
parsers: parsers$
|
|
2964
|
+
const plugin$c = {
|
|
2965
|
+
compilers: compilers$c,
|
|
2966
|
+
parsers: parsers$c,
|
|
2911
2967
|
};
|
|
2912
2968
|
|
|
2913
2969
|
async function compileComment({ val, loc }, compiler) {
|
|
@@ -2915,7 +2971,7 @@ async function compileComment({ val, loc }, compiler) {
|
|
|
2915
2971
|
compiler.pushStr(loc, `<!--${val}-->`);
|
|
2916
2972
|
}
|
|
2917
2973
|
}
|
|
2918
|
-
const compilers$
|
|
2974
|
+
const compilers$b = {
|
|
2919
2975
|
COMMENT: compileComment,
|
|
2920
2976
|
};
|
|
2921
2977
|
|
|
@@ -2953,13 +3009,13 @@ function* parseComment({ val, loc, strip }) {
|
|
|
2953
3009
|
yield 'NEXT';
|
|
2954
3010
|
yield new CommentNode(val, loc, strip);
|
|
2955
3011
|
}
|
|
2956
|
-
const parsers$
|
|
3012
|
+
const parsers$b = {
|
|
2957
3013
|
comment: parseComment,
|
|
2958
3014
|
};
|
|
2959
3015
|
|
|
2960
|
-
const plugin$
|
|
2961
|
-
compilers: compilers$
|
|
2962
|
-
parsers: parsers$
|
|
3016
|
+
const plugin$b = {
|
|
3017
|
+
compilers: compilers$b,
|
|
3018
|
+
parsers: parsers$b,
|
|
2963
3019
|
};
|
|
2964
3020
|
|
|
2965
3021
|
const loop = 'loop:{first:i_===0,index:i_++,last:i_===n_}';
|
|
@@ -3056,7 +3112,7 @@ function compileBreak({ loc }, compiler) {
|
|
|
3056
3112
|
function compileContinue({ loc }, compiler) {
|
|
3057
3113
|
compiler.pushRaw(loc, 'continue;');
|
|
3058
3114
|
}
|
|
3059
|
-
const compilers$
|
|
3115
|
+
const compilers$a = {
|
|
3060
3116
|
FOR: compileFor,
|
|
3061
3117
|
BREAK: compileBreak,
|
|
3062
3118
|
CONTINUE: compileContinue,
|
|
@@ -3272,16 +3328,16 @@ function* parseContinue(token, parser) {
|
|
|
3272
3328
|
yield 'NEXT';
|
|
3273
3329
|
yield new ContinueNode(token.val, token.loc, token.strip);
|
|
3274
3330
|
}
|
|
3275
|
-
const parsers$
|
|
3331
|
+
const parsers$a = {
|
|
3276
3332
|
for: parseFor,
|
|
3277
3333
|
break: parseBreak,
|
|
3278
3334
|
continue: parseContinue,
|
|
3279
3335
|
endfor: 'unexpected',
|
|
3280
3336
|
};
|
|
3281
3337
|
|
|
3282
|
-
const plugin$
|
|
3283
|
-
compilers: compilers$
|
|
3284
|
-
parsers: parsers$
|
|
3338
|
+
const plugin$a = {
|
|
3339
|
+
compilers: compilers$a,
|
|
3340
|
+
parsers: parsers$a,
|
|
3285
3341
|
};
|
|
3286
3342
|
|
|
3287
3343
|
async function compileIf({ test, body, alternatives }, compiler) {
|
|
@@ -3299,7 +3355,7 @@ async function compileIf({ test, body, alternatives }, compiler) {
|
|
|
3299
3355
|
compiler.pushRaw(null, '}');
|
|
3300
3356
|
}
|
|
3301
3357
|
}
|
|
3302
|
-
const compilers$
|
|
3358
|
+
const compilers$9 = {
|
|
3303
3359
|
IF: compileIf,
|
|
3304
3360
|
};
|
|
3305
3361
|
|
|
@@ -3486,7 +3542,7 @@ function* parseElse(token, parser) {
|
|
|
3486
3542
|
yield 'NEXT';
|
|
3487
3543
|
yield new ElseNode(parser.parseUntil(['endif']), token.loc, token.strip);
|
|
3488
3544
|
}
|
|
3489
|
-
const parsers$
|
|
3545
|
+
const parsers$9 = {
|
|
3490
3546
|
if: parseIf,
|
|
3491
3547
|
elseif: 'unexpected',
|
|
3492
3548
|
elsif: 'unexpected',
|
|
@@ -3494,20 +3550,28 @@ const parsers$8 = {
|
|
|
3494
3550
|
endif: 'unexpected',
|
|
3495
3551
|
};
|
|
3496
3552
|
|
|
3497
|
-
const plugin$
|
|
3498
|
-
compilers: compilers$
|
|
3499
|
-
parsers: parsers$
|
|
3553
|
+
const plugin$9 = {
|
|
3554
|
+
compilers: compilers$9,
|
|
3555
|
+
parsers: parsers$9,
|
|
3500
3556
|
};
|
|
3501
3557
|
|
|
3502
|
-
async function compileInclude({ val: { value }, loc }, compiler) {
|
|
3503
|
-
|
|
3558
|
+
async function compileInclude({ val: { value }, loc, withExp }, compiler) {
|
|
3559
|
+
const path = JSON.stringify(value);
|
|
3560
|
+
if (withExp) {
|
|
3561
|
+
const expCompiler = new ExpCompiler();
|
|
3562
|
+
const merged = `{...${compiler.context},...${expCompiler.compile(withExp, compiler.context)}}`;
|
|
3563
|
+
compiler.pushRaw(loc, `s+=await p[${path}]?.(${merged})??"";`);
|
|
3564
|
+
return;
|
|
3565
|
+
}
|
|
3566
|
+
// Plain include always sees the top-level context, not loop/with scopes.
|
|
3567
|
+
compiler.pushRaw(loc, `s+=await p[${path}]?.(${TOP_CONTEXT})??"";`);
|
|
3504
3568
|
}
|
|
3505
|
-
const compilers$
|
|
3569
|
+
const compilers$8 = {
|
|
3506
3570
|
INCLUDE: compileInclude,
|
|
3507
3571
|
};
|
|
3508
3572
|
|
|
3509
3573
|
class IncludeNode extends Traversal {
|
|
3510
|
-
constructor(val, loc, strip) {
|
|
3574
|
+
constructor(val, loc, strip, withExp = null) {
|
|
3511
3575
|
super();
|
|
3512
3576
|
Object.defineProperty(this, "val", {
|
|
3513
3577
|
enumerable: true,
|
|
@@ -3527,6 +3591,12 @@ class IncludeNode extends Traversal {
|
|
|
3527
3591
|
writable: true,
|
|
3528
3592
|
value: strip
|
|
3529
3593
|
});
|
|
3594
|
+
Object.defineProperty(this, "withExp", {
|
|
3595
|
+
enumerable: true,
|
|
3596
|
+
configurable: true,
|
|
3597
|
+
writable: true,
|
|
3598
|
+
value: withExp
|
|
3599
|
+
});
|
|
3530
3600
|
Object.defineProperty(this, "type", {
|
|
3531
3601
|
enumerable: true,
|
|
3532
3602
|
configurable: true,
|
|
@@ -3536,21 +3606,39 @@ class IncludeNode extends Traversal {
|
|
|
3536
3606
|
}
|
|
3537
3607
|
}
|
|
3538
3608
|
|
|
3609
|
+
function isStringLit(exp) {
|
|
3610
|
+
return exp.type === 'LIT' && typeof exp.value === 'string';
|
|
3611
|
+
}
|
|
3539
3612
|
function* parseInclude(token, parser) {
|
|
3540
3613
|
if (!token.expression) {
|
|
3541
3614
|
parser.emitExpErr(token);
|
|
3542
3615
|
return;
|
|
3543
3616
|
}
|
|
3544
3617
|
yield 'NEXT';
|
|
3545
|
-
|
|
3618
|
+
const exp = parser.parseExp(token.expression);
|
|
3619
|
+
if (!exp) {
|
|
3620
|
+
return;
|
|
3621
|
+
}
|
|
3622
|
+
if (isStringLit(exp)) {
|
|
3623
|
+
yield new IncludeNode(exp, token.loc, token.strip);
|
|
3624
|
+
return;
|
|
3625
|
+
}
|
|
3626
|
+
if (exp.type === 'WITH') {
|
|
3627
|
+
const { left, right } = exp;
|
|
3628
|
+
if (isStringLit(left)) {
|
|
3629
|
+
yield new IncludeNode(left, token.loc, token.strip, right);
|
|
3630
|
+
return;
|
|
3631
|
+
}
|
|
3632
|
+
}
|
|
3633
|
+
parser.options.debug?.(new CompileError('"include" expects "path" or "path" with expr', parser.template, token.loc));
|
|
3546
3634
|
}
|
|
3547
|
-
const parsers$
|
|
3635
|
+
const parsers$8 = {
|
|
3548
3636
|
include: parseInclude,
|
|
3549
3637
|
};
|
|
3550
3638
|
|
|
3551
|
-
const plugin$
|
|
3552
|
-
compilers: compilers$
|
|
3553
|
-
parsers: parsers$
|
|
3639
|
+
const plugin$8 = {
|
|
3640
|
+
compilers: compilers$8,
|
|
3641
|
+
parsers: parsers$8,
|
|
3554
3642
|
};
|
|
3555
3643
|
|
|
3556
3644
|
async function compileMacro({ val: { value, args }, loc, body }, compiler) {
|
|
@@ -3576,7 +3664,7 @@ async function compileMacro({ val: { value, args }, loc, body }, compiler) {
|
|
|
3576
3664
|
function compileCaller({ loc }, compiler) {
|
|
3577
3665
|
compiler.pushRaw(loc, 's+=await _c?.()??"";');
|
|
3578
3666
|
}
|
|
3579
|
-
const compilers$
|
|
3667
|
+
const compilers$7 = {
|
|
3580
3668
|
MACRO: compileMacro,
|
|
3581
3669
|
CALLER: compileCaller,
|
|
3582
3670
|
};
|
|
@@ -3695,21 +3783,21 @@ function* parseCaller(token, parser) {
|
|
|
3695
3783
|
yield 'NEXT';
|
|
3696
3784
|
yield new CallerNode(token.val, token.loc, token.strip);
|
|
3697
3785
|
}
|
|
3698
|
-
const parsers$
|
|
3786
|
+
const parsers$7 = {
|
|
3699
3787
|
macro: parseMacro,
|
|
3700
3788
|
caller: parseCaller,
|
|
3701
3789
|
endmacro: 'unexpected',
|
|
3702
3790
|
};
|
|
3703
3791
|
|
|
3704
|
-
const plugin$
|
|
3705
|
-
compilers: compilers$
|
|
3706
|
-
parsers: parsers$
|
|
3792
|
+
const plugin$7 = {
|
|
3793
|
+
compilers: compilers$7,
|
|
3794
|
+
parsers: parsers$7,
|
|
3707
3795
|
};
|
|
3708
3796
|
|
|
3709
3797
|
async function compileOutput({ val }, compiler) {
|
|
3710
3798
|
compiler.pushVar(val.loc, new ExpCompiler().compile(val, compiler.context));
|
|
3711
3799
|
}
|
|
3712
|
-
const compilers$
|
|
3800
|
+
const compilers$6 = {
|
|
3713
3801
|
OUTPUT: compileOutput,
|
|
3714
3802
|
};
|
|
3715
3803
|
|
|
@@ -3749,19 +3837,19 @@ function* parseOutput(token, parser) {
|
|
|
3749
3837
|
const start = updatePosition(`${parser.options.outputOpen}${token.strip.before ? '-' : ''}`, { ...token.loc.start });
|
|
3750
3838
|
yield new OutputNode(parser.parseExp({ val: token.val, loc: { start, end: token.loc.end } }), token.loc, token.strip);
|
|
3751
3839
|
}
|
|
3752
|
-
const parsers$
|
|
3840
|
+
const parsers$6 = {
|
|
3753
3841
|
output: parseOutput,
|
|
3754
3842
|
};
|
|
3755
3843
|
|
|
3756
|
-
const plugin$
|
|
3757
|
-
compilers: compilers$
|
|
3758
|
-
parsers: parsers$
|
|
3844
|
+
const plugin$6 = {
|
|
3845
|
+
compilers: compilers$6,
|
|
3846
|
+
parsers: parsers$6,
|
|
3759
3847
|
};
|
|
3760
3848
|
|
|
3761
3849
|
async function compileSet({ val, loc }, compiler) {
|
|
3762
3850
|
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
3763
3851
|
}
|
|
3764
|
-
const compilers$
|
|
3852
|
+
const compilers$5 = {
|
|
3765
3853
|
SET: compileSet,
|
|
3766
3854
|
};
|
|
3767
3855
|
|
|
@@ -3803,10 +3891,117 @@ function* parseSet(token, parser) {
|
|
|
3803
3891
|
yield 'NEXT';
|
|
3804
3892
|
yield new SetNode(parser.parseExp(token.expression), token.loc, token.strip);
|
|
3805
3893
|
}
|
|
3806
|
-
const parsers$
|
|
3894
|
+
const parsers$5 = {
|
|
3807
3895
|
set: parseSet,
|
|
3808
3896
|
};
|
|
3809
3897
|
|
|
3898
|
+
const plugin$5 = {
|
|
3899
|
+
compilers: compilers$5,
|
|
3900
|
+
parsers: parsers$5,
|
|
3901
|
+
};
|
|
3902
|
+
|
|
3903
|
+
/**
|
|
3904
|
+
* Stylesheet bodies are collected at compile time (like blocks) and emitted
|
|
3905
|
+
* only by `{{ stylesheets }}`. The site of `{{ stylesheet }}` itself writes
|
|
3906
|
+
* nothing.
|
|
3907
|
+
*/
|
|
3908
|
+
async function compileStylesheet() {
|
|
3909
|
+
// no-op: collected into Compiler.stylesheets
|
|
3910
|
+
}
|
|
3911
|
+
async function compileStylesheets({ loc }, compiler) {
|
|
3912
|
+
compiler.pushRaw(loc,
|
|
3913
|
+
// First `stylesheets` wins; later ones are ignored (same idea as blocks).
|
|
3914
|
+
'if(!ss_u++){', 's+="<style>";', 'for(const f_ of ss_)s+=await f_();', 's+="</style>";', '}');
|
|
3915
|
+
}
|
|
3916
|
+
const compilers$4 = {
|
|
3917
|
+
STYLESHEET: compileStylesheet,
|
|
3918
|
+
STYLESHEETS: compileStylesheets,
|
|
3919
|
+
};
|
|
3920
|
+
|
|
3921
|
+
/** CSS co-located with a section/snippet (Shopify `{% stylesheet %}` analogue). */
|
|
3922
|
+
class StylesheetNode extends Traversal {
|
|
3923
|
+
constructor(body, loc, strip) {
|
|
3924
|
+
super();
|
|
3925
|
+
Object.defineProperty(this, "body", {
|
|
3926
|
+
enumerable: true,
|
|
3927
|
+
configurable: true,
|
|
3928
|
+
writable: true,
|
|
3929
|
+
value: body
|
|
3930
|
+
});
|
|
3931
|
+
Object.defineProperty(this, "loc", {
|
|
3932
|
+
enumerable: true,
|
|
3933
|
+
configurable: true,
|
|
3934
|
+
writable: true,
|
|
3935
|
+
value: loc
|
|
3936
|
+
});
|
|
3937
|
+
Object.defineProperty(this, "strip", {
|
|
3938
|
+
enumerable: true,
|
|
3939
|
+
configurable: true,
|
|
3940
|
+
writable: true,
|
|
3941
|
+
value: strip
|
|
3942
|
+
});
|
|
3943
|
+
Object.defineProperty(this, "type", {
|
|
3944
|
+
enumerable: true,
|
|
3945
|
+
configurable: true,
|
|
3946
|
+
writable: true,
|
|
3947
|
+
value: 'STYLESHEET'
|
|
3948
|
+
});
|
|
3949
|
+
}
|
|
3950
|
+
}
|
|
3951
|
+
/** Emits every collected stylesheet into the document (typically in `<head>`). */
|
|
3952
|
+
class StylesheetsNode extends Traversal {
|
|
3953
|
+
constructor(loc, strip) {
|
|
3954
|
+
super();
|
|
3955
|
+
Object.defineProperty(this, "loc", {
|
|
3956
|
+
enumerable: true,
|
|
3957
|
+
configurable: true,
|
|
3958
|
+
writable: true,
|
|
3959
|
+
value: loc
|
|
3960
|
+
});
|
|
3961
|
+
Object.defineProperty(this, "strip", {
|
|
3962
|
+
enumerable: true,
|
|
3963
|
+
configurable: true,
|
|
3964
|
+
writable: true,
|
|
3965
|
+
value: strip
|
|
3966
|
+
});
|
|
3967
|
+
Object.defineProperty(this, "type", {
|
|
3968
|
+
enumerable: true,
|
|
3969
|
+
configurable: true,
|
|
3970
|
+
writable: true,
|
|
3971
|
+
value: 'STYLESHEETS'
|
|
3972
|
+
});
|
|
3973
|
+
}
|
|
3974
|
+
}
|
|
3975
|
+
|
|
3976
|
+
function* parseStylesheet(token, parser) {
|
|
3977
|
+
if (token.expression) {
|
|
3978
|
+
parser.emitExpErr(token, false);
|
|
3979
|
+
return;
|
|
3980
|
+
}
|
|
3981
|
+
yield 'NEXT';
|
|
3982
|
+
const body = parser.parseUntil(['endstylesheet']);
|
|
3983
|
+
if (parser.match(['endstylesheet'])) {
|
|
3984
|
+
yield 'NEXT';
|
|
3985
|
+
}
|
|
3986
|
+
else {
|
|
3987
|
+
parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
3988
|
+
}
|
|
3989
|
+
yield new StylesheetNode(body, token.loc, token.strip);
|
|
3990
|
+
}
|
|
3991
|
+
function* parseStylesheets(token, parser) {
|
|
3992
|
+
if (token.expression) {
|
|
3993
|
+
parser.emitExpErr(token, false);
|
|
3994
|
+
return;
|
|
3995
|
+
}
|
|
3996
|
+
yield 'NEXT';
|
|
3997
|
+
yield new StylesheetsNode(token.loc, token.strip);
|
|
3998
|
+
}
|
|
3999
|
+
const parsers$4 = {
|
|
4000
|
+
stylesheet: parseStylesheet,
|
|
4001
|
+
endstylesheet: 'unexpected',
|
|
4002
|
+
stylesheets: parseStylesheets,
|
|
4003
|
+
};
|
|
4004
|
+
|
|
3810
4005
|
const plugin$4 = {
|
|
3811
4006
|
compilers: compilers$4,
|
|
3812
4007
|
parsers: parsers$4,
|
|
@@ -4063,17 +4258,18 @@ const plugin = {
|
|
|
4063
4258
|
};
|
|
4064
4259
|
|
|
4065
4260
|
const plugins = [
|
|
4261
|
+
plugin$e,
|
|
4066
4262
|
plugin$d,
|
|
4263
|
+
plugin$b,
|
|
4067
4264
|
plugin$c,
|
|
4068
4265
|
plugin$a,
|
|
4069
|
-
plugin$b,
|
|
4070
4266
|
plugin$9,
|
|
4071
4267
|
plugin$8,
|
|
4072
4268
|
plugin$7,
|
|
4073
|
-
plugin$
|
|
4269
|
+
plugin$5,
|
|
4074
4270
|
plugin$4,
|
|
4075
4271
|
plugin$3,
|
|
4076
|
-
plugin$
|
|
4272
|
+
plugin$6,
|
|
4077
4273
|
plugin$2,
|
|
4078
4274
|
plugin$1,
|
|
4079
4275
|
plugin,
|
|
@@ -4466,7 +4662,13 @@ class Analyzer {
|
|
|
4466
4662
|
this.blocks.add(node.val.value);
|
|
4467
4663
|
await this.nodes(node.body, scope);
|
|
4468
4664
|
break;
|
|
4665
|
+
case 'STYLESHEET':
|
|
4666
|
+
await this.nodes(node.body, scope);
|
|
4667
|
+
break;
|
|
4469
4668
|
case 'INCLUDE':
|
|
4669
|
+
if (node.withExp) {
|
|
4670
|
+
this.exp(node.withExp, scope);
|
|
4671
|
+
}
|
|
4470
4672
|
await this.include(node.val.value, scope);
|
|
4471
4673
|
break;
|
|
4472
4674
|
default:
|
|
@@ -5149,6 +5351,7 @@ const expTokenOperators = {
|
|
|
5149
5351
|
IN: 'in',
|
|
5150
5352
|
NI: 'ni',
|
|
5151
5353
|
OF: 'of',
|
|
5354
|
+
WITH: 'with',
|
|
5152
5355
|
ADD: '+',
|
|
5153
5356
|
SUB: '-',
|
|
5154
5357
|
MUL: '*',
|
|
@@ -5189,6 +5392,7 @@ class ExpFormatter {
|
|
|
5189
5392
|
case 'IN':
|
|
5190
5393
|
case 'NI':
|
|
5191
5394
|
case 'OF':
|
|
5395
|
+
case 'WITH':
|
|
5192
5396
|
case 'ADD':
|
|
5193
5397
|
case 'SUB':
|
|
5194
5398
|
case 'MUL':
|
|
@@ -5230,18 +5434,318 @@ class ExpFormatter {
|
|
|
5230
5434
|
}
|
|
5231
5435
|
}
|
|
5232
5436
|
|
|
5437
|
+
/** HTML void elements — never take children, so they do not change indent. */
|
|
5438
|
+
const VOID = new Set([
|
|
5439
|
+
'area',
|
|
5440
|
+
'base',
|
|
5441
|
+
'br',
|
|
5442
|
+
'col',
|
|
5443
|
+
'embed',
|
|
5444
|
+
'hr',
|
|
5445
|
+
'img',
|
|
5446
|
+
'input',
|
|
5447
|
+
'link',
|
|
5448
|
+
'meta',
|
|
5449
|
+
'param',
|
|
5450
|
+
'source',
|
|
5451
|
+
'track',
|
|
5452
|
+
'wbr',
|
|
5453
|
+
]);
|
|
5454
|
+
/** Preserve inner whitespace exactly (like Prettier raw nodes). */
|
|
5455
|
+
const SENSITIVE = new Set(['pre', 'textarea', 'script', 'style']);
|
|
5456
|
+
const DIR_OPEN = new Set([
|
|
5457
|
+
'if',
|
|
5458
|
+
'for',
|
|
5459
|
+
'macro',
|
|
5460
|
+
'block',
|
|
5461
|
+
'call',
|
|
5462
|
+
'capture',
|
|
5463
|
+
'with',
|
|
5464
|
+
'stylesheet',
|
|
5465
|
+
]);
|
|
5466
|
+
/** Directives whose bodies keep leading whitespace (like `<style>`). */
|
|
5467
|
+
const DIR_SENSITIVE = new Set(['stylesheet']);
|
|
5468
|
+
const DIR_MID = new Set(['else', 'elif', 'elseif', 'elsif']);
|
|
5469
|
+
const DIR_CLOSE = new Set([
|
|
5470
|
+
'endif',
|
|
5471
|
+
'endfor',
|
|
5472
|
+
'endmacro',
|
|
5473
|
+
'endblock',
|
|
5474
|
+
'endcall',
|
|
5475
|
+
'endcapture',
|
|
5476
|
+
'endwith',
|
|
5477
|
+
'endstylesheet',
|
|
5478
|
+
]);
|
|
5479
|
+
/**
|
|
5480
|
+
* Reindent a template that already had its Janja tags normalised.
|
|
5481
|
+
* Only leading whitespace on each line changes; line breaks and
|
|
5482
|
+
* intra-line spacing stay put. Content inside pre/textarea/script/style
|
|
5483
|
+
* is left alone; the closing tag of those elements is still reindented.
|
|
5484
|
+
*/
|
|
5485
|
+
function indentHtml(source, options) {
|
|
5486
|
+
const tabWidth = options.tabWidth ?? 2;
|
|
5487
|
+
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5488
|
+
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5489
|
+
const lines = source.split(/\r?\n/);
|
|
5490
|
+
let depth = 0;
|
|
5491
|
+
let sensitive = null;
|
|
5492
|
+
const out = [];
|
|
5493
|
+
for (const line of lines) {
|
|
5494
|
+
const leading = line.match(/^[ \t]*/)?.[0].length ?? 0;
|
|
5495
|
+
const content = line.slice(leading);
|
|
5496
|
+
const { pieces, prefixClosers } = scanLine(content, options);
|
|
5497
|
+
const activeSensitive = sensitive;
|
|
5498
|
+
const closesSensitive = activeSensitive !== null &&
|
|
5499
|
+
pieces.some((piece) => piece.kind === 'close' && piece.sensitive === activeSensitive);
|
|
5500
|
+
if (sensitive && !closesSensitive) {
|
|
5501
|
+
out.push(line);
|
|
5502
|
+
applyDepth(pieces);
|
|
5503
|
+
continue;
|
|
5504
|
+
}
|
|
5505
|
+
const lineDepth = Math.max(0, depth - prefixClosers);
|
|
5506
|
+
out.push(content.length === 0 ? '' : pad.repeat(lineDepth) + content);
|
|
5507
|
+
applyDepth(pieces);
|
|
5508
|
+
}
|
|
5509
|
+
// split() keeps a final empty string when the source already ends in a
|
|
5510
|
+
// newline, so join() alone preserves that trailing newline — do not add
|
|
5511
|
+
// another one or each format grows an extra blank line.
|
|
5512
|
+
return out.join(eol);
|
|
5513
|
+
function applyDepth(pieces) {
|
|
5514
|
+
for (const piece of pieces) {
|
|
5515
|
+
if (piece.kind === 'open') {
|
|
5516
|
+
depth += 1;
|
|
5517
|
+
if (piece.sensitive && !sensitive) {
|
|
5518
|
+
({ sensitive } = piece);
|
|
5519
|
+
}
|
|
5520
|
+
}
|
|
5521
|
+
else if (piece.kind === 'close') {
|
|
5522
|
+
depth = Math.max(0, depth - 1);
|
|
5523
|
+
if (piece.sensitive && sensitive === piece.sensitive) {
|
|
5524
|
+
sensitive = null;
|
|
5525
|
+
}
|
|
5526
|
+
}
|
|
5527
|
+
else if (piece.kind === 'mid') ;
|
|
5528
|
+
}
|
|
5529
|
+
}
|
|
5530
|
+
}
|
|
5531
|
+
function scanLine(line, options) {
|
|
5532
|
+
const pieces = [];
|
|
5533
|
+
let prefixClosers = 0;
|
|
5534
|
+
let atPrefix = true;
|
|
5535
|
+
let i = 0;
|
|
5536
|
+
while (i < line.length) {
|
|
5537
|
+
if (/\s/.test(line[i])) {
|
|
5538
|
+
i += 1;
|
|
5539
|
+
continue;
|
|
5540
|
+
}
|
|
5541
|
+
const next = readAt(line, i, options);
|
|
5542
|
+
if (!next) {
|
|
5543
|
+
// Plain text ends the leading-closer prefix
|
|
5544
|
+
atPrefix = false;
|
|
5545
|
+
i += 1;
|
|
5546
|
+
continue;
|
|
5547
|
+
}
|
|
5548
|
+
if (next.piece) {
|
|
5549
|
+
pieces.push(next.piece);
|
|
5550
|
+
if (atPrefix &&
|
|
5551
|
+
(next.piece.kind === 'close' || next.piece.kind === 'mid')) {
|
|
5552
|
+
prefixClosers += 1;
|
|
5553
|
+
}
|
|
5554
|
+
else {
|
|
5555
|
+
atPrefix = false;
|
|
5556
|
+
}
|
|
5557
|
+
}
|
|
5558
|
+
else {
|
|
5559
|
+
// Comment / output / unknown directive sitting in the prefix
|
|
5560
|
+
atPrefix = false;
|
|
5561
|
+
}
|
|
5562
|
+
i = next.end;
|
|
5563
|
+
}
|
|
5564
|
+
return { pieces, prefixClosers };
|
|
5565
|
+
}
|
|
5566
|
+
function readAt(line, i, options) {
|
|
5567
|
+
const { commentOpen, directiveOpen, outputOpen } = options;
|
|
5568
|
+
if (line.startsWith(commentOpen, i)) {
|
|
5569
|
+
return {
|
|
5570
|
+
end: endOfDelimited(line, i + commentOpen.length, options.commentClose),
|
|
5571
|
+
};
|
|
5572
|
+
}
|
|
5573
|
+
if (line.startsWith(outputOpen, i)) {
|
|
5574
|
+
return {
|
|
5575
|
+
end: endOfDelimited(line, i + outputOpen.length, options.outputClose),
|
|
5576
|
+
};
|
|
5577
|
+
}
|
|
5578
|
+
if (line.startsWith(directiveOpen, i)) {
|
|
5579
|
+
return readDirective(line, i, options);
|
|
5580
|
+
}
|
|
5581
|
+
if (line.startsWith('<!--', i)) {
|
|
5582
|
+
const close = line.indexOf('-->', i + 4);
|
|
5583
|
+
return { end: close === -1 ? line.length : close + 3 };
|
|
5584
|
+
}
|
|
5585
|
+
if (line[i] === '<') {
|
|
5586
|
+
return readHtmlTag(line, i, options);
|
|
5587
|
+
}
|
|
5588
|
+
return null;
|
|
5589
|
+
}
|
|
5590
|
+
function readDirective(line, start, options) {
|
|
5591
|
+
const { directiveOpen, directiveClose } = options;
|
|
5592
|
+
const end = endOfDelimited(line, start + directiveOpen.length, directiveClose);
|
|
5593
|
+
const inner = line.slice(start + directiveOpen.length, end - directiveClose.length);
|
|
5594
|
+
const name = inner
|
|
5595
|
+
.replace(/^\s*-?\s*/, '')
|
|
5596
|
+
.match(/^([A-Za-z_]\w*)/)?.[1]
|
|
5597
|
+
?.toLowerCase();
|
|
5598
|
+
if (!name) {
|
|
5599
|
+
return { end };
|
|
5600
|
+
}
|
|
5601
|
+
if (DIR_CLOSE.has(name)) {
|
|
5602
|
+
const base = name.startsWith('end') ? name.slice(3) : name;
|
|
5603
|
+
return {
|
|
5604
|
+
end,
|
|
5605
|
+
piece: {
|
|
5606
|
+
kind: 'close',
|
|
5607
|
+
sensitive: DIR_SENSITIVE.has(base) ? base : undefined,
|
|
5608
|
+
},
|
|
5609
|
+
};
|
|
5610
|
+
}
|
|
5611
|
+
if (DIR_MID.has(name)) {
|
|
5612
|
+
return { end, piece: { kind: 'mid' } };
|
|
5613
|
+
}
|
|
5614
|
+
if (DIR_OPEN.has(name)) {
|
|
5615
|
+
return {
|
|
5616
|
+
end,
|
|
5617
|
+
piece: {
|
|
5618
|
+
kind: 'open',
|
|
5619
|
+
sensitive: DIR_SENSITIVE.has(name) ? name : undefined,
|
|
5620
|
+
},
|
|
5621
|
+
};
|
|
5622
|
+
}
|
|
5623
|
+
return { end };
|
|
5624
|
+
}
|
|
5625
|
+
function readHtmlTag(line, start, options) {
|
|
5626
|
+
if (line.startsWith('</', start)) {
|
|
5627
|
+
let i = start + 2;
|
|
5628
|
+
while (i < line.length && /\s/.test(line[i])) {
|
|
5629
|
+
i += 1;
|
|
5630
|
+
}
|
|
5631
|
+
const nameStart = i;
|
|
5632
|
+
while (i < line.length && /[A-Za-z0-9:-]/.test(line[i])) {
|
|
5633
|
+
i += 1;
|
|
5634
|
+
}
|
|
5635
|
+
const name = line.slice(nameStart, i).toLowerCase();
|
|
5636
|
+
const end = endOfHtmlTag(line, i, options);
|
|
5637
|
+
if (!name || VOID.has(name)) {
|
|
5638
|
+
return { end };
|
|
5639
|
+
}
|
|
5640
|
+
return {
|
|
5641
|
+
end,
|
|
5642
|
+
piece: {
|
|
5643
|
+
kind: 'close',
|
|
5644
|
+
sensitive: SENSITIVE.has(name) ? name : undefined,
|
|
5645
|
+
},
|
|
5646
|
+
};
|
|
5647
|
+
}
|
|
5648
|
+
if (line.startsWith('<!', start) || line.startsWith('<?', start)) {
|
|
5649
|
+
return { end: endOfHtmlTag(line, start + 2, options) };
|
|
5650
|
+
}
|
|
5651
|
+
let i = start + 1;
|
|
5652
|
+
while (i < line.length && /\s/.test(line[i])) {
|
|
5653
|
+
i += 1;
|
|
5654
|
+
}
|
|
5655
|
+
const nameStart = i;
|
|
5656
|
+
while (i < line.length && /[A-Za-z0-9:-]/.test(line[i])) {
|
|
5657
|
+
i += 1;
|
|
5658
|
+
}
|
|
5659
|
+
const name = line.slice(nameStart, i).toLowerCase();
|
|
5660
|
+
const end = endOfHtmlTag(line, i, options);
|
|
5661
|
+
const raw = line.slice(start, end);
|
|
5662
|
+
const selfClosing = /\/\s*>$/.test(raw) || VOID.has(name);
|
|
5663
|
+
if (!name || selfClosing) {
|
|
5664
|
+
return { end };
|
|
5665
|
+
}
|
|
5666
|
+
return {
|
|
5667
|
+
end,
|
|
5668
|
+
piece: {
|
|
5669
|
+
kind: 'open',
|
|
5670
|
+
sensitive: SENSITIVE.has(name) ? name : undefined,
|
|
5671
|
+
},
|
|
5672
|
+
};
|
|
5673
|
+
}
|
|
5674
|
+
/**
|
|
5675
|
+
* Find the `>` that ends an HTML tag, skipping quotes and Janja tags
|
|
5676
|
+
* (so `class="{{ if a > b }}x{{ endif }}"` does not end early).
|
|
5677
|
+
*/
|
|
5678
|
+
function endOfHtmlTag(line, from, options) {
|
|
5679
|
+
let i = from;
|
|
5680
|
+
let quote = null;
|
|
5681
|
+
while (i < line.length) {
|
|
5682
|
+
const c = line[i];
|
|
5683
|
+
if (quote) {
|
|
5684
|
+
if (c === quote) {
|
|
5685
|
+
quote = null;
|
|
5686
|
+
}
|
|
5687
|
+
else {
|
|
5688
|
+
const skipped = trySkipJanja(line, i, options);
|
|
5689
|
+
if (skipped !== null) {
|
|
5690
|
+
i = skipped;
|
|
5691
|
+
continue;
|
|
5692
|
+
}
|
|
5693
|
+
}
|
|
5694
|
+
i += 1;
|
|
5695
|
+
continue;
|
|
5696
|
+
}
|
|
5697
|
+
const skipped = trySkipJanja(line, i, options);
|
|
5698
|
+
if (skipped !== null) {
|
|
5699
|
+
i = skipped;
|
|
5700
|
+
continue;
|
|
5701
|
+
}
|
|
5702
|
+
if (c === '"' || c === "'") {
|
|
5703
|
+
quote = c;
|
|
5704
|
+
i += 1;
|
|
5705
|
+
continue;
|
|
5706
|
+
}
|
|
5707
|
+
if (c === '>') {
|
|
5708
|
+
return i + 1;
|
|
5709
|
+
}
|
|
5710
|
+
i += 1;
|
|
5711
|
+
}
|
|
5712
|
+
return line.length;
|
|
5713
|
+
}
|
|
5714
|
+
function trySkipJanja(line, i, options) {
|
|
5715
|
+
const { commentOpen, commentClose, outputOpen, outputClose, directiveOpen, directiveClose, } = options;
|
|
5716
|
+
if (line.startsWith(commentOpen, i)) {
|
|
5717
|
+
return endOfDelimited(line, i + commentOpen.length, commentClose);
|
|
5718
|
+
}
|
|
5719
|
+
if (line.startsWith(outputOpen, i)) {
|
|
5720
|
+
return endOfDelimited(line, i + outputOpen.length, outputClose);
|
|
5721
|
+
}
|
|
5722
|
+
if (line.startsWith(directiveOpen, i)) {
|
|
5723
|
+
return endOfDelimited(line, i + directiveOpen.length, directiveClose);
|
|
5724
|
+
}
|
|
5725
|
+
return null;
|
|
5726
|
+
}
|
|
5727
|
+
function endOfDelimited(line, from, close) {
|
|
5728
|
+
const idx = line.indexOf(close, from);
|
|
5729
|
+
return idx === -1 ? line.length : idx + close.length;
|
|
5730
|
+
}
|
|
5731
|
+
|
|
5233
5732
|
// An expression without the positions and spellings that formatting changes
|
|
5234
5733
|
const shape = (exp) => JSON.stringify(exp, (key, value) => key === 'loc' || key === 'raw' ? undefined : (value ?? null));
|
|
5235
5734
|
/**
|
|
5236
|
-
*
|
|
5735
|
+
* Format a template:
|
|
5736
|
+
*
|
|
5737
|
+
* 1. Normalise spacing inside tags:
|
|
5237
5738
|
*
|
|
5238
|
-
*
|
|
5239
|
-
*
|
|
5240
|
-
*
|
|
5739
|
+
* {{=name|upper}} -> {{= name | upper }}
|
|
5740
|
+
* {{if a eq 1}} -> {{ if a eq 1 }}
|
|
5741
|
+
* {{#note#}} -> {{# note #}}
|
|
5241
5742
|
*
|
|
5242
|
-
*
|
|
5243
|
-
*
|
|
5244
|
-
*
|
|
5743
|
+
* 2. Unless `html: false`, reindent HTML elements and block directives
|
|
5744
|
+
* (`if` / `for` / …). Only leading whitespace on each line changes;
|
|
5745
|
+
* attribute values (including embedded tags) and whitespace-sensitive
|
|
5746
|
+
* elements (`pre`, `script`, …) are left alone.
|
|
5747
|
+
*
|
|
5748
|
+
* A tag is left exactly as written whenever its expression does not parse or
|
|
5245
5749
|
* reformatting would not provably keep its meaning. Formatting twice gives
|
|
5246
5750
|
* the same result as formatting once.
|
|
5247
5751
|
*
|
|
@@ -5298,7 +5802,7 @@ function formatTemplate(template, options = {}) {
|
|
|
5298
5802
|
return (formatted !== null &&
|
|
5299
5803
|
`${merged.directiveOpen}${before} ${name}${formatted && ` ${formatted}`} ${after}${merged.directiveClose}`);
|
|
5300
5804
|
};
|
|
5301
|
-
|
|
5805
|
+
const tagged = tokens
|
|
5302
5806
|
.map((token) => {
|
|
5303
5807
|
const source = template.slice(offset(token.loc.start), offset(token.loc.end));
|
|
5304
5808
|
if (token.type === TokenType.TEXT) {
|
|
@@ -5311,6 +5815,18 @@ function formatTemplate(template, options = {}) {
|
|
|
5311
5815
|
return formatTag(token) || source;
|
|
5312
5816
|
})
|
|
5313
5817
|
.join('');
|
|
5818
|
+
if (options.html === false) {
|
|
5819
|
+
return tagged;
|
|
5820
|
+
}
|
|
5821
|
+
return indentHtml(tagged, {
|
|
5822
|
+
tabWidth: options.tabWidth,
|
|
5823
|
+
commentOpen: merged.commentOpen,
|
|
5824
|
+
commentClose: merged.commentClose,
|
|
5825
|
+
directiveOpen: merged.directiveOpen,
|
|
5826
|
+
directiveClose: merged.directiveClose,
|
|
5827
|
+
outputOpen: merged.outputOpen,
|
|
5828
|
+
outputClose: merged.outputClose,
|
|
5829
|
+
});
|
|
5314
5830
|
}
|
|
5315
5831
|
|
|
5316
5832
|
/**
|