diff --git a/mailpoet/lib/EmailEditor/Integrations/Utils/DomDocumentHelper.php b/mailpoet/lib/EmailEditor/Integrations/Utils/DomDocumentHelper.php index 28d1d50080..b99b47fc1d 100644 --- a/mailpoet/lib/EmailEditor/Integrations/Utils/DomDocumentHelper.php +++ b/mailpoet/lib/EmailEditor/Integrations/Utils/DomDocumentHelper.php @@ -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); } diff --git a/mailpoet/tests/unit/EmailEditor/Integrations/Utils/DomDocumentHelperTest.php b/mailpoet/tests/unit/EmailEditor/Integrations/Utils/DomDocumentHelperTest.php index bcf94be18a..4547fbd256 100644 --- a/mailpoet/tests/unit/EmailEditor/Integrations/Utils/DomDocumentHelperTest.php +++ b/mailpoet/tests/unit/EmailEditor/Integrations/Utils/DomDocumentHelperTest.php @@ -37,4 +37,11 @@ class DomDocumentHelperTest extends \MailPoetUnitTest { $this->assertInstanceOf(\DOMElement::class, $element); $this->assertEquals('', $domDocumentHelper->getOuterHtml($element)); } + + public function testItGetsAttributeValueByTagName(): void { + $html = '

Some text

'; + $domDocumentHelper = new DomDocumentHelper($html); + $this->assertEquals('some-class', $domDocumentHelper->getAttributeValueByTagName('p', 'class')); + $this->assertNull($domDocumentHelper->getAttributeValueByTagName('span', 'class')); + } }