Commit 184823a4 authored by Barnaby Walters's avatar Barnaby Walters

Merge branch 'voxpelli-patch-1'

parents 5d1425e2 46385653
......@@ -231,6 +231,19 @@ function convertTimeFormat($time) {
}
}
function applySrcsetUrlTransformation($srcset, $transformation) {
return implode(', ', array_filter(array_map(function ($srcsetPart) use ($transformation) {
$parts = explode(" \t\n\r\0\x0B", trim($srcsetPart), 2);
$parts[0] = rtrim($parts[0]);
if (empty($parts[0])) { return false; }
$parts[0] = call_user_func($transformation, $parts[0]);
return $parts[0] . (empty($parts[1]) ? '' : ' ' . $parts[1]);
}, explode(',', trim($srcset)))));
}
/**
* Microformats2 Parser
*
......@@ -336,6 +349,8 @@ class Parser {
$child->setAttribute('href', $this->resolveUrl($child->getAttribute('href')));
if ($child->hasAttribute('src'))
$child->setAttribute('src', $this->resolveUrl($child->getAttribute('src')));
if ($child->hasAttribute('srcset'))
$child->setAttribute('srcset', applySrcsetUrlTransformation($child->getAttribute('href'), [$this, 'resolveUrl']));
if ($child->hasAttribute('data'))
$child->setAttribute('data', $this->resolveUrl($child->getAttribute('data')));
}
......
......@@ -314,4 +314,15 @@ EOT;
$this->assertArrayHasKey('url', $output['items'][0]['properties']['category'][0]['properties']);
$this->assertEquals('http://b.example.com/', $output['items'][0]['properties']['category'][0]['properties']['url'][0]);
}
public function testApplyTransformationToSrcset() {
$transformation = function ($url) {
return 'https://example.com/' . ltrim($url, '/');
};
// Example from https://developers.whatwg.org/edits.html#attr-img-srcset
$srcset = 'banner-HD.jpeg 2x, banner-phone.jpeg 100w, banner-phone-HD.jpeg 100w 2x';
$result = Mf2\applySrcsetUrlTransformation($srcset, $transformation);
$this->assertEquals('https://example.com/banner-HD.jpeg 2x, https://example.com/banner-phone.jpeg 100w, https://example.com/banner-phone-HD.jpeg 100w 2x', $result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment