configuration = $configuration; } /** * Apply transformations to the provided DOM document. * * @param Document $document DOM document to apply the transformations to. * @param ErrorCollection $errors Collection of errors that are collected during transformation. * @return void */ public function transform(Document $document, ErrorCollection $errors) { if ($this->configuration->get(OptimizeAmpBindConfiguration::ENABLED) === false) { return; } if (!$this->hasAmpBindScriptElement($document)) { return; } $document->html->addBooleanAttribute(Attribute::I_AMPHTML_BINDING); for ($node = $document->html; $node !== null; $node = NodeWalker::nextNode($node)) { if (!$node instanceof Element) { continue; } if (Amp::isTemplate($node)) { $node = NodeWalker::skipNodeAndChildren($node); continue; } /** @var DOMAttr $attribute */ foreach ($node->attributes as $attribute) { if (strpos($attribute->name, Document::AMP_BIND_DATA_ATTR_PREFIX) === 0) { $node->addBooleanAttribute(Attribute::I_AMPHTML_BINDING); break; } } } } /** * Check whether the document has an amp-bind script element. * * @param Document $document Document to check. * @return bool Whether the document has an amp-bind script element. */ private function hasAmpBindScriptElement(Document $document) { for ($element = $document->head->firstChild; $element !== null; $element = $element->nextSibling) { if (!$element instanceof Element) { continue; } if ($element->tagName !== Tag::SCRIPT) { continue; } if ($element->getAttribute(Attribute::CUSTOM_ELEMENT) !== Extension::BIND) { continue; } return true; } return false; } }