janja 4.5.0 → 4.7.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 +207 -193
- package/dist/index.d.ts +5 -1
- package/dist/index.esm.js +207 -193
- package/dist/index.js +207 -193
- package/dist/index.min.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1812,6 +1812,15 @@ class Parser extends Tokenizer {
|
|
|
1812
1812
|
}
|
|
1813
1813
|
}
|
|
1814
1814
|
|
|
1815
|
+
/**
|
|
1816
|
+
* Add the page-wide `include … last` registry and resolver to the root script.
|
|
1817
|
+
* The runtime walks rendered markers in reverse document order, so nested
|
|
1818
|
+
* partials and repeated includes inside loops are handled in render order.
|
|
1819
|
+
*/
|
|
1820
|
+
function pushIncludeLastRuntime(compiler) {
|
|
1821
|
+
compiler.pushRaw(null, 'const il_=new Map();', 'let il_i_=0;', 'const il_n_=Math.random().toString(36).slice(2);', 'const il_re_=/\\u0000JLAST[a-z0-9]+:\\d+\\u0000/g;', 'const il_resolve_=async(v_,seen_=new Set())=>{', 'const matches_=[...v_.matchAll(il_re_)];', 'let cursor_=v_.length;', 'const parts_=[];', 'for(let i_=matches_.length-1;i_>=0;i_--){', 'const match_=matches_[i_],marker_=match_[0],start_=match_.index,item_=il_.get(marker_);', 'parts_.push(v_.slice(start_+marker_.length,cursor_));', 'if(item_&&!seen_.has(item_.path)){', 'const output_=await il_resolve_(await item_.render(),seen_);', 'if(!seen_.has(item_.path)){seen_.add(item_.path);parts_.push(output_);}', '}', 'cursor_=start_;', '}', 'parts_.push(v_.slice(0,cursor_));', 'return parts_.reverse().join("");', '};');
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1815
1824
|
function isStringLit(exp) {
|
|
1816
1825
|
return exp.type === 'LIT' && typeof exp.value === 'string';
|
|
1817
1826
|
}
|
|
@@ -1864,7 +1873,7 @@ const PRELOAD_LOC = {
|
|
|
1864
1873
|
// Directives whose body is still the template's own top-level output
|
|
1865
1874
|
const transparent = new Set(['IF', 'FOR']);
|
|
1866
1875
|
class Compiler extends Context {
|
|
1867
|
-
constructor(options, cache, revalidate = true, stream = false) {
|
|
1876
|
+
constructor(options, cache, revalidate = true, stream = false, includeLastState = { active: false }) {
|
|
1868
1877
|
super();
|
|
1869
1878
|
Object.defineProperty(this, "options", {
|
|
1870
1879
|
enumerable: true,
|
|
@@ -1884,6 +1893,12 @@ class Compiler extends Context {
|
|
|
1884
1893
|
writable: true,
|
|
1885
1894
|
value: stream
|
|
1886
1895
|
});
|
|
1896
|
+
Object.defineProperty(this, "includeLastState", {
|
|
1897
|
+
enumerable: true,
|
|
1898
|
+
configurable: true,
|
|
1899
|
+
writable: true,
|
|
1900
|
+
value: includeLastState
|
|
1901
|
+
});
|
|
1887
1902
|
Object.defineProperty(this, "template", {
|
|
1888
1903
|
enumerable: true,
|
|
1889
1904
|
configurable: true,
|
|
@@ -2005,12 +2020,15 @@ class Compiler extends Context {
|
|
|
2005
2020
|
}
|
|
2006
2021
|
await this.parseTemplates();
|
|
2007
2022
|
if (this.isRoot) {
|
|
2023
|
+
if (this.includeLastState.active) {
|
|
2024
|
+
pushIncludeLastRuntime(this);
|
|
2025
|
+
}
|
|
2008
2026
|
if (this.partials.size || this.hasDynamicInclude) {
|
|
2009
2027
|
const { maxIncludeDepth } = resolveSecurityLimits(this.options.security);
|
|
2010
2028
|
// Alias the render data before partials shadow `c` with their argument.
|
|
2011
2029
|
// `inc_` tracks `include … once` occurrences across the whole render,
|
|
2012
2030
|
// root template and partials alike (they share this closure).
|
|
2013
|
-
this.pushRaw(null, `const ${TOP_CONTEXT}=${CONTEXT};`, 'let d_=0;', 'const inc_=
|
|
2031
|
+
this.pushRaw(null, `const ${TOP_CONTEXT}=${CONTEXT};`, 'let d_=0;', 'const inc_=new Set();', 'const p={');
|
|
2014
2032
|
for (const [path, code] of this.partials) {
|
|
2015
2033
|
this.pushRaw(null,
|
|
2016
2034
|
// Partials take the context they should render with: plain include
|
|
@@ -2045,9 +2063,15 @@ class Compiler extends Context {
|
|
|
2045
2063
|
}
|
|
2046
2064
|
this.pushRaw(null, '];');
|
|
2047
2065
|
}
|
|
2048
|
-
|
|
2066
|
+
// Last includes are resolved after the complete page has rendered, so
|
|
2067
|
+
// streaming must buffer the page to avoid flushing unresolved markers.
|
|
2068
|
+
this.flushable =
|
|
2069
|
+
this.isRoot && this.stream && !this.includeLastState.active;
|
|
2049
2070
|
await this.compileNode(this.rootNode);
|
|
2050
2071
|
this.flushable = false;
|
|
2072
|
+
if (this.isRoot && this.includeLastState.active) {
|
|
2073
|
+
this.pushRaw(null, 's=await il_resolve_(s);');
|
|
2074
|
+
}
|
|
2051
2075
|
this.end();
|
|
2052
2076
|
// Store in cache
|
|
2053
2077
|
if (this.cache && root) {
|
|
@@ -2151,7 +2175,6 @@ class Compiler extends Context {
|
|
|
2151
2175
|
const partials = new Map();
|
|
2152
2176
|
const blocks = [];
|
|
2153
2177
|
const stylesheets = [];
|
|
2154
|
-
const includeLast = new Map();
|
|
2155
2178
|
const traverse = (node) => {
|
|
2156
2179
|
if (node.type === 'INCLUDE') {
|
|
2157
2180
|
const include = node;
|
|
@@ -2160,13 +2183,7 @@ class Compiler extends Context {
|
|
|
2160
2183
|
partials.set(include.val.value, include);
|
|
2161
2184
|
}
|
|
2162
2185
|
if (include.modifier === 'last') {
|
|
2163
|
-
|
|
2164
|
-
if (existing) {
|
|
2165
|
-
existing.total += 1;
|
|
2166
|
-
}
|
|
2167
|
-
else {
|
|
2168
|
-
includeLast.set(include.val.value, { total: 1, emitted: 0 });
|
|
2169
|
-
}
|
|
2186
|
+
this.includeLastState.active = true;
|
|
2170
2187
|
}
|
|
2171
2188
|
}
|
|
2172
2189
|
else {
|
|
@@ -2182,7 +2199,6 @@ class Compiler extends Context {
|
|
|
2182
2199
|
node.traverse(traverse);
|
|
2183
2200
|
};
|
|
2184
2201
|
traverse(this.rootNode);
|
|
2185
|
-
this.state.includeLast = includeLast;
|
|
2186
2202
|
return {
|
|
2187
2203
|
partials,
|
|
2188
2204
|
blocks,
|
|
@@ -2236,7 +2252,7 @@ class Compiler extends Context {
|
|
|
2236
2252
|
}
|
|
2237
2253
|
async compilePartial(path, node, template) {
|
|
2238
2254
|
try {
|
|
2239
|
-
const { code } = await new Compiler(this.options).compile(template, false, this.partials, this.blocks, this.sources, this.stylesheets, path);
|
|
2255
|
+
const { code } = await new Compiler(this.options, undefined, true, false, this.includeLastState).compile(template, false, this.partials, this.blocks, this.sources, this.stylesheets, path);
|
|
2240
2256
|
this.partials.set(path, code);
|
|
2241
2257
|
}
|
|
2242
2258
|
catch {
|
|
@@ -3652,32 +3668,23 @@ async function compileInclude({ val, loc, withExp, modifier }, compiler) {
|
|
|
3652
3668
|
const path = isStringLit(val)
|
|
3653
3669
|
? JSON.stringify(val.value)
|
|
3654
3670
|
: expCompiler.compile(val, compiler.context);
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
|
|
3671
|
+
const context = withExp
|
|
3672
|
+
? `{...${compiler.context},...${expCompiler.compile(withExp, compiler.context)}}`
|
|
3673
|
+
: TOP_CONTEXT;
|
|
3674
|
+
if (modifier === 'last' && isStringLit(val)) {
|
|
3675
|
+
// Mark every runtime occurrence. The root script resolves these markers
|
|
3676
|
+
// after rendering and keeps only the last visible occurrence per path.
|
|
3677
|
+
compiler.pushRaw(loc, '{', 'const ilm_="\\u0000JLAST"+il_n_+":"+(il_i_++)+"\\u0000";', `const ilc_=${context};`, `il_.set(ilm_,{path:${JSON.stringify(val.value)},render:async()=>{return (await p[${path}]?.(ilc_))??"";}});`, 's+=ilm_;', '}');
|
|
3678
|
+
return;
|
|
3679
|
+
}
|
|
3680
|
+
const emit = `s+=await p[${path}]?.(${context})??"";`;
|
|
3660
3681
|
// `once`: render the first occurrence only. The registry lives in the root
|
|
3661
|
-
// script's closure, so occurrences spread across different files (
|
|
3682
|
+
// script's closure, so occurrences spread across different files (the root
|
|
3662
3683
|
// template and its partials) still deduplicate at runtime.
|
|
3663
3684
|
if (modifier === 'once') {
|
|
3664
|
-
compiler.pushRaw(loc, `if(!inc_
|
|
3685
|
+
compiler.pushRaw(loc, `{const inc_p_=${path};if(!inc_.has(inc_p_)){inc_.add(inc_p_);s+=await p[inc_p_]?.(${context})??"";}}`);
|
|
3665
3686
|
return;
|
|
3666
3687
|
}
|
|
3667
|
-
// `last`: emit only at the final occurrence within the template file,
|
|
3668
|
-
// determined at compile time from the counts collected alongside partials.
|
|
3669
|
-
// Static string paths only — a dynamic path has no stable identity to count.
|
|
3670
|
-
if (modifier === 'last' && isStringLit(val)) {
|
|
3671
|
-
const counts = compiler.state.includeLast;
|
|
3672
|
-
const entry = counts?.get(val.value);
|
|
3673
|
-
if (entry) {
|
|
3674
|
-
entry.emitted += 1;
|
|
3675
|
-
if (entry.emitted < entry.total) {
|
|
3676
|
-
// Not the last occurrence — emit nothing.
|
|
3677
|
-
return;
|
|
3678
|
-
}
|
|
3679
|
-
}
|
|
3680
|
-
}
|
|
3681
3688
|
compiler.pushRaw(loc, emit);
|
|
3682
3689
|
}
|
|
3683
3690
|
const compilers$8 = {
|
|
@@ -5587,84 +5594,6 @@ function countCssBraces(line) {
|
|
|
5587
5594
|
return { opens, closes };
|
|
5588
5595
|
}
|
|
5589
5596
|
|
|
5590
|
-
/**
|
|
5591
|
-
* Brace-depth reindent for `<script>` bodies. Only leading whitespace changes;
|
|
5592
|
-
* braces inside strings, comments, template literals, and Janja tags are ignored.
|
|
5593
|
-
*/
|
|
5594
|
-
function indentJs(source, tabWidth) {
|
|
5595
|
-
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5596
|
-
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5597
|
-
const lines = source.split(/\r?\n/);
|
|
5598
|
-
let depth = 0;
|
|
5599
|
-
const out = [];
|
|
5600
|
-
for (const line of lines) {
|
|
5601
|
-
const content = line.trim();
|
|
5602
|
-
if (content.length === 0) {
|
|
5603
|
-
out.push('');
|
|
5604
|
-
continue;
|
|
5605
|
-
}
|
|
5606
|
-
const { opens, closes } = countJsBraces(content);
|
|
5607
|
-
const lineDepth = closes > opens ? Math.max(0, depth - (closes - opens)) : depth;
|
|
5608
|
-
out.push(pad.repeat(lineDepth) + content);
|
|
5609
|
-
depth = Math.max(0, depth + opens - closes);
|
|
5610
|
-
}
|
|
5611
|
-
return out.join(eol);
|
|
5612
|
-
}
|
|
5613
|
-
function countJsBraces(line) {
|
|
5614
|
-
let opens = 0;
|
|
5615
|
-
let closes = 0;
|
|
5616
|
-
let quote = null;
|
|
5617
|
-
let i = 0;
|
|
5618
|
-
while (i < line.length) {
|
|
5619
|
-
const c = line[i];
|
|
5620
|
-
if (quote) {
|
|
5621
|
-
if (c === '\\') {
|
|
5622
|
-
i += 2;
|
|
5623
|
-
continue;
|
|
5624
|
-
}
|
|
5625
|
-
if (c === quote) {
|
|
5626
|
-
quote = null;
|
|
5627
|
-
}
|
|
5628
|
-
i += 1;
|
|
5629
|
-
continue;
|
|
5630
|
-
}
|
|
5631
|
-
if (c === '{' && line[i + 1] === '{') {
|
|
5632
|
-
i = endOfJanja(line, i);
|
|
5633
|
-
continue;
|
|
5634
|
-
}
|
|
5635
|
-
if (c === '"' || c === "'" || c === '`') {
|
|
5636
|
-
quote = c;
|
|
5637
|
-
i += 1;
|
|
5638
|
-
continue;
|
|
5639
|
-
}
|
|
5640
|
-
if (c === '/' && line[i + 1] === '*') {
|
|
5641
|
-
const end = line.indexOf('*/', i + 2);
|
|
5642
|
-
i = end === -1 ? line.length : end + 2;
|
|
5643
|
-
continue;
|
|
5644
|
-
}
|
|
5645
|
-
if (c === '/' && line[i + 1] === '/') {
|
|
5646
|
-
break;
|
|
5647
|
-
}
|
|
5648
|
-
if (c === '{') {
|
|
5649
|
-
opens += 1;
|
|
5650
|
-
}
|
|
5651
|
-
else if (c === '}') {
|
|
5652
|
-
closes += 1;
|
|
5653
|
-
}
|
|
5654
|
-
i += 1;
|
|
5655
|
-
}
|
|
5656
|
-
return { opens, closes };
|
|
5657
|
-
}
|
|
5658
|
-
/** Skip a Janja tag starting at `i` (`{{`, `{{=`, `{{#` …). */
|
|
5659
|
-
function endOfJanja(line, i) {
|
|
5660
|
-
if (line.startsWith('{{#', i)) {
|
|
5661
|
-
const end = line.indexOf('#}}', i + 3);
|
|
5662
|
-
return end === -1 ? line.length : end + 3;
|
|
5663
|
-
}
|
|
5664
|
-
const end = line.indexOf('}}', i + 2);
|
|
5665
|
-
return end === -1 ? line.length : end + 2;
|
|
5666
|
-
}
|
|
5667
|
-
|
|
5668
5597
|
/** HTML void elements — never take children, so they do not change indent. */
|
|
5669
5598
|
const VOID = new Set([
|
|
5670
5599
|
'area',
|
|
@@ -5687,8 +5616,6 @@ const VOID = new Set([
|
|
|
5687
5616
|
* whitespace; `script` / `style` are reindented (see body buffer below).
|
|
5688
5617
|
*/
|
|
5689
5618
|
const SENSITIVE = new Set(['pre', 'textarea', 'script', 'style']);
|
|
5690
|
-
/** HTML elements whose bodies are reindented (not left raw). */
|
|
5691
|
-
const REINDENT_HTML = new Set(['script', 'style']);
|
|
5692
5619
|
const DIR_OPEN = new Set([
|
|
5693
5620
|
'if',
|
|
5694
5621
|
'for',
|
|
@@ -5699,7 +5626,6 @@ const DIR_OPEN = new Set([
|
|
|
5699
5626
|
'with',
|
|
5700
5627
|
'stylesheet',
|
|
5701
5628
|
]);
|
|
5702
|
-
/** Directives whose bodies keep leading whitespace (like `<style>`). */
|
|
5703
5629
|
const DIR_SENSITIVE = new Set(['stylesheet']);
|
|
5704
5630
|
const DIR_MID = new Set(['else', 'elif', 'elseif', 'elsif']);
|
|
5705
5631
|
const DIR_CLOSE = new Set([
|
|
@@ -5712,84 +5638,7 @@ const DIR_CLOSE = new Set([
|
|
|
5712
5638
|
'endwith',
|
|
5713
5639
|
'endstylesheet',
|
|
5714
5640
|
]);
|
|
5715
|
-
/**
|
|
5716
|
-
* Reindent a template that already had its Janja tags normalised.
|
|
5717
|
-
* Only leading whitespace on each line changes; line breaks and
|
|
5718
|
-
* intra-line spacing stay put. Content inside pre/textarea is left alone;
|
|
5719
|
-
* `script` / `style` bodies are brace-reindented; `stylesheet` bodies are
|
|
5720
|
-
* formatted as an HTML fragment (so nested `<style>` gets CSS indent).
|
|
5721
|
-
*/
|
|
5722
|
-
function indentHtml(source, options) {
|
|
5723
|
-
const tabWidth = options.tabWidth ?? 2;
|
|
5724
|
-
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5725
|
-
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5726
|
-
const lines = source.split(/\r?\n/);
|
|
5727
|
-
let depth = 0;
|
|
5728
|
-
let sensitive = null;
|
|
5729
|
-
/** Raw lines inside a reindented body (`stylesheet` / `script` / `style`). */
|
|
5730
|
-
let bodyBuf = null;
|
|
5731
|
-
const out = [];
|
|
5732
|
-
/** Open `{{#` comment: inner lines are never scanned for pieces. */
|
|
5733
|
-
const state = { inComment: false };
|
|
5734
|
-
for (const line of lines) {
|
|
5735
|
-
const leading = line.match(/^[ \t]*/)?.[0].length ?? 0;
|
|
5736
|
-
const content = line.slice(leading);
|
|
5737
|
-
const { pieces, prefixClosers } = scanLine(content, options, state);
|
|
5738
|
-
const activeSensitive = sensitive;
|
|
5739
|
-
const closesSensitive = activeSensitive !== null &&
|
|
5740
|
-
pieces.some((piece) => piece.kind === 'close' && piece.sensitive === activeSensitive);
|
|
5741
|
-
const reindentBody = activeSensitive === 'stylesheet' ||
|
|
5742
|
-
(activeSensitive !== null && REINDENT_HTML.has(activeSensitive));
|
|
5743
|
-
if (reindentBody && !closesSensitive) {
|
|
5744
|
-
// Do not applyDepth: bodies may contain nested Janja if/for tags.
|
|
5745
|
-
(bodyBuf ?? (bodyBuf = [])).push(line);
|
|
5746
|
-
continue;
|
|
5747
|
-
}
|
|
5748
|
-
if (bodyBuf && closesSensitive && reindentBody) {
|
|
5749
|
-
// stylesheet: same column as the tags; script/style: one level inside.
|
|
5750
|
-
const baseDepth = activeSensitive === 'stylesheet' ? Math.max(0, depth - 1) : depth;
|
|
5751
|
-
const raw = bodyBuf.join(eol);
|
|
5752
|
-
const formatted = activeSensitive === 'stylesheet'
|
|
5753
|
-
? indentHtml(raw, options)
|
|
5754
|
-
: activeSensitive === 'style'
|
|
5755
|
-
? indentCss(raw, tabWidth)
|
|
5756
|
-
: indentJs(raw, tabWidth);
|
|
5757
|
-
for (const bodyLine of formatted.split(/\r?\n/)) {
|
|
5758
|
-
out.push(bodyLine.length === 0 ? '' : pad.repeat(baseDepth) + bodyLine);
|
|
5759
|
-
}
|
|
5760
|
-
bodyBuf = null;
|
|
5761
|
-
}
|
|
5762
|
-
else if (sensitive && !closesSensitive) {
|
|
5763
|
-
out.push(line);
|
|
5764
|
-
applyDepth(pieces);
|
|
5765
|
-
continue;
|
|
5766
|
-
}
|
|
5767
|
-
const lineDepth = Math.max(0, depth - prefixClosers);
|
|
5768
|
-
out.push(content.length === 0 ? '' : pad.repeat(lineDepth) + content);
|
|
5769
|
-
applyDepth(pieces);
|
|
5770
|
-
}
|
|
5771
|
-
// split() keeps a final empty string when the source already ends in a
|
|
5772
|
-
// newline, so join() alone preserves that trailing newline — do not add
|
|
5773
|
-
// another one or each format grows an extra blank line.
|
|
5774
|
-
return out.join(eol);
|
|
5775
|
-
function applyDepth(pieces) {
|
|
5776
|
-
for (const piece of pieces) {
|
|
5777
|
-
if (piece.kind === 'open') {
|
|
5778
|
-
depth += 1;
|
|
5779
|
-
if (piece.sensitive && !sensitive) {
|
|
5780
|
-
({ sensitive } = piece);
|
|
5781
|
-
}
|
|
5782
|
-
}
|
|
5783
|
-
else if (piece.kind === 'close') {
|
|
5784
|
-
depth = Math.max(0, depth - 1);
|
|
5785
|
-
if (piece.sensitive && sensitive === piece.sensitive) {
|
|
5786
|
-
sensitive = null;
|
|
5787
|
-
}
|
|
5788
|
-
}
|
|
5789
|
-
else if (piece.kind === 'mid') ;
|
|
5790
|
-
}
|
|
5791
|
-
}
|
|
5792
|
-
}
|
|
5641
|
+
/** Scan one line for HTML and Janja constructs that affect indentation. */
|
|
5793
5642
|
function scanLine(line, options, state) {
|
|
5794
5643
|
const pieces = [];
|
|
5795
5644
|
let prefixClosers = 0;
|
|
@@ -6005,6 +5854,171 @@ function endOfDelimited(line, from, close) {
|
|
|
6005
5854
|
return idx === -1 ? line.length : idx + close.length;
|
|
6006
5855
|
}
|
|
6007
5856
|
|
|
5857
|
+
/**
|
|
5858
|
+
* Brace-depth reindent for `<script>` bodies. Only leading whitespace changes;
|
|
5859
|
+
* braces inside strings, comments, template literals, and Janja tags are ignored.
|
|
5860
|
+
*/
|
|
5861
|
+
function indentJs(source, tabWidth) {
|
|
5862
|
+
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5863
|
+
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5864
|
+
const lines = source.split(/\r?\n/);
|
|
5865
|
+
let depth = 0;
|
|
5866
|
+
const out = [];
|
|
5867
|
+
for (const line of lines) {
|
|
5868
|
+
const content = line.trim();
|
|
5869
|
+
if (content.length === 0) {
|
|
5870
|
+
out.push('');
|
|
5871
|
+
continue;
|
|
5872
|
+
}
|
|
5873
|
+
const { opens, closes } = countJsBraces(content);
|
|
5874
|
+
const lineDepth = closes > opens ? Math.max(0, depth - (closes - opens)) : depth;
|
|
5875
|
+
out.push(pad.repeat(lineDepth) + content);
|
|
5876
|
+
depth = Math.max(0, depth + opens - closes);
|
|
5877
|
+
}
|
|
5878
|
+
return out.join(eol);
|
|
5879
|
+
}
|
|
5880
|
+
function countJsBraces(line) {
|
|
5881
|
+
let opens = 0;
|
|
5882
|
+
let closes = 0;
|
|
5883
|
+
let quote = null;
|
|
5884
|
+
let i = 0;
|
|
5885
|
+
while (i < line.length) {
|
|
5886
|
+
const c = line[i];
|
|
5887
|
+
if (quote) {
|
|
5888
|
+
if (c === '\\') {
|
|
5889
|
+
i += 2;
|
|
5890
|
+
continue;
|
|
5891
|
+
}
|
|
5892
|
+
if (c === quote) {
|
|
5893
|
+
quote = null;
|
|
5894
|
+
}
|
|
5895
|
+
i += 1;
|
|
5896
|
+
continue;
|
|
5897
|
+
}
|
|
5898
|
+
if (c === '{' && line[i + 1] === '{') {
|
|
5899
|
+
i = endOfJanja(line, i);
|
|
5900
|
+
continue;
|
|
5901
|
+
}
|
|
5902
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
5903
|
+
quote = c;
|
|
5904
|
+
i += 1;
|
|
5905
|
+
continue;
|
|
5906
|
+
}
|
|
5907
|
+
if (c === '/' && line[i + 1] === '*') {
|
|
5908
|
+
const end = line.indexOf('*/', i + 2);
|
|
5909
|
+
i = end === -1 ? line.length : end + 2;
|
|
5910
|
+
continue;
|
|
5911
|
+
}
|
|
5912
|
+
if (c === '/' && line[i + 1] === '/') {
|
|
5913
|
+
break;
|
|
5914
|
+
}
|
|
5915
|
+
if (c === '{') {
|
|
5916
|
+
opens += 1;
|
|
5917
|
+
}
|
|
5918
|
+
else if (c === '}') {
|
|
5919
|
+
closes += 1;
|
|
5920
|
+
}
|
|
5921
|
+
i += 1;
|
|
5922
|
+
}
|
|
5923
|
+
return { opens, closes };
|
|
5924
|
+
}
|
|
5925
|
+
/** Skip a Janja tag starting at `i` (`{{`, `{{=`, `{{#` …). */
|
|
5926
|
+
function endOfJanja(line, i) {
|
|
5927
|
+
if (line.startsWith('{{#', i)) {
|
|
5928
|
+
const end = line.indexOf('#}}', i + 3);
|
|
5929
|
+
return end === -1 ? line.length : end + 3;
|
|
5930
|
+
}
|
|
5931
|
+
const end = line.indexOf('}}', i + 2);
|
|
5932
|
+
return end === -1 ? line.length : end + 2;
|
|
5933
|
+
}
|
|
5934
|
+
|
|
5935
|
+
/** HTML elements whose bodies are reindented (not left raw). */
|
|
5936
|
+
const REINDENT_HTML = new Set(['script', 'style']);
|
|
5937
|
+
/**
|
|
5938
|
+
* Reindent a template that already had its Janja tags normalised.
|
|
5939
|
+
* Only leading whitespace on each line changes; line breaks and
|
|
5940
|
+
* intra-line spacing stay put. Content inside pre/textarea is left alone;
|
|
5941
|
+
* `script` / `style` bodies are brace-reindented; `stylesheet` bodies are
|
|
5942
|
+
* formatted as an HTML fragment (so nested `<style>` gets CSS indent).
|
|
5943
|
+
*/
|
|
5944
|
+
function indentHtml(source, options) {
|
|
5945
|
+
const tabWidth = options.tabWidth ?? 2;
|
|
5946
|
+
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5947
|
+
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5948
|
+
const lines = source.split(/\r?\n/);
|
|
5949
|
+
let depth = 0;
|
|
5950
|
+
let sensitive = null;
|
|
5951
|
+
/** Raw lines inside a reindented body (`stylesheet` / `script` / `style`). */
|
|
5952
|
+
let bodyBuf = null;
|
|
5953
|
+
const out = [];
|
|
5954
|
+
/** Open `{{#` comment: inner lines are never scanned for pieces. */
|
|
5955
|
+
const state = { inComment: false };
|
|
5956
|
+
for (const line of lines) {
|
|
5957
|
+
const leading = line.match(/^[ \t]*/)?.[0].length ?? 0;
|
|
5958
|
+
const content = line.slice(leading);
|
|
5959
|
+
const { pieces, prefixClosers } = scanLine(content, options, state);
|
|
5960
|
+
const activeSensitive = sensitive;
|
|
5961
|
+
const closesSensitive = activeSensitive !== null &&
|
|
5962
|
+
pieces.some((piece) => piece.kind === 'close' && piece.sensitive === activeSensitive);
|
|
5963
|
+
const reindentBody = activeSensitive === 'stylesheet' ||
|
|
5964
|
+
(activeSensitive !== null && REINDENT_HTML.has(activeSensitive));
|
|
5965
|
+
if (reindentBody && !closesSensitive) {
|
|
5966
|
+
// Do not applyDepth: bodies may contain nested Janja if/for tags.
|
|
5967
|
+
(bodyBuf ?? (bodyBuf = [])).push(line);
|
|
5968
|
+
continue;
|
|
5969
|
+
}
|
|
5970
|
+
if (bodyBuf && closesSensitive && reindentBody) {
|
|
5971
|
+
// stylesheet: same column as the tags; script/style: one level inside.
|
|
5972
|
+
const baseDepth = activeSensitive === 'stylesheet' ? Math.max(0, depth - 1) : depth;
|
|
5973
|
+
const raw = bodyBuf.join(eol);
|
|
5974
|
+
const formatted = activeSensitive === 'stylesheet'
|
|
5975
|
+
? indentHtml(raw, options)
|
|
5976
|
+
: activeSensitive === 'style'
|
|
5977
|
+
? indentCss(raw, tabWidth)
|
|
5978
|
+
: indentJs(raw, tabWidth);
|
|
5979
|
+
for (const bodyLine of formatted.split(/\r?\n/)) {
|
|
5980
|
+
out.push(bodyLine.length === 0 ? '' : pad.repeat(baseDepth) + bodyLine);
|
|
5981
|
+
}
|
|
5982
|
+
bodyBuf = null;
|
|
5983
|
+
}
|
|
5984
|
+
else if (sensitive && !closesSensitive) {
|
|
5985
|
+
out.push(line);
|
|
5986
|
+
applyDepth(pieces);
|
|
5987
|
+
continue;
|
|
5988
|
+
}
|
|
5989
|
+
const lineDepth = Math.max(0, depth - prefixClosers);
|
|
5990
|
+
const continuesSelfClosingTag = /^\/\s*>\s*$/.test(content);
|
|
5991
|
+
out.push(content.length === 0 ? '' : pad.repeat(lineDepth) + content);
|
|
5992
|
+
applyDepth(pieces);
|
|
5993
|
+
// The opening `<tag` was counted on an earlier line. A standalone `/>`
|
|
5994
|
+
// closes that element after this continuation line has been indented.
|
|
5995
|
+
if (continuesSelfClosingTag) {
|
|
5996
|
+
depth = Math.max(0, depth - 1);
|
|
5997
|
+
}
|
|
5998
|
+
}
|
|
5999
|
+
// split() keeps a final empty string when the source already ends in a
|
|
6000
|
+
// newline, so join() alone preserves that trailing newline — do not add
|
|
6001
|
+
// another one or each format grows an extra blank line.
|
|
6002
|
+
return out.join(eol);
|
|
6003
|
+
function applyDepth(pieces) {
|
|
6004
|
+
for (const piece of pieces) {
|
|
6005
|
+
if (piece.kind === 'open') {
|
|
6006
|
+
depth += 1;
|
|
6007
|
+
if (piece.sensitive && !sensitive) {
|
|
6008
|
+
({ sensitive } = piece);
|
|
6009
|
+
}
|
|
6010
|
+
}
|
|
6011
|
+
else if (piece.kind === 'close') {
|
|
6012
|
+
depth = Math.max(0, depth - 1);
|
|
6013
|
+
if (piece.sensitive && sensitive === piece.sensitive) {
|
|
6014
|
+
sensitive = null;
|
|
6015
|
+
}
|
|
6016
|
+
}
|
|
6017
|
+
else if (piece.kind === 'mid') ;
|
|
6018
|
+
}
|
|
6019
|
+
}
|
|
6020
|
+
}
|
|
6021
|
+
|
|
6008
6022
|
// An expression without the positions and spellings that formatting changes
|
|
6009
6023
|
const shape = (exp) => JSON.stringify(exp, (key, value) => key === 'loc' || key === 'raw' ? undefined : (value ?? null));
|
|
6010
6024
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -188,10 +188,14 @@ declare class StylesheetNode extends Traversal {
|
|
|
188
188
|
constructor(body: SyntaxNode[], loc: Loc, strip: Strip);
|
|
189
189
|
}
|
|
190
190
|
|
|
191
|
+
type IncludeLastState = {
|
|
192
|
+
active: boolean;
|
|
193
|
+
};
|
|
191
194
|
declare class Compiler extends Context {
|
|
192
195
|
options: Required<CompilerOptions>;
|
|
193
196
|
private revalidate;
|
|
194
197
|
private stream;
|
|
198
|
+
private includeLastState;
|
|
195
199
|
template: string;
|
|
196
200
|
state: Record<string, any>;
|
|
197
201
|
private isRoot;
|
|
@@ -212,7 +216,7 @@ declare class Compiler extends Context {
|
|
|
212
216
|
* only in stream mode, for the template's own top-level output.
|
|
213
217
|
*/
|
|
214
218
|
flushable: boolean;
|
|
215
|
-
constructor(options: Required<CompilerOptions>, cache?: LRUCache<CompiledTemplate>, revalidate?: boolean, stream?: boolean);
|
|
219
|
+
constructor(options: Required<CompilerOptions>, cache?: LRUCache<CompiledTemplate>, revalidate?: boolean, stream?: boolean, includeLastState?: IncludeLastState);
|
|
216
220
|
compile(template: string, root?: boolean, partials?: Map<string, string>, blocks?: Map<string, BlockNode[]>, sources?: Map<string, string>, stylesheets?: Map<string, StylesheetNode[]>, stylesheetKey?: string): Promise<this>;
|
|
217
221
|
get script(): Script;
|
|
218
222
|
private generateCacheKey;
|