|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * HTML markup normalization tests for writeHTML(). |
| 7 | + * |
| 8 | + * @package com.tecnick.tcpdf |
| 9 | + */ |
| 10 | + |
| 11 | +require_once __DIR__ . '/TcpdfTestCase.php'; |
| 12 | + |
| 13 | +class TcpdfHtmlMarkupTest extends TcpdfTestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Run the protected markup normalization on the given HTML. |
| 17 | + */ |
| 18 | + private function normalize(string $html): string |
| 19 | + { |
| 20 | + $pdf = new class('P', 'mm', 'A4', true, 'UTF-8', false) extends TCPDF { |
| 21 | + public function normalize(string $html): string |
| 22 | + { |
| 23 | + return $this->normalizeHtmlMarkup($html); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + return $pdf->normalize($html); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * The test runs in a separate process: the JIT does not honour the backtrack |
| 32 | + * limit, and turning it off only affects patterns compiled afterwards. |
| 33 | + * The limits are restored before returning, as PHPUnit itself calls |
| 34 | + * preg_replace() after the test method. |
| 35 | + * |
| 36 | + * @mago-expect lint:no-ini-set |
| 37 | + */ |
| 38 | + #[\PHPUnit\Framework\Attributes\RunInSeparateProcess] |
| 39 | + #[\PHPUnit\Framework\Attributes\PreserveGlobalState(false)] |
| 40 | + public function testTableLargerThanBacktrackLimitIsKept(): void |
| 41 | + { |
| 42 | + $html = '<table border="1">'; |
| 43 | + for ($row = 1; $row <= 200; ++$row) { |
| 44 | + $html .= '<tr><td>Row ' . $row . '</td><td><img src="data:," align="middle"></td></tr>'; |
| 45 | + } |
| 46 | + |
| 47 | + $html .= '</table>'; |
| 48 | + |
| 49 | + $jit = (string) ini_get('pcre.jit'); |
| 50 | + $limit = (string) ini_get('pcre.backtrack_limit'); |
| 51 | + ini_set('pcre.jit', '0'); |
| 52 | + ini_set('pcre.backtrack_limit', '1000'); |
| 53 | + try { |
| 54 | + $out = $this->normalize($html); |
| 55 | + } finally { |
| 56 | + ini_set('pcre.backtrack_limit', $limit); |
| 57 | + ini_set('pcre.jit', $jit); |
| 58 | + } |
| 59 | + |
| 60 | + $this->assertSame(200, substr_count($out, '<tr>'), 'every table row must be kept'); |
| 61 | + $this->assertStringContainsString('Row 200</td>', $out); |
| 62 | + $this->assertStringEndsWith('</table>', $out); |
| 63 | + $this->assertStringNotContainsString('align=', $out); |
| 64 | + } |
| 65 | + |
| 66 | + public function testImageAlignIsDroppedOnlyInsideTables(): void |
| 67 | + { |
| 68 | + $out = $this->normalize( |
| 69 | + '<p><img src="data:," align="middle"></p>' |
| 70 | + . '<TABLE><tr><td>' |
| 71 | + . '<table><tr><td><img src="data:," align="top"></td></tr></table>' |
| 72 | + . '<img src="data:," align="bottom">' |
| 73 | + . '</td></tr></TABLE>' |
| 74 | + . '<img src="data:," align="baseline">' |
| 75 | + . '<tablex><img src="data:," align="top"></tablex>', |
| 76 | + ); |
| 77 | + |
| 78 | + $this->assertStringContainsString('<p><img src="data:," align="middle"></p>', $out); |
| 79 | + $this->assertStringContainsString('<td><img src="data:,"></td></tr></table><img src="data:,"></td>', $out); |
| 80 | + $this->assertStringContainsString('</TABLE><img src="data:," align="baseline">', $out); |
| 81 | + $this->assertStringContainsString('<tablex><img src="data:," align="top"></tablex>', $out); |
| 82 | + } |
| 83 | +} |
0 commit comments