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