diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9541bf010..00dc20fbb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 }} strategy: fail-fast: false diff --git a/composer.json b/composer.json index 725701496..cf5d70e99 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/resources/local_fallback/rtv/metadata b/resources/local_fallback/rtv/metadata index 42c60fc09..5fff55868 100644 --- a/resources/local_fallback/rtv/metadata +++ b/resources/local_fallback/rtv/metadata @@ -1 +1 @@ -{"ampRuntimeVersion":"012603032146000","ampCssUrl":"https://cdn.ampproject.org/rtv/012603032146000/v0.css","canaryPercentage":"0.005","diversions":["002603032146000","022603032146000","032603032146000","042603032146000","052603032146000"],"ltsRuntimeVersion":"012601162341000","ltsCssUrl":"https://cdn.ampproject.org/rtv/012601162341000/v0.css"} \ No newline at end of file +{"ampRuntimeVersion":"012605071401000","ampCssUrl":"https://cdn.ampproject.org/rtv/012605071401000/v0.css","canaryPercentage":"0.005","diversions":["002605071401000","022605071401000","032605071401000","042607151349000","052605071401000"],"ltsRuntimeVersion":"012605071401000","ltsCssUrl":"https://cdn.ampproject.org/rtv/012605071401000/v0.css"} \ No newline at end of file diff --git a/src/Amp.php b/src/Amp.php index 1c3b0ddf3..453266523 100644 --- a/src/Amp.php +++ b/src/Amp.php @@ -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. * @@ -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; + } + } + + if (! $hasCacheHost) { return false; } @@ -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; + } + } + + if (! $hasViewerHost) { return false; } diff --git a/src/Optimizer/Transformer/RewriteAmpUrls.php b/src/Optimizer/Transformer/RewriteAmpUrls.php index 7d135738a..51ccf34fc 100644 --- a/src/Optimizer/Transformer/RewriteAmpUrls.php +++ b/src/Optimizer/Transformer/RewriteAmpUrls.php @@ -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; } /** @@ -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; } /** diff --git a/src/Url.php b/src/Url.php index 76228c6be..522c075ee 100644 --- a/src/Url.php +++ b/src/Url.php @@ -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; + } } diff --git a/tests/AmpTest.php b/tests/AmpTest.php index fddd92fd5..2cae7850c 100644 --- a/tests/AmpTest.php +++ b/tests/AmpTest.php @@ -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], diff --git a/tests/UrlTest.php b/tests/UrlTest.php index 98bb260db..9d13f8137 100644 --- a/tests/UrlTest.php +++ b/tests/UrlTest.php @@ -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; diff --git a/tests/spec/transformers/valid/RewriteAmpUrls/adds_lts_ampjs/expected_output.html b/tests/spec/transformers/valid/RewriteAmpUrls/adds_lts_ampjs/expected_output.html index 9bacd36ca..aa570f4cf 100644 --- a/tests/spec/transformers/valid/RewriteAmpUrls/adds_lts_ampjs/expected_output.html +++ b/tests/spec/transformers/valid/RewriteAmpUrls/adds_lts_ampjs/expected_output.html @@ -1,11 +1,13 @@
- - - + + + + + - +