From 1d2aefe05d6e65a3eb8f427931a8f5d3f479cb16 Mon Sep 17 00:00:00 2001 From: Edi Amin Date: Mon, 27 Sep 2021 13:27:05 +0600 Subject: [PATCH] Add logic for removeUnneededExtensions --- src/Optimizer/Transformer/AutoExtensions.php | 32 +++++++++++--- .../Transformer/AutoExtensionsTest.php | 42 ++++++++++++++++++- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/src/Optimizer/Transformer/AutoExtensions.php b/src/Optimizer/Transformer/AutoExtensions.php index 7ea1bbd49..86a63af9b 100644 --- a/src/Optimizer/Transformer/AutoExtensions.php +++ b/src/Optimizer/Transformer/AutoExtensions.php @@ -53,6 +53,22 @@ final class AutoExtensions implements Transformer */ private $spec; + /** + * List of extensions added by the maybeAddExtension method. + * + * @var array + */ + private $addedExtensions = []; + + /** + * List of extensions that should not be removed even when there is no usage in the HTML. + * + * @var array + */ + private $protectedExtensions = [ + 'amp-carousel', + ]; + /** * Instantiate an AutoExtensions object. * @@ -80,7 +96,7 @@ public function transform(Document $document, ErrorCollection $errors) $extensionScripts = $this->addMissingExtensions($document, $extensionScripts); } - $extensionScripts = $this->removeUnneededExtensions($document, $extensionScripts); + $extensionScripts = $this->removeUnneededExtensions($extensionScripts); $this->renderExtensionScripts($document, $extensionScripts); } @@ -414,15 +430,19 @@ private function getTagAttributeList($tagName) /** * Remove unneeded extension scripts. * - * @param Document $document Document to scan for unneeded extensions. * @param Element[] $extensionScripts Array of extension scripts to check. * @return Element[] Adapted array of extension scripts. */ - private function removeUnneededExtensions(Document $document, $extensionScripts) + private function removeUnneededExtensions($extensionScripts) { - // TODO: Add logic. + if (! $this->configuration->get(AutoExtensionsConfiguration::REMOVE_UNNEEDED_EXTENSIONS)) { + return $extensionScripts; + } - return $extensionScripts; + return array_filter($extensionScripts, function ($extension) { + return array_key_exists($extension, $this->addedExtensions) + || in_array($extension, $this->protectedExtensions); + }, ARRAY_FILTER_USE_KEY); } /** @@ -521,6 +541,8 @@ private function maybeAddExtension(Document $document, $extensionScripts, $requi } } + $this->addedExtensions[$requiredExtension] = true; + return $extensionScripts; } diff --git a/tests/Optimizer/Transformer/AutoExtensionsTest.php b/tests/Optimizer/Transformer/AutoExtensionsTest.php index 56cbee82a..9e7fb3030 100644 --- a/tests/Optimizer/Transformer/AutoExtensionsTest.php +++ b/tests/Optimizer/Transformer/AutoExtensionsTest.php @@ -124,7 +124,47 @@ public function dataTransform() . ' ad goes here' . ' ' . '', - ] + ], + + 'keep unneeded amp-youtube extension when removeUnneededExtensions is false' => [ + TestMarkup::DOCTYPE . '' . TestMarkup::META_CHARSET + . '' + . '' + . '', + TestMarkup::DOCTYPE . '' . TestMarkup::META_CHARSET + . '' + . '' + . '', + ], + + 'remove unneeded amp-youtube extension when removeUnneededExtensions is true' => [ + TestMarkup::DOCTYPE . '' . TestMarkup::META_CHARSET + . '' + . '' + . '', + TestMarkup::DOCTYPE . '' . TestMarkup::META_CHARSET + . '' + . '', + [], + [ + AutoExtensionsConfiguration::REMOVE_UNNEEDED_EXTENSIONS => true, + ] + ], + + 'keep unneeded amp-carousel extension when removeUnneededExtensions is true' => [ + TestMarkup::DOCTYPE . '' . TestMarkup::META_CHARSET + . '' + . '' + . '', + TestMarkup::DOCTYPE . '' . TestMarkup::META_CHARSET + . '' + . '' + . '', + [], + [ + AutoExtensionsConfiguration::REMOVE_UNNEEDED_EXTENSIONS => true, + ] + ], ]; }