From 6fa218eee745ec4490c734ab839376f01500e7dd Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Wed, 25 Nov 2020 16:45:43 +0100 Subject: [PATCH 01/37] Add initial scaffolding for link management --- src/Dom/Document.php | 15 ++++++ src/Dom/LinkManager.php | 61 ++++++++++++++++++++++ src/Optimizer/Configuration.php | 2 + src/Optimizer/Transformer/BrowserHints.php | 45 ++++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 src/Dom/LinkManager.php create mode 100644 src/Optimizer/Transformer/BrowserHints.php diff --git a/src/Dom/Document.php b/src/Dom/Document.php index 45059e3d7..071826b24 100644 --- a/src/Dom/Document.php +++ b/src/Dom/Document.php @@ -34,6 +34,7 @@ * @property Element $ampCustomStyle The document's ' + ), + $output( + '', + '' + ), + ], + ]; + } + + /** + * Test the transform() method. + * + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::transform() + * @dataProvider dataTransform() + * + * @param string $source String of source HTML. + * @param string $expectedHtml String of expected HTML output. + * @param ErrorCollection|Error[] $expectedErrors Set of expected errors. + * @param array $config Configuration data to use. + */ + public function testTransform($source, $expectedHtml, $expectedErrors = [], $config = []) + { + $document = Document::fromHtml($source); + $transformer = new AmpRuntimePreloads(); + $errors = new ErrorCollection(); + + $transformer->transform($document, $errors); + + $this->assertSimilarMarkup($expectedHtml, $document->saveHTML()); + $this->assertSameErrors($expectedErrors, $errors); + } +} From b0f6d93d229f0f688948c460034f0b3a1b9c40e1 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 15 Dec 2020 16:46:41 +0000 Subject: [PATCH 17/37] Add GoogleFontsPreconnect spec tests --- tests/Optimizer/SpecTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/Optimizer/SpecTest.php b/tests/Optimizer/SpecTest.php index 9d02d35a9..4bb937b0c 100644 --- a/tests/Optimizer/SpecTest.php +++ b/tests/Optimizer/SpecTest.php @@ -76,6 +76,10 @@ public function dataTransformerSpecFiles() AmpRuntimeCss::class, self::TRANSFORMER_SPEC_PATH . '/valid/AmpBoilerplateTransformer', ], + 'GoogleFontsPreconnect' => [ + GoogleFontsPreconnect::class, + self::TRANSFORMER_SPEC_PATH . '/valid/GoogleFontsPreconnect' + ] 'OptimizeAmpBind' => [ OptimizeAmpBind::class, self::TRANSFORMER_SPEC_PATH . '/valid/OptimizeAmpBind', From dfcbd1436240f87eaea7dbca04dcb89e12f5ec80 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 May 2021 13:42:03 +0100 Subject: [PATCH 18/37] Fix test failures --- src/Optimizer/Configuration.php | 1 + src/Optimizer/Transformer/GoogleFontsPreconnect.php | 4 ++-- tests/Optimizer/SpecTest.php | 2 +- tests/Optimizer/Transformer/GoogleFontsPreconnectTest.php | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Optimizer/Configuration.php b/src/Optimizer/Configuration.php index 433c8d905..762fe9d37 100644 --- a/src/Optimizer/Configuration.php +++ b/src/Optimizer/Configuration.php @@ -50,6 +50,7 @@ interface Configuration ServerSideRendering::class, AmpRuntimeCss::class, AmpBoilerplateErrorHandler::class, + GoogleFontsPreconnect::class, ResourceHints::class, RewriteAmpUrls::class, ReorderHead::class, diff --git a/src/Optimizer/Transformer/GoogleFontsPreconnect.php b/src/Optimizer/Transformer/GoogleFontsPreconnect.php index 54264db94..14b17b317 100644 --- a/src/Optimizer/Transformer/GoogleFontsPreconnect.php +++ b/src/Optimizer/Transformer/GoogleFontsPreconnect.php @@ -19,14 +19,14 @@ final class GoogleFontsPreconnect implements Transformer * * @var string */ - const GOOGLE_FONTS_STATIC_DOMAIN = 'https://fonts.gstatic.com/'; + const GOOGLE_FONTS_STATIC_DOMAIN = 'https://fonts.gstatic.com'; /** * Domain that the Google Fonts API is accepting requests from. * * @var string */ - const GOOGLE_FONTS_API_DOMAIN = 'https://fonts.googleapis.com/'; + const GOOGLE_FONTS_API_DOMAIN = 'https://fonts.googleapis.com'; /** * XPath query to fetch links pointing to the Google Fonts API. diff --git a/tests/Optimizer/SpecTest.php b/tests/Optimizer/SpecTest.php index 4bb937b0c..b8ac9aff9 100644 --- a/tests/Optimizer/SpecTest.php +++ b/tests/Optimizer/SpecTest.php @@ -79,7 +79,7 @@ public function dataTransformerSpecFiles() 'GoogleFontsPreconnect' => [ GoogleFontsPreconnect::class, self::TRANSFORMER_SPEC_PATH . '/valid/GoogleFontsPreconnect' - ] + ], 'OptimizeAmpBind' => [ OptimizeAmpBind::class, self::TRANSFORMER_SPEC_PATH . '/valid/OptimizeAmpBind', diff --git a/tests/Optimizer/Transformer/GoogleFontsPreconnectTest.php b/tests/Optimizer/Transformer/GoogleFontsPreconnectTest.php index 8a0e40146..611d2322c 100644 --- a/tests/Optimizer/Transformer/GoogleFontsPreconnectTest.php +++ b/tests/Optimizer/Transformer/GoogleFontsPreconnectTest.php @@ -50,8 +50,8 @@ public function dataTransform() ), $output( '', - '' - . '' + '' + . '' ), ], From e1d4687a662bde770c11b3718bbe16e0f12294bf Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 May 2021 13:57:39 +0100 Subject: [PATCH 19/37] Rename property $linkManager -> $resourceHints --- src/Dom/Document.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Dom/Document.php b/src/Dom/Document.php index 09321d23a..d5b91d248 100644 --- a/src/Dom/Document.php +++ b/src/Dom/Document.php @@ -335,11 +335,11 @@ final class Document extends DOMDocument private $convertedAmpBindAttributes = []; /** - * Link manager to manage tags in the . + * Resource hint manager to manage resource hint tags in the . * * @var ResourceHintManager|null */ - private $linkManager; + private $resourceHints; /** * Creates a new AmpProject\Dom\Document object @@ -2038,11 +2038,11 @@ public function __get($name) return $this->inlineStyleByteCount; case 'resourceHints': - if (! isset($this->linkManager)) { - $this->linkManager = new ResourceHintManager($this); + if (! isset($this->resourceHints)) { + $this->resourceHints = new ResourceHintManager($this); } - return $this->linkManager; + return $this->resourceHints; } // Mimic regular PHP behavior for missing notices. From 2c1588c76a4a3b3ce362edd21b01a57f8eb93d4e Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 May 2021 14:41:28 +0100 Subject: [PATCH 20/37] Add tests for resource hint manager --- tests/Dom/ResourceHintManagerTest.php | 175 ++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 tests/Dom/ResourceHintManagerTest.php diff --git a/tests/Dom/ResourceHintManagerTest.php b/tests/Dom/ResourceHintManagerTest.php new file mode 100644 index 000000000..8101afcc9 --- /dev/null +++ b/tests/Dom/ResourceHintManagerTest.php @@ -0,0 +1,175 @@ + [ + 'https://example.com', + false, + [ + '', + '' + ] + ], + + 'crossorigin true' => [ + 'https://example.com', + true, + [ + '', + '' + ] + ] + ]; + } + + /** + * @dataProvider dataAddPreconnect() + * @covers \AmpProject\Dom\ResourceHintManager::addPreconnect() + */ + public function testAddPreconnect($href, $crossorigin, $expectedLinks) + { + $document = Document::fromHtml(''); + $document->resourceHints->addPreconnect($href, $crossorigin); + + $this->assertSimilarMarkup( + '' + . '' + . implode('', $expectedLinks) + . '', + $document->saveHTMLFragment($document->head) + ); + } + + public function dataAddPreload() + { + return [ + 'no media query' => [ + 'https://example.com/test.jpg', + RequestDestination::IMAGE, + null, + [ + '' + ] + ], + + 'with media query' => [ + 'https://example.com/test.jpg', + RequestDestination::IMAGE, + '(max-width: 415px)', + [ + '' + ] + ], + ]; + } + + /** + * @dataProvider dataAddPreload() + * @covers \AmpProject\Dom\ResourceHintManager::addPreload() + */ + public function testAddPreload($href, $type, $media, $expectedLinks) + { + $document = Document::fromHtml(''); + $document->resourceHints->addPreload($href, $type, $media); + + $this->assertSimilarMarkup( + '' + . '' + . implode('', $expectedLinks) + . '', + $document->saveHTMLFragment($document->head) + ); + } + + public function dataAddLinkTag() + { + return [ + 'canonical link' => [ + Attribute::REL_CANONICAL, + 'https://example.com/index.html', + [], + [ + '' + ] + ], + + 'add arbitrary attributes' => [ + Attribute::REL_NOFOLLOW, + 'https://example.com/index.html', + [ + 'pretty' => 'please', + 'keep' => 'away', + ], + [ + '' + ] + ], + ]; + } + + /** + * @dataProvider dataAddLinkTag() + * @covers \AmpProject\Dom\ResourceHintManager::addLinkTag() + */ + public function testAddLinkTag($rel, $href, $attributes, $expectedLinks) + { + $document = Document::fromHtml(''); + $document->resourceHints->addLinkTag($rel, $href, $attributes); + + $this->assertSimilarMarkup( + '' + . '' + . implode('', $expectedLinks) + . '', + $document->saveHTMLFragment($document->head) + ); + } + + /** + * @covers \AmpProject\Dom\ResourceHintManager::addLinkTag() + */ + public function testLinkOrdering() + { + $document = Document::fromHtml( + '' + . '' + . '' + . '' + . '' + ); + + $document->resourceHints->addLinkTag('test', 'https://example.com/1'); + $document->resourceHints->addLinkTag('test', 'https://example.com/2'); + $document->resourceHints->addLinkTag('test', 'https://example.com/3'); + + $this->assertSimilarMarkup( + '' + . '' + . '' + . '' + . '' + . '' + . '' + . '', + $document->saveHTMLFragment($document->head) + ); + } +} From 08285b0f45a085d8278d80df2da3e4152e90e6ed Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 May 2021 14:44:47 +0100 Subject: [PATCH 21/37] Include AmpRuntimePreloads in list of default transformers --- src/Optimizer/Configuration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Optimizer/Configuration.php b/src/Optimizer/Configuration.php index 762fe9d37..79acfd1dd 100644 --- a/src/Optimizer/Configuration.php +++ b/src/Optimizer/Configuration.php @@ -49,6 +49,7 @@ interface Configuration PreloadHeroImage::class, ServerSideRendering::class, AmpRuntimeCss::class, + AmpRuntimePreloads::class, AmpBoilerplateErrorHandler::class, GoogleFontsPreconnect::class, ResourceHints::class, From 26cd73700691c07632eca8f4100eba99ec6eb652 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 May 2021 15:00:02 +0100 Subject: [PATCH 22/37] Add covers annotations for AmpRuntimePreloadsTest --- tests/Optimizer/Transformer/AmpRuntimePreloadsTest.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/Optimizer/Transformer/AmpRuntimePreloadsTest.php b/tests/Optimizer/Transformer/AmpRuntimePreloadsTest.php index 0f38fad01..dfd9b3b3d 100644 --- a/tests/Optimizer/Transformer/AmpRuntimePreloadsTest.php +++ b/tests/Optimizer/Transformer/AmpRuntimePreloadsTest.php @@ -69,7 +69,11 @@ public function dataTransform() /** * Test the transform() method. * - * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::transform() + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::transform() + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::isAmpRuntimeCssNeeded() + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::isAmpRuntimeScriptNeeded() + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::getAmpRuntimeCssHost() + * @covers \AmpProject\Optimizer\Transformer\AmpRuntimePreloads::getAmpRuntimeScriptHost() * @dataProvider dataTransform() * * @param string $source String of source HTML. From 34b9189e21cb3e4fc4a4907e98510cb4523577fa Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 3 May 2021 15:08:02 +0100 Subject: [PATCH 23/37] Make URL check for Google API domain stricter --- src/Optimizer/Transformer/GoogleFontsPreconnect.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Optimizer/Transformer/GoogleFontsPreconnect.php b/src/Optimizer/Transformer/GoogleFontsPreconnect.php index 14b17b317..a704c4dfc 100644 --- a/src/Optimizer/Transformer/GoogleFontsPreconnect.php +++ b/src/Optimizer/Transformer/GoogleFontsPreconnect.php @@ -26,7 +26,7 @@ final class GoogleFontsPreconnect implements Transformer * * @var string */ - const GOOGLE_FONTS_API_DOMAIN = 'https://fonts.googleapis.com'; + const GOOGLE_FONTS_API_DOMAIN = 'https://fonts.googleapis.com/'; /** * XPath query to fetch links pointing to the Google Fonts API. From 1cb67968759c44d9bdeea871bc82071aad61c9b8 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Tue, 11 May 2021 17:52:11 +0100 Subject: [PATCH 24/37] Rename ResourceHintManager back to LinkManager --- src/Dom/Document.php | 32 +++++++++---------- ...esourceHintManager.php => LinkManager.php} | 21 ++++++------ .../Transformer/AmpRuntimePreloads.php | 4 +-- .../Transformer/GoogleFontsPreconnect.php | 2 +- tests/Dom/ResourceHintManagerTest.php | 22 ++++++------- 5 files changed, 41 insertions(+), 40 deletions(-) rename src/Dom/{ResourceHintManager.php => LinkManager.php} (83%) diff --git a/src/Dom/Document.php b/src/Dom/Document.php index d5b91d248..a28f0e225 100644 --- a/src/Dom/Document.php +++ b/src/Dom/Document.php @@ -25,16 +25,16 @@ /** * Abstract away some of the difficulties of working with PHP's DOMDocument. * - * @property DOMXPath $xpath XPath query object for this document. - * @property Element $html The document's element. - * @property Element $head The document's element. - * @property Element $body The document's element. - * @property Element|null $viewport The document's viewport meta element. - * @property DOMNodeList $ampElements The document's elements. - * @property Element $ampCustomStyle The document's