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