4
0
Эх сурвалжийг харах

perf(image): replace js images lazy loading by html `loading` attribute (#5816)

* perf(image): replace js images lazy loading by html `loading` attribute

* Improve lazy images regex
Fix https://github.com/FreshRSS/FreshRSS/issues/2371
Replaces https://github.com/FreshRSS/FreshRSS/pull/5816
Replaces https://github.com/FreshRSS/FreshRSS/pull/9004

* Reduce comments

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
Sadetdin EYILI 8 цаг өмнө
parent
commit
5ad167e6f3

+ 5 - 13
lib/lib_rss.php

@@ -335,22 +335,14 @@ function cleanCache(int $hours = 720): void {
 }
 }
 
 
 /**
 /**
- * Add support of image lazy loading
- * Move content from src/poster attribute to data-original
+ * Add native lazy loading by inserting a `loading="lazy"` attribute right after
+ * the tag name of img/image/iframe/track/video elements that do not have one.
  * @param string $content is the text we want to parse
  * @param string $content is the text we want to parse
  */
  */
 function lazyimg(string $content): string {
 function lazyimg(string $content): string {
-	return preg_replace([
-			'/<((?:img|image|iframe|track)[^>]+?)src="([^"]+)"([^>]*)>/i',
-			"/<((?:img|image|iframe|track)[^>]+?)src='([^']+)'([^>]*)>/i",
-			'/<((?:video)[^>]+?)poster="([^"]+)"([^>]*)>/i',
-			"/<((?:video)[^>]+?)poster='([^']+)'([^>]*)>/i",
-		], [
-			'<$1src="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
-			"<$1src='" . Minz_Url::display('/themes/icons/grey.gif') . "' data-original='$2'$3>",
-			'<$1poster="' . Minz_Url::display('/themes/icons/grey.gif') . '" data-original="$2"$3>',
-			"<$1poster='" . Minz_Url::display('/themes/icons/grey.gif') . "' data-original='$2'$3>",
-		],
+	return preg_replace(
+		'/<((?:img|image|iframe|track|video)\b)(?![^>]*\b(?:loading|data-original)=)([^>]*+)>/i',
+		'<$1 loading="lazy"$2>',
 		$content
 		$content
 	) ?? '';
 	) ?? '';
 }
 }

+ 3 - 5
p/scripts/main.js

@@ -2437,16 +2437,14 @@ function enforce_referrer_allowlist(stream) {
 	for (const iframe of stream.querySelectorAll('div.content iframe[src], div.content iframe[data-original]')) {
 	for (const iframe of stream.querySelectorAll('div.content iframe[src], div.content iframe[data-original]')) {
 		let hostname;
 		let hostname;
 		try {
 		try {
-			hostname = new URL(context.does_lazyload ? iframe.getAttribute('data-original') : iframe.src).hostname;
+			hostname = new URL(iframe.src).hostname;
 		} catch (_) {
 		} catch (_) {
 			continue;
 			continue;
 		}
 		}
 		if (context.send_referrer_allowlist.includes(hostname) && !iframe.hasAttribute('referrerpolicy')) {
 		if (context.send_referrer_allowlist.includes(hostname) && !iframe.hasAttribute('referrerpolicy')) {
 			iframe.setAttribute('referrerpolicy', 'strict-origin-when-cross-origin');
 			iframe.setAttribute('referrerpolicy', 'strict-origin-when-cross-origin');
-			if (!context.does_lazyload) {
-				// iframe must be reloaded to apply the `referrerpolicy` change
-				iframe.src = iframe.src; // eslint-disable-line no-self-assign
-			}
+			// iframe must be reloaded to apply the `referrerpolicy` change
+			iframe.src = iframe.src; // eslint-disable-line no-self-assign
 		}
 		}
 	}
 	}
 }
 }

+ 49 - 0
tests/lib/LibRssTest.php

@@ -0,0 +1,49 @@
+<?php
+declare(strict_types=1);
+
+use PHPUnit\Framework\Attributes\DataProvider;
+
+final class LibRssTest extends \PHPUnit\Framework\TestCase {
+
+	#[DataProvider('provideLazyimg')]
+	public function test_lazyimg(string $content, string $expected): void {
+		self::assertSame($expected, lazyimg($content));
+	}
+
+	/** @return array<string,array{string,string}> */
+	public static function provideLazyimg(): array {
+		return [
+			'ordinary image' => ['<img src="a.png">', '<img loading="lazy" src="a.png">'],
+			'multiple tags' => ['<img src="1.png"><iframe src="2.html" width="9"></iframe>',
+				'<img loading="lazy" src="1.png"><iframe loading="lazy" src="2.html" width="9"></iframe>'],
+			'video poster' => ['<video poster="p.jpg"></video>', '<video loading="lazy" poster="p.jpg"></video>'],
+			'track element' => ['<track src="t.vtt">', '<track loading="lazy" src="t.vtt">'],
+			'case insensitive' => ['<IMG SRC="A.PNG">', '<IMG loading="lazy" SRC="A.PNG">'],
+
+			'already lazy' => ['<img loading="lazy" src="a.png">', '<img loading="lazy" src="a.png">'],
+			'deliberate eager opt-out kept' => ['<img loading="eager" src="a.png">', '<img loading="eager" src="a.png">'],
+			'legacy data-original untouched' => ['<iframe data-original="x.pdf" src="alias.pdf">',
+				'<iframe data-original="x.pdf" src="alias.pdf">'],
+			'tag name boundary' => ['<imgish src="a.png">', '<imgish src="a.png">'],
+			'end tag untouched' => ['</img src="a.png">', '</img src="a.png">'],
+
+			'data-src is not rewritten' => ['<iframe data-src="javascript:x()">', '<iframe loading="lazy" data-src="javascript:x()">'],
+
+			'src inside a title value' => [
+				'<iframe title="src=" data-x=" srcdoc=\'<p>doc</p>\' sandbox=\'allow-scripts allow-same-origin allow-forms\' " src="doc.pdf">',
+				'<iframe loading="lazy" title="src=" data-x=" srcdoc=\'<p>doc</p>\' sandbox=\'allow-scripts allow-same-origin allow-forms\' " src="doc.pdf">',
+			],
+			'value holding a comment and a tag' => [
+				'<img alt="src=" title=" <!-- ><a href=x>t</a><i title=--> " src="u.png">',
+				'<img loading="lazy" alt="src=" title=" <!-- ><a href=x>t</a><i title=--> " src="u.png">',
+			],
+			'single quote look-alike' => [
+				'<img alt=\'" src=\' title="x\' onerror=\'e()\' data-x=" src="u.png">',
+				'<img loading="lazy" alt=\'" src=\' title="x\' onerror=\'e()\' data-x=" src="u.png">',
+			],
+
+			'processing instruction' => ['<?pi <img title="?><p title=" src="u.png">',
+				'<?pi <img loading="lazy" title="?><p title=" src="u.png">'],
+		];
+	}
+}