• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / latte / 28710508693

04 Jul 2026 03:15PM UTC coverage: 94.108% (-0.8%) from 94.946%
28710508693

push

github

dg
added |json filter with context-aware encoding

New |json filter (Helpers::encodeJson(), refactored out of escapeJs()
without the HtmlStringable unwrap branch). As the last filter in a chain
it carries a special, context-dependent meaning:

- HTML attribute as a whole value (via ExpressionAttributeNode), e.g.
  `data-x={$arr|json}`, routes to HtmlHelpers::formatJsonAttribute(),
  the same JSON + smart-quoting mechanism used by formatDataAttribute().
- JavaScript context (a <script> element, an on* attribute, or a JS
  content-type template, detected via Escaper::isJavaScript()) drops
  |json in PrintNode: escapeJs() already encodes to JSON there, so the
  filter is redundant and would otherwise double-encode. Dropped only
  when auto-escaping applies, so |json|noescape keeps it.
- Everywhere else (HTML text, XML, ...) |json runs as a regular filter
  and its output passes through the normal escaper, so '<', '>', '&' and
  quotes stay escaped.

Not being last, |json is a plain filter with no special handling. Both
nodes special-case it without mutating the AST: they clone the modifier
before removing the filter.

29 of 29 new or added lines in 6 files covered. (100.0%)

11 existing lines in 5 files now uncovered.

6021 of 6398 relevant lines covered (94.11%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.33
/src/Latte/Extension.php
1
<?php declare(strict_types=1);
1✔
2

3
/**
4
 * This file is part of the Latte (https://latte.nette.org)
5
 * Copyright (c) 2008 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace Latte;
9

10
use function get_defined_vars;
11

12

13
/**
14
 * Latte extension.
15
 */
16
abstract class Extension
17
{
18
        /**
19
         * Initializes before template is compiled.
20
         */
21
        public function beforeCompile(Engine $engine): void
1✔
22
        {
23
        }
1✔
24

25

26
        /**
27
         * Returns a list of parsers for Latte tags.
28
         * @return array<string, callable(Compiler\Tag, Compiler\TemplateParser): (Compiler\Node|\Generator|void)|\stdClass>
29
         */
30
        public function getTags(): array
31
        {
UNCOV
32
                return [];
×
33
        }
34

35

36
        /**
37
         * Returns a list of compiler passes.
38
         * @return array<string, callable(Compiler\Nodes\TemplateNode): void|\stdClass>
39
         */
40
        public function getPasses(): array
41
        {
42
                return [];
1✔
43
        }
44

45

46
        /**
47
         * Returns a list of |filters.
48
         * @return array<string, callable>
49
         */
50
        public function getFilters(): array
51
        {
52
                return [];
1✔
53
        }
54

55

56
        /**
57
         * Returns a list of functions used in templates.
58
         * @return array<string, callable>
59
         */
60
        public function getFunctions(): array
61
        {
62
                return [];
1✔
63
        }
64

65

66
        /**
67
         * Returns a list of providers.
68
         * @return array<string, mixed>
69
         */
70
        public function getProviders(): array
71
        {
72
                return [];
1✔
73
        }
74

75

76
        /**
77
         * Returns a value that invalidates the template cache when the extension configuration changes.
78
         */
79
        public function getCacheKey(Engine $engine): mixed
1✔
80
        {
81
                return null;
1✔
82
        }
83

84

85
        /**
86
         * Initializes before template is rendered.
87
         */
88
        public function beforeRender(Runtime\Template $template): void
1✔
89
        {
90
        }
1✔
91

92

93
        /**
94
         * Wraps callable with ordering metadata for tags and passes.
95
         * @param  array<string>|string  $before
96
         * @param  array<string>|string  $after
97
         */
98
        public static function order(callable $subject, array|string $before = [], array|string $after = []): \stdClass
1✔
99
        {
100
                $subject = $subject(...);
1✔
101
                return (object) get_defined_vars();
1✔
102
        }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc