miki-template 1.2.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.
Files changed (73) hide show
  1. package/.github/workflows/ci.yml +54 -0
  2. package/AGENT.md +71 -0
  3. package/API_REFERENCE.md +314 -0
  4. package/CHANGELOG.md +97 -0
  5. package/CODE_OF_CONDUCT.md +14 -0
  6. package/CONTRIBUTING.md +27 -0
  7. package/README.md +304 -0
  8. package/ROADMAP.md +40 -0
  9. package/benchmarks/report.json +17 -0
  10. package/benchmarks/run.js +49 -0
  11. package/benchmarks/templates/large.dtpl +7 -0
  12. package/benchmarks/templates/medium.dtpl +3 -0
  13. package/benchmarks/templates/small.dtpl +7 -0
  14. package/context/component.md +109 -0
  15. package/context/prd.md +131 -0
  16. package/context/project-structure.md +33 -0
  17. package/docs/README.md +18 -0
  18. package/docs/advanced_usage.md +71 -0
  19. package/docs/api.md +102 -0
  20. package/docs/filters.md +540 -0
  21. package/docs/installation.md +106 -0
  22. package/docs/overview.md +57 -0
  23. package/docs/partialdef.md +41 -0
  24. package/docs/security.md +27 -0
  25. package/docs/tags.md +610 -0
  26. package/docs/usage.md +599 -0
  27. package/eslint.config.mjs +34 -0
  28. package/miki-template-1.2.0.vsix +0 -0
  29. package/miki-template-extension/LICENSE +21 -0
  30. package/miki-template-extension/README.md +82 -0
  31. package/miki-template-extension/icon.png +0 -0
  32. package/miki-template-extension/icon.svg +10 -0
  33. package/miki-template-extension/package.json +46 -0
  34. package/miki-template-extension/snippets/miki-template.json +177 -0
  35. package/miki-template-extension/syntaxes/language-configuration.json +26 -0
  36. package/miki-template-extension/syntaxes/miki-template.tmLanguage.json +146 -0
  37. package/package.json +31 -0
  38. package/snippets/miki-template.json +177 -0
  39. package/src/asyncRender.js +21 -0
  40. package/src/cache.js +41 -0
  41. package/src/context.js +122 -0
  42. package/src/context_processors.js +41 -0
  43. package/src/esm.mjs +72 -0
  44. package/src/filters.js +527 -0
  45. package/src/i18n.js +171 -0
  46. package/src/index.js +454 -0
  47. package/src/lexer.js +92 -0
  48. package/src/libraries.js +240 -0
  49. package/src/parser.js +250 -0
  50. package/src/security.js +51 -0
  51. package/src/tags/control.js +591 -0
  52. package/src/tags/helpers.js +27 -0
  53. package/src/tags/i18n.js +230 -0
  54. package/src/tags/inheritance.js +216 -0
  55. package/src/tags/registry.js +18 -0
  56. package/src/tags/util.js +322 -0
  57. package/src/types.d.ts +107 -0
  58. package/syntaxes/language-configuration.json +26 -0
  59. package/syntaxes/miki-template.tmLanguage.json +146 -0
  60. package/tests/asyncRender.test.js +17 -0
  61. package/tests/base.html +6 -0
  62. package/tests/child.html +3 -0
  63. package/tests/context_processors.test.js +13 -0
  64. package/tests/esm.test.mjs +26 -0
  65. package/tests/filters.test.js +99 -0
  66. package/tests/include_security.test.js +9 -0
  67. package/tests/lexer.test.js +45 -0
  68. package/tests/parser.test.js +55 -0
  69. package/tests/partial.html +1 -0
  70. package/tests/partialdef.test.js +40 -0
  71. package/tests/production_checks.js +57 -0
  72. package/tests/security.test.js +28 -0
  73. package/tests/tags.test.js +203 -0
@@ -0,0 +1,99 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert');
3
+ const { getFilter } = require('../src/filters');
4
+
5
+ test('Filters - Text filters', () => {
6
+ const upper = getFilter('upper');
7
+ assert.strictEqual(upper('hello'), 'HELLO');
8
+
9
+ const capfirst = getFilter('capfirst');
10
+ assert.strictEqual(capfirst('hello'), 'Hello');
11
+
12
+ const truncatewords = getFilter('truncatewords');
13
+ assert.strictEqual(truncatewords('one two three four', 2), 'one two ...');
14
+
15
+ const slugify = getFilter('slugify');
16
+ assert.strictEqual(slugify('Hello World! New-Post'), 'hello-world-new-post');
17
+ });
18
+
19
+ test('Filters - HTML / Security filters', () => {
20
+ const safe = getFilter('safe');
21
+ const escape = getFilter('escape');
22
+
23
+ assert.strictEqual(String(escape('<script>')), '&lt;script&gt;');
24
+ // Safe returns SafeString which resolves when cast to string
25
+ assert.strictEqual(String(safe('<p>')), '<p>');
26
+ });
27
+
28
+ test('Filters - List filters', () => {
29
+ const length = getFilter('length');
30
+ assert.strictEqual(length([1, 2, 3]), 3);
31
+
32
+ const join = getFilter('join');
33
+ assert.strictEqual(join(['a', 'b', 'c'], '-'), 'a-b-c');
34
+
35
+ const slice = getFilter('slice');
36
+ assert.deepEqual(slice([1, 2, 3, 4], '1:3'), [2, 3]);
37
+ });
38
+
39
+ test('Filters - Default filters', () => {
40
+ const def = getFilter('default');
41
+ assert.strictEqual(def(null, 'fallback'), 'fallback');
42
+ assert.strictEqual(def('value', 'fallback'), 'value');
43
+ });
44
+
45
+ test('Filters - Misc filters', () => {
46
+ const pluralize = getFilter('pluralize');
47
+ assert.strictEqual(pluralize(1), '');
48
+ assert.strictEqual(pluralize(2), 's');
49
+ assert.strictEqual(pluralize(1, 'y,ies'), 'y');
50
+ assert.strictEqual(pluralize(2, 'y,ies'), 'ies');
51
+ });
52
+
53
+ test('Filters - urlencode and escapeuri', () => {
54
+ const urlencode = getFilter('urlencode');
55
+ assert.strictEqual(urlencode('hello world'), 'hello+world');
56
+ assert.strictEqual(urlencode('a b c'), 'a+b+c');
57
+
58
+ const escapeuri = getFilter('escapeuri');
59
+ assert.ok(escapeuri('http://example.com/path with spaces').includes('path%20with%20spaces'));
60
+ });
61
+
62
+ test('Filters - stringformat', () => {
63
+ const stringformat = getFilter('stringformat');
64
+ assert.strictEqual(stringformat('hello', '%s'), 'hello');
65
+ assert.strictEqual(stringformat(42, '%d'), '42');
66
+ assert.strictEqual(stringformat(3.14159, '%.2f'), '3.14');
67
+ });
68
+
69
+ test('Filters - cut and addslashes', () => {
70
+ const cut = getFilter('cut');
71
+ assert.strictEqual(cut('hello hello', ' '), 'hellohello');
72
+
73
+ const addslashes = getFilter('addslashes');
74
+ assert.strictEqual(addslashes('He said "Hi"'), 'He said \\"Hi\\"');
75
+ });
76
+
77
+ test('Filters - length_is', () => {
78
+ const length_is = getFilter('length_is');
79
+ assert.strictEqual(length_is([1, 2, 3], 3), true);
80
+ assert.strictEqual(length_is([1, 2], 3), false);
81
+ });
82
+
83
+ test('Filter chaining via template', () => {
84
+ const { render } = require('../src/index');
85
+ const result = render('{{ value|lower|capfirst }}', { value: 'Hello World' });
86
+ assert.strictEqual(result, 'Hello world');
87
+
88
+ const result2 = render('{{ items|length|add:5 }}', { items: [1, 2, 3] });
89
+ assert.strictEqual(result2, '8');
90
+ });
91
+
92
+ test('Filter chaining on string literal', () => {
93
+ const { render } = require('../src/index');
94
+ const result = render('{{ "Hello World"|lower|capfirst }}', {});
95
+ assert.strictEqual(result, 'Hello world');
96
+
97
+ const result2 = render('{{ " trim me "|cut:" " }}', {});
98
+ assert.strictEqual(result2, 'trimme');
99
+ });
@@ -0,0 +1,9 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert');
3
+ const { render } = require('../src/index');
4
+
5
+ test('Security - include path traversal prevented', () => {
6
+ const tpl = `{% include "../secret.html" %}`;
7
+ // Expect an error indicating path traversal
8
+ assert.throws(() => render(tpl, {}, { views: '.' }), /path traversal/);
9
+ });
@@ -0,0 +1,45 @@
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
+ });
@@ -0,0 +1,55 @@
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
+ assert.strictEqual(ctx.get('user.profile.missing'), '');
34
+ });
35
+
36
+ test('Context - scope pushing and popping', () => {
37
+ const ctx = new Context({ name: 'Global' });
38
+ ctx.push({ name: 'Local', age: 25 });
39
+
40
+ assert.strictEqual(ctx.get('name'), 'Local');
41
+ assert.strictEqual(ctx.get('age'), 25);
42
+
43
+ ctx.pop();
44
+ assert.strictEqual(ctx.get('name'), 'Global');
45
+ assert.strictEqual(ctx.get('age'), '');
46
+ });
47
+
48
+ test('Context - automatic function calling', () => {
49
+ const ctx = new Context({
50
+ user: {
51
+ getName: () => 'Dynamic Name'
52
+ }
53
+ });
54
+ assert.strictEqual(ctx.get('user.getName'), 'Dynamic Name');
55
+ });
@@ -0,0 +1 @@
1
+ <p>Included: {{ item }}</p>
@@ -0,0 +1,40 @@
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 { render, compile } = require('../src/index');
5
+
6
+ // Basic partialdef rendering via {% partial %}
7
+ test('Partialdef basic rendering', () => {
8
+ const tpl = `{% partialdef greeting %}Hello {{ name }}{% endpartialdef %}{% partial greeting %}`;
9
+ const out = render(tpl, { name: 'Miki' });
10
+ assert.strictEqual(out, 'Hello Miki');
11
+ });
12
+
13
+ // Partialdef with conditional logic
14
+ test('Partialdef with if/else', () => {
15
+ const tpl = `{% partialdef cond %}{% if show %}Visible{% else %}Hidden{% endif %}{% endpartialdef %}{% partial cond %}`;
16
+ assert.strictEqual(render(tpl, { show: true }), 'Visible');
17
+ assert.strictEqual(render(tpl, { show: false }), 'Hidden');
18
+ });
19
+
20
+ // Partialdef with loop and metadata
21
+ test('Partialdef with for loop', () => {
22
+ const tpl = `{% partialdef list %}{% for item in items %}{{ item }},{% empty %}none{% endfor %}{% endpartialdef %}{% partial list %}`;
23
+ assert.strictEqual(render(tpl, { items: ['a', 'b'] }), 'a,b,');
24
+ assert.strictEqual(render(tpl, { items: [] }), 'none');
25
+ });
26
+
27
+ // Inline partialdef (renders immediately)
28
+ test('Inline partialdef renders inline', () => {
29
+ const tpl = `{% partialdef inline_example inline %}Inline {{ val }}{% endpartialdef %}`;
30
+ const out = render(tpl, { val: 'X' });
31
+ assert.strictEqual(out, 'Inline X');
32
+ });
33
+
34
+ // Rendering a partial via compile.renderPartial API
35
+ test('renderPartial API works', () => {
36
+ const tpl = `{% partialdef api %}API {{ data }}{% endpartialdef %}`;
37
+ const compiled = compile(tpl);
38
+ const out = compiled.renderPartial('api', { data: 123 });
39
+ assert.strictEqual(out, 'API 123');
40
+ });
@@ -0,0 +1,57 @@
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'));
@@ -0,0 +1,28 @@
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
+ });
@@ -0,0 +1,203 @@
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
+ });