better support for image srcset attributes as discussed in https://community.tt-rss.org/t/problem-with-img-srcset/3519

This commit is contained in:
Andrew Dolgov 2020-06-15 11:58:59 +03:00
parent 697418f863
commit 6eb94f1e13
3 changed files with 43 additions and 25 deletions

View File

@ -314,22 +314,19 @@ class DiskCache {
} }
if ($entry->hasAttribute("srcset")) { if ($entry->hasAttribute("srcset")) {
$tokens = explode(",", $entry->getAttribute('srcset')); $matches = RSSUtils::decode_srcset($entry->getAttribute('srcset'));
for ($i = 0; $i < count($tokens); $i++) { for ($i = 0; $i < count($matches); $i++) {
$token = trim($tokens[$i]); $cached_filename = sha1($matches[$i]["url"]);
list ($url, $width) = explode(" ", $token, 2);
$cached_filename = sha1($url);
if ($cache->exists($cached_filename)) { if ($cache->exists($cached_filename)) {
$tokens[$i] = $cache->getUrl($cached_filename) . " " . $width; $matches[$i]["url"] = $cache->getUrl($cached_filename);
$need_saving = true; $need_saving = true;
} }
} }
$entry->setAttribute("srcset", implode(", ", $tokens)); $entry->setAttribute("srcset", RSSUtils::encode_srcset($matches));
} }
} }

View File

@ -1196,6 +1196,7 @@ class RSSUtils {
return true; return true;
} }
/* TODO: move to DiskCache? */
static function cache_enclosures($enclosures, $site_url) { static function cache_enclosures($enclosures, $site_url) {
$cache = new DiskCache("images"); $cache = new DiskCache("images");
@ -1231,6 +1232,7 @@ class RSSUtils {
} }
} }
/* TODO: move to DiskCache? */
static function cache_media_url($cache, $url, $site_url) { static function cache_media_url($cache, $url, $site_url) {
$url = rewrite_relative_url($site_url, $url); $url = rewrite_relative_url($site_url, $url);
$local_filename = sha1($url); $local_filename = sha1($url);
@ -1257,6 +1259,7 @@ class RSSUtils {
} }
} }
/* TODO: move to DiskCache? */
static function cache_media($html, $site_url) { static function cache_media($html, $site_url) {
$cache = new DiskCache("images"); $cache = new DiskCache("images");
@ -1275,14 +1278,10 @@ class RSSUtils {
} }
if ($entry->hasAttribute("srcset")) { if ($entry->hasAttribute("srcset")) {
$tokens = explode(",", $entry->getAttribute('srcset')); $matches = RSSUtils::decode_srcset($entry->getAttribute('srcset'));
for ($i = 0; $i < count($tokens); $i++) { for ($i = 0; $i < count($matches); $i++) {
$token = trim($tokens[$i]); RSSUtils::cache_media_url($cache, $matches[$i]["url"], $site_url);
list ($url, $width) = explode(" ", $token, 2);
RSSUtils::cache_media_url($cache, $url, $site_url);
} }
} }
} }
@ -1738,4 +1737,32 @@ class RSSUtils {
return $favicon_url; return $favicon_url;
} }
// https://community.tt-rss.org/t/problem-with-img-srcset/3519
static function decode_srcset($srcset) {
$matches = [];
preg_match_all(
'/(?:\A|,)\s*(?P<url>(?!,)\S+(?<!,))\s*(?P<size>\s\d+w|\s\d+(?:\.\d+)?(?:[eE][+-]?\d+)?x|)\s*(?=,|\Z)/',
$srcset, $matches, PREG_SET_ORDER
);
foreach ($matches as $m) {
array_push($matches, [
"url" => trim($m["url"]),
"size" => trim($m["size"])
]);
}
return $matches;
}
static function encode_srcset($matches) {
$tokens = [];
foreach ($matches as $m) {
array_push($tokens, trim($m["url"]) . " " . trim($m["size"]));
}
return implode(",", $tokens);
}
} }

View File

@ -1289,19 +1289,13 @@
} }
if ($entry->hasAttribute('srcset')) { if ($entry->hasAttribute('srcset')) {
$tokens = explode(",", $entry->getAttribute('srcset')); $matches = RSSUtils::decode_srcset($entry->getAttribute('srcset'));
for ($i = 0; $i < count($tokens); $i++) { for ($i = 0; $i < count($matches); $i++) {
$token = trim($tokens[$i]); $matches[$i]["url"] = rewrite_relative_url($rewrite_base_url, $matches[$i]["url"]);
list ($url, $width) = explode(" ", $token, 2);
$url = rewrite_relative_url($rewrite_base_url, $url);
$tokens[$i] = "$url $width";
} }
$entry->setAttribute("srcset", implode(", ", $tokens)); $entry->setAttribute("srcset", RSSUtils::encode_srcset($matches));
} }
if ($entry->hasAttribute('src') && if ($entry->hasAttribute('src') &&