Add getAttributeValueByTagName to dom helper

I'm adding this method to cover use-cases where we need to extract classes
from a block's HTML.
[MAILPOET-5741]
This commit is contained in:
Rostislav Wolny
2024-02-22 12:24:56 +01:00
committed by Rostislav Wolný
parent 875fde56e9
commit d35e498f36
2 changed files with 18 additions and 0 deletions

View File

@@ -33,6 +33,17 @@ class DomDocumentHelper {
return $element->hasAttribute($attribute) ? $element->getAttribute($attribute) : '';
}
/**
* Searches for the first appearance of the given tag name and returns the value of specified attribute.
*/
public function getAttributeValueByTagName(string $tagName, string $attribute): ?string {
$element = $this->findElement($tagName);
if (!$element) {
return null;
}
return $this->getAttributeValue($element, $attribute);
}
public function getOuterHtml(\DOMElement $element): string {
return (string)$this->dom->saveHTML($element);
}