Skip to content

Commit

Permalink
Add logic for removeUnneededExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ediamin authored and schlessera committed Oct 1, 2021
1 parent 7231700 commit 1d2aefe
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
32 changes: 27 additions & 5 deletions src/Optimizer/Transformer/AutoExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -521,6 +541,8 @@ private function maybeAddExtension(Document $document, $extensionScripts, $requi
}
}

$this->addedExtensions[$requiredExtension] = true;

return $extensionScripts;
}

Expand Down
42 changes: 41 additions & 1 deletion tests/Optimizer/Transformer/AutoExtensionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,47 @@ public function dataTransform()
. ' ad goes here'
. ' </section>'
. '</body></html>',
]
],

'keep unneeded amp-youtube extension when removeUnneededExtensions is false' => [
TestMarkup::DOCTYPE . '<html><head>' . TestMarkup::META_CHARSET
. '<script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>'
. '<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>'
. '</head><body><amp-anim></amp-anim></body></html>',
TestMarkup::DOCTYPE . '<html><head>' . TestMarkup::META_CHARSET
. '<script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>'
. '<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>'
. '</head><body><amp-anim></amp-anim></body></html>',
],

'remove unneeded amp-youtube extension when removeUnneededExtensions is true' => [
TestMarkup::DOCTYPE . '<html><head>' . TestMarkup::META_CHARSET
. '<script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>'
. '<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>'
. '</head><body><amp-anim></amp-anim></body></html>',
TestMarkup::DOCTYPE . '<html><head>' . TestMarkup::META_CHARSET
. '<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>'
. '</head><body><amp-anim></amp-anim></body></html>',
[],
[
AutoExtensionsConfiguration::REMOVE_UNNEEDED_EXTENSIONS => true,
]
],

'keep unneeded amp-carousel extension when removeUnneededExtensions is true' => [
TestMarkup::DOCTYPE . '<html><head>' . TestMarkup::META_CHARSET
. '<script async src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js" custom-element="amp-carousel"></script>'
. '<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>'
. '</head><body><amp-anim></amp-anim></body></html>',
TestMarkup::DOCTYPE . '<html><head>' . TestMarkup::META_CHARSET
. '<script async src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js" custom-element="amp-carousel"></script>'
. '<script async custom-element="amp-anim" src="https://cdn.ampproject.org/v0/amp-anim-0.1.js"></script>'
. '</head><body><amp-anim></amp-anim></body></html>',
[],
[
AutoExtensionsConfiguration::REMOVE_UNNEEDED_EXTENSIONS => true,
]
],
];
}

Expand Down

0 comments on commit 1d2aefe

Please sign in to comment.