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