Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ jobs:
unit-test:
name: Unit test${{ matrix.coverage && ' (with coverage)' || '' }}${{ matrix.random && ' (in random order)' || '' }} / PHP ${{ matrix.php }}
runs-on: ubuntu-latest
# We want to run on external PRs, but not on our own internal PRs as they'll be run
# by the push to the branch. This avoids running the workflows twice in such a case.
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
continue-on-error: ${{ matrix.experimental == true }}
Comment thread
swissspidy marked this conversation as resolved.
strategy:
fail-fast: false
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
"scripts": {
"cbf": "phpcbf",
"compat": "if [ -z $TEST_SKIP_PHPCOMPAT ]; then phpcs --standard=PHPCompatibility -s -p src --runtime-set testVersion 5.6; fi",
"compat": "if [ -z $TEST_SKIP_PHPCOMPAT ]; then phpcs --standard=PHPCompatibility -s -p src --runtime-set testVersion 7.4; fi",
"cs": "if [ -z $TEST_SKIP_PHPCS ]; then phpcs; fi",
"lint": "if [ -z $TEST_SKIP_LINTING ]; then parallel-lint -j 10 --colors --exclude vendor .; fi",
"test": [
Expand Down
2 changes: 1 addition & 1 deletion resources/local_fallback/rtv/metadata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 35 additions & 2 deletions src/Amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ final class Amp
*/
const CACHE_HOST = 'https://cdn.ampproject.org';

/**
* Alternative host and scheme of the AMP cache.
*
* @var string
*/
const CACHE_HOST_AMPJS = 'https://ampjs.org';

/**
* List of valid AMP cache hosts.
*
* @var string[]
*/
const CACHE_HOSTS = [
self::CACHE_HOST,
self::CACHE_HOST_AMPJS,
];

/**
* URL of the AMP cache.
*
Expand Down Expand Up @@ -152,7 +169,15 @@ public static function isRuntimeScript(DOMNode $node)

$src = $node->getAttribute(Attribute::SRC);

if (strpos($src, self::CACHE_ROOT_URL) !== 0) {
$hasCacheHost = false;
foreach (self::CACHE_HOSTS as $cacheHost) {
if (strpos($src, $cacheHost . '/') === 0) {
$hasCacheHost = true;
break;
Comment thread
swissspidy marked this conversation as resolved.
}
}

if (! $hasCacheHost) {
return false;
}

Expand Down Expand Up @@ -190,7 +215,15 @@ public static function isViewerScript(DOMNode $node)

$src = $node->getAttribute(Attribute::SRC);

if (strpos($src, self::CACHE_HOST . '/v0/amp-viewer-integration-') !== 0) {
$hasViewerHost = false;
foreach (self::CACHE_HOSTS as $cacheHost) {
if (strpos($src, $cacheHost . '/v0/amp-viewer-integration-') === 0) {
$hasViewerHost = true;
break;
Comment thread
swissspidy marked this conversation as resolved.
}
}

if (! $hasViewerHost) {
return false;
}

Expand Down
16 changes: 14 additions & 2 deletions src/Optimizer/Transformer/RewriteAmpUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,13 @@ private function usesAmpCacheUrl($url)
return false;
}

return strpos($url, Amp::CACHE_HOST) === 0;
foreach (Amp::CACHE_HOSTS as $cacheHost) {
if ($url === $cacheHost || strpos($url, $cacheHost . '/') === 0) {
return true;
}
}

return false;
Comment thread
Copilot marked this conversation as resolved.
}

/**
Expand All @@ -179,7 +185,13 @@ private function replaceUrl($url, $host)
return $url;
}

return str_replace(Amp::CACHE_HOST, $host, $url);
foreach (Amp::CACHE_HOSTS as $cacheHost) {
if (strpos($url, $cacheHost . '/') === 0) {
return $host . substr($url, strlen($cacheHost));
}
}

return $url;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,15 @@ public function __get($name)
trigger_error(self::PROPERTY_GETTER_ERROR_MESSAGE . $name, E_USER_NOTICE);
return null;
}

/**
* Magic isset to check if an individual part is set.
*
* @param string $name Name of the part to check.
* @return bool Whether the part is set.
*/
public function __isset($name)
{
return array_key_exists($name, self::URL_DEFAULT_PARTS) && $this->$name !== null;
}
}
2 changes: 2 additions & 0 deletions tests/AmpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ public function dataIsRuntimeScript()
{
$dom = new Document();
$ampSrc = 'https://cdn.ampproject.org/v0.js';
$ampjsSrc = 'https://ampjs.org/v0.js';
$ampForAdsSrc = 'https://cdn.ampproject.org/amp4ads-v0.js';

return [
'amp-runtime' => [$this->createAmpCDNScript($dom, $ampSrc), true],
'ampjs-runtime' => [$this->createAmpCDNScript($dom, $ampjsSrc), true],
'amp-for-ads-runtime' => [$this->createAmpCDNScript($dom, $ampForAdsSrc), true],
'amp-extension-script' => [$this->createExtensionScript($dom, 'amp-runtime'), false],
'not-a-script' => [$dom->createElement(Tag::STYLE), false],
Expand Down
4 changes: 4 additions & 0 deletions tests/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public function testMagicProperties()
$this->assertEquals('/demo/', $url->path);
$this->assertEquals('filter=true', $url->query);
$this->assertEquals('anchor', $url->fragment);
$this->assertTrue(isset($url->scheme));
$this->assertFalse(empty($url->scheme));
$this->assertFalse(isset($url->nonsense));
$this->assertTrue(empty($url->nonsense));

$this->expectError();
$url->nonsense;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/spec/valid-amp/files/Advanced_Video_Docking.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/spec/valid-amp/files/Rich_Media_Notifications.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/spec/valid-amp/files/amp-ima-video.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/spec/valid-amp/files/amp-video.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading