Skip to content
Open
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
17 changes: 14 additions & 3 deletions src/Optimizer/Transformer/RewriteAmpUrls.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check warning on line 1 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 73 and the first side effect is on line 59.

namespace AmpProject\Optimizer\Transformer;

Expand Down Expand Up @@ -56,7 +56,7 @@
*
* @package ampproject/amp-toolbox
*/
final class RewriteAmpUrls implements Transformer

Check warning on line 59 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Possible parse error: class missing opening or closing brace

Check warning on line 59 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Possible parse error: class missing opening or closing brace

Check warning on line 59 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Possible parse error: class missing opening or closing brace
{
/**
* Configuration to use.
Expand All @@ -70,10 +70,10 @@
*
* @param TransformerConfiguration $configuration Configuration to use.
*/
public function __construct(TransformerConfiguration $configuration)

Check failure on line 73 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 0 spaces, found 4
{
$this->configuration = $configuration;
}

Check failure on line 76 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 0 spaces, found 4

/**
* Apply transformations to the provided DOM document.
Expand All @@ -84,13 +84,13 @@
* during transformation.
* @return void
*/
public function transform(Document $document, ErrorCollection $errors)

Check failure on line 87 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 0 spaces, found 4
{
$host = $this->calculateHost();

$this->adaptForEsmSupport($document, $host);
$this->adaptForSelfHosting($document, $host, $errors);
}

Check failure on line 93 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 0 spaces, found 4

/**
* Adapt for ES modules support.
Expand All @@ -98,24 +98,24 @@
* @param Document $document Document to collect preload nodes for.
* @param string $host Host URL to use.
*/
private function adaptForEsmSupport(Document $document, $host)

Check failure on line 101 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 0 spaces, found 4
{
$usesEsm = $this->configuration->get(RewriteAmpUrlsConfiguration::ESM_MODULES_ENABLED);

$node = $document->head->firstChild;
while ($node) {

Check failure on line 106 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 4 spaces, found 8
$nextSibling = $node->nextSibling;
if (! $node instanceof Element) {

Check failure on line 108 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 8 spaces, found 12
$node = $nextSibling;
continue;
}

Check failure on line 111 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 8 spaces, found 12

$src = $node->getAttribute(Attribute::SRC);
$href = $node->getAttribute(Attribute::HREF);
if ($node->tagName === Tag::SCRIPT && $this->usesAmpCacheUrl($src)) {

Check failure on line 115 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 8 spaces, found 12
$newUrl = $this->replaceUrl($src, $host);
$node->setAttribute(Attribute::SRC, $newUrl);
if ($usesEsm) {

Check failure on line 118 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Code style / PHP 7.4

Line indented incorrectly; expected 12 spaces, found 16
$this->addEsm($document, $node);
} else {
$this->addPreload($document, $newUrl, Tag::SCRIPT);
Expand Down Expand Up @@ -161,7 +161,13 @@
return false;
}

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

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

/**
Expand All @@ -179,8 +185,13 @@
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;

/**
* Replace <script> elements with their ES module counterparts.
Expand All @@ -188,7 +199,7 @@
* @param Document $document Document to add the ES module scripts to.
* @param Element $scriptNode Script element to replace.
*/
private function addEsm(Document $document, Element $scriptNode)

Check failure on line 202 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Syntax error, unexpected T_PRIVATE on line 202
{
$scriptUrl = $scriptNode->getAttribute(Attribute::SRC);
$esmScriptUrl = preg_replace('/\.js$/', '.mjs', $scriptUrl);
Expand Down Expand Up @@ -221,7 +232,7 @@
* @param string $href Href to use for the preload.
* @param string $type Type to use for the preload.
*/
private function addPreload(Document $document, $href, $type)

Check failure on line 235 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Syntax error, unexpected T_PRIVATE on line 235
{
if (! $this->shouldPreload($href, $document)) {
return;
Expand All @@ -238,7 +249,7 @@
* @param string $host Host URL to use.
* @param ErrorCollection $errors Error collection to add potential errors to.
*/
private function adaptForSelfHosting(Document $document, $host, $errors)

Check failure on line 252 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Syntax error, unexpected T_PRIVATE on line 252
{
// runtime-host and amp-geo-api meta tags should appear before the first script.
if (
Expand Down Expand Up @@ -279,7 +290,7 @@
* @param Document $document Document on which to check.
* @return bool Whether the provided URL should be preloaded.
*/
private function shouldPreload($url, Document $document)

Check failure on line 293 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Syntax error, unexpected T_PRIVATE on line 293
{
if ($document->documentElement->hasAttribute(Amp::NO_BOILERPLATE_ATTRIBUTE)) {
return false;
Expand All @@ -295,7 +306,7 @@
*
* @return string Host to use.
*/
private function calculateHost()

Check failure on line 309 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Syntax error, unexpected T_PRIVATE on line 309
{
$lts = $this->configuration->get(RewriteAmpUrlsConfiguration::LTS);
$rtv = $this->configuration->get(RewriteAmpUrlsConfiguration::RTV);
Expand Down Expand Up @@ -328,7 +339,7 @@
* @param string $name Name of the meta element.
* @param string $content Value of the meta element.
*/
private function addMeta(Document $document, $name, $content)

Check failure on line 342 in src/Optimizer/Transformer/RewriteAmpUrls.php

View workflow job for this annotation

GitHub Actions / Static analysis / PHP 8.0

Syntax error, unexpected T_PRIVATE on line 342
{
$meta = $document->createElement(Tag::META);
$meta->setAttribute(Attribute::NAME, $name);
Expand All @@ -337,3 +348,3 @@
$document->head->insertBefore($meta, $firstScript);
}
}
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

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