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