Skip to content

Commit 173e468

Browse files
Copilotdg
andauthored
Fix CssInliner tokenizer crash on unquoted data: URLs containing parentheses
Agent-Logs-Url: https://github.com/nette/mail/sessions/040c9a82-923c-4f72-ab7f-3733ef611406 Co-authored-by: dg <194960+dg@users.noreply.github.com>
1 parent 18b5d0b commit 173e468

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/Mail/CssInliner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CssInliner
2222
self::T_Comment => '/\*[^*]*\*+(?:[^/*][^*]*\*+)*/',
2323
self::T_Whitespace => '[\s]+',
2424
self::T_String => '"(?:[^"\\\]|\\\.)*"|\'(?:[^\'\\\]|\\\.)*\'',
25-
self::T_Url => 'url\(\s*(?:"(?:[^"\\\]|\\\.)*"|\'(?:[^\'\\\]|\\\.)*\'|[^)]*?)\s*\)',
25+
self::T_Url => 'url\(\s*(?:"(?:[^"\\\]|\\\.)*"|\'(?:[^\'\\\]|\\\.)*\'|(?:[^()\\\]|\\\.|\((?:[^)\\\]|\\\.)*\))*)\s*\)',
2626
self::T_Escape => '\x5c[^\n\r\f]',
2727
self::T_AtIdent => '@-?[a-zA-Z_][\w-]*',
2828
self::T_Hash => '\#[\w-]+',

tests/Mail/CssInliner.phpt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,33 @@ test('data: URI in <style> tag', function () {
200200
});
201201

202202

203+
test('unquoted SVG data: URI with parentheses in content', function () {
204+
$result = (new CssInliner)
205+
->addCss("div { background-image: url(data:image/svg+xml,<svg><g transform='translate(50,50)'></g></svg>); }")
206+
->inline('<html><body><div>X</div></body></html>');
207+
208+
Assert::contains("background-image: url(data:image/svg+xml,<svg><g transform='translate(50,50)'></g></svg>)", $result);
209+
});
210+
211+
212+
test('unquoted SVG data: URI with rgba() in content', function () {
213+
$result = (new CssInliner)
214+
->addCss("div { background-image: url(data:image/svg+xml,<svg><rect fill='rgba(255,0,0,0.5)'/></svg>); }")
215+
->inline('<html><body><div>X</div></body></html>');
216+
217+
Assert::contains("background-image: url(data:image/svg+xml,<svg><rect fill='rgba(255,0,0,0.5)'/></svg>)", $result);
218+
});
219+
220+
221+
test('unquoted SVG data: URI with url() reference in content', function () {
222+
$result = (new CssInliner)
223+
->addCss("div { background-image: url(data:image/svg+xml,<svg><rect fill='url(#grad)'/></svg>); }")
224+
->inline('<html><body><div>X</div></body></html>');
225+
226+
Assert::contains("background-image: url(data:image/svg+xml,<svg><rect fill='url(#grad)'/></svg>)", $result);
227+
});
228+
229+
203230
test('braces inside string values in stylesheet', function () {
204231
$result = (new CssInliner)->inline(
205232
'<html><head><style>.a { content: "{hello}"; color: red; } .b { margin: 0; }</style></head><body><p class="b">X</p></body></html>',

0 commit comments

Comments
 (0)