|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: |json filter |
| 5 | + */ |
| 6 | + |
| 7 | +use Latte\Runtime\Helpers; |
| 8 | +use Tester\Assert; |
| 9 | + |
| 10 | +require __DIR__ . '/../bootstrap.php'; |
| 11 | + |
| 12 | + |
| 13 | +class MyHtmlStringable implements Latte\Runtime\HtmlStringable |
| 14 | +{ |
| 15 | + public function __toString(): string |
| 16 | + { |
| 17 | + return '<b>html</b>'; |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +class TestJsonSerializable implements \JsonSerializable |
| 23 | +{ |
| 24 | + public function jsonSerialize(): array |
| 25 | + { |
| 26 | + return ['key' => 'value']; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +test('scalars', function () { |
| 32 | + Assert::same('null', Helpers::encodeJson(null)); |
| 33 | + Assert::same('true', Helpers::encodeJson(true)); |
| 34 | + Assert::same('false', Helpers::encodeJson(false)); |
| 35 | + Assert::same('0', Helpers::encodeJson(0)); |
| 36 | + Assert::same('42', Helpers::encodeJson(42)); |
| 37 | + Assert::same('1.5', Helpers::encodeJson(1.5)); |
| 38 | + Assert::same('""', Helpers::encodeJson('')); |
| 39 | + Assert::same('"abc"', Helpers::encodeJson('abc')); |
| 40 | +}); |
| 41 | + |
| 42 | + |
| 43 | +test('arrays and objects', function () { |
| 44 | + Assert::same('[]', Helpers::encodeJson([])); |
| 45 | + Assert::same('[1,2,3]', Helpers::encodeJson([1, 2, 3])); |
| 46 | + Assert::same('{"a":1,"b":2}', Helpers::encodeJson(['a' => 1, 'b' => 2])); |
| 47 | + Assert::same('{"a":1}', Helpers::encodeJson((object) ['a' => 1])); |
| 48 | + Assert::same('{"key":"value"}', Helpers::encodeJson(new TestJsonSerializable)); |
| 49 | +}); |
| 50 | + |
| 51 | + |
| 52 | +test('UTF-8 preserved', function () { |
| 53 | + Assert::same('"čau"', Helpers::encodeJson('čau')); |
| 54 | + Assert::same('"日本"', Helpers::encodeJson('日本')); |
| 55 | +}); |
| 56 | + |
| 57 | + |
| 58 | +test('slashes unescaped', function () { |
| 59 | + Assert::same('"a/b"', Helpers::encodeJson('a/b')); |
| 60 | + Assert::same('"http://example.com"', Helpers::encodeJson('http://example.com')); |
| 61 | +}); |
| 62 | + |
| 63 | + |
| 64 | +test('dangerous sequences escaped', function () { |
| 65 | + Assert::same('"]]\u003E"', Helpers::encodeJson(']]>')); |
| 66 | + Assert::same('"\u003C!--"', Helpers::encodeJson('<!--')); |
| 67 | + Assert::same('"<\/script>"', Helpers::encodeJson('</script>')); |
| 68 | +}); |
| 69 | + |
| 70 | + |
| 71 | +test('HtmlStringable is NOT unwrapped (unlike escapeJs)', function () { |
| 72 | + // object with only private/empty public props serializes to {} |
| 73 | + Assert::same('{}', Helpers::encodeJson(new MyHtmlStringable)); |
| 74 | + // escapeJs for comparison unwraps to string |
| 75 | + Assert::same('"<b>html<\/b>"', Helpers::escapeJs(new MyHtmlStringable)); |
| 76 | +}); |
| 77 | + |
| 78 | + |
| 79 | +test('via Latte: HTML text context', function () { |
| 80 | + // { escaped to {, " kept literal (ENT_NOQUOTES) |
| 81 | + Assert::same( |
| 82 | + '{"a":1}', |
| 83 | + createLatte()->renderToString('{$arr|json}', ['arr' => ['a' => 1]]), |
| 84 | + ); |
| 85 | +}); |
| 86 | + |
| 87 | + |
| 88 | +test('via Latte: JS context', function () { |
| 89 | + $latte = createLatte(); |
| 90 | + |
| 91 | + // auto-escapeJs wraps |json output as a JS string literal |
| 92 | + Assert::same( |
| 93 | + '<script>var x = "{\"a\":1}";</script>', |
| 94 | + $latte->renderToString('<script>var x = {$arr|json};</script>', ['arr' => ['a' => 1]]), |
| 95 | + ); |
| 96 | + // use |noescape to get raw JSON in JS context |
| 97 | + Assert::same( |
| 98 | + '<script>var x = {"a":1};</script>', |
| 99 | + $latte->renderToString('<script>var x = {$arr|json|noescape};</script>', ['arr' => ['a' => 1]]), |
| 100 | + ); |
| 101 | +}); |
| 102 | + |
| 103 | + |
| 104 | +test('via Latte: HTML attribute context', function () { |
| 105 | + $latte = createLatte(); |
| 106 | + |
| 107 | + // array -> single quotes because JSON contains " |
| 108 | + Assert::same( |
| 109 | + '<meta content=\'{"a":1}\'>', |
| 110 | + $latte->renderToString('<meta content={$arr|json}>', ['arr' => ['a' => 1]]), |
| 111 | + ); |
| 112 | + // string -> JSON-encoded as "abc", wrapped in single quotes |
| 113 | + Assert::same( |
| 114 | + '<meta content=\'"abc"\'>', |
| 115 | + $latte->renderToString('<meta content={$str|json}>', ['str' => 'abc']), |
| 116 | + ); |
| 117 | + // number -> JSON literal, wrapped in double quotes (no " in JSON) |
| 118 | + Assert::same( |
| 119 | + '<meta content="42">', |
| 120 | + $latte->renderToString('<meta content={$n|json}>', ['n' => 42]), |
| 121 | + ); |
| 122 | + // bool -> JSON literal true |
| 123 | + Assert::same( |
| 124 | + '<meta content="true">', |
| 125 | + $latte->renderToString('<meta content={$b|json}>', ['b' => true]), |
| 126 | + ); |
| 127 | + // null -> JSON literal null |
| 128 | + Assert::same( |
| 129 | + '<meta content="null">', |
| 130 | + $latte->renderToString('<meta content={$v|json}>', ['v' => null]), |
| 131 | + ); |
| 132 | + // float |
| 133 | + Assert::same( |
| 134 | + '<meta content="1.5">', |
| 135 | + $latte->renderToString('<meta content={$v|json}>', ['v' => 1.5]), |
| 136 | + ); |
| 137 | +}); |
| 138 | + |
| 139 | + |
| 140 | +test('via Latte: |json overrides special-attribute handling', function () { |
| 141 | + $latte = createLatte(); |
| 142 | + |
| 143 | + // class: without |json -> list attribute (space-joined) |
| 144 | + Assert::same( |
| 145 | + '<div class="a b"></div>', |
| 146 | + $latte->renderToString('<div class={$cls}></div>', ['cls' => ['a', 'b']]), |
| 147 | + ); |
| 148 | + // class: with |json -> JSON array |
| 149 | + Assert::same( |
| 150 | + '<div class=\'["a","b"]\'></div>', |
| 151 | + $latte->renderToString('<div class={$cls|json}></div>', ['cls' => ['a', 'b']]), |
| 152 | + ); |
| 153 | + // aria-*: with |json -> JSON-encoded |
| 154 | + Assert::same( |
| 155 | + '<div aria-x=\'{"a":1}\'></div>', |
| 156 | + $latte->renderToString('<div aria-x={$arr|json}></div>', ['arr' => ['a' => 1]]), |
| 157 | + ); |
| 158 | +}); |
| 159 | + |
| 160 | + |
| 161 | +test('via Latte: |json in event handler attribute', function () { |
| 162 | + // onclick is JS context, but |json detection fires first -> raw JSON literal |
| 163 | + Assert::same( |
| 164 | + '<div onclick=\'{"a":1}\'></div>', |
| 165 | + createLatte()->renderToString('<div onclick={$arr|json}></div>', ['arr' => ['a' => 1]]), |
| 166 | + ); |
| 167 | +}); |
| 168 | + |
| 169 | + |
| 170 | +test('via Latte: |json in XML template', function () { |
| 171 | + $latte = createLatte(); |
| 172 | + $latte->setContentType(Latte\ContentType::Xml); |
| 173 | + |
| 174 | + // XML: no compile-time detection, |json runs as regular filter and output is XML-escaped |
| 175 | + Assert::same( |
| 176 | + '<meta content="{"a":1}"/>', |
| 177 | + $latte->renderToString('<meta content={$arr|json}/>', ['arr' => ['a' => 1]]), |
| 178 | + ); |
| 179 | +}); |
| 180 | + |
| 181 | + |
| 182 | +test('via Latte: data-* attribute', function () { |
| 183 | + $latte = createLatte(); |
| 184 | + |
| 185 | + // data-x without |json: auto JSON for arrays |
| 186 | + Assert::same( |
| 187 | + '<div data-x=\'{"a":1}\'></div>', |
| 188 | + $latte->renderToString('<div data-x={$arr}></div>', ['arr' => ['a' => 1]]), |
| 189 | + ); |
| 190 | + // data-x with |json for array: same result |
| 191 | + Assert::same( |
| 192 | + '<div data-x=\'{"a":1}\'></div>', |
| 193 | + $latte->renderToString('<div data-x={$arr|json}></div>', ['arr' => ['a' => 1]]), |
| 194 | + ); |
| 195 | + // data-x without |json: string passthrough |
| 196 | + Assert::same( |
| 197 | + '<div data-x="hello"></div>', |
| 198 | + $latte->renderToString('<div data-x={$str}></div>', ['str' => 'hello']), |
| 199 | + ); |
| 200 | + // data-x with |json: string JSON-encoded as "hello" |
| 201 | + Assert::same( |
| 202 | + '<div data-x=\'"hello"\'></div>', |
| 203 | + $latte->renderToString('<div data-x={$str|json}></div>', ['str' => 'hello']), |
| 204 | + ); |
| 205 | +}); |
| 206 | + |
| 207 | + |
| 208 | +test('via Latte: chain with other filter', function () { |
| 209 | + $latte = createLatte(); |
| 210 | + |
| 211 | + // |json is last -> compile-time detection fires, |upper runs first, then formatJsonAttribute |
| 212 | + Assert::same( |
| 213 | + '<meta content=\'"HELLO"\'>', |
| 214 | + $latte->renderToString('<meta content={$s|upper|json}>', ['s' => 'hello']), |
| 215 | + ); |
| 216 | + // |json is NOT last -> detection skipped, both filters run as regular and output is escaped by formatAttribute |
| 217 | + Assert::same( |
| 218 | + '<meta content=""HELLO"">', |
| 219 | + $latte->renderToString('<meta content={$s|json|upper}>', ['s' => 'hello']), |
| 220 | + ); |
| 221 | +}); |
0 commit comments