fix: prevent PDF export crash on tables with nested quotes in inline styles - #86
fix: prevent PDF export crash on tables with nested quotes in inline styles#86Herafia wants to merge 5 commits into
Conversation
|
|
||
| // Remove table-layout:fixed style (prevents auto-sizing) | ||
| $html = preg_replace('/table-layout\s*:\s*fixed\s*;?/i', '', $html); | ||
| // Parse with DOMDocument rather than regexes: style/attribute values coming |
There was a problem hiding this comment.
Can you add a unit test for cleanTableHtml() with a nested-quote url('...') style value (case from !45825)? Verified locally that the old regex code produced a <table> with duplicate border/style attributes on this input, the actual crash mechanism, and that the new code doesn't; a test would pin the regression.
| use function Safe\realpath; | ||
|
|
||
| $current_plugin_folder = basename(realpath(__DIR__ . '/../')); |
There was a problem hiding this comment.
Please use pluginsGLPI/empty for example
| use function Safe\realpath; | |
| $current_plugin_folder = basename(realpath(__DIR__ . '/../')); | |
| $current_plugin_folder = basename(dirname(__DIR__)); |
| <phpunit bootstrap="tests/bootstrap.php" colors="true" testdox="true"> | ||
| <testsuites> | ||
| <testsuite name="Tests"> | ||
| <directory>tests</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
| </phpunit> |
There was a problem hiding this comment.
| <phpunit bootstrap="tests/bootstrap.php" colors="true" testdox="true"> | |
| <testsuites> | |
| <testsuite name="Tests"> | |
| <directory>tests</directory> | |
| </testsuite> | |
| </testsuites> | |
| </phpunit> | |
| <phpunit | |
| bootstrap="tests/bootstrap.php" | |
| colors="true" | |
| testdox="true" | |
| cacheDirectory="var/phpunit" | |
| > | |
| <source> | |
| <include> | |
| <directory>src</directory> | |
| </include> | |
| </source> | |
| <testsuites> | |
| <testsuite name="Tests"> | |
| <directory suffix="Test.php">tests</directory> | |
| </testsuite> | |
| </testsuites> | |
| </phpunit> |
And add var/ in .gitignore
| // Force table to 100% width for PDF (do this LAST) | ||
| $html = preg_replace('/<table([^>]*)>/i', '<table$1 style="width:100%">', $html, 1); | ||
| $kept = []; | ||
| foreach (explode(';', $style) as $declaration) { |
There was a problem hiding this comment.
explode(';', ...) also splits inside url(...), so a data URI is rebuilt with an extra space and becomes invalid: url(data:image/png;base64,X) comes out as url(data:image/png; base64,X).
| foreach (explode(';', $style) as $declaration) { | |
| foreach (preg_split('/;(?![^(]*\))/', $style) as $declaration) { |
| // which regex-based quote matching cannot handle reliably and ends up corrupting | ||
| // the markup fed to TCPDF (causing crashes on malformed HTML). | ||
| $dom = new DOMDocument(); | ||
| libxml_use_internal_errors(true); |
There was a problem hiding this comment.
libxml_use_internal_errors(true) is never restored, so libxml error reporting stays silenced for the rest of the request.
| libxml_use_internal_errors(true); | |
| $libxml_previous_state = libxml_use_internal_errors(true); |
and after libxml_clear_errors(); (line 481):
libxml_use_internal_errors($libxml_previous_state);
| foreach (iterator_to_array($dom->getElementsByTagName('table')) as $table) { | ||
| // Remove width/height (attributes and styles) on the table and its rows/cells | ||
| $xpath = new DOMXPath($dom); |
There was a problem hiding this comment.
DOMXPath is rebuilt on every table; hoist it out of the loop.
| foreach (iterator_to_array($dom->getElementsByTagName('table')) as $table) { | |
| // Remove width/height (attributes and styles) on the table and its rows/cells | |
| $xpath = new DOMXPath($dom); | |
| $xpath = new DOMXPath($dom); | |
| foreach (iterator_to_array($dom->getElementsByTagName('table')) as $table) { | |
| // Remove width/height (attributes and styles) on the table and its rows/cells |
Checklist before requesting a review
Please delete options that are not relevant.
Description
Fix: Replaced the regular expressions with DOMDocument/DOMXPath parsing in cleanTableHtml, which is robust against nested quotes.