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