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