Skip to content

Commit 8740fc4

Browse files
Merge branch '6.4' into 7.3
* 6.4: [GitHub] Update .github/PULL_REQUEST_TEMPLATE.md to remove SF 7.2 as it's not supported anymore [WebProfilerBundle] Fix toolbar not rendering after replacing it [HtmlSanitizer] Fix force_attributes not replacing existing attribute in initial data
2 parents 3388e20 + e080770 commit 8740fc4

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Tests/HtmlSanitizerCustomTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,17 @@ public function testForceAttribute()
232232
{
233233
$config = (new HtmlSanitizerConfig())
234234
->allowElement('div')
235+
->allowElement('img', '*')
235236
->allowElement('a', ['href'])
236237
->forceAttribute('a', 'rel', 'noopener noreferrer')
238+
->forceAttribute('img', 'loading', 'lazy')
237239
;
238240

241+
$this->assertSame(
242+
'<img title="My image" src="https://example.com/image.png" loading="lazy" />',
243+
$this->sanitize($config, '<img title="My image" src="https://example.com/image.png" loading="eager" onerror="alert(\'1234\')" />')
244+
);
245+
239246
$this->assertSame(
240247
'<a rel="noopener noreferrer">Hello</a> world',
241248
$this->sanitize($config, '<a>Hello</a> world')
@@ -250,6 +257,11 @@ public function testForceAttribute()
250257
'<div>Hello</div> world',
251258
$this->sanitize($config, '<div style="width: 100px">Hello</div> world')
252259
);
260+
261+
$this->assertSame(
262+
'<a href="https://symfony.com" rel="noopener noreferrer">Hello</a> world',
263+
$this->sanitize($config, '<a href="https://symfony.com" rel="noopener">Hello</a> world')
264+
);
253265
}
254266

255267
public function testForceHttps()

Visitor/DomVisitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private function enterNode(string $domNodeName, \DOMNode $domNode, Cursor $curso
129129

130130
// Force configured attributes
131131
foreach ($this->forcedAttributes[$domNodeName] ?? [] as $attribute => $value) {
132-
$node->setAttribute($attribute, $value);
132+
$node->setAttribute($attribute, $value, true);
133133
}
134134

135135
$cursor->node->addChild($node);

Visitor/Node/Node.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public function getAttribute(string $name): ?string
5656
return $this->attributes[$name] ?? null;
5757
}
5858

59-
public function setAttribute(string $name, ?string $value): void
59+
public function setAttribute(string $name, ?string $value, bool $override = false): void
6060
{
6161
// Always use only the first declaration (ease sanitization)
62-
if (!\array_key_exists($name, $this->attributes)) {
62+
if ($override || !\array_key_exists($name, $this->attributes)) {
6363
$this->attributes[$name] = $value;
6464
}
6565
}

0 commit comments

Comments
 (0)