Files
piratepoet/mailpoet/lib/EmailEditor/Integrations/Utils/DomDocumentHelper.php
Rostislav Wolny d35e498f36 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]
2024-02-28 09:01:20 +01:00

51 lines
1.5 KiB
PHP

<?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Integrations\Utils;
/**
* This class should guarantee that our work with the DOMDocument is unified and safe.
*/
class DomDocumentHelper {
private \DOMDocument $dom;
public function __construct(
string $htmlContent
) {
$this->loadHtml($htmlContent);
}
private function loadHtml(string $htmlContent): void {
libxml_use_internal_errors(true);
$this->dom = new \DOMDocument();
if (!empty($htmlContent)) {
// prefixing the content with the XML declaration to force the input encoding to UTF-8
$this->dom->loadHTML('<?xml encoding="UTF-8">' . $htmlContent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
}
libxml_clear_errors();
}
public function findElement(string $tagName): ?\DOMElement {
$elements = $this->dom->getElementsByTagName($tagName);
return $elements->item(0) ?: null;
}
public function getAttributeValue(\DOMElement $element, string $attribute): string {
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);
}
}