Skip to content

Commit a2c96c3

Browse files
authored
Merge branch 'twigphp:3.x' into 3.x
2 parents a8b428d + d26e8f2 commit a2c96c3

File tree

5 files changed

+104
-11
lines changed

5 files changed

+104
-11
lines changed

doc/filters/escape.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ documents:
5353
* ``url``: escapes a string for the **URI or parameter** contexts. This should
5454
not be used to escape an entire URI; only a subcomponent being inserted.
5555

56-
* ``html_attr``: escapes a string for the **HTML attribute** context,
57-
**without quotes** around HTML attribute values.
56+
* ``html_attr``: escapes a string when used as an **HTML attribute** name, and
57+
also when used as the value of an HTML attribute **without quotes**
58+
(e.g. ``data-attribute={{ some_value }}``).
5859

5960
Note that doing contextual escaping in HTML documents is hard and choosing the
6061
right escaping strategy depends on a lot of factors. Please, read related
@@ -96,23 +97,22 @@ to learn more about this topic.
9697
9798
.. tip::
9899

99-
The ``html_attr`` escaping strategy can be useful when you need to
100-
escape a **dynamic HTML attribute name**:
100+
The ``html_attr`` escaping strategy can be useful when you need to escape a
101+
**dynamic HTML attribute name**:
101102

102103
.. code-block:: html+twig
103104

104105
<p {{ your_html_attr|e('html_attr') }}="attribute value">
105106

106-
It can also be used for escaping a **dynamic HTML attribute value**
107-
if it is not quoted, but this is **less performant**.
108-
Instead, it is recommended to quote the HTML attribute value and use
109-
the ``html`` escaping strategy:
107+
It can also be used for escaping a **dynamic HTML attribute value** if it is
108+
not quoted, but this is **less performant**. Instead, it is recommended to
109+
quote the HTML attribute value and use the ``html`` escaping strategy:
110110

111111
.. code-block:: html+twig
112112

113113
<p data-content="{{ content|e('html') }}">
114114

115-
{# is equivalent to, but is less performant #}
115+
{# this is equivalent, but less performant #}
116116
<p data-content={{ content|e('html_attr') }}>
117117

118118
Custom Escapers

doc/filters/inky_to_html.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
================
33

44
The ``inky_to_html`` filter processes an `inky email template
5-
<https://github.com/zurb/inky>`_:
5+
<https://github.com/foundation/inky>`_:
66

77
.. code-block:: html+twig
88

doc/filters/replace.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ format is free-form):
66

77
.. code-block:: twig
88
9+
{% set fruit = 'apples' %}
10+
911
{{ "I like %this% and %that%."|replace({'%this%': fruit, '%that%': "oranges"}) }}
1012
{# if the "fruit" variable is set to "apples", #}
1113
{# it outputs "I like apples and oranges" #}

src/Node/EmbedNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function addGetTemplate(Compiler $compiler, string $template = ''): vo
4141
->raw(', ')
4242
->repr($this->getTemplateLine())
4343
->raw(', ')
44-
->string($this->getAttribute('index'))
44+
->repr($this->getAttribute('index'))
4545
->raw(')')
4646
;
4747
if ($this->getAttribute('ignore_missing')) {

tests/Node/EmbedTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Twig.
5+
*
6+
* (c) Fabien Potencier
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Twig\Tests\Node;
13+
14+
use Twig\Node\EmbedNode;
15+
use Twig\Node\Expression\ArrayExpression;
16+
use Twig\Node\Expression\ConstantExpression;
17+
use Twig\Test\NodeTestCase;
18+
19+
class EmbedTest extends NodeTestCase
20+
{
21+
public function testConstructor()
22+
{
23+
$node = new EmbedNode('foo.twig', 0, null, false, false, 1);
24+
25+
$this->assertFalse($node->hasNode('variables'));
26+
$this->assertEquals('foo.twig', $node->getAttribute('name'));
27+
$this->assertEquals(0, $node->getAttribute('index'));
28+
$this->assertFalse($node->getAttribute('only'));
29+
$this->assertFalse($node->getAttribute('ignore_missing'));
30+
31+
$vars = new ArrayExpression([new ConstantExpression('foo', 1), new ConstantExpression(true, 1)], 1);
32+
$node = new EmbedNode('bar.twig', 1, $vars, true, false, 1);
33+
$this->assertEquals($vars, $node->getNode('variables'));
34+
$this->assertTrue($node->getAttribute('only'));
35+
$this->assertEquals('bar.twig', $node->getAttribute('name'));
36+
$this->assertEquals(1, $node->getAttribute('index'));
37+
}
38+
39+
public static function provideTests(): iterable
40+
{
41+
$tests = [];
42+
43+
$node = new EmbedNode('foo.twig', 0, null, false, false, 1);
44+
$tests[] = [$node, <<<'EOF'
45+
// line 1
46+
yield from $this->load("foo.twig", 1, 0)->unwrap()->yield($context);
47+
EOF
48+
];
49+
50+
$node = new EmbedNode('foo.twig', 1, null, false, false, 1);
51+
$tests[] = [$node, <<<'EOF'
52+
// line 1
53+
yield from $this->load("foo.twig", 1, 1)->unwrap()->yield($context);
54+
EOF
55+
];
56+
57+
$vars = new ArrayExpression([new ConstantExpression('foo', 1), new ConstantExpression(true, 1)], 1);
58+
$node = new EmbedNode('foo.twig', 0, $vars, false, false, 1);
59+
$tests[] = [$node, <<<'EOF'
60+
// line 1
61+
yield from $this->load("foo.twig", 1, 0)->unwrap()->yield(CoreExtension::merge($context, ["foo" => true]));
62+
EOF
63+
];
64+
65+
$node = new EmbedNode('foo.twig', 0, $vars, true, false, 1);
66+
$tests[] = [$node, <<<'EOF'
67+
// line 1
68+
yield from $this->load("foo.twig", 1, 0)->unwrap()->yield(CoreExtension::toArray(["foo" => true]));
69+
EOF
70+
];
71+
72+
$node = new EmbedNode('foo.twig', 2, $vars, true, true, 1);
73+
$tests[] = [$node, <<<EOF
74+
// line 1
75+
try {
76+
\$_v0 = \$this->load("foo.twig", 1, 2);
77+
\$_v0->getParent(\$context);
78+
;
79+
} catch (LoaderError \$e) {
80+
// ignore missing template
81+
\$_v0 = null;
82+
}
83+
if (\$_v0) {
84+
yield from \$_v0->unwrap()->yield(CoreExtension::toArray(["foo" => true]));
85+
}
86+
EOF
87+
];
88+
89+
return $tests;
90+
}
91+
}

0 commit comments

Comments
 (0)