miki-template 2.3.1 → 2.3.3

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.
Files changed (167) hide show
  1. package/README.md +1 -1
  2. package/package.json +24 -2
  3. package/src/codegen.js +140 -9
  4. package/src/parser.js +69 -1
  5. package/src/tags/control.js +112 -51
  6. package/src/tags/extra.js +4 -17
  7. package/src/tags/i18n.js +6 -17
  8. package/src/tags/inheritance.js +6 -20
  9. package/src/tags/util.js +5 -20
  10. package/.eslintrc.json +0 -16
  11. package/.github/release-notes/v1.3.1.md +0 -55
  12. package/.github/release-notes/v1.3.3.md +0 -77
  13. package/.github/release-notes/v1.3.7.md +0 -20
  14. package/.github/workflows/ci.yml +0 -42
  15. package/.github/workflows/docs.yml +0 -107
  16. package/.github/workflows/release.yml +0 -107
  17. package/AGENT.md +0 -71
  18. package/API_REFERENCE.md +0 -341
  19. package/CHANGELOG.md +0 -173
  20. package/CODE_OF_CONDUCT.md +0 -14
  21. package/CONTRIBUTING.md +0 -27
  22. package/ROADMAP.md +0 -40
  23. package/assets/banner.png +0 -0
  24. package/assets/logo.png +0 -0
  25. package/benchmarks/ejs-results.json +0 -17
  26. package/benchmarks/ejs.js +0 -38
  27. package/benchmarks/handlebars-results.json +0 -17
  28. package/benchmarks/handlebars.js +0 -45
  29. package/benchmarks/miki-results.json +0 -17
  30. package/benchmarks/miki.js +0 -39
  31. package/benchmarks/pug-results.json +0 -17
  32. package/benchmarks/pug.js +0 -38
  33. package/benchmarks/report.json +0 -17
  34. package/benchmarks/run.js +0 -81
  35. package/benchmarks/stress.mjs +0 -647
  36. package/benchmarks/templates/large.dtpl +0 -7
  37. package/benchmarks/templates/medium.dtpl +0 -3
  38. package/benchmarks/templates/small.dtpl +0 -7
  39. package/context/component.md +0 -109
  40. package/context/prd.md +0 -131
  41. package/context/project-structure.md +0 -33
  42. package/dir/base.html +0 -23
  43. package/dir/cmpnt.html +0 -11
  44. package/dir/footer.html +0 -3
  45. package/dir/home.html +0 -80
  46. package/dir/index.html +0 -80
  47. package/dir/navbar.html +0 -9
  48. package/docs/api/async-render.md +0 -170
  49. package/docs/api/cache.md +0 -174
  50. package/docs/api/compile.md +0 -256
  51. package/docs/api/context-processors.md +0 -154
  52. package/docs/api/filters.md +0 -437
  53. package/docs/api/finder.md +0 -188
  54. package/docs/api/helpers.md +0 -106
  55. package/docs/api/i18n.md +0 -314
  56. package/docs/api/index.md +0 -108
  57. package/docs/api/libraries.md +0 -414
  58. package/docs/api/render-partial.md +0 -162
  59. package/docs/api/render.md +0 -184
  60. package/docs/api/security.md +0 -290
  61. package/docs/api/setup-express.md +0 -152
  62. package/docs/api/tags.md +0 -268
  63. package/docs/assets/banner.png +0 -0
  64. package/docs/assets/logo.png +0 -0
  65. package/docs/filter.md +0 -0
  66. package/docs/guide/advanced-usage.md +0 -794
  67. package/docs/guide/async-rendering.md +0 -616
  68. package/docs/guide/context-processors.md +0 -514
  69. package/docs/guide/custom-filters.md +0 -622
  70. package/docs/guide/custom-tags.md +0 -542
  71. package/docs/guide/filters.md +0 -1314
  72. package/docs/guide/getting-started.md +0 -204
  73. package/docs/guide/installation.md +0 -190
  74. package/docs/guide/partial-templates.md +0 -734
  75. package/docs/guide/quick-start.md +0 -444
  76. package/docs/guide/security.md +0 -690
  77. package/docs/guide/tags.md +0 -1566
  78. package/docs/guide/template-discovery.md +0 -340
  79. package/docs/guide/template-inheritance.md +0 -546
  80. package/docs/guide/what-is-miki-template.md +0 -28
  81. package/docs/guide/why-miki-template.md +0 -75
  82. package/docs/index.md +0 -86
  83. package/docs/integrations/elysia.md +0 -80
  84. package/docs/integrations/express.md +0 -219
  85. package/docs/integrations/fastify.md +0 -79
  86. package/docs/integrations/hono.md +0 -80
  87. package/docs/integrations/index.md +0 -68
  88. package/docs/integrations/koa.md +0 -90
  89. package/docs/integrations/nestjs.md +0 -80
  90. package/docs/integrations/tsed.md +0 -83
  91. package/docs/performance.md +0 -74
  92. package/eslint.config.mjs +0 -42
  93. package/ex.mjs +0 -36
  94. package/live-test/integrations/elysia-example.js +0 -16
  95. package/live-test/integrations/express-example.js +0 -24
  96. package/live-test/integrations/fastify-example.js +0 -20
  97. package/live-test/integrations/hono-example.js +0 -16
  98. package/live-test/integrations/koa-example.js +0 -30
  99. package/live-test/integrations/nestjs-example.js +0 -25
  100. package/live-test/integrations/smoke-test.js +0 -166
  101. package/live-test/integrations/tsed-example.js +0 -23
  102. package/live-test/package-lock.json +0 -1150
  103. package/live-test/package.json +0 -13
  104. package/live-test/packages/product/templates/product/detail.html +0 -7
  105. package/live-test/server.js +0 -38
  106. package/live-test/templates/app_templates/detail.html +0 -6
  107. package/live-test/views/base.html +0 -8
  108. package/live-test/views/child.html +0 -7
  109. package/live-test/views/home.html +0 -17
  110. package/live-test/views/index.html +0 -1
  111. package/miki-template-extension/.github/workflows/ci.yml +0 -116
  112. package/miki-template-extension/.vscodeignore +0 -7
  113. package/miki-template-extension/CHANGELOG.md +0 -99
  114. package/miki-template-extension/LICENSE +0 -21
  115. package/miki-template-extension/README.md +0 -273
  116. package/miki-template-extension/extension.js +0 -1013
  117. package/miki-template-extension/icon.png +0 -0
  118. package/miki-template-extension/icon.svg +0 -10
  119. package/miki-template-extension/miki-template-1.7.1.vsix +0 -0
  120. package/miki-template-extension/package.json +0 -280
  121. package/miki-template-extension/snippets/miki-template.json +0 -717
  122. package/miki-template-extension/syntaxes/language-configuration.json +0 -114
  123. package/miki-template-extension/syntaxes/miki-template.tmLanguage.json +0 -355
  124. package/miki-template-extension/tests/grammar-tests.json +0 -162
  125. package/miki-template-extension/tests/run-grammar-tests.js +0 -82
  126. package/mkdocs.yml +0 -195
  127. package/overrides/main.html +0 -26
  128. package/requirements-docs.txt +0 -2
  129. package/sample-app/package-lock.json +0 -901
  130. package/sample-app/package.json +0 -9
  131. package/sample-app/server.js +0 -14
  132. package/sample-app/views/index.html +0 -1
  133. package/scripts/build-vsix.js +0 -129
  134. package/scripts/build-vsix.ps1 +0 -15
  135. package/snippets/miki-template.json +0 -177
  136. package/src/asyncRender.js +0 -21
  137. package/src/types.d.ts +0 -107
  138. package/syntaxes/language-configuration.json +0 -26
  139. package/syntaxes/miki-template.tmLanguage.json +0 -146
  140. package/tests/asyncRender.test.js +0 -17
  141. package/tests/base.html +0 -6
  142. package/tests/child.html +0 -3
  143. package/tests/context_processors.test.js +0 -13
  144. package/tests/esm.test.mjs +0 -61
  145. package/tests/filters.test.js +0 -263
  146. package/tests/finder-appdirs.test.js +0 -19
  147. package/tests/finder.test.js +0 -17
  148. package/tests/fixtures/views/nested/index.html +0 -1
  149. package/tests/fixtures/views/partial.html +0 -1
  150. package/tests/fixtures/views/sub/deepfile.html +0 -1
  151. package/tests/fixtures/views-appdirs/product/site/detail.html +0 -1
  152. package/tests/include_security.test.js +0 -9
  153. package/tests/integration/README.md +0 -32
  154. package/tests/integration/features.test.cjs +0 -1681
  155. package/tests/integration/features.test.mjs +0 -1697
  156. package/tests/integration/finder.esm.test.mjs +0 -13
  157. package/tests/integration/partial-render.test.cjs +0 -13
  158. package/tests/integration/templates/base.miki +0 -6
  159. package/tests/integration/templates/child.miki +0 -6
  160. package/tests/integration/templates/index.html +0 -17
  161. package/tests/lexer.test.js +0 -45
  162. package/tests/parser.test.js +0 -57
  163. package/tests/partial.html +0 -1
  164. package/tests/partialdef.test.js +0 -79
  165. package/tests/production_checks.js +0 -57
  166. package/tests/security.test.js +0 -28
  167. 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,6 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <body>
4
- {% block content %}Default content from base{% endblock %}
5
- </body>
6
- </html>
@@ -1,6 +0,0 @@
1
- {% extends "base.miki" %}
2
- {% block content %}
3
- <h1>{{ heading }}</h1>
4
- <p>Welcome, {{ user.name }}!</p>
5
- {{ block.super }}
6
- {% endblock %}
@@ -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>
@@ -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
- });
@@ -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
- });
@@ -1 +0,0 @@
1
- <p>Included: {{ item }}</p>
@@ -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('&lt;script&gt;') ? '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'));
@@ -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, '&lt;script&gt;alert(&quot;xss&quot;)&lt;/script&gt;');
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, '&lt;b&gt;Hello&lt;/b&gt;');
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
- });
@@ -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
- });