janja 4.1.0 → 4.2.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 +505 -61
- package/dist/index.d.ts +35 -9
- package/dist/index.esm.js +505 -61
- package/dist/index.js +505 -61
- package/dist/index.min.js +1 -1
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
|
@@ -1852,6 +1852,20 @@ class Compiler extends Context {
|
|
|
1852
1852
|
writable: true,
|
|
1853
1853
|
value: void 0
|
|
1854
1854
|
});
|
|
1855
|
+
/** Stylesheet bodies keyed by include path (or `\0root` for the root template). */
|
|
1856
|
+
Object.defineProperty(this, "stylesheets", {
|
|
1857
|
+
enumerable: true,
|
|
1858
|
+
configurable: true,
|
|
1859
|
+
writable: true,
|
|
1860
|
+
value: void 0
|
|
1861
|
+
});
|
|
1862
|
+
/** Key used when collecting stylesheets from the template currently compiling. */
|
|
1863
|
+
Object.defineProperty(this, "stylesheetKey", {
|
|
1864
|
+
enumerable: true,
|
|
1865
|
+
configurable: true,
|
|
1866
|
+
writable: true,
|
|
1867
|
+
value: void 0
|
|
1868
|
+
});
|
|
1855
1869
|
Object.defineProperty(this, "sources", {
|
|
1856
1870
|
enumerable: true,
|
|
1857
1871
|
configurable: true,
|
|
@@ -1882,11 +1896,13 @@ class Compiler extends Context {
|
|
|
1882
1896
|
});
|
|
1883
1897
|
this.cache = cache;
|
|
1884
1898
|
}
|
|
1885
|
-
async compile(template, root = true, partials = new Map(), blocks = new Map(), sources = new Map()) {
|
|
1899
|
+
async compile(template, root = true, partials = new Map(), blocks = new Map(), sources = new Map(), stylesheets = new Map(), stylesheetKey = '\0root') {
|
|
1886
1900
|
this.template = template;
|
|
1887
1901
|
this.isRoot = root;
|
|
1888
1902
|
this.partials = partials;
|
|
1889
1903
|
this.blocks = blocks;
|
|
1904
|
+
this.stylesheets = stylesheets;
|
|
1905
|
+
this.stylesheetKey = stylesheetKey;
|
|
1890
1906
|
this.sources = sources;
|
|
1891
1907
|
// Generate cache key from template and options
|
|
1892
1908
|
const cacheKey = this.generateCacheKey(template);
|
|
@@ -1909,6 +1925,7 @@ class Compiler extends Context {
|
|
|
1909
1925
|
this.start(this.isRoot);
|
|
1910
1926
|
if (this.isRoot) {
|
|
1911
1927
|
this.pushResourceLimitCounters();
|
|
1928
|
+
this.pushRaw(null, 'let ss_u=0;');
|
|
1912
1929
|
}
|
|
1913
1930
|
await this.parseTemplates();
|
|
1914
1931
|
if (this.isRoot) {
|
|
@@ -1935,6 +1952,16 @@ class Compiler extends Context {
|
|
|
1935
1952
|
}
|
|
1936
1953
|
this.pushRaw(null, '};');
|
|
1937
1954
|
}
|
|
1955
|
+
// Always emit ss_ so `{{ stylesheets }}` can run even when empty.
|
|
1956
|
+
this.pushRaw(null, 'const ss_=[');
|
|
1957
|
+
for (const [, nodes] of this.stylesheets) {
|
|
1958
|
+
this.pushRaw(null, 'async()=>{', 'let s="";');
|
|
1959
|
+
for (const node of nodes) {
|
|
1960
|
+
await this.compileNodes(node.body);
|
|
1961
|
+
}
|
|
1962
|
+
this.pushRaw(null, 'return s;', '},');
|
|
1963
|
+
}
|
|
1964
|
+
this.pushRaw(null, '];');
|
|
1938
1965
|
}
|
|
1939
1966
|
this.flushable = this.isRoot && this.stream;
|
|
1940
1967
|
await this.compileNode(this.rootNode);
|
|
@@ -1995,7 +2022,7 @@ class Compiler extends Context {
|
|
|
1995
2022
|
return true;
|
|
1996
2023
|
}
|
|
1997
2024
|
async parseTemplates() {
|
|
1998
|
-
const { partials, blocks } = this.collectPartialsAndBlocks();
|
|
2025
|
+
const { partials, blocks, stylesheets } = this.collectPartialsAndBlocks();
|
|
1999
2026
|
for (const [path, node] of partials) {
|
|
2000
2027
|
if (!this.partials.has(path)) {
|
|
2001
2028
|
let template;
|
|
@@ -2022,10 +2049,15 @@ class Compiler extends Context {
|
|
|
2022
2049
|
}
|
|
2023
2050
|
this.blocks.get(value).push(block);
|
|
2024
2051
|
}
|
|
2052
|
+
// One entry per source file (Shopify: at most one stylesheet tag per file).
|
|
2053
|
+
if (stylesheets.length && !this.stylesheets.has(this.stylesheetKey)) {
|
|
2054
|
+
this.stylesheets.set(this.stylesheetKey, stylesheets);
|
|
2055
|
+
}
|
|
2025
2056
|
}
|
|
2026
2057
|
collectPartialsAndBlocks() {
|
|
2027
2058
|
const partials = new Map();
|
|
2028
2059
|
const blocks = [];
|
|
2060
|
+
const stylesheets = [];
|
|
2029
2061
|
const traverse = (node) => {
|
|
2030
2062
|
if (node.type === 'INCLUDE') {
|
|
2031
2063
|
if (!partials.has(node.val.value)) {
|
|
@@ -2035,12 +2067,16 @@ class Compiler extends Context {
|
|
|
2035
2067
|
if (node.type === 'BLOCK') {
|
|
2036
2068
|
blocks.push(node);
|
|
2037
2069
|
}
|
|
2070
|
+
if (node.type === 'STYLESHEET') {
|
|
2071
|
+
stylesheets.push(node);
|
|
2072
|
+
}
|
|
2038
2073
|
node.traverse(traverse);
|
|
2039
2074
|
};
|
|
2040
2075
|
traverse(this.rootNode);
|
|
2041
2076
|
return {
|
|
2042
2077
|
partials,
|
|
2043
2078
|
blocks,
|
|
2079
|
+
stylesheets,
|
|
2044
2080
|
};
|
|
2045
2081
|
}
|
|
2046
2082
|
async compileNode(node) {
|
|
@@ -2090,7 +2126,7 @@ class Compiler extends Context {
|
|
|
2090
2126
|
}
|
|
2091
2127
|
async compilePartial(path, node, template) {
|
|
2092
2128
|
try {
|
|
2093
|
-
const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks, this.sources);
|
|
2129
|
+
const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks, this.sources, this.stylesheets, path);
|
|
2094
2130
|
this.partials.set(path, code);
|
|
2095
2131
|
}
|
|
2096
2132
|
catch {
|
|
@@ -2496,7 +2532,7 @@ async function compileSuper({ loc }, compiler) {
|
|
|
2496
2532
|
// Each super() call pops the next level from the stack
|
|
2497
2533
|
compiler.pushRaw(loc, `s+=await b[${JSON.stringify(compiler.state.block)}]?.s?.pop()?.()??"";`);
|
|
2498
2534
|
}
|
|
2499
|
-
const compilers$
|
|
2535
|
+
const compilers$e = {
|
|
2500
2536
|
BLOCK: compileBlock,
|
|
2501
2537
|
SUPER: compileSuper,
|
|
2502
2538
|
};
|
|
@@ -2589,15 +2625,15 @@ function* parseSuper(token, parser) {
|
|
|
2589
2625
|
yield 'NEXT';
|
|
2590
2626
|
yield new SuperNode(token.val, token.loc, token.strip);
|
|
2591
2627
|
}
|
|
2592
|
-
const parsers$
|
|
2628
|
+
const parsers$e = {
|
|
2593
2629
|
block: parseBlock,
|
|
2594
2630
|
super: parseSuper,
|
|
2595
2631
|
endblock: 'unexpected',
|
|
2596
2632
|
};
|
|
2597
2633
|
|
|
2598
|
-
const plugin$
|
|
2599
|
-
compilers: compilers$
|
|
2600
|
-
parsers: parsers$
|
|
2634
|
+
const plugin$e = {
|
|
2635
|
+
compilers: compilers$e,
|
|
2636
|
+
parsers: parsers$e,
|
|
2601
2637
|
};
|
|
2602
2638
|
|
|
2603
2639
|
const expTokenOperators$1 = {
|
|
@@ -2773,7 +2809,7 @@ async function compileCall({ val, body, loc }, compiler) {
|
|
|
2773
2809
|
await compiler.compileNodes(body);
|
|
2774
2810
|
compiler.pushRaw(null, 'return s;', '})??"";');
|
|
2775
2811
|
}
|
|
2776
|
-
const compilers$
|
|
2812
|
+
const compilers$d = {
|
|
2777
2813
|
CALL: compileCall,
|
|
2778
2814
|
};
|
|
2779
2815
|
|
|
@@ -2828,14 +2864,14 @@ function* parseCall(token, parser) {
|
|
|
2828
2864
|
}
|
|
2829
2865
|
yield new CallNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
2830
2866
|
}
|
|
2831
|
-
const parsers$
|
|
2867
|
+
const parsers$d = {
|
|
2832
2868
|
call: parseCall,
|
|
2833
2869
|
endcall: 'unexpected',
|
|
2834
2870
|
};
|
|
2835
2871
|
|
|
2836
|
-
const plugin$
|
|
2837
|
-
compilers: compilers$
|
|
2838
|
-
parsers: parsers$
|
|
2872
|
+
const plugin$d = {
|
|
2873
|
+
compilers: compilers$d,
|
|
2874
|
+
parsers: parsers$d,
|
|
2839
2875
|
};
|
|
2840
2876
|
|
|
2841
2877
|
async function compileCapture({ val, body, loc }, compiler) {
|
|
@@ -2845,7 +2881,7 @@ async function compileCapture({ val, body, loc }, compiler) {
|
|
|
2845
2881
|
await compiler.compileNodes(body);
|
|
2846
2882
|
compiler.pushRaw(null, 'return s;})(""));');
|
|
2847
2883
|
}
|
|
2848
|
-
const compilers$
|
|
2884
|
+
const compilers$c = {
|
|
2849
2885
|
CAPTURE: compileCapture,
|
|
2850
2886
|
};
|
|
2851
2887
|
|
|
@@ -2900,14 +2936,14 @@ function* parseCapture(token, parser) {
|
|
|
2900
2936
|
}
|
|
2901
2937
|
yield new CaptureNode(parser.parseExp(token.expression), body, token.loc, token.strip);
|
|
2902
2938
|
}
|
|
2903
|
-
const parsers$
|
|
2939
|
+
const parsers$c = {
|
|
2904
2940
|
capture: parseCapture,
|
|
2905
2941
|
endcapture: 'unexpected',
|
|
2906
2942
|
};
|
|
2907
2943
|
|
|
2908
|
-
const plugin$
|
|
2909
|
-
compilers: compilers$
|
|
2910
|
-
parsers: parsers$
|
|
2944
|
+
const plugin$c = {
|
|
2945
|
+
compilers: compilers$c,
|
|
2946
|
+
parsers: parsers$c,
|
|
2911
2947
|
};
|
|
2912
2948
|
|
|
2913
2949
|
async function compileComment({ val, loc }, compiler) {
|
|
@@ -2915,7 +2951,7 @@ async function compileComment({ val, loc }, compiler) {
|
|
|
2915
2951
|
compiler.pushStr(loc, `<!--${val}-->`);
|
|
2916
2952
|
}
|
|
2917
2953
|
}
|
|
2918
|
-
const compilers$
|
|
2954
|
+
const compilers$b = {
|
|
2919
2955
|
COMMENT: compileComment,
|
|
2920
2956
|
};
|
|
2921
2957
|
|
|
@@ -2953,13 +2989,13 @@ function* parseComment({ val, loc, strip }) {
|
|
|
2953
2989
|
yield 'NEXT';
|
|
2954
2990
|
yield new CommentNode(val, loc, strip);
|
|
2955
2991
|
}
|
|
2956
|
-
const parsers$
|
|
2992
|
+
const parsers$b = {
|
|
2957
2993
|
comment: parseComment,
|
|
2958
2994
|
};
|
|
2959
2995
|
|
|
2960
|
-
const plugin$
|
|
2961
|
-
compilers: compilers$
|
|
2962
|
-
parsers: parsers$
|
|
2996
|
+
const plugin$b = {
|
|
2997
|
+
compilers: compilers$b,
|
|
2998
|
+
parsers: parsers$b,
|
|
2963
2999
|
};
|
|
2964
3000
|
|
|
2965
3001
|
const loop = 'loop:{first:i_===0,index:i_++,last:i_===n_}';
|
|
@@ -3056,7 +3092,7 @@ function compileBreak({ loc }, compiler) {
|
|
|
3056
3092
|
function compileContinue({ loc }, compiler) {
|
|
3057
3093
|
compiler.pushRaw(loc, 'continue;');
|
|
3058
3094
|
}
|
|
3059
|
-
const compilers$
|
|
3095
|
+
const compilers$a = {
|
|
3060
3096
|
FOR: compileFor,
|
|
3061
3097
|
BREAK: compileBreak,
|
|
3062
3098
|
CONTINUE: compileContinue,
|
|
@@ -3272,16 +3308,16 @@ function* parseContinue(token, parser) {
|
|
|
3272
3308
|
yield 'NEXT';
|
|
3273
3309
|
yield new ContinueNode(token.val, token.loc, token.strip);
|
|
3274
3310
|
}
|
|
3275
|
-
const parsers$
|
|
3311
|
+
const parsers$a = {
|
|
3276
3312
|
for: parseFor,
|
|
3277
3313
|
break: parseBreak,
|
|
3278
3314
|
continue: parseContinue,
|
|
3279
3315
|
endfor: 'unexpected',
|
|
3280
3316
|
};
|
|
3281
3317
|
|
|
3282
|
-
const plugin$
|
|
3283
|
-
compilers: compilers$
|
|
3284
|
-
parsers: parsers$
|
|
3318
|
+
const plugin$a = {
|
|
3319
|
+
compilers: compilers$a,
|
|
3320
|
+
parsers: parsers$a,
|
|
3285
3321
|
};
|
|
3286
3322
|
|
|
3287
3323
|
async function compileIf({ test, body, alternatives }, compiler) {
|
|
@@ -3299,7 +3335,7 @@ async function compileIf({ test, body, alternatives }, compiler) {
|
|
|
3299
3335
|
compiler.pushRaw(null, '}');
|
|
3300
3336
|
}
|
|
3301
3337
|
}
|
|
3302
|
-
const compilers$
|
|
3338
|
+
const compilers$9 = {
|
|
3303
3339
|
IF: compileIf,
|
|
3304
3340
|
};
|
|
3305
3341
|
|
|
@@ -3486,7 +3522,7 @@ function* parseElse(token, parser) {
|
|
|
3486
3522
|
yield 'NEXT';
|
|
3487
3523
|
yield new ElseNode(parser.parseUntil(['endif']), token.loc, token.strip);
|
|
3488
3524
|
}
|
|
3489
|
-
const parsers$
|
|
3525
|
+
const parsers$9 = {
|
|
3490
3526
|
if: parseIf,
|
|
3491
3527
|
elseif: 'unexpected',
|
|
3492
3528
|
elsif: 'unexpected',
|
|
@@ -3494,15 +3530,15 @@ const parsers$8 = {
|
|
|
3494
3530
|
endif: 'unexpected',
|
|
3495
3531
|
};
|
|
3496
3532
|
|
|
3497
|
-
const plugin$
|
|
3498
|
-
compilers: compilers$
|
|
3499
|
-
parsers: parsers$
|
|
3533
|
+
const plugin$9 = {
|
|
3534
|
+
compilers: compilers$9,
|
|
3535
|
+
parsers: parsers$9,
|
|
3500
3536
|
};
|
|
3501
3537
|
|
|
3502
3538
|
async function compileInclude({ val: { value }, loc }, compiler) {
|
|
3503
3539
|
compiler.pushRaw(loc, `s+=await p[${JSON.stringify(value)}]?.()??"";`);
|
|
3504
3540
|
}
|
|
3505
|
-
const compilers$
|
|
3541
|
+
const compilers$8 = {
|
|
3506
3542
|
INCLUDE: compileInclude,
|
|
3507
3543
|
};
|
|
3508
3544
|
|
|
@@ -3544,13 +3580,13 @@ function* parseInclude(token, parser) {
|
|
|
3544
3580
|
yield 'NEXT';
|
|
3545
3581
|
yield new IncludeNode(parser.parseExp(token.expression), token.loc, token.strip);
|
|
3546
3582
|
}
|
|
3547
|
-
const parsers$
|
|
3583
|
+
const parsers$8 = {
|
|
3548
3584
|
include: parseInclude,
|
|
3549
3585
|
};
|
|
3550
3586
|
|
|
3551
|
-
const plugin$
|
|
3552
|
-
compilers: compilers$
|
|
3553
|
-
parsers: parsers$
|
|
3587
|
+
const plugin$8 = {
|
|
3588
|
+
compilers: compilers$8,
|
|
3589
|
+
parsers: parsers$8,
|
|
3554
3590
|
};
|
|
3555
3591
|
|
|
3556
3592
|
async function compileMacro({ val: { value, args }, loc, body }, compiler) {
|
|
@@ -3576,7 +3612,7 @@ async function compileMacro({ val: { value, args }, loc, body }, compiler) {
|
|
|
3576
3612
|
function compileCaller({ loc }, compiler) {
|
|
3577
3613
|
compiler.pushRaw(loc, 's+=await _c?.()??"";');
|
|
3578
3614
|
}
|
|
3579
|
-
const compilers$
|
|
3615
|
+
const compilers$7 = {
|
|
3580
3616
|
MACRO: compileMacro,
|
|
3581
3617
|
CALLER: compileCaller,
|
|
3582
3618
|
};
|
|
@@ -3695,21 +3731,21 @@ function* parseCaller(token, parser) {
|
|
|
3695
3731
|
yield 'NEXT';
|
|
3696
3732
|
yield new CallerNode(token.val, token.loc, token.strip);
|
|
3697
3733
|
}
|
|
3698
|
-
const parsers$
|
|
3734
|
+
const parsers$7 = {
|
|
3699
3735
|
macro: parseMacro,
|
|
3700
3736
|
caller: parseCaller,
|
|
3701
3737
|
endmacro: 'unexpected',
|
|
3702
3738
|
};
|
|
3703
3739
|
|
|
3704
|
-
const plugin$
|
|
3705
|
-
compilers: compilers$
|
|
3706
|
-
parsers: parsers$
|
|
3740
|
+
const plugin$7 = {
|
|
3741
|
+
compilers: compilers$7,
|
|
3742
|
+
parsers: parsers$7,
|
|
3707
3743
|
};
|
|
3708
3744
|
|
|
3709
3745
|
async function compileOutput({ val }, compiler) {
|
|
3710
3746
|
compiler.pushVar(val.loc, new ExpCompiler().compile(val, compiler.context));
|
|
3711
3747
|
}
|
|
3712
|
-
const compilers$
|
|
3748
|
+
const compilers$6 = {
|
|
3713
3749
|
OUTPUT: compileOutput,
|
|
3714
3750
|
};
|
|
3715
3751
|
|
|
@@ -3749,19 +3785,19 @@ function* parseOutput(token, parser) {
|
|
|
3749
3785
|
const start = updatePosition(`${parser.options.outputOpen}${token.strip.before ? '-' : ''}`, { ...token.loc.start });
|
|
3750
3786
|
yield new OutputNode(parser.parseExp({ val: token.val, loc: { start, end: token.loc.end } }), token.loc, token.strip);
|
|
3751
3787
|
}
|
|
3752
|
-
const parsers$
|
|
3788
|
+
const parsers$6 = {
|
|
3753
3789
|
output: parseOutput,
|
|
3754
3790
|
};
|
|
3755
3791
|
|
|
3756
|
-
const plugin$
|
|
3757
|
-
compilers: compilers$
|
|
3758
|
-
parsers: parsers$
|
|
3792
|
+
const plugin$6 = {
|
|
3793
|
+
compilers: compilers$6,
|
|
3794
|
+
parsers: parsers$6,
|
|
3759
3795
|
};
|
|
3760
3796
|
|
|
3761
3797
|
async function compileSet({ val, loc }, compiler) {
|
|
3762
3798
|
compiler.pushRaw(loc, new ExpCompiler().compile(val, compiler.context));
|
|
3763
3799
|
}
|
|
3764
|
-
const compilers$
|
|
3800
|
+
const compilers$5 = {
|
|
3765
3801
|
SET: compileSet,
|
|
3766
3802
|
};
|
|
3767
3803
|
|
|
@@ -3803,10 +3839,117 @@ function* parseSet(token, parser) {
|
|
|
3803
3839
|
yield 'NEXT';
|
|
3804
3840
|
yield new SetNode(parser.parseExp(token.expression), token.loc, token.strip);
|
|
3805
3841
|
}
|
|
3806
|
-
const parsers$
|
|
3842
|
+
const parsers$5 = {
|
|
3807
3843
|
set: parseSet,
|
|
3808
3844
|
};
|
|
3809
3845
|
|
|
3846
|
+
const plugin$5 = {
|
|
3847
|
+
compilers: compilers$5,
|
|
3848
|
+
parsers: parsers$5,
|
|
3849
|
+
};
|
|
3850
|
+
|
|
3851
|
+
/**
|
|
3852
|
+
* Stylesheet bodies are collected at compile time (like blocks) and emitted
|
|
3853
|
+
* only by `{{ stylesheets }}`. The site of `{{ stylesheet }}` itself writes
|
|
3854
|
+
* nothing.
|
|
3855
|
+
*/
|
|
3856
|
+
async function compileStylesheet() {
|
|
3857
|
+
// no-op: collected into Compiler.stylesheets
|
|
3858
|
+
}
|
|
3859
|
+
async function compileStylesheets({ loc }, compiler) {
|
|
3860
|
+
compiler.pushRaw(loc,
|
|
3861
|
+
// First `stylesheets` wins; later ones are ignored (same idea as blocks).
|
|
3862
|
+
'if(!ss_u++){', 's+="<style>";', 'for(const f_ of ss_)s+=await f_();', 's+="</style>";', '}');
|
|
3863
|
+
}
|
|
3864
|
+
const compilers$4 = {
|
|
3865
|
+
STYLESHEET: compileStylesheet,
|
|
3866
|
+
STYLESHEETS: compileStylesheets,
|
|
3867
|
+
};
|
|
3868
|
+
|
|
3869
|
+
/** CSS co-located with a section/snippet (Shopify `{% stylesheet %}` analogue). */
|
|
3870
|
+
class StylesheetNode extends Traversal {
|
|
3871
|
+
constructor(body, loc, strip) {
|
|
3872
|
+
super();
|
|
3873
|
+
Object.defineProperty(this, "body", {
|
|
3874
|
+
enumerable: true,
|
|
3875
|
+
configurable: true,
|
|
3876
|
+
writable: true,
|
|
3877
|
+
value: body
|
|
3878
|
+
});
|
|
3879
|
+
Object.defineProperty(this, "loc", {
|
|
3880
|
+
enumerable: true,
|
|
3881
|
+
configurable: true,
|
|
3882
|
+
writable: true,
|
|
3883
|
+
value: loc
|
|
3884
|
+
});
|
|
3885
|
+
Object.defineProperty(this, "strip", {
|
|
3886
|
+
enumerable: true,
|
|
3887
|
+
configurable: true,
|
|
3888
|
+
writable: true,
|
|
3889
|
+
value: strip
|
|
3890
|
+
});
|
|
3891
|
+
Object.defineProperty(this, "type", {
|
|
3892
|
+
enumerable: true,
|
|
3893
|
+
configurable: true,
|
|
3894
|
+
writable: true,
|
|
3895
|
+
value: 'STYLESHEET'
|
|
3896
|
+
});
|
|
3897
|
+
}
|
|
3898
|
+
}
|
|
3899
|
+
/** Emits every collected stylesheet into the document (typically in `<head>`). */
|
|
3900
|
+
class StylesheetsNode extends Traversal {
|
|
3901
|
+
constructor(loc, strip) {
|
|
3902
|
+
super();
|
|
3903
|
+
Object.defineProperty(this, "loc", {
|
|
3904
|
+
enumerable: true,
|
|
3905
|
+
configurable: true,
|
|
3906
|
+
writable: true,
|
|
3907
|
+
value: loc
|
|
3908
|
+
});
|
|
3909
|
+
Object.defineProperty(this, "strip", {
|
|
3910
|
+
enumerable: true,
|
|
3911
|
+
configurable: true,
|
|
3912
|
+
writable: true,
|
|
3913
|
+
value: strip
|
|
3914
|
+
});
|
|
3915
|
+
Object.defineProperty(this, "type", {
|
|
3916
|
+
enumerable: true,
|
|
3917
|
+
configurable: true,
|
|
3918
|
+
writable: true,
|
|
3919
|
+
value: 'STYLESHEETS'
|
|
3920
|
+
});
|
|
3921
|
+
}
|
|
3922
|
+
}
|
|
3923
|
+
|
|
3924
|
+
function* parseStylesheet(token, parser) {
|
|
3925
|
+
if (token.expression) {
|
|
3926
|
+
parser.emitExpErr(token, false);
|
|
3927
|
+
return;
|
|
3928
|
+
}
|
|
3929
|
+
yield 'NEXT';
|
|
3930
|
+
const body = parser.parseUntil(['endstylesheet']);
|
|
3931
|
+
if (parser.match(['endstylesheet'])) {
|
|
3932
|
+
yield 'NEXT';
|
|
3933
|
+
}
|
|
3934
|
+
else {
|
|
3935
|
+
parser.options.debug?.(new CompileError(`Unclosed "${token.name}"`, parser.template, token.loc));
|
|
3936
|
+
}
|
|
3937
|
+
yield new StylesheetNode(body, token.loc, token.strip);
|
|
3938
|
+
}
|
|
3939
|
+
function* parseStylesheets(token, parser) {
|
|
3940
|
+
if (token.expression) {
|
|
3941
|
+
parser.emitExpErr(token, false);
|
|
3942
|
+
return;
|
|
3943
|
+
}
|
|
3944
|
+
yield 'NEXT';
|
|
3945
|
+
yield new StylesheetsNode(token.loc, token.strip);
|
|
3946
|
+
}
|
|
3947
|
+
const parsers$4 = {
|
|
3948
|
+
stylesheet: parseStylesheet,
|
|
3949
|
+
endstylesheet: 'unexpected',
|
|
3950
|
+
stylesheets: parseStylesheets,
|
|
3951
|
+
};
|
|
3952
|
+
|
|
3810
3953
|
const plugin$4 = {
|
|
3811
3954
|
compilers: compilers$4,
|
|
3812
3955
|
parsers: parsers$4,
|
|
@@ -4063,17 +4206,18 @@ const plugin = {
|
|
|
4063
4206
|
};
|
|
4064
4207
|
|
|
4065
4208
|
const plugins = [
|
|
4209
|
+
plugin$e,
|
|
4066
4210
|
plugin$d,
|
|
4211
|
+
plugin$b,
|
|
4067
4212
|
plugin$c,
|
|
4068
4213
|
plugin$a,
|
|
4069
|
-
plugin$b,
|
|
4070
4214
|
plugin$9,
|
|
4071
4215
|
plugin$8,
|
|
4072
4216
|
plugin$7,
|
|
4073
|
-
plugin$
|
|
4217
|
+
plugin$5,
|
|
4074
4218
|
plugin$4,
|
|
4075
4219
|
plugin$3,
|
|
4076
|
-
plugin$
|
|
4220
|
+
plugin$6,
|
|
4077
4221
|
plugin$2,
|
|
4078
4222
|
plugin$1,
|
|
4079
4223
|
plugin,
|
|
@@ -4466,6 +4610,9 @@ class Analyzer {
|
|
|
4466
4610
|
this.blocks.add(node.val.value);
|
|
4467
4611
|
await this.nodes(node.body, scope);
|
|
4468
4612
|
break;
|
|
4613
|
+
case 'STYLESHEET':
|
|
4614
|
+
await this.nodes(node.body, scope);
|
|
4615
|
+
break;
|
|
4469
4616
|
case 'INCLUDE':
|
|
4470
4617
|
await this.include(node.val.value, scope);
|
|
4471
4618
|
break;
|
|
@@ -5230,18 +5377,303 @@ class ExpFormatter {
|
|
|
5230
5377
|
}
|
|
5231
5378
|
}
|
|
5232
5379
|
|
|
5380
|
+
/** HTML void elements — never take children, so they do not change indent. */
|
|
5381
|
+
const VOID = new Set([
|
|
5382
|
+
'area',
|
|
5383
|
+
'base',
|
|
5384
|
+
'br',
|
|
5385
|
+
'col',
|
|
5386
|
+
'embed',
|
|
5387
|
+
'hr',
|
|
5388
|
+
'img',
|
|
5389
|
+
'input',
|
|
5390
|
+
'link',
|
|
5391
|
+
'meta',
|
|
5392
|
+
'param',
|
|
5393
|
+
'source',
|
|
5394
|
+
'track',
|
|
5395
|
+
'wbr',
|
|
5396
|
+
]);
|
|
5397
|
+
/** Preserve inner whitespace exactly (like Prettier raw nodes). */
|
|
5398
|
+
const SENSITIVE = new Set(['pre', 'textarea', 'script', 'style']);
|
|
5399
|
+
const DIR_OPEN = new Set([
|
|
5400
|
+
'if',
|
|
5401
|
+
'for',
|
|
5402
|
+
'macro',
|
|
5403
|
+
'block',
|
|
5404
|
+
'call',
|
|
5405
|
+
'capture',
|
|
5406
|
+
'with',
|
|
5407
|
+
'stylesheet',
|
|
5408
|
+
]);
|
|
5409
|
+
const DIR_MID = new Set(['else', 'elif', 'elseif', 'elsif']);
|
|
5410
|
+
const DIR_CLOSE = new Set([
|
|
5411
|
+
'endif',
|
|
5412
|
+
'endfor',
|
|
5413
|
+
'endmacro',
|
|
5414
|
+
'endblock',
|
|
5415
|
+
'endcall',
|
|
5416
|
+
'endcapture',
|
|
5417
|
+
'endwith',
|
|
5418
|
+
'endstylesheet',
|
|
5419
|
+
]);
|
|
5420
|
+
/**
|
|
5421
|
+
* Reindent a template that already had its Janja tags normalised.
|
|
5422
|
+
* Only leading whitespace on each line changes; line breaks and
|
|
5423
|
+
* intra-line spacing stay put. Content inside pre/textarea/script/style
|
|
5424
|
+
* is left alone; the closing tag of those elements is still reindented.
|
|
5425
|
+
*/
|
|
5426
|
+
function indentHtml(source, options) {
|
|
5427
|
+
const tabWidth = options.tabWidth ?? 2;
|
|
5428
|
+
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5429
|
+
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5430
|
+
const lines = source.split(/\r?\n/);
|
|
5431
|
+
let depth = 0;
|
|
5432
|
+
let sensitive = null;
|
|
5433
|
+
const out = [];
|
|
5434
|
+
for (const line of lines) {
|
|
5435
|
+
const leading = line.match(/^[ \t]*/)?.[0].length ?? 0;
|
|
5436
|
+
const content = line.slice(leading);
|
|
5437
|
+
const { pieces, prefixClosers } = scanLine(content, options);
|
|
5438
|
+
const activeSensitive = sensitive;
|
|
5439
|
+
const closesSensitive = activeSensitive !== null &&
|
|
5440
|
+
pieces.some((piece) => piece.kind === 'close' && piece.sensitive === activeSensitive);
|
|
5441
|
+
if (sensitive && !closesSensitive) {
|
|
5442
|
+
out.push(line);
|
|
5443
|
+
applyDepth(pieces);
|
|
5444
|
+
continue;
|
|
5445
|
+
}
|
|
5446
|
+
const lineDepth = Math.max(0, depth - prefixClosers);
|
|
5447
|
+
out.push(content.length === 0 ? '' : pad.repeat(lineDepth) + content);
|
|
5448
|
+
applyDepth(pieces);
|
|
5449
|
+
}
|
|
5450
|
+
// split() keeps a final empty string when the source already ends in a
|
|
5451
|
+
// newline, so join() alone preserves that trailing newline — do not add
|
|
5452
|
+
// another one or each format grows an extra blank line.
|
|
5453
|
+
return out.join(eol);
|
|
5454
|
+
function applyDepth(pieces) {
|
|
5455
|
+
for (const piece of pieces) {
|
|
5456
|
+
if (piece.kind === 'open') {
|
|
5457
|
+
depth += 1;
|
|
5458
|
+
if (piece.sensitive && !sensitive) {
|
|
5459
|
+
({ sensitive } = piece);
|
|
5460
|
+
}
|
|
5461
|
+
}
|
|
5462
|
+
else if (piece.kind === 'close') {
|
|
5463
|
+
depth = Math.max(0, depth - 1);
|
|
5464
|
+
if (piece.sensitive && sensitive === piece.sensitive) {
|
|
5465
|
+
sensitive = null;
|
|
5466
|
+
}
|
|
5467
|
+
}
|
|
5468
|
+
else if (piece.kind === 'mid') ;
|
|
5469
|
+
}
|
|
5470
|
+
}
|
|
5471
|
+
}
|
|
5472
|
+
function scanLine(line, options) {
|
|
5473
|
+
const pieces = [];
|
|
5474
|
+
let prefixClosers = 0;
|
|
5475
|
+
let atPrefix = true;
|
|
5476
|
+
let i = 0;
|
|
5477
|
+
while (i < line.length) {
|
|
5478
|
+
if (/\s/.test(line[i])) {
|
|
5479
|
+
i += 1;
|
|
5480
|
+
continue;
|
|
5481
|
+
}
|
|
5482
|
+
const next = readAt(line, i, options);
|
|
5483
|
+
if (!next) {
|
|
5484
|
+
// Plain text ends the leading-closer prefix
|
|
5485
|
+
atPrefix = false;
|
|
5486
|
+
i += 1;
|
|
5487
|
+
continue;
|
|
5488
|
+
}
|
|
5489
|
+
if (next.piece) {
|
|
5490
|
+
pieces.push(next.piece);
|
|
5491
|
+
if (atPrefix &&
|
|
5492
|
+
(next.piece.kind === 'close' || next.piece.kind === 'mid')) {
|
|
5493
|
+
prefixClosers += 1;
|
|
5494
|
+
}
|
|
5495
|
+
else {
|
|
5496
|
+
atPrefix = false;
|
|
5497
|
+
}
|
|
5498
|
+
}
|
|
5499
|
+
else {
|
|
5500
|
+
// Comment / output / unknown directive sitting in the prefix
|
|
5501
|
+
atPrefix = false;
|
|
5502
|
+
}
|
|
5503
|
+
i = next.end;
|
|
5504
|
+
}
|
|
5505
|
+
return { pieces, prefixClosers };
|
|
5506
|
+
}
|
|
5507
|
+
function readAt(line, i, options) {
|
|
5508
|
+
const { commentOpen, directiveOpen, outputOpen } = options;
|
|
5509
|
+
if (line.startsWith(commentOpen, i)) {
|
|
5510
|
+
return {
|
|
5511
|
+
end: endOfDelimited(line, i + commentOpen.length, options.commentClose),
|
|
5512
|
+
};
|
|
5513
|
+
}
|
|
5514
|
+
if (line.startsWith(outputOpen, i)) {
|
|
5515
|
+
return {
|
|
5516
|
+
end: endOfDelimited(line, i + outputOpen.length, options.outputClose),
|
|
5517
|
+
};
|
|
5518
|
+
}
|
|
5519
|
+
if (line.startsWith(directiveOpen, i)) {
|
|
5520
|
+
return readDirective(line, i, options);
|
|
5521
|
+
}
|
|
5522
|
+
if (line.startsWith('<!--', i)) {
|
|
5523
|
+
const close = line.indexOf('-->', i + 4);
|
|
5524
|
+
return { end: close === -1 ? line.length : close + 3 };
|
|
5525
|
+
}
|
|
5526
|
+
if (line[i] === '<') {
|
|
5527
|
+
return readHtmlTag(line, i, options);
|
|
5528
|
+
}
|
|
5529
|
+
return null;
|
|
5530
|
+
}
|
|
5531
|
+
function readDirective(line, start, options) {
|
|
5532
|
+
const { directiveOpen, directiveClose } = options;
|
|
5533
|
+
const end = endOfDelimited(line, start + directiveOpen.length, directiveClose);
|
|
5534
|
+
const inner = line.slice(start + directiveOpen.length, end - directiveClose.length);
|
|
5535
|
+
const name = inner
|
|
5536
|
+
.replace(/^\s*-?\s*/, '')
|
|
5537
|
+
.match(/^([A-Za-z_]\w*)/)?.[1]
|
|
5538
|
+
?.toLowerCase();
|
|
5539
|
+
if (!name) {
|
|
5540
|
+
return { end };
|
|
5541
|
+
}
|
|
5542
|
+
if (DIR_CLOSE.has(name)) {
|
|
5543
|
+
return { end, piece: { kind: 'close' } };
|
|
5544
|
+
}
|
|
5545
|
+
if (DIR_MID.has(name)) {
|
|
5546
|
+
return { end, piece: { kind: 'mid' } };
|
|
5547
|
+
}
|
|
5548
|
+
if (DIR_OPEN.has(name)) {
|
|
5549
|
+
return { end, piece: { kind: 'open' } };
|
|
5550
|
+
}
|
|
5551
|
+
return { end };
|
|
5552
|
+
}
|
|
5553
|
+
function readHtmlTag(line, start, options) {
|
|
5554
|
+
if (line.startsWith('</', start)) {
|
|
5555
|
+
let i = start + 2;
|
|
5556
|
+
while (i < line.length && /\s/.test(line[i])) {
|
|
5557
|
+
i += 1;
|
|
5558
|
+
}
|
|
5559
|
+
const nameStart = i;
|
|
5560
|
+
while (i < line.length && /[A-Za-z0-9:-]/.test(line[i])) {
|
|
5561
|
+
i += 1;
|
|
5562
|
+
}
|
|
5563
|
+
const name = line.slice(nameStart, i).toLowerCase();
|
|
5564
|
+
const end = endOfHtmlTag(line, i, options);
|
|
5565
|
+
if (!name || VOID.has(name)) {
|
|
5566
|
+
return { end };
|
|
5567
|
+
}
|
|
5568
|
+
return {
|
|
5569
|
+
end,
|
|
5570
|
+
piece: {
|
|
5571
|
+
kind: 'close',
|
|
5572
|
+
sensitive: SENSITIVE.has(name) ? name : undefined,
|
|
5573
|
+
},
|
|
5574
|
+
};
|
|
5575
|
+
}
|
|
5576
|
+
if (line.startsWith('<!', start) || line.startsWith('<?', start)) {
|
|
5577
|
+
return { end: endOfHtmlTag(line, start + 2, options) };
|
|
5578
|
+
}
|
|
5579
|
+
let i = start + 1;
|
|
5580
|
+
while (i < line.length && /\s/.test(line[i])) {
|
|
5581
|
+
i += 1;
|
|
5582
|
+
}
|
|
5583
|
+
const nameStart = i;
|
|
5584
|
+
while (i < line.length && /[A-Za-z0-9:-]/.test(line[i])) {
|
|
5585
|
+
i += 1;
|
|
5586
|
+
}
|
|
5587
|
+
const name = line.slice(nameStart, i).toLowerCase();
|
|
5588
|
+
const end = endOfHtmlTag(line, i, options);
|
|
5589
|
+
const raw = line.slice(start, end);
|
|
5590
|
+
const selfClosing = /\/\s*>$/.test(raw) || VOID.has(name);
|
|
5591
|
+
if (!name || selfClosing) {
|
|
5592
|
+
return { end };
|
|
5593
|
+
}
|
|
5594
|
+
return {
|
|
5595
|
+
end,
|
|
5596
|
+
piece: {
|
|
5597
|
+
kind: 'open',
|
|
5598
|
+
sensitive: SENSITIVE.has(name) ? name : undefined,
|
|
5599
|
+
},
|
|
5600
|
+
};
|
|
5601
|
+
}
|
|
5602
|
+
/**
|
|
5603
|
+
* Find the `>` that ends an HTML tag, skipping quotes and Janja tags
|
|
5604
|
+
* (so `class="{{ if a > b }}x{{ endif }}"` does not end early).
|
|
5605
|
+
*/
|
|
5606
|
+
function endOfHtmlTag(line, from, options) {
|
|
5607
|
+
let i = from;
|
|
5608
|
+
let quote = null;
|
|
5609
|
+
while (i < line.length) {
|
|
5610
|
+
const c = line[i];
|
|
5611
|
+
if (quote) {
|
|
5612
|
+
if (c === quote) {
|
|
5613
|
+
quote = null;
|
|
5614
|
+
}
|
|
5615
|
+
else {
|
|
5616
|
+
const skipped = trySkipJanja(line, i, options);
|
|
5617
|
+
if (skipped !== null) {
|
|
5618
|
+
i = skipped;
|
|
5619
|
+
continue;
|
|
5620
|
+
}
|
|
5621
|
+
}
|
|
5622
|
+
i += 1;
|
|
5623
|
+
continue;
|
|
5624
|
+
}
|
|
5625
|
+
const skipped = trySkipJanja(line, i, options);
|
|
5626
|
+
if (skipped !== null) {
|
|
5627
|
+
i = skipped;
|
|
5628
|
+
continue;
|
|
5629
|
+
}
|
|
5630
|
+
if (c === '"' || c === "'") {
|
|
5631
|
+
quote = c;
|
|
5632
|
+
i += 1;
|
|
5633
|
+
continue;
|
|
5634
|
+
}
|
|
5635
|
+
if (c === '>') {
|
|
5636
|
+
return i + 1;
|
|
5637
|
+
}
|
|
5638
|
+
i += 1;
|
|
5639
|
+
}
|
|
5640
|
+
return line.length;
|
|
5641
|
+
}
|
|
5642
|
+
function trySkipJanja(line, i, options) {
|
|
5643
|
+
const { commentOpen, commentClose, outputOpen, outputClose, directiveOpen, directiveClose, } = options;
|
|
5644
|
+
if (line.startsWith(commentOpen, i)) {
|
|
5645
|
+
return endOfDelimited(line, i + commentOpen.length, commentClose);
|
|
5646
|
+
}
|
|
5647
|
+
if (line.startsWith(outputOpen, i)) {
|
|
5648
|
+
return endOfDelimited(line, i + outputOpen.length, outputClose);
|
|
5649
|
+
}
|
|
5650
|
+
if (line.startsWith(directiveOpen, i)) {
|
|
5651
|
+
return endOfDelimited(line, i + directiveOpen.length, directiveClose);
|
|
5652
|
+
}
|
|
5653
|
+
return null;
|
|
5654
|
+
}
|
|
5655
|
+
function endOfDelimited(line, from, close) {
|
|
5656
|
+
const idx = line.indexOf(close, from);
|
|
5657
|
+
return idx === -1 ? line.length : idx + close.length;
|
|
5658
|
+
}
|
|
5659
|
+
|
|
5233
5660
|
// An expression without the positions and spellings that formatting changes
|
|
5234
5661
|
const shape = (exp) => JSON.stringify(exp, (key, value) => key === 'loc' || key === 'raw' ? undefined : (value ?? null));
|
|
5235
5662
|
/**
|
|
5236
|
-
*
|
|
5663
|
+
* Format a template:
|
|
5664
|
+
*
|
|
5665
|
+
* 1. Normalise spacing inside tags:
|
|
5237
5666
|
*
|
|
5238
|
-
*
|
|
5239
|
-
*
|
|
5240
|
-
*
|
|
5667
|
+
* {{=name|upper}} -> {{= name | upper }}
|
|
5668
|
+
* {{if a eq 1}} -> {{ if a eq 1 }}
|
|
5669
|
+
* {{#note#}} -> {{# note #}}
|
|
5241
5670
|
*
|
|
5242
|
-
*
|
|
5243
|
-
*
|
|
5244
|
-
*
|
|
5671
|
+
* 2. Unless `html: false`, reindent HTML elements and block directives
|
|
5672
|
+
* (`if` / `for` / …). Only leading whitespace on each line changes;
|
|
5673
|
+
* attribute values (including embedded tags) and whitespace-sensitive
|
|
5674
|
+
* elements (`pre`, `script`, …) are left alone.
|
|
5675
|
+
*
|
|
5676
|
+
* A tag is left exactly as written whenever its expression does not parse or
|
|
5245
5677
|
* reformatting would not provably keep its meaning. Formatting twice gives
|
|
5246
5678
|
* the same result as formatting once.
|
|
5247
5679
|
*
|
|
@@ -5298,7 +5730,7 @@ function formatTemplate(template, options = {}) {
|
|
|
5298
5730
|
return (formatted !== null &&
|
|
5299
5731
|
`${merged.directiveOpen}${before} ${name}${formatted && ` ${formatted}`} ${after}${merged.directiveClose}`);
|
|
5300
5732
|
};
|
|
5301
|
-
|
|
5733
|
+
const tagged = tokens
|
|
5302
5734
|
.map((token) => {
|
|
5303
5735
|
const source = template.slice(offset(token.loc.start), offset(token.loc.end));
|
|
5304
5736
|
if (token.type === TokenType.TEXT) {
|
|
@@ -5311,6 +5743,18 @@ function formatTemplate(template, options = {}) {
|
|
|
5311
5743
|
return formatTag(token) || source;
|
|
5312
5744
|
})
|
|
5313
5745
|
.join('');
|
|
5746
|
+
if (options.html === false) {
|
|
5747
|
+
return tagged;
|
|
5748
|
+
}
|
|
5749
|
+
return indentHtml(tagged, {
|
|
5750
|
+
tabWidth: options.tabWidth,
|
|
5751
|
+
commentOpen: merged.commentOpen,
|
|
5752
|
+
commentClose: merged.commentClose,
|
|
5753
|
+
directiveOpen: merged.directiveOpen,
|
|
5754
|
+
directiveClose: merged.directiveClose,
|
|
5755
|
+
outputOpen: merged.outputOpen,
|
|
5756
|
+
outputClose: merged.outputClose,
|
|
5757
|
+
});
|
|
5314
5758
|
}
|
|
5315
5759
|
|
|
5316
5760
|
/**
|