miki-template 2.3.0 → 2.3.2
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/README.md +17 -5
- package/package.json +45 -1
- package/.eslintrc.json +0 -16
- package/.github/release-notes/v1.3.1.md +0 -55
- package/.github/release-notes/v1.3.3.md +0 -77
- package/.github/release-notes/v1.3.7.md +0 -20
- package/.github/workflows/ci.yml +0 -42
- package/.github/workflows/docs.yml +0 -107
- package/.github/workflows/release.yml +0 -107
- package/AGENT.md +0 -71
- package/API_REFERENCE.md +0 -341
- package/CHANGELOG.md +0 -173
- package/CODE_OF_CONDUCT.md +0 -14
- package/CONTRIBUTING.md +0 -27
- package/ROADMAP.md +0 -40
- package/assets/banner.png +0 -0
- package/assets/logo.png +0 -0
- package/benchmarks/ejs-results.json +0 -17
- package/benchmarks/ejs.js +0 -38
- package/benchmarks/handlebars-results.json +0 -17
- package/benchmarks/handlebars.js +0 -45
- package/benchmarks/miki-results.json +0 -17
- package/benchmarks/miki.js +0 -39
- package/benchmarks/pug-results.json +0 -17
- package/benchmarks/pug.js +0 -38
- package/benchmarks/report.json +0 -17
- package/benchmarks/run.js +0 -81
- package/benchmarks/stress.mjs +0 -647
- package/benchmarks/templates/large.dtpl +0 -7
- package/benchmarks/templates/medium.dtpl +0 -3
- package/benchmarks/templates/small.dtpl +0 -7
- package/context/component.md +0 -109
- package/context/prd.md +0 -131
- package/context/project-structure.md +0 -33
- package/dir/base.html +0 -23
- package/dir/cmpnt.html +0 -11
- package/dir/footer.html +0 -3
- package/dir/home.html +0 -80
- package/dir/index.html +0 -80
- package/dir/navbar.html +0 -9
- package/docs/api/async-render.md +0 -170
- package/docs/api/cache.md +0 -174
- package/docs/api/compile.md +0 -256
- package/docs/api/context-processors.md +0 -154
- package/docs/api/filters.md +0 -437
- package/docs/api/finder.md +0 -188
- package/docs/api/helpers.md +0 -106
- package/docs/api/i18n.md +0 -314
- package/docs/api/index.md +0 -108
- package/docs/api/libraries.md +0 -414
- package/docs/api/render-partial.md +0 -162
- package/docs/api/render.md +0 -184
- package/docs/api/security.md +0 -290
- package/docs/api/setup-express.md +0 -152
- package/docs/api/tags.md +0 -268
- package/docs/assets/banner.png +0 -0
- package/docs/assets/logo.png +0 -0
- package/docs/filter.md +0 -0
- package/docs/guide/advanced-usage.md +0 -794
- package/docs/guide/async-rendering.md +0 -616
- package/docs/guide/context-processors.md +0 -514
- package/docs/guide/custom-filters.md +0 -622
- package/docs/guide/custom-tags.md +0 -542
- package/docs/guide/filters.md +0 -1314
- package/docs/guide/getting-started.md +0 -204
- package/docs/guide/installation.md +0 -190
- package/docs/guide/partial-templates.md +0 -734
- package/docs/guide/quick-start.md +0 -444
- package/docs/guide/security.md +0 -690
- package/docs/guide/tags.md +0 -1566
- package/docs/guide/template-discovery.md +0 -340
- package/docs/guide/template-inheritance.md +0 -546
- package/docs/guide/what-is-miki-template.md +0 -28
- package/docs/guide/why-miki-template.md +0 -75
- package/docs/index.md +0 -86
- package/docs/integrations/elysia.md +0 -80
- package/docs/integrations/express.md +0 -219
- package/docs/integrations/fastify.md +0 -79
- package/docs/integrations/hono.md +0 -80
- package/docs/integrations/index.md +0 -68
- package/docs/integrations/koa.md +0 -90
- package/docs/integrations/nestjs.md +0 -80
- package/docs/integrations/tsed.md +0 -83
- package/docs/performance.md +0 -74
- package/eslint.config.mjs +0 -42
- package/ex.mjs +0 -36
- package/live-test/integrations/elysia-example.js +0 -16
- package/live-test/integrations/express-example.js +0 -24
- package/live-test/integrations/fastify-example.js +0 -20
- package/live-test/integrations/hono-example.js +0 -16
- package/live-test/integrations/koa-example.js +0 -30
- package/live-test/integrations/nestjs-example.js +0 -25
- package/live-test/integrations/smoke-test.js +0 -166
- package/live-test/integrations/tsed-example.js +0 -23
- package/live-test/package-lock.json +0 -1150
- package/live-test/package.json +0 -13
- package/live-test/packages/product/templates/product/detail.html +0 -7
- package/live-test/server.js +0 -38
- package/live-test/templates/app_templates/detail.html +0 -6
- package/live-test/views/base.html +0 -8
- package/live-test/views/child.html +0 -7
- package/live-test/views/home.html +0 -17
- package/live-test/views/index.html +0 -1
- package/miki-template-extension/.github/workflows/ci.yml +0 -116
- package/miki-template-extension/.vscodeignore +0 -7
- package/miki-template-extension/CHANGELOG.md +0 -99
- package/miki-template-extension/LICENSE +0 -21
- package/miki-template-extension/README.md +0 -273
- package/miki-template-extension/extension.js +0 -1013
- package/miki-template-extension/icon.png +0 -0
- package/miki-template-extension/icon.svg +0 -10
- package/miki-template-extension/miki-template-1.7.1.vsix +0 -0
- package/miki-template-extension/package.json +0 -280
- package/miki-template-extension/snippets/miki-template.json +0 -717
- package/miki-template-extension/syntaxes/language-configuration.json +0 -114
- package/miki-template-extension/syntaxes/miki-template.tmLanguage.json +0 -355
- package/miki-template-extension/tests/grammar-tests.json +0 -162
- package/miki-template-extension/tests/run-grammar-tests.js +0 -82
- package/mkdocs.yml +0 -195
- package/overrides/main.html +0 -26
- package/requirements-docs.txt +0 -2
- package/sample-app/package-lock.json +0 -901
- package/sample-app/package.json +0 -9
- package/sample-app/server.js +0 -14
- package/sample-app/views/index.html +0 -1
- package/scripts/build-vsix.js +0 -129
- package/scripts/build-vsix.ps1 +0 -15
- package/snippets/miki-template.json +0 -177
- package/src/asyncRender.js +0 -21
- package/src/types.d.ts +0 -107
- package/syntaxes/language-configuration.json +0 -26
- package/syntaxes/miki-template.tmLanguage.json +0 -146
- package/tests/asyncRender.test.js +0 -17
- package/tests/base.html +0 -6
- package/tests/child.html +0 -3
- package/tests/context_processors.test.js +0 -13
- package/tests/esm.test.mjs +0 -61
- package/tests/filters.test.js +0 -263
- package/tests/finder-appdirs.test.js +0 -19
- package/tests/finder.test.js +0 -17
- package/tests/fixtures/views/nested/index.html +0 -1
- package/tests/fixtures/views/partial.html +0 -1
- package/tests/fixtures/views/sub/deepfile.html +0 -1
- package/tests/fixtures/views-appdirs/product/site/detail.html +0 -1
- package/tests/include_security.test.js +0 -9
- package/tests/integration/README.md +0 -32
- package/tests/integration/features.test.cjs +0 -1681
- package/tests/integration/features.test.mjs +0 -1697
- package/tests/integration/finder.esm.test.mjs +0 -13
- package/tests/integration/partial-render.test.cjs +0 -13
- package/tests/integration/templates/base.miki +0 -6
- package/tests/integration/templates/child.miki +0 -6
- package/tests/integration/templates/index.html +0 -17
- package/tests/lexer.test.js +0 -45
- package/tests/parser.test.js +0 -57
- package/tests/partial.html +0 -1
- package/tests/partialdef.test.js +0 -79
- package/tests/production_checks.js +0 -57
- package/tests/security.test.js +0 -28
- package/tests/tags.test.js +0 -233
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import assert from 'node:assert';
|
|
2
|
-
import path from 'node:path';
|
|
3
|
-
import { fileURLToPath } from 'node:url';
|
|
4
|
-
import test from 'node:test';
|
|
5
|
-
import { findTemplateInViews } from '../../src/esm.mjs';
|
|
6
|
-
|
|
7
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
8
|
-
|
|
9
|
-
test('ESM Template finder - nested index', () => {
|
|
10
|
-
const views = [path.join(__dirname, '..', 'fixtures', 'views')];
|
|
11
|
-
const found = findTemplateInViews('nested/index', views);
|
|
12
|
-
assert.ok(found && typeof found === 'string');
|
|
13
|
-
});
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
const test = require('node:test');
|
|
2
|
-
const assert = require('assert');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const miki = require('../../src/index.js');
|
|
5
|
-
|
|
6
|
-
test('engine partial render via render() and asyncRender()', async () => {
|
|
7
|
-
const views = path.resolve(__dirname, '..', '..', 'live-test', 'views');
|
|
8
|
-
const sync = miki.render('home#card', { user: 'Tester', title: 'T' }, { views });
|
|
9
|
-
assert.ok(sync.includes('Card'), 'sync partial should contain Card');
|
|
10
|
-
|
|
11
|
-
const asyncRes = await miki.asyncRender('home#card', { user: 'Tester', title: 'T' }, { views });
|
|
12
|
-
assert.ok(asyncRes.includes('Card'), 'async partial should contain Card');
|
|
13
|
-
});
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html>
|
|
3
|
-
<head>
|
|
4
|
-
<title>{{ title }}</title>
|
|
5
|
-
</head>
|
|
6
|
-
<body>
|
|
7
|
-
<h1>{{ heading }}</h1>
|
|
8
|
-
<ul>
|
|
9
|
-
{% for item in items %}
|
|
10
|
-
<li>{{ forloop.counter }}: {{ item|upper }}</li>
|
|
11
|
-
{% empty %}
|
|
12
|
-
<li>No items</li>
|
|
13
|
-
{% endfor %}
|
|
14
|
-
</ul>
|
|
15
|
-
<p>User: {{ user.name|title }}</p>
|
|
16
|
-
</body>
|
|
17
|
-
</html>
|
package/tests/lexer.test.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const test = require('node:test');
|
|
2
|
-
const assert = require('node:assert');
|
|
3
|
-
const { tokenize } = require('../src/lexer');
|
|
4
|
-
|
|
5
|
-
test('Lexer - plain text tokenizing', () => {
|
|
6
|
-
const tokens = tokenize('Hello World');
|
|
7
|
-
assert.strictEqual(tokens.length, 1);
|
|
8
|
-
assert.strictEqual(tokens[0].type, 'text');
|
|
9
|
-
assert.strictEqual(tokens[0].content, 'Hello World');
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
test('Lexer - variable tokenizing', () => {
|
|
13
|
-
const tokens = tokenize('Hello {{ name }}!');
|
|
14
|
-
assert.strictEqual(tokens.length, 3);
|
|
15
|
-
assert.strictEqual(tokens[0].content, 'Hello ');
|
|
16
|
-
assert.strictEqual(tokens[1].type, 'var');
|
|
17
|
-
assert.strictEqual(tokens[1].content, 'name');
|
|
18
|
-
assert.strictEqual(tokens[2].content, '!');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('Lexer - block tag tokenizing', () => {
|
|
22
|
-
const tokens = tokenize('{% if active %}Yes{% endif %}');
|
|
23
|
-
assert.strictEqual(tokens.length, 3);
|
|
24
|
-
assert.strictEqual(tokens[0].type, 'block');
|
|
25
|
-
assert.strictEqual(tokens[0].content, 'if active');
|
|
26
|
-
assert.strictEqual(tokens[1].content, 'Yes');
|
|
27
|
-
assert.strictEqual(tokens[2].type, 'block');
|
|
28
|
-
assert.strictEqual(tokens[2].content, 'endif');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('Lexer - inline comment exclusion', () => {
|
|
32
|
-
const tokens = tokenize('Before {# comment #} After');
|
|
33
|
-
assert.strictEqual(tokens.length, 2);
|
|
34
|
-
assert.strictEqual(tokens[0].content, 'Before ');
|
|
35
|
-
assert.strictEqual(tokens[1].content, ' After');
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('Lexer - verbatim tag handling', () => {
|
|
39
|
-
const tokens = tokenize('Text {% verbatim %} {{ unparsed }} {% endverbatim %} End');
|
|
40
|
-
assert.strictEqual(tokens.length, 3);
|
|
41
|
-
assert.strictEqual(tokens[0].content, 'Text ');
|
|
42
|
-
assert.strictEqual(tokens[1].type, 'text');
|
|
43
|
-
assert.strictEqual(tokens[1].content, ' {{ unparsed }} ');
|
|
44
|
-
assert.strictEqual(tokens[2].content, ' End');
|
|
45
|
-
});
|
package/tests/parser.test.js
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
const test = require('node:test');
|
|
2
|
-
const assert = require('node:assert');
|
|
3
|
-
const { parseVariableExpression } = require('../src/parser');
|
|
4
|
-
const { Context } = require('../src/context');
|
|
5
|
-
|
|
6
|
-
test('Parser - variable expression parsing', () => {
|
|
7
|
-
const res = parseVariableExpression('user.name|lower|default:"Guest"');
|
|
8
|
-
assert.strictEqual(res.varPath, 'user.name');
|
|
9
|
-
assert.strictEqual(res.filters.length, 2);
|
|
10
|
-
|
|
11
|
-
assert.strictEqual(res.filters[0].name, 'lower');
|
|
12
|
-
assert.strictEqual(res.filters[0].arg, null);
|
|
13
|
-
|
|
14
|
-
assert.strictEqual(res.filters[1].name, 'default');
|
|
15
|
-
assert.strictEqual(res.filters[1].arg.type, 'literal');
|
|
16
|
-
assert.strictEqual(res.filters[1].arg.value, 'Guest');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test('Context - basic dotted lookups', () => {
|
|
20
|
-
const ctx = new Context({
|
|
21
|
-
user: {
|
|
22
|
-
profile: {
|
|
23
|
-
name: 'Miki',
|
|
24
|
-
age: 30
|
|
25
|
-
}
|
|
26
|
-
},
|
|
27
|
-
items: ['apple', 'banana']
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
assert.strictEqual(ctx.get('user.profile.name'), 'Miki');
|
|
31
|
-
assert.strictEqual(ctx.get('user.profile.age'), 30);
|
|
32
|
-
assert.strictEqual(ctx.get('items.1'), 'banana');
|
|
33
|
-
// Missing key now returns undefined (so filters like default_if_none
|
|
34
|
-
// can detect missing values) instead of empty string.
|
|
35
|
-
assert.strictEqual(ctx.get('user.profile.missing'), undefined);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('Context - scope pushing and popping', () => {
|
|
39
|
-
const ctx = new Context({ name: 'Global' });
|
|
40
|
-
ctx.push({ name: 'Local', age: 25 });
|
|
41
|
-
|
|
42
|
-
assert.strictEqual(ctx.get('name'), 'Local');
|
|
43
|
-
assert.strictEqual(ctx.get('age'), 25);
|
|
44
|
-
|
|
45
|
-
ctx.pop();
|
|
46
|
-
assert.strictEqual(ctx.get('name'), 'Global');
|
|
47
|
-
assert.strictEqual(ctx.get('age'), undefined);
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('Context - automatic function calling', () => {
|
|
51
|
-
const ctx = new Context({
|
|
52
|
-
user: {
|
|
53
|
-
getName: () => 'Dynamic Name'
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
assert.strictEqual(ctx.get('user.getName'), 'Dynamic Name');
|
|
57
|
-
});
|
package/tests/partial.html
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
<p>Included: {{ item }}</p>
|
package/tests/partialdef.test.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
// tests for partialdef support, including conditionals, loops, and inline rendering
|
|
2
|
-
const test = require('node:test');
|
|
3
|
-
const assert = require('node:assert');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const os = require('os');
|
|
7
|
-
const { render, compile, renderPartialFromSource } = require('../src/index');
|
|
8
|
-
|
|
9
|
-
// Basic partialdef rendering via {% partial %}
|
|
10
|
-
test('Partialdef basic rendering', () => {
|
|
11
|
-
const tpl = `{% partialdef greeting %}Hello {{ name }}{% endpartialdef %}{% partial greeting %}`;
|
|
12
|
-
const out = render(tpl, { name: 'Miki' });
|
|
13
|
-
assert.strictEqual(out, 'Hello Miki');
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
// Partialdef with conditional logic
|
|
17
|
-
test('Partialdef with if/else', () => {
|
|
18
|
-
const tpl = `{% partialdef cond %}{% if show %}Visible{% else %}Hidden{% endif %}{% endpartialdef %}{% partial cond %}`;
|
|
19
|
-
assert.strictEqual(render(tpl, { show: true }), 'Visible');
|
|
20
|
-
assert.strictEqual(render(tpl, { show: false }), 'Hidden');
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
// Partialdef with loop and metadata
|
|
24
|
-
test('Partialdef with for loop', () => {
|
|
25
|
-
const tpl = `{% partialdef list %}{% for item in items %}{{ item }},{% empty %}none{% endfor %}{% endpartialdef %}{% partial list %}`;
|
|
26
|
-
assert.strictEqual(render(tpl, { items: ['a', 'b'] }), 'a,b,');
|
|
27
|
-
assert.strictEqual(render(tpl, { items: [] }), 'none');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
// Inline partialdef (renders immediately)
|
|
31
|
-
test('Inline partialdef renders inline', () => {
|
|
32
|
-
const tpl = `{% partialdef inline_example inline %}Inline {{ val }}{% endpartialdef %}`;
|
|
33
|
-
const out = render(tpl, { val: 'X' });
|
|
34
|
-
assert.strictEqual(out, 'Inline X');
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// Rendering a partial via compile.renderPartial API
|
|
38
|
-
test('renderPartial API works', () => {
|
|
39
|
-
const tpl = `{% partialdef api %}API {{ data }}{% endpartialdef %}`;
|
|
40
|
-
const compiled = compile(tpl);
|
|
41
|
-
const out = compiled.renderPartial('api', { data: 123 });
|
|
42
|
-
assert.strictEqual(out, 'API 123');
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
// Bug fix: partials defined inside for loops are found by renderPartialFromSource
|
|
46
|
-
test('renderPartialFromSource finds partials inside for loops', () => {
|
|
47
|
-
const tpl = `{% for item in items %}{% partialdef loop_item %}{{ item }}{% endpartialdef %}{% endfor %}`;
|
|
48
|
-
const out = renderPartialFromSource(tpl, 'loop_item', { item: 'X' });
|
|
49
|
-
assert.strictEqual(out, 'X');
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// Bug fix: include with partial selector does not leak other partials from included file
|
|
53
|
-
test('include with partial selector does not leak other partials', () => {
|
|
54
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'miki-partial-'));
|
|
55
|
-
fs.writeFileSync(path.join(dir, 'main.html'), `{% include "frag.html#only_me" %}`);
|
|
56
|
-
fs.writeFileSync(path.join(dir, 'frag.html'), `{% partialdef only_me %}ME{% endpartialdef %}{% partialdef secret %}SECRET{% endpartialdef %}`);
|
|
57
|
-
|
|
58
|
-
const mainTpl = fs.readFileSync(path.join(dir, 'main.html'), 'utf8');
|
|
59
|
-
assert.throws(
|
|
60
|
-
() => renderPartialFromSource(mainTpl, 'secret', {}, { views: dir }),
|
|
61
|
-
/Partial 'secret' not found/
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
fs.rmSync(dir, { recursive: true });
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
// Bug fix: full include makes all partials from included file available
|
|
68
|
-
test('full include makes all included partials available', () => {
|
|
69
|
-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'miki-partial-full-'));
|
|
70
|
-
fs.writeFileSync(path.join(dir, 'main.html'), `{% include "frag.html" %}`);
|
|
71
|
-
fs.writeFileSync(path.join(dir, 'frag.html'), `{% partialdef only_me %}ME{% endpartialdef %}{% partialdef secret %}SECRET{% endpartialdef %}`);
|
|
72
|
-
|
|
73
|
-
const mainTpl = fs.readFileSync(path.join(dir, 'main.html'), 'utf8');
|
|
74
|
-
const out = renderPartialFromSource(mainTpl, 'secret', {}, { views: dir });
|
|
75
|
-
assert.strictEqual(out, 'SECRET');
|
|
76
|
-
|
|
77
|
-
fs.rmSync(dir, { recursive: true });
|
|
78
|
-
});
|
|
79
|
-
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
// Production readiness checks
|
|
2
|
-
const { render, compile } = require('../src/index');
|
|
3
|
-
const checks = [];
|
|
4
|
-
|
|
5
|
-
try {
|
|
6
|
-
render('{% if x %}hello', {});
|
|
7
|
-
checks.push('FAIL: should throw on unclosed if');
|
|
8
|
-
} catch(e) {
|
|
9
|
-
checks.push('OK: unclosed if throws: ' + e.message.slice(0, 50));
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
try {
|
|
13
|
-
render('{{ x|nonexistent }}', { x: 'hi' });
|
|
14
|
-
checks.push('FAIL: should throw on unknown filter');
|
|
15
|
-
} catch(e) {
|
|
16
|
-
checks.push('OK: unknown filter throws: ' + e.message.slice(0, 50));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
render('{% nonexistent_tag %}', {});
|
|
21
|
-
checks.push('FAIL: should throw on unknown tag');
|
|
22
|
-
} catch(e) {
|
|
23
|
-
checks.push('OK: unknown tag throws: ' + e.message.slice(0, 50));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const xss = render('{{ x }}', { x: '<script>alert(1)</script>' });
|
|
27
|
-
checks.push(xss.includes('<script>') ? 'OK: XSS auto-escaped' : 'FAIL: XSS not escaped');
|
|
28
|
-
|
|
29
|
-
const safe = render('{{ x|safe }}', { x: '<b>bold</b>' });
|
|
30
|
-
checks.push(safe === '<b>bold</b>' ? 'OK: safe filter works' : 'FAIL: safe: ' + safe);
|
|
31
|
-
|
|
32
|
-
const c1 = compile('{% cycle a b c %}');
|
|
33
|
-
const r1 = c1.render({});
|
|
34
|
-
const r2 = c1.render({});
|
|
35
|
-
checks.push(r1 !== r2 ? 'OK: cycle resets' : 'WARN: cycle state: ' + r1 + '/' + r2);
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
render('{{ x }}', { x: 'a'.repeat(100000) });
|
|
39
|
-
checks.push('OK: handles large input');
|
|
40
|
-
} catch(e) {
|
|
41
|
-
checks.push('FAIL: large input: ' + e.message);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const nullTest = render('{{ x }}', { x: null });
|
|
45
|
-
checks.push(nullTest === '' ? 'OK: null renders empty' : 'FAIL: null=' + nullTest);
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
render('{% extends "../etc/passwd" %}', {});
|
|
49
|
-
checks.push('FAIL: path traversal not blocked');
|
|
50
|
-
} catch(e) {
|
|
51
|
-
checks.push('OK: path traversal blocked: ' + e.message.slice(0, 50));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const nested = render('{% for i in outer %}{% for j in i %}{{ forloop.parentloop.counter }}.{{ forloop.counter }} {% endfor %}{% endfor %}', { outer: [[1,2],[3,4]] });
|
|
55
|
-
checks.push(nested.trim() === '1.1 1.2 2.1 2.2' ? 'OK: nested forloop' : 'WARN nested: ' + nested);
|
|
56
|
-
|
|
57
|
-
console.log(checks.join('\n'));
|
package/tests/security.test.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
const test = require('node:test');
|
|
2
|
-
const assert = require('node:assert');
|
|
3
|
-
const { render, markSafe } = require('../src/index');
|
|
4
|
-
|
|
5
|
-
test('Security - HTML Auto-escaping enabled by default', () => {
|
|
6
|
-
const tpl = '{{ value }}';
|
|
7
|
-
const output = render(tpl, { value: '<script>alert("xss")</script>' });
|
|
8
|
-
assert.strictEqual(output, '<script>alert("xss")</script>');
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
test('Security - safe filter disables escaping', () => {
|
|
12
|
-
const tpl = '{{ value|safe }}';
|
|
13
|
-
const output = render(tpl, { value: '<b>Hello</b>' });
|
|
14
|
-
assert.strictEqual(output, '<b>Hello</b>');
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
test('Security - escape filter forces escaping', () => {
|
|
18
|
-
// In autoescape off mode, escape filter still escapes
|
|
19
|
-
const tpl = '{% autoescape off %}{{ value|escape }}{% endautoescape %}';
|
|
20
|
-
const output = render(tpl, { value: '<b>Hello</b>' });
|
|
21
|
-
assert.strictEqual(output, '<b>Hello</b>');
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('Security - markSafe variables bypassed', () => {
|
|
25
|
-
const tpl = '{{ value }}';
|
|
26
|
-
const output = render(tpl, { value: markSafe('<h1>Title</h1>') });
|
|
27
|
-
assert.strictEqual(output, '<h1>Title</h1>');
|
|
28
|
-
});
|
package/tests/tags.test.js
DELETED
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
const test = require('node:test');
|
|
2
|
-
const assert = require('node:assert');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
const { render, compile } = require('../src/index');
|
|
5
|
-
|
|
6
|
-
test('Tags - If / Elif / Else', () => {
|
|
7
|
-
const tpl = '{% if value == 1 %}One{% elif value == 2 %}Two{% else %}Other{% endif %}';
|
|
8
|
-
assert.strictEqual(render(tpl, { value: 1 }), 'One');
|
|
9
|
-
assert.strictEqual(render(tpl, { value: 2 }), 'Two');
|
|
10
|
-
assert.strictEqual(render(tpl, { value: 3 }), 'Other');
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
test('Tags - For loop with empty and unpack', () => {
|
|
14
|
-
const tpl = '{% for x in items %}{{ x }}{% empty %}No items{% endfor %}';
|
|
15
|
-
assert.strictEqual(render(tpl, { items: ['a', 'b'] }), 'ab');
|
|
16
|
-
assert.strictEqual(render(tpl, { items: [] }), 'No items');
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
test('Tags - For loop loop metadata', () => {
|
|
20
|
-
const tpl = '{% for x in items %}{{ forloop.counter }}:{{ x }}{% if not forloop.last %},{% endif %}{% endfor %}';
|
|
21
|
-
assert.strictEqual(render(tpl, { items: ['a', 'b'] }), '1:a,2:b');
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('Tags - With scope blocks', () => {
|
|
25
|
-
const tpl = '{% with a=x b=y %}{{ a }}-{{ b }}{% endwith %}';
|
|
26
|
-
assert.strictEqual(render(tpl, { x: 10, y: 20 }), '10-20');
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
test('Tags - Cycle alternating tags', () => {
|
|
30
|
-
const tpl = '{% for x in items %}{% cycle "odd" "even" %} {% endfor %}';
|
|
31
|
-
assert.strictEqual(render(tpl, { items: [1, 2, 3] }), 'odd even odd ');
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('Tags - Template inheritance and block.super', () => {
|
|
35
|
-
const views = __dirname;
|
|
36
|
-
const childTpl = '{% extends "child.html" %}';
|
|
37
|
-
const output = render(childTpl, {}, { views });
|
|
38
|
-
|
|
39
|
-
assert.match(output, /Child Header/);
|
|
40
|
-
assert.match(output, /Default Header/);
|
|
41
|
-
assert.match(output, /Child Content/);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test('Tags - Include tag', () => {
|
|
45
|
-
const views = __dirname;
|
|
46
|
-
const tpl = '{% include "partial.html" with item="Apple" %}';
|
|
47
|
-
const output = render(tpl, {}, { views });
|
|
48
|
-
assert.strictEqual(output.trim(), '<p>Included: Apple</p>');
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
test('Tags - Regroup array by property', () => {
|
|
52
|
-
const people = [
|
|
53
|
-
{ name: 'Miki', gender: 'male' },
|
|
54
|
-
{ name: 'Anna', gender: 'female' },
|
|
55
|
-
{ name: 'John', gender: 'male' }
|
|
56
|
-
];
|
|
57
|
-
const tpl = '{% regroup people by gender as grouped %}{% for g in grouped %}{{ g.grouper }}:{% for p in g.list %}{{ p.name }}{% endfor %} {% endfor %}';
|
|
58
|
-
assert.strictEqual(render(tpl, { people }), 'male:MikiJohn female:Anna ');
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test('Tags - Spaceless HTML formatting', () => {
|
|
62
|
-
const tpl = '{% spaceless %} <div> <p> Hello </p> </div> {% endspaceless %}';
|
|
63
|
-
assert.strictEqual(render(tpl), ' <div><p> Hello </p></div> ');
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
test('Tags - Static file path generator', () => {
|
|
67
|
-
const tpl = '{% static "images/logo.png" %}';
|
|
68
|
-
assert.strictEqual(render(tpl, {}, { staticUrl: '/assets/' }), '/assets/images/logo.png');
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
test('Tags - Url mapping', () => {
|
|
72
|
-
const tpl = '{% url "user-profile" "miki" %}';
|
|
73
|
-
const urlHelper = (route, arg) => `/users/${arg}`;
|
|
74
|
-
assert.strictEqual(render(tpl, {}, { urlHelper }), '/users/miki');
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
test('Tags - csrf_token security tag', () => {
|
|
78
|
-
const tpl = '{% csrf_token %}';
|
|
79
|
-
const output = render(tpl, { csrf_token: '12345' });
|
|
80
|
-
assert.strictEqual(output, '<input type="hidden" name="csrfmiddlewaretoken" value="12345">');
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
test('Tags - csp_nonce_attr security tag', () => {
|
|
84
|
-
const tpl = '<script {% csp_nonce_attr %} src="app.js"></script>';
|
|
85
|
-
const output = render(tpl, { csp_nonce: 'xyz789' });
|
|
86
|
-
assert.strictEqual(output, '<script nonce="xyz789" src="app.js"></script>');
|
|
87
|
-
|
|
88
|
-
// If no nonce in context, output nothing
|
|
89
|
-
const outputEmpty = render(tpl, {});
|
|
90
|
-
assert.strictEqual(outputEmpty, '<script src="app.js"></script>');
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
test('Tags - Partial Block Rendering via compile.renderBlock', () => {
|
|
94
|
-
const views = __dirname;
|
|
95
|
-
const childTpl = '{% extends "child.html" %}';
|
|
96
|
-
const compiled = compile(childTpl, { views });
|
|
97
|
-
|
|
98
|
-
// Render only the 'content' block
|
|
99
|
-
const contentOnly = compiled.renderBlock('content');
|
|
100
|
-
assert.strictEqual(contentOnly, 'Child Content');
|
|
101
|
-
|
|
102
|
-
// Render only the 'header' block (which includes block.super)
|
|
103
|
-
const headerOnly = compiled.renderBlock('header');
|
|
104
|
-
assert.match(headerOnly, /Child Header/);
|
|
105
|
-
assert.match(headerOnly, /Default Header/);
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
test('Tags - templatetag', () => {
|
|
109
|
-
const output = render('{% templatetag openvariable %}hello{% templatetag closevariable %}', {});
|
|
110
|
-
assert.strictEqual(output, '{{hello}}');
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
test('Tags - templatetag openblock/closeblock', () => {
|
|
114
|
-
const output = render('{% templatetag openblock %}body{% templatetag closeblock %}', {});
|
|
115
|
-
assert.strictEqual(output, '{%body%}');
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('Tags - load', () => {
|
|
119
|
-
const output = render('{% load i18n %}', {});
|
|
120
|
-
assert.strictEqual(output, '');
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
test('Tags - unclosed if throws', () => {
|
|
124
|
-
assert.throws(() => {
|
|
125
|
-
render('{% if x %}hello', {});
|
|
126
|
-
}, /Unclosed.*if|endif/i);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
test('Tags - unclosed for throws', () => {
|
|
130
|
-
assert.throws(() => {
|
|
131
|
-
render('{% for x in items %}{{ x }}', { items: [1, 2] });
|
|
132
|
-
}, /Unexpected end|endfor/i);
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
test('Tags - unclosed with throws', () => {
|
|
136
|
-
assert.throws(() => {
|
|
137
|
-
render('{% with a=b %}{{ a }}', { b: 1 });
|
|
138
|
-
}, /Unexpected end|endwith/i);
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
test('Tags - widthratio', () => {
|
|
142
|
-
// 25 out of 100 with max width 150 = floor(25/100 * 150) = 37
|
|
143
|
-
assert.strictEqual(render('{% widthratio 25 100 150 %}', {}), '37');
|
|
144
|
-
// Edge cases
|
|
145
|
-
assert.strictEqual(render('{% widthratio 0 100 150 %}', {}), '0');
|
|
146
|
-
assert.strictEqual(render('{% widthratio 100 100 150 %}', {}), '150');
|
|
147
|
-
assert.strictEqual(render('{% widthratio 200 100 150 %}', {}), '150');
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
test('Tags - debug', () => {
|
|
151
|
-
const output = render('{% debug %}', { foo: 'bar' });
|
|
152
|
-
assert.ok(output.includes('foo'));
|
|
153
|
-
assert.ok(output.includes('bar'));
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
test('Tags - i18n trans tag', () => {
|
|
157
|
-
const { setLanguage, registerTranslation, render } = require('../src/index');
|
|
158
|
-
registerTranslation('en', { 'Hello, World!': 'Bonjour, le monde !' });
|
|
159
|
-
setLanguage('en');
|
|
160
|
-
const output = render('{% trans "Hello, World!" %}', {});
|
|
161
|
-
assert.strictEqual(output, 'Bonjour, le monde !');
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
test('Tags - i18n language block', () => {
|
|
165
|
-
const { setLanguage, registerTranslation, render } = require('../src/index');
|
|
166
|
-
registerTranslation('fr', { 'Welcome': 'Bienvenue' });
|
|
167
|
-
const output = render('{% language "fr" %}{% trans "Welcome" %}{% endlanguage %}', {});
|
|
168
|
-
assert.strictEqual(output, 'Bienvenue');
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
test('Tags - load with real library', () => {
|
|
172
|
-
const { render, registerLibrary } = require('../src/index');
|
|
173
|
-
registerLibrary('mytestlib', {
|
|
174
|
-
filters: {
|
|
175
|
-
shout: (val) => String(val).toUpperCase() + '!'
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
const output = render('{% load mytestlib %}{{ "hello"|shout }}', {});
|
|
179
|
-
assert.strictEqual(output, 'HELLO!');
|
|
180
|
-
});
|
|
181
|
-
|
|
182
|
-
test('Tags - regroup filter', () => {
|
|
183
|
-
const { render } = require('../src/index');
|
|
184
|
-
const items = [
|
|
185
|
-
{ category: 'A', name: 'a1' },
|
|
186
|
-
{ category: 'B', name: 'b1' },
|
|
187
|
-
{ category: 'A', name: 'a2' }
|
|
188
|
-
];
|
|
189
|
-
const output = render(
|
|
190
|
-
'{% for g in items|regroup:"category" %}{{ g.grouper }}{% for i in g.list %}{{ i.name }}{% endfor %}{% endfor %}',
|
|
191
|
-
{ items }
|
|
192
|
-
);
|
|
193
|
-
assert.strictEqual(output, 'Aa1a2Bb1');
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
test('Filters - strftime with date-fns', () => {
|
|
197
|
-
const { render, getFilter } = require('../src/index');
|
|
198
|
-
const strftime = getFilter('strftime');
|
|
199
|
-
const result = strftime(new Date('2026-08-31T22:00:00'), 'yyyy-MM-dd');
|
|
200
|
-
assert.strictEqual(result, '2026-08-31');
|
|
201
|
-
const result2 = strftime(new Date('2026-08-31T22:00:00'), 'HH:mm');
|
|
202
|
-
assert.ok(result2.startsWith('22:'));
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
test('Tags - now tag', () => {
|
|
206
|
-
const { render } = require('../src/index');
|
|
207
|
-
const result = render('{% now "yyyy-MM-dd" %}');
|
|
208
|
-
assert.ok(result.match(/^\d{4}-\d{2}-\d{2}$/));
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
test('Tags - set tag inline', () => {
|
|
212
|
-
const { render } = require('../src/index');
|
|
213
|
-
const result = render('{% set x = 42 %}{{ x }}');
|
|
214
|
-
assert.strictEqual(result, '42');
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
test('Tags - set tag block form', () => {
|
|
218
|
-
const { render } = require('../src/index');
|
|
219
|
-
const result = render('{% set greeting %}Hello{% endset %}{{ greeting }}');
|
|
220
|
-
assert.strictEqual(result, 'Hello');
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
test('Tags - ifchanged tag', () => {
|
|
224
|
-
const { render } = require('../src/index');
|
|
225
|
-
const result = render('{% for i in items %}{% ifchanged i %}{{ i }}{% endifchanged %}{% endfor %}', { items: [1, 1, 2, 2, 3] });
|
|
226
|
-
assert.strictEqual(result, '123');
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
test('Tags - ifchanged with else', () => {
|
|
230
|
-
const { render } = require('../src/index');
|
|
231
|
-
const result = render('{% for i in items %}{% ifchanged i %}{{ i }}{% else %}*{% endifchanged %}{% endfor %}', { items: [1, 1, 2, 2, 3] });
|
|
232
|
-
assert.strictEqual(result, '1*2*3');
|
|
233
|
-
});
|