-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retain class attribute of embedded svg element during conversion to img element #1405
base: development
Are you sure you want to change the base?
Conversation
- Add Test - Update CHANGELOG.md
e9ee59e
to
d8cca24
Compare
@finwe is there anything else I can do to get this merged? |
src/Mpdf.php
Outdated
@@ -26933,7 +26933,8 @@ function AdjustHTML($html, $tabSpaces = 8) | |||
if (count($svgi[0])) { | |||
for ($i = 0; $i < count($svgi[0]); $i++) { | |||
$file = $this->cache->write('/_tempSVG' . uniqid(random_int(1, 100000), true) . '_' . $i . '.svg', $svgi[0][$i]); | |||
$html = str_replace($svgi[0][$i], '<img src="' . $file . '" />', $html); | |||
preg_match("/class=['\"].*['\"]/iU", $svgi[0][$i], $class); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work with single quote in double quoted class string or vice versa, a backreference of the first quote is needed. It also will not work with unquoted class string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the regex (please see https://regex101.com/r/UK6HvN/3 for more details about the regex).
It will now only match the class attribute of the svg element and it can handle
- single or double quoted class string
- single quote in double quoted class string and vice versa
- escaped double quote in double quoted class string
- escaped single quote in single quoted class string and
- unquoted class string
However, 3, 4, and 5 did not work for me
[php:error] PHP Fatal error: Uncaught DivisionByZeroError: Division by zero in mpdf/mpdf/src/Tag/Img.php:372
Stack trace:
#0 mpdf/mpdf/src/Tag.php(240): Mpdf\\Tag\\Img->open()
#1 mpdf/mpdf/src/Mpdf.php(13771): Mpdf\\Tag->OpenTag()
Mpdf\\Mpdf->WriteHTML()
due to an issue not related to this PR and if I get it right unquoted class strings don't seem to be anticipated if I look at e.g. src/Image/Svg.php#L3131 but maybe (most likely) I miss something here.
function ImageSVG($data)
{
...
preg_match_all('/(<(linearGradient|radialgradient)[^>]*)xlink:href\s*=\s*["\']#(.*?)["\'](.*?)\/>/si', $data, $links);
7f89858
to
6b42330
Compare
Hi @finwe, $html = str_replace($svgi[0][$i], '<img src="' . $file . '" class="mpdf-embedded-svg" />', $html); |
Hi @finwe , Do you have some comment on this PR? |
See #1404