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