Skip to content
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

Open
wants to merge 6 commits into
base: development
Choose a base branch
from

Conversation

MarcelBolten
Copy link

See #1404

- Add Test
- Update CHANGELOG.md
@MarcelBolten
Copy link
Author

@finwe is there anything else I can do to get this merged?
Guidance is very welcome.

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);
Copy link
Member

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.

Copy link
Author

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

  1. single or double quoted class string
  2. single quote in double quoted class string and vice versa
  3. escaped double quote in double quoted class string
  4. escaped single quote in single quoted class string and
  5. 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);

@MarcelBolten MarcelBolten force-pushed the svgClass2img branch 2 times, most recently from 7f89858 to 6b42330 Compare April 14, 2021 07:04
@MarcelBolten
Copy link
Author

Hi @finwe,
It has been some time since I made the requested changes. What do you think about the altered regex?
If the regex is to complicated a simpler approach could be to use a static class name like:

$html = str_replace($svgi[0][$i], '<img src="' . $file . '" class="mpdf-embedded-svg" />', $html);

@MarcelBolten
Copy link
Author

Hi @finwe ,

Do you have some comment on this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants