mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-04-13 18:38:48 +00:00
[html] parseSrcset: Add type hints, check preg_match_all
This commit is contained in:
parent
96acb66fc4
commit
059514ea54
@ -245,7 +245,7 @@ function defaultLinkTo($dom, $url)
|
|||||||
* @param bool $return_largest_url Instead of returning an array, return URL for the largest entry
|
* @param bool $return_largest_url Instead of returning an array, return URL for the largest entry
|
||||||
* @return array|string Content of srcset attribute as { size => url } array, or largest entry URL if requested
|
* @return array|string Content of srcset attribute as { size => url } array, or largest entry URL if requested
|
||||||
*/
|
*/
|
||||||
function parseSrcset($srcset, $return_largest_url = false)
|
function parseSrcset(string $srcset, bool $return_largest_url = false)
|
||||||
{
|
{
|
||||||
// The srcset format is more tricky to parse that it seems:
|
// The srcset format is more tricky to parse that it seems:
|
||||||
// URLs may contain commas, and space after comma is not mandatory, so the following is valid:
|
// URLs may contain commas, and space after comma is not mandatory, so the following is valid:
|
||||||
@ -257,14 +257,16 @@ function parseSrcset($srcset, $return_largest_url = false)
|
|||||||
// 2. Any amount of characters up to the next whitespace (space, tab, newline...): This is the URL
|
// 2. Any amount of characters up to the next whitespace (space, tab, newline...): This is the URL
|
||||||
// 3. A nonnegative number followed by lowercase w, x or h: This is the image size
|
// 3. A nonnegative number followed by lowercase w, x or h: This is the image size
|
||||||
// We parse the srcset entries using a regex to mimick the above parser/tokenizer behavior.
|
// We parse the srcset entries using a regex to mimick the above parser/tokenizer behavior.
|
||||||
preg_match_all('/[\s]*,?[\s]*([^\s]+)\s+([0-9]+[wxh])/', $srcset, $matches);
|
$preg_status = preg_match_all('/[\s]*,?[\s]*([^\s]+)\s+([0-9]+[wxh])/', $srcset, $matches);
|
||||||
$entries = [];
|
$entries = [];
|
||||||
|
if ($preg_status !== false && $preg_status > 0) {
|
||||||
foreach ($matches[1] as $index => $url) {
|
foreach ($matches[1] as $index => $url) {
|
||||||
if (array_key_exists($index, $matches[2])) {
|
if (array_key_exists($index, $matches[2])) {
|
||||||
$size = $matches[2][$index];
|
$size = $matches[2][$index];
|
||||||
$entries[$size] = html_entity_decode($url);
|
$entries[$size] = html_entity_decode($url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if ($return_largest_url) {
|
if ($return_largest_url) {
|
||||||
$largest_image_url = null;
|
$largest_image_url = null;
|
||||||
$largest_image_size = -1;
|
$largest_image_size = -1;
|
||||||
|
Loading…
Reference in New Issue
Block a user