janja 4.4.0 → 4.6.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 +249 -175
- package/dist/index.esm.js +249 -175
- package/dist/index.js +249 -175
- package/dist/index.min.js +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1817,7 +1817,7 @@ ${suggestionText}`;
|
|
|
1817
1817
|
return exp.type === 'LIT' && typeof exp.value === 'string';
|
|
1818
1818
|
}
|
|
1819
1819
|
class IncludeNode extends Traversal {
|
|
1820
|
-
constructor(val, loc, strip, withExp = null) {
|
|
1820
|
+
constructor(val, loc, strip, withExp = null, modifier = null) {
|
|
1821
1821
|
super();
|
|
1822
1822
|
Object.defineProperty(this, "val", {
|
|
1823
1823
|
enumerable: true,
|
|
@@ -1843,6 +1843,12 @@ ${suggestionText}`;
|
|
|
1843
1843
|
writable: true,
|
|
1844
1844
|
value: withExp
|
|
1845
1845
|
});
|
|
1846
|
+
Object.defineProperty(this, "modifier", {
|
|
1847
|
+
enumerable: true,
|
|
1848
|
+
configurable: true,
|
|
1849
|
+
writable: true,
|
|
1850
|
+
value: modifier
|
|
1851
|
+
});
|
|
1846
1852
|
Object.defineProperty(this, "type", {
|
|
1847
1853
|
enumerable: true,
|
|
1848
1854
|
configurable: true,
|
|
@@ -2003,7 +2009,9 @@ ${suggestionText}`;
|
|
|
2003
2009
|
if (this.partials.size || this.hasDynamicInclude) {
|
|
2004
2010
|
const { maxIncludeDepth } = resolveSecurityLimits(this.options.security);
|
|
2005
2011
|
// Alias the render data before partials shadow `c` with their argument.
|
|
2006
|
-
|
|
2012
|
+
// `inc_` tracks `include … once` occurrences across the whole render,
|
|
2013
|
+
// root template and partials alike (they share this closure).
|
|
2014
|
+
this.pushRaw(null, `const ${TOP_CONTEXT}=${CONTEXT};`, 'let d_=0;', 'const inc_={};', 'const p={');
|
|
2007
2015
|
for (const [path, code] of this.partials) {
|
|
2008
2016
|
this.pushRaw(null,
|
|
2009
2017
|
// Partials take the context they should render with: plain include
|
|
@@ -2144,6 +2152,7 @@ ${suggestionText}`;
|
|
|
2144
2152
|
const partials = new Map();
|
|
2145
2153
|
const blocks = [];
|
|
2146
2154
|
const stylesheets = [];
|
|
2155
|
+
const includeLast = new Map();
|
|
2147
2156
|
const traverse = (node) => {
|
|
2148
2157
|
if (node.type === 'INCLUDE') {
|
|
2149
2158
|
const include = node;
|
|
@@ -2151,6 +2160,15 @@ ${suggestionText}`;
|
|
|
2151
2160
|
if (!partials.has(include.val.value)) {
|
|
2152
2161
|
partials.set(include.val.value, include);
|
|
2153
2162
|
}
|
|
2163
|
+
if (include.modifier === 'last') {
|
|
2164
|
+
const existing = includeLast.get(include.val.value);
|
|
2165
|
+
if (existing) {
|
|
2166
|
+
existing.total += 1;
|
|
2167
|
+
}
|
|
2168
|
+
else {
|
|
2169
|
+
includeLast.set(include.val.value, { total: 1, emitted: 0 });
|
|
2170
|
+
}
|
|
2171
|
+
}
|
|
2154
2172
|
}
|
|
2155
2173
|
else {
|
|
2156
2174
|
this.hasDynamicInclude = true;
|
|
@@ -2165,6 +2183,7 @@ ${suggestionText}`;
|
|
|
2165
2183
|
node.traverse(traverse);
|
|
2166
2184
|
};
|
|
2167
2185
|
traverse(this.rootNode);
|
|
2186
|
+
this.state.includeLast = includeLast;
|
|
2168
2187
|
return {
|
|
2169
2188
|
partials,
|
|
2170
2189
|
blocks,
|
|
@@ -3629,39 +3648,71 @@ ${suggestionText}`;
|
|
|
3629
3648
|
parsers: parsers$9,
|
|
3630
3649
|
};
|
|
3631
3650
|
|
|
3632
|
-
async function compileInclude({ val, loc, withExp }, compiler) {
|
|
3651
|
+
async function compileInclude({ val, loc, withExp, modifier }, compiler) {
|
|
3633
3652
|
const expCompiler = new ExpCompiler();
|
|
3634
3653
|
const path = isStringLit(val)
|
|
3635
3654
|
? JSON.stringify(val.value)
|
|
3636
3655
|
: expCompiler.compile(val, compiler.context);
|
|
3637
|
-
|
|
3638
|
-
|
|
3639
|
-
|
|
3656
|
+
// Emit the partial call, against the top context for a plain include or a
|
|
3657
|
+
// merged scope for `include … with`.
|
|
3658
|
+
const emit = withExp
|
|
3659
|
+
? `s+=await p[${path}]?.({...${compiler.context},...${expCompiler.compile(withExp, compiler.context)}})??"";`
|
|
3660
|
+
: `s+=await p[${path}]?.(${TOP_CONTEXT})??"";`;
|
|
3661
|
+
// `once`: render the first occurrence only. The registry lives in the root
|
|
3662
|
+
// script's closure, so occurrences spread across different files (a root
|
|
3663
|
+
// template and its partials) still deduplicate at runtime.
|
|
3664
|
+
if (modifier === 'once') {
|
|
3665
|
+
compiler.pushRaw(loc, `if(!inc_[${path}]){inc_[${path}]=1;${emit}}`);
|
|
3640
3666
|
return;
|
|
3641
3667
|
}
|
|
3642
|
-
//
|
|
3643
|
-
|
|
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
|
+
compiler.pushRaw(loc, emit);
|
|
3644
3683
|
}
|
|
3645
3684
|
const compilers$8 = {
|
|
3646
3685
|
INCLUDE: compileInclude,
|
|
3647
3686
|
};
|
|
3648
3687
|
|
|
3688
|
+
// Modifier keywords that may appear after the path expression.
|
|
3689
|
+
// e.g. `{{ include "foo" once }}` or `{{ include "foo" last }}`
|
|
3690
|
+
const MODIFIER_RE = /\s+(once|last)\s*$/;
|
|
3649
3691
|
function* parseInclude(token, parser) {
|
|
3650
3692
|
if (!token.expression) {
|
|
3651
3693
|
parser.emitExpErr(token);
|
|
3652
3694
|
return;
|
|
3653
3695
|
}
|
|
3654
3696
|
yield 'NEXT';
|
|
3655
|
-
|
|
3697
|
+
// Strip a trailing `once` or `last` keyword before parsing the expression,
|
|
3698
|
+
// so the expression parser does not trip over an unknown identifier.
|
|
3699
|
+
let rawVal = token.expression.val;
|
|
3700
|
+
let modifier = null;
|
|
3701
|
+
const modifierMatch = MODIFIER_RE.exec(rawVal);
|
|
3702
|
+
if (modifierMatch) {
|
|
3703
|
+
modifier = modifierMatch[1];
|
|
3704
|
+
rawVal = rawVal.slice(0, modifierMatch.index);
|
|
3705
|
+
}
|
|
3706
|
+
const exp = parser.parseExp({ val: rawVal, loc: token.expression.loc });
|
|
3656
3707
|
if (!exp) {
|
|
3657
3708
|
return;
|
|
3658
3709
|
}
|
|
3659
3710
|
if (exp.type === 'WITH') {
|
|
3660
3711
|
const { left, right } = exp;
|
|
3661
|
-
yield new IncludeNode(left, token.loc, token.strip, right);
|
|
3712
|
+
yield new IncludeNode(left, token.loc, token.strip, right, modifier);
|
|
3662
3713
|
return;
|
|
3663
3714
|
}
|
|
3664
|
-
yield new IncludeNode(exp, token.loc, token.strip);
|
|
3715
|
+
yield new IncludeNode(exp, token.loc, token.strip, null, modifier);
|
|
3665
3716
|
}
|
|
3666
3717
|
const parsers$8 = {
|
|
3667
3718
|
include: parseInclude,
|
|
@@ -5537,84 +5588,6 @@ export default script;
|
|
|
5537
5588
|
return { opens, closes };
|
|
5538
5589
|
}
|
|
5539
5590
|
|
|
5540
|
-
/**
|
|
5541
|
-
* Brace-depth reindent for `<script>` bodies. Only leading whitespace changes;
|
|
5542
|
-
* braces inside strings, comments, template literals, and Janja tags are ignored.
|
|
5543
|
-
*/
|
|
5544
|
-
function indentJs(source, tabWidth) {
|
|
5545
|
-
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5546
|
-
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5547
|
-
const lines = source.split(/\r?\n/);
|
|
5548
|
-
let depth = 0;
|
|
5549
|
-
const out = [];
|
|
5550
|
-
for (const line of lines) {
|
|
5551
|
-
const content = line.trim();
|
|
5552
|
-
if (content.length === 0) {
|
|
5553
|
-
out.push('');
|
|
5554
|
-
continue;
|
|
5555
|
-
}
|
|
5556
|
-
const { opens, closes } = countJsBraces(content);
|
|
5557
|
-
const lineDepth = closes > opens ? Math.max(0, depth - (closes - opens)) : depth;
|
|
5558
|
-
out.push(pad.repeat(lineDepth) + content);
|
|
5559
|
-
depth = Math.max(0, depth + opens - closes);
|
|
5560
|
-
}
|
|
5561
|
-
return out.join(eol);
|
|
5562
|
-
}
|
|
5563
|
-
function countJsBraces(line) {
|
|
5564
|
-
let opens = 0;
|
|
5565
|
-
let closes = 0;
|
|
5566
|
-
let quote = null;
|
|
5567
|
-
let i = 0;
|
|
5568
|
-
while (i < line.length) {
|
|
5569
|
-
const c = line[i];
|
|
5570
|
-
if (quote) {
|
|
5571
|
-
if (c === '\\') {
|
|
5572
|
-
i += 2;
|
|
5573
|
-
continue;
|
|
5574
|
-
}
|
|
5575
|
-
if (c === quote) {
|
|
5576
|
-
quote = null;
|
|
5577
|
-
}
|
|
5578
|
-
i += 1;
|
|
5579
|
-
continue;
|
|
5580
|
-
}
|
|
5581
|
-
if (c === '{' && line[i + 1] === '{') {
|
|
5582
|
-
i = endOfJanja(line, i);
|
|
5583
|
-
continue;
|
|
5584
|
-
}
|
|
5585
|
-
if (c === '"' || c === "'" || c === '`') {
|
|
5586
|
-
quote = c;
|
|
5587
|
-
i += 1;
|
|
5588
|
-
continue;
|
|
5589
|
-
}
|
|
5590
|
-
if (c === '/' && line[i + 1] === '*') {
|
|
5591
|
-
const end = line.indexOf('*/', i + 2);
|
|
5592
|
-
i = end === -1 ? line.length : end + 2;
|
|
5593
|
-
continue;
|
|
5594
|
-
}
|
|
5595
|
-
if (c === '/' && line[i + 1] === '/') {
|
|
5596
|
-
break;
|
|
5597
|
-
}
|
|
5598
|
-
if (c === '{') {
|
|
5599
|
-
opens += 1;
|
|
5600
|
-
}
|
|
5601
|
-
else if (c === '}') {
|
|
5602
|
-
closes += 1;
|
|
5603
|
-
}
|
|
5604
|
-
i += 1;
|
|
5605
|
-
}
|
|
5606
|
-
return { opens, closes };
|
|
5607
|
-
}
|
|
5608
|
-
/** Skip a Janja tag starting at `i` (`{{`, `{{=`, `{{#` …). */
|
|
5609
|
-
function endOfJanja(line, i) {
|
|
5610
|
-
if (line.startsWith('{{#', i)) {
|
|
5611
|
-
const end = line.indexOf('#}}', i + 3);
|
|
5612
|
-
return end === -1 ? line.length : end + 3;
|
|
5613
|
-
}
|
|
5614
|
-
const end = line.indexOf('}}', i + 2);
|
|
5615
|
-
return end === -1 ? line.length : end + 2;
|
|
5616
|
-
}
|
|
5617
|
-
|
|
5618
5591
|
/** HTML void elements — never take children, so they do not change indent. */
|
|
5619
5592
|
const VOID = new Set([
|
|
5620
5593
|
'area',
|
|
@@ -5637,8 +5610,6 @@ export default script;
|
|
|
5637
5610
|
* whitespace; `script` / `style` are reindented (see body buffer below).
|
|
5638
5611
|
*/
|
|
5639
5612
|
const SENSITIVE = new Set(['pre', 'textarea', 'script', 'style']);
|
|
5640
|
-
/** HTML elements whose bodies are reindented (not left raw). */
|
|
5641
|
-
const REINDENT_HTML = new Set(['script', 'style']);
|
|
5642
5613
|
const DIR_OPEN = new Set([
|
|
5643
5614
|
'if',
|
|
5644
5615
|
'for',
|
|
@@ -5649,7 +5620,6 @@ export default script;
|
|
|
5649
5620
|
'with',
|
|
5650
5621
|
'stylesheet',
|
|
5651
5622
|
]);
|
|
5652
|
-
/** Directives whose bodies keep leading whitespace (like `<style>`). */
|
|
5653
5623
|
const DIR_SENSITIVE = new Set(['stylesheet']);
|
|
5654
5624
|
const DIR_MID = new Set(['else', 'elif', 'elseif', 'elsif']);
|
|
5655
5625
|
const DIR_CLOSE = new Set([
|
|
@@ -5662,92 +5632,36 @@ export default script;
|
|
|
5662
5632
|
'endwith',
|
|
5663
5633
|
'endstylesheet',
|
|
5664
5634
|
]);
|
|
5665
|
-
/**
|
|
5666
|
-
|
|
5667
|
-
* Only leading whitespace on each line changes; line breaks and
|
|
5668
|
-
* intra-line spacing stay put. Content inside pre/textarea is left alone;
|
|
5669
|
-
* `script` / `style` bodies are brace-reindented; `stylesheet` bodies are
|
|
5670
|
-
* formatted as an HTML fragment (so nested `<style>` gets CSS indent).
|
|
5671
|
-
*/
|
|
5672
|
-
function indentHtml(source, options) {
|
|
5673
|
-
const tabWidth = options.tabWidth ?? 2;
|
|
5674
|
-
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5675
|
-
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5676
|
-
const lines = source.split(/\r?\n/);
|
|
5677
|
-
let depth = 0;
|
|
5678
|
-
let sensitive = null;
|
|
5679
|
-
/** Raw lines inside a reindented body (`stylesheet` / `script` / `style`). */
|
|
5680
|
-
let bodyBuf = null;
|
|
5681
|
-
const out = [];
|
|
5682
|
-
for (const line of lines) {
|
|
5683
|
-
const leading = line.match(/^[ \t]*/)?.[0].length ?? 0;
|
|
5684
|
-
const content = line.slice(leading);
|
|
5685
|
-
const { pieces, prefixClosers } = scanLine(content, options);
|
|
5686
|
-
const activeSensitive = sensitive;
|
|
5687
|
-
const closesSensitive = activeSensitive !== null &&
|
|
5688
|
-
pieces.some((piece) => piece.kind === 'close' && piece.sensitive === activeSensitive);
|
|
5689
|
-
const reindentBody = activeSensitive === 'stylesheet' ||
|
|
5690
|
-
(activeSensitive !== null && REINDENT_HTML.has(activeSensitive));
|
|
5691
|
-
if (reindentBody && !closesSensitive) {
|
|
5692
|
-
// Do not applyDepth: bodies may contain nested Janja if/for tags.
|
|
5693
|
-
(bodyBuf ?? (bodyBuf = [])).push(line);
|
|
5694
|
-
continue;
|
|
5695
|
-
}
|
|
5696
|
-
if (bodyBuf && closesSensitive && reindentBody) {
|
|
5697
|
-
// stylesheet: same column as the tags; script/style: one level inside.
|
|
5698
|
-
const baseDepth = activeSensitive === 'stylesheet' ? Math.max(0, depth - 1) : depth;
|
|
5699
|
-
const raw = bodyBuf.join(eol);
|
|
5700
|
-
const formatted = activeSensitive === 'stylesheet'
|
|
5701
|
-
? indentHtml(raw, options)
|
|
5702
|
-
: activeSensitive === 'style'
|
|
5703
|
-
? indentCss(raw, tabWidth)
|
|
5704
|
-
: indentJs(raw, tabWidth);
|
|
5705
|
-
for (const bodyLine of formatted.split(/\r?\n/)) {
|
|
5706
|
-
out.push(bodyLine.length === 0 ? '' : pad.repeat(baseDepth) + bodyLine);
|
|
5707
|
-
}
|
|
5708
|
-
bodyBuf = null;
|
|
5709
|
-
}
|
|
5710
|
-
else if (sensitive && !closesSensitive) {
|
|
5711
|
-
out.push(line);
|
|
5712
|
-
applyDepth(pieces);
|
|
5713
|
-
continue;
|
|
5714
|
-
}
|
|
5715
|
-
const lineDepth = Math.max(0, depth - prefixClosers);
|
|
5716
|
-
out.push(content.length === 0 ? '' : pad.repeat(lineDepth) + content);
|
|
5717
|
-
applyDepth(pieces);
|
|
5718
|
-
}
|
|
5719
|
-
// split() keeps a final empty string when the source already ends in a
|
|
5720
|
-
// newline, so join() alone preserves that trailing newline — do not add
|
|
5721
|
-
// another one or each format grows an extra blank line.
|
|
5722
|
-
return out.join(eol);
|
|
5723
|
-
function applyDepth(pieces) {
|
|
5724
|
-
for (const piece of pieces) {
|
|
5725
|
-
if (piece.kind === 'open') {
|
|
5726
|
-
depth += 1;
|
|
5727
|
-
if (piece.sensitive && !sensitive) {
|
|
5728
|
-
({ sensitive } = piece);
|
|
5729
|
-
}
|
|
5730
|
-
}
|
|
5731
|
-
else if (piece.kind === 'close') {
|
|
5732
|
-
depth = Math.max(0, depth - 1);
|
|
5733
|
-
if (piece.sensitive && sensitive === piece.sensitive) {
|
|
5734
|
-
sensitive = null;
|
|
5735
|
-
}
|
|
5736
|
-
}
|
|
5737
|
-
else if (piece.kind === 'mid') ;
|
|
5738
|
-
}
|
|
5739
|
-
}
|
|
5740
|
-
}
|
|
5741
|
-
function scanLine(line, options) {
|
|
5635
|
+
/** Scan one line for HTML and Janja constructs that affect indentation. */
|
|
5636
|
+
function scanLine(line, options, state) {
|
|
5742
5637
|
const pieces = [];
|
|
5743
5638
|
let prefixClosers = 0;
|
|
5744
5639
|
let atPrefix = true;
|
|
5745
5640
|
let i = 0;
|
|
5641
|
+
if (state.inComment) {
|
|
5642
|
+
const close = line.indexOf(options.commentClose);
|
|
5643
|
+
if (close === -1) {
|
|
5644
|
+
return { pieces, prefixClosers };
|
|
5645
|
+
}
|
|
5646
|
+
state.inComment = false;
|
|
5647
|
+
i = close + options.commentClose.length;
|
|
5648
|
+
}
|
|
5746
5649
|
while (i < line.length) {
|
|
5747
5650
|
if (/\s/.test(line[i])) {
|
|
5748
5651
|
i += 1;
|
|
5749
5652
|
continue;
|
|
5750
5653
|
}
|
|
5654
|
+
if (line.startsWith(options.commentOpen, i)) {
|
|
5655
|
+
const close = line.indexOf(options.commentClose, i + options.commentOpen.length);
|
|
5656
|
+
// Comment runs past this line; the rest is comment text
|
|
5657
|
+
if (close === -1) {
|
|
5658
|
+
state.inComment = true;
|
|
5659
|
+
return { pieces, prefixClosers };
|
|
5660
|
+
}
|
|
5661
|
+
atPrefix = false;
|
|
5662
|
+
i = close + options.commentClose.length;
|
|
5663
|
+
continue;
|
|
5664
|
+
}
|
|
5751
5665
|
const next = readAt(line, i, options);
|
|
5752
5666
|
if (!next) {
|
|
5753
5667
|
// Plain text ends the leading-closer prefix
|
|
@@ -5774,12 +5688,7 @@ export default script;
|
|
|
5774
5688
|
return { pieces, prefixClosers };
|
|
5775
5689
|
}
|
|
5776
5690
|
function readAt(line, i, options) {
|
|
5777
|
-
const {
|
|
5778
|
-
if (line.startsWith(commentOpen, i)) {
|
|
5779
|
-
return {
|
|
5780
|
-
end: endOfDelimited(line, i + commentOpen.length, options.commentClose),
|
|
5781
|
-
};
|
|
5782
|
-
}
|
|
5691
|
+
const { directiveOpen, outputOpen } = options;
|
|
5783
5692
|
if (line.startsWith(outputOpen, i)) {
|
|
5784
5693
|
return {
|
|
5785
5694
|
end: endOfDelimited(line, i + outputOpen.length, options.outputClose),
|
|
@@ -5939,6 +5848,171 @@ export default script;
|
|
|
5939
5848
|
return idx === -1 ? line.length : idx + close.length;
|
|
5940
5849
|
}
|
|
5941
5850
|
|
|
5851
|
+
/**
|
|
5852
|
+
* Brace-depth reindent for `<script>` bodies. Only leading whitespace changes;
|
|
5853
|
+
* braces inside strings, comments, template literals, and Janja tags are ignored.
|
|
5854
|
+
*/
|
|
5855
|
+
function indentJs(source, tabWidth) {
|
|
5856
|
+
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5857
|
+
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5858
|
+
const lines = source.split(/\r?\n/);
|
|
5859
|
+
let depth = 0;
|
|
5860
|
+
const out = [];
|
|
5861
|
+
for (const line of lines) {
|
|
5862
|
+
const content = line.trim();
|
|
5863
|
+
if (content.length === 0) {
|
|
5864
|
+
out.push('');
|
|
5865
|
+
continue;
|
|
5866
|
+
}
|
|
5867
|
+
const { opens, closes } = countJsBraces(content);
|
|
5868
|
+
const lineDepth = closes > opens ? Math.max(0, depth - (closes - opens)) : depth;
|
|
5869
|
+
out.push(pad.repeat(lineDepth) + content);
|
|
5870
|
+
depth = Math.max(0, depth + opens - closes);
|
|
5871
|
+
}
|
|
5872
|
+
return out.join(eol);
|
|
5873
|
+
}
|
|
5874
|
+
function countJsBraces(line) {
|
|
5875
|
+
let opens = 0;
|
|
5876
|
+
let closes = 0;
|
|
5877
|
+
let quote = null;
|
|
5878
|
+
let i = 0;
|
|
5879
|
+
while (i < line.length) {
|
|
5880
|
+
const c = line[i];
|
|
5881
|
+
if (quote) {
|
|
5882
|
+
if (c === '\\') {
|
|
5883
|
+
i += 2;
|
|
5884
|
+
continue;
|
|
5885
|
+
}
|
|
5886
|
+
if (c === quote) {
|
|
5887
|
+
quote = null;
|
|
5888
|
+
}
|
|
5889
|
+
i += 1;
|
|
5890
|
+
continue;
|
|
5891
|
+
}
|
|
5892
|
+
if (c === '{' && line[i + 1] === '{') {
|
|
5893
|
+
i = endOfJanja(line, i);
|
|
5894
|
+
continue;
|
|
5895
|
+
}
|
|
5896
|
+
if (c === '"' || c === "'" || c === '`') {
|
|
5897
|
+
quote = c;
|
|
5898
|
+
i += 1;
|
|
5899
|
+
continue;
|
|
5900
|
+
}
|
|
5901
|
+
if (c === '/' && line[i + 1] === '*') {
|
|
5902
|
+
const end = line.indexOf('*/', i + 2);
|
|
5903
|
+
i = end === -1 ? line.length : end + 2;
|
|
5904
|
+
continue;
|
|
5905
|
+
}
|
|
5906
|
+
if (c === '/' && line[i + 1] === '/') {
|
|
5907
|
+
break;
|
|
5908
|
+
}
|
|
5909
|
+
if (c === '{') {
|
|
5910
|
+
opens += 1;
|
|
5911
|
+
}
|
|
5912
|
+
else if (c === '}') {
|
|
5913
|
+
closes += 1;
|
|
5914
|
+
}
|
|
5915
|
+
i += 1;
|
|
5916
|
+
}
|
|
5917
|
+
return { opens, closes };
|
|
5918
|
+
}
|
|
5919
|
+
/** Skip a Janja tag starting at `i` (`{{`, `{{=`, `{{#` …). */
|
|
5920
|
+
function endOfJanja(line, i) {
|
|
5921
|
+
if (line.startsWith('{{#', i)) {
|
|
5922
|
+
const end = line.indexOf('#}}', i + 3);
|
|
5923
|
+
return end === -1 ? line.length : end + 3;
|
|
5924
|
+
}
|
|
5925
|
+
const end = line.indexOf('}}', i + 2);
|
|
5926
|
+
return end === -1 ? line.length : end + 2;
|
|
5927
|
+
}
|
|
5928
|
+
|
|
5929
|
+
/** HTML elements whose bodies are reindented (not left raw). */
|
|
5930
|
+
const REINDENT_HTML = new Set(['script', 'style']);
|
|
5931
|
+
/**
|
|
5932
|
+
* Reindent a template that already had its Janja tags normalised.
|
|
5933
|
+
* Only leading whitespace on each line changes; line breaks and
|
|
5934
|
+
* intra-line spacing stay put. Content inside pre/textarea is left alone;
|
|
5935
|
+
* `script` / `style` bodies are brace-reindented; `stylesheet` bodies are
|
|
5936
|
+
* formatted as an HTML fragment (so nested `<style>` gets CSS indent).
|
|
5937
|
+
*/
|
|
5938
|
+
function indentHtml(source, options) {
|
|
5939
|
+
const tabWidth = options.tabWidth ?? 2;
|
|
5940
|
+
const pad = ' '.repeat(Math.max(0, tabWidth));
|
|
5941
|
+
const eol = source.includes('\r\n') ? '\r\n' : '\n';
|
|
5942
|
+
const lines = source.split(/\r?\n/);
|
|
5943
|
+
let depth = 0;
|
|
5944
|
+
let sensitive = null;
|
|
5945
|
+
/** Raw lines inside a reindented body (`stylesheet` / `script` / `style`). */
|
|
5946
|
+
let bodyBuf = null;
|
|
5947
|
+
const out = [];
|
|
5948
|
+
/** Open `{{#` comment: inner lines are never scanned for pieces. */
|
|
5949
|
+
const state = { inComment: false };
|
|
5950
|
+
for (const line of lines) {
|
|
5951
|
+
const leading = line.match(/^[ \t]*/)?.[0].length ?? 0;
|
|
5952
|
+
const content = line.slice(leading);
|
|
5953
|
+
const { pieces, prefixClosers } = scanLine(content, options, state);
|
|
5954
|
+
const activeSensitive = sensitive;
|
|
5955
|
+
const closesSensitive = activeSensitive !== null &&
|
|
5956
|
+
pieces.some((piece) => piece.kind === 'close' && piece.sensitive === activeSensitive);
|
|
5957
|
+
const reindentBody = activeSensitive === 'stylesheet' ||
|
|
5958
|
+
(activeSensitive !== null && REINDENT_HTML.has(activeSensitive));
|
|
5959
|
+
if (reindentBody && !closesSensitive) {
|
|
5960
|
+
// Do not applyDepth: bodies may contain nested Janja if/for tags.
|
|
5961
|
+
(bodyBuf ?? (bodyBuf = [])).push(line);
|
|
5962
|
+
continue;
|
|
5963
|
+
}
|
|
5964
|
+
if (bodyBuf && closesSensitive && reindentBody) {
|
|
5965
|
+
// stylesheet: same column as the tags; script/style: one level inside.
|
|
5966
|
+
const baseDepth = activeSensitive === 'stylesheet' ? Math.max(0, depth - 1) : depth;
|
|
5967
|
+
const raw = bodyBuf.join(eol);
|
|
5968
|
+
const formatted = activeSensitive === 'stylesheet'
|
|
5969
|
+
? indentHtml(raw, options)
|
|
5970
|
+
: activeSensitive === 'style'
|
|
5971
|
+
? indentCss(raw, tabWidth)
|
|
5972
|
+
: indentJs(raw, tabWidth);
|
|
5973
|
+
for (const bodyLine of formatted.split(/\r?\n/)) {
|
|
5974
|
+
out.push(bodyLine.length === 0 ? '' : pad.repeat(baseDepth) + bodyLine);
|
|
5975
|
+
}
|
|
5976
|
+
bodyBuf = null;
|
|
5977
|
+
}
|
|
5978
|
+
else if (sensitive && !closesSensitive) {
|
|
5979
|
+
out.push(line);
|
|
5980
|
+
applyDepth(pieces);
|
|
5981
|
+
continue;
|
|
5982
|
+
}
|
|
5983
|
+
const lineDepth = Math.max(0, depth - prefixClosers);
|
|
5984
|
+
const continuesSelfClosingTag = /^\/\s*>\s*$/.test(content);
|
|
5985
|
+
out.push(content.length === 0 ? '' : pad.repeat(lineDepth) + content);
|
|
5986
|
+
applyDepth(pieces);
|
|
5987
|
+
// The opening `<tag` was counted on an earlier line. A standalone `/>`
|
|
5988
|
+
// closes that element after this continuation line has been indented.
|
|
5989
|
+
if (continuesSelfClosingTag) {
|
|
5990
|
+
depth = Math.max(0, depth - 1);
|
|
5991
|
+
}
|
|
5992
|
+
}
|
|
5993
|
+
// split() keeps a final empty string when the source already ends in a
|
|
5994
|
+
// newline, so join() alone preserves that trailing newline — do not add
|
|
5995
|
+
// another one or each format grows an extra blank line.
|
|
5996
|
+
return out.join(eol);
|
|
5997
|
+
function applyDepth(pieces) {
|
|
5998
|
+
for (const piece of pieces) {
|
|
5999
|
+
if (piece.kind === 'open') {
|
|
6000
|
+
depth += 1;
|
|
6001
|
+
if (piece.sensitive && !sensitive) {
|
|
6002
|
+
({ sensitive } = piece);
|
|
6003
|
+
}
|
|
6004
|
+
}
|
|
6005
|
+
else if (piece.kind === 'close') {
|
|
6006
|
+
depth = Math.max(0, depth - 1);
|
|
6007
|
+
if (piece.sensitive && sensitive === piece.sensitive) {
|
|
6008
|
+
sensitive = null;
|
|
6009
|
+
}
|
|
6010
|
+
}
|
|
6011
|
+
else if (piece.kind === 'mid') ;
|
|
6012
|
+
}
|
|
6013
|
+
}
|
|
6014
|
+
}
|
|
6015
|
+
|
|
5942
6016
|
// An expression without the positions and spellings that formatting changes
|
|
5943
6017
|
const shape = (exp) => JSON.stringify(exp, (key, value) => key === 'loc' || key === 'raw' ? undefined : (value ?? null));
|
|
5944
6018
|
/**
|