LibRssTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. final class LibRssTest extends \PHPUnit\Framework\TestCase {
  5. #[DataProvider('provideLazyimg')]
  6. public function test_lazyimg(string $content, string $expected): void {
  7. self::assertSame($expected, lazyimg($content));
  8. }
  9. public function test_lazyimg_regex_failure_preserves_content(): void {
  10. // Simulate the failure of PCRE on potentially large documents, e.g. because of backtracking limits
  11. $limit = ini_get('pcre.backtrack_limit');
  12. self::assertIsString($limit);
  13. ini_set('pcre.backtrack_limit', '1');
  14. try {
  15. $input = '<video poster="p.jpg"><img src="a.png"></video>';
  16. self::assertSame($input, lazyimg($input));
  17. } finally {
  18. ini_set('pcre.backtrack_limit', $limit);
  19. }
  20. }
  21. public function test_lazyimg_overlong_tags_are_skipped(): void {
  22. // Tags with more than 1000 characters of attributes are expected to be skipped (eager loading)
  23. $overlong = str_repeat('a', 1001);
  24. self::assertSame('<img alt="' . $overlong . '" src="a.png">', lazyimg('<img alt="' . $overlong . '" src="a.png">'));
  25. self::assertSame('<video data-x="' . $overlong . '" poster="p.jpg"></video>',
  26. lazyimg('<video data-x="' . $overlong . '" poster="p.jpg"></video>'));
  27. // Under the limit, the work is still bounded and lazy loading is applied
  28. $atLimit = str_repeat('a', 980); // 6 + 980 + 13 = 999 characters of attributes
  29. $placeholder = Minz_Url::display('/themes/icons/grey.gif');
  30. self::assertSame('<img loading="lazy" alt="' . $atLimit . '" src="a.png">',
  31. lazyimg('<img alt="' . $atLimit . '" src="a.png">'));
  32. self::assertSame('<video data-x="' . $atLimit . '" poster="' . $placeholder . '" data-original="p.jpg"></video>',
  33. lazyimg('<video data-x="' . $atLimit . '" poster="p.jpg"></video>'));
  34. }
  35. /** @return array<string,array{string,string}> */
  36. public static function provideLazyimg(): array {
  37. $placeholder = Minz_Url::display('/themes/icons/grey.gif');
  38. return [
  39. 'ordinary image' => ['<img src="a.png">', '<img loading="lazy" src="a.png">'],
  40. 'multiple tags' => ['<img src="1.png"><iframe src="2.html" width="9"></iframe>',
  41. '<img loading="lazy" src="1.png"><iframe loading="lazy" src="2.html" width="9"></iframe>'],
  42. 'video poster' => ['<video poster="p.jpg"></video>', '<video poster="' . $placeholder . '" data-original="p.jpg"></video>'],
  43. 'video poster with other attributes' => ['<video poster="p.jpg" width="9"></video>',
  44. '<video poster="' . $placeholder . '" data-original="p.jpg" width="9"></video>'],
  45. 'video without poster untouched' => ['<video src="v.mp4"></video>', '<video src="v.mp4"></video>'],
  46. 'track element' => ['<track src="t.vtt">', '<track src="' . $placeholder . '" data-original="t.vtt">'],
  47. 'video and track elements' => ['<video poster="p.jpg" width="9"><track src="t.vtt" srclang="en"></video>',
  48. '<video poster="' . $placeholder . '" data-original="p.jpg" width="9">'
  49. . '<track src="' . $placeholder . '" data-original="t.vtt" srclang="en"></video>'],
  50. 'track data-src is not rewritten' => ['<track data-src="x.vtt">', '<track data-src="x.vtt">'],
  51. 'poster inside attribute value untouched' => ['<video title="poster=" src="v.mp4"></video>',
  52. '<video title="poster=" src="v.mp4"></video>'],
  53. 'legacy video data-original untouched' => ['<video data-original="p.jpg" poster="p.jpg"></video>',
  54. '<video data-original="p.jpg" poster="p.jpg"></video>'],
  55. 'case insensitive' => ['<IMG SRC="A.PNG">', '<IMG loading="lazy" SRC="A.PNG">'],
  56. 'already lazy' => ['<img loading="lazy" src="a.png">', '<img loading="lazy" src="a.png">'],
  57. 'deliberate eager opt-out kept' => ['<img loading="eager" src="a.png">', '<img loading="eager" src="a.png">'],
  58. 'legacy data-original untouched' => ['<iframe data-original="x.pdf" src="alias.pdf">',
  59. '<iframe data-original="x.pdf" src="alias.pdf">'],
  60. 'tag name boundary' => ['<imgish src="a.png">', '<imgish src="a.png">'],
  61. 'end tag untouched' => ['</img src="a.png">', '</img src="a.png">'],
  62. 'iframe data-src is not rewritten' => ['<iframe data-src="javascript:x()">', '<iframe loading="lazy" data-src="javascript:x()">'],
  63. 'src inside a title value' => [
  64. '<iframe title="src=" data-x=" srcdoc=\'<p>doc</p>\' sandbox=\'allow-scripts allow-same-origin allow-forms\' " src="doc.pdf">',
  65. '<iframe loading="lazy" title="src=" data-x=" srcdoc=\'<p>doc</p>\' sandbox=\'allow-scripts allow-same-origin allow-forms\' " src="doc.pdf">',
  66. ],
  67. 'value holding a comment and a tag' => [
  68. '<img alt="src=" title=" <!-- ><a href=x>t</a><i title=--> " src="u.png">',
  69. '<img loading="lazy" alt="src=" title=" <!-- ><a href=x>t</a><i title=--> " src="u.png">',
  70. ],
  71. 'single quote look-alike' => [
  72. '<img alt=\'" src=\' title="x\' onerror=\'e()\' data-x=" src="u.png">',
  73. '<img loading="lazy" alt=\'" src=\' title="x\' onerror=\'e()\' data-x=" src="u.png">',
  74. ],
  75. 'processing instruction' => ['<?pi <img title="?><p title=" src="u.png">',
  76. '<?pi <img loading="lazy" title="?><p title=" src="u.png">'],
  77. ];
  78. }
  79. }