Skip to content

Commit

Permalink
Ignore extension from being auto import if found in ignore list
Browse files Browse the repository at this point in the history
  • Loading branch information
ediamin committed Aug 23, 2021
1 parent 1e86602 commit d626d4d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Optimizer/Transformer/AutoExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private function addRequiredExtensionByAttributes(Document $document, Element $n
$globalAttributes = $this->spec->attributeLists()->get(GlobalAttrs::ID);

foreach ($node->attributes as $attribute) {
// Add attribute dependencies (e.g. amp-img => lightbox => amp-lightbox-gallery)
// Add attribute dependencies (e.g. amp-img => lightbox => amp-lightbox-gallery).
if (array_key_exists($attribute->name, self::MANUAL_ATTRIBUTE_TO_EXTENSION_MAPPING)) {
$extensionScripts = $this->maybeAddExtension(
$document,
Expand All @@ -299,7 +299,12 @@ private function addRequiredExtensionByAttributes(Document $document, Element $n
}
}

// Attribute that requires AMP components (e.g. amp-fx).
/**
* Attribute that requires AMP components (e.g. amp-fx).
*
* Ignoring next line in phpstan because Attributes data type doesn't match.
* @phpstan-ignore-next-line
*/
if (! empty($globalAttributes::ATTRIBUTES[$attribute->name][SpecRule::REQUIRES_EXTENSION])) {
$requiresExtensions = $globalAttributes::ATTRIBUTES[$attribute->name][SpecRule::REQUIRES_EXTENSION];
foreach ($requiresExtensions as $requiresExtension) {
Expand All @@ -314,7 +319,8 @@ private function addRequiredExtensionByAttributes(Document $document, Element $n

/**
* EXPERIMENTAL FEATURE: Rewrite short-form `bindtext` to `data-amp-bind-text` to avoid false-positives we
* check for each tag only the supported bindable attributes (e.g. for a div only bindtext, but not bindvalue).
* check for each tag only the supported bindable attributes (e.g. for a div only bindtext, but not
* bindvalue).
*/
if (
$this->configuration->get(AutoExtensionsConfiguration::EXPERIMENT_BIND_ATTRIBUTE)
Expand All @@ -324,9 +330,11 @@ private function addRequiredExtensionByAttributes(Document $document, Element $n
$attributeNameWithoutBindPrefix = substr($attribute->name, strlen(self::BIND_SHORT_FORM_PREFIX));

// Change the name from aria-labelledby to [ARIA_LABELLEDBY].
$bindableAttributeName = '[' . preg_replace('/\-/', '_', strtoupper($attributeNameWithoutBindPrefix)) . ']';
$bindableAttributeName = '['
. preg_replace('/\-/', '_', strtoupper($attributeNameWithoutBindPrefix))
. ']';

// Rename attribute from bindx to data-amp-bind-x
// Rename attribute from bindx to data-amp-bind-x.
if ($globalAttributes->has($bindableAttributeName)) {
$newAttributeName = Amp::BIND_DATA_ATTR_PREFIX . $attributeNameWithoutBindPrefix;
$newAttributes[$newAttributeName] = $attribute->value;
Expand Down Expand Up @@ -473,6 +481,10 @@ private function getExtensionScriptsReferenceNode(Document $document)
*/
private function maybeAddExtension(Document $document, $extensionScripts, $requiredExtension)
{
if (in_array($requiredExtension, $this->configuration->get(AutoExtensionsConfiguration::IGNORE))) {
return $extensionScripts;
}

if (!array_key_exists($requiredExtension, $extensionScripts)) {
$tagSpecs = $this->spec->tags()->byExtensionSpec($requiredExtension);

Expand Down
19 changes: 19 additions & 0 deletions tests/Optimizer/Transformer/AutoExtensionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace AmpProject\Optimizer\Transformer;

use AmpProject\Dom\Document;
use AmpProject\Extension;
use AmpProject\Optimizer\Configuration\AutoExtensionsConfiguration;
use AmpProject\Optimizer\Error;
use AmpProject\Optimizer\ErrorCollection;
Expand Down Expand Up @@ -42,6 +43,24 @@ public function dataTransform()
. '<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>',
],

'do not include extension if set in ignore list' => [
TestMarkup::DOCTYPE . '<html><head>' . TestMarkup::META_CHARSET . '</head><body>'
. ' <form class="sample-form" method="GET" action="/documentation/examples/api/submit-form" target="_top">'
. ' <input type="search" placeholder="Search..." name="search">'
. ' <input type="submit" value="OK">'
. ' </form>'
. '</body></html>',
TestMarkup::DOCTYPE . '<html><head>' . TestMarkup::META_CHARSET
. '</head><body>'
. '<form class="sample-form" method="GET" action="/documentation/examples/api/submit-form" target="_top">'
. '<input type="search" placeholder="Search..." name="search"><input type="submit" value="OK"></form>'
. '</body></html>',
[],
[
AutoExtensionsConfiguration::IGNORE => [Extension::FORM]
]
],
];
}

Expand Down

0 comments on commit d626d4d

Please sign in to comment.