Migrate email editor dom document helper class to WP Coding Standard

[MAILPOET-6240]
This commit is contained in:
Jan Lysý
2024-11-01 20:11:44 +01:00
committed by Jan Lysý
parent eccf8ce0a2
commit fc7e06730b
7 changed files with 88 additions and 45 deletions

View File

@@ -86,14 +86,14 @@ class Button extends Abstract_Block_Renderer {
}
$dom_helper = new Dom_Document_Helper( $parsed_block['innerHTML'] );
$block_classname = $dom_helper->getAttributeValueByTagName( 'div', 'class' ) ?? '';
$button_link = $dom_helper->findElement( 'a' );
$block_classname = $dom_helper->get_attribute_value_by_tag_name( 'div', 'class' ) ?? '';
$button_link = $dom_helper->find_element( 'a' );
if ( ! $button_link ) {
return '';
}
$button_text = $dom_helper->getElementInnerHTML( $button_link ) ? $dom_helper->getElementInnerHTML( $button_link ) : '';
$button_text = $dom_helper->get_element_inner_html( $button_link ) ? $dom_helper->get_element_inner_html( $button_link ) : '';
$button_url = $button_link->getAttribute( 'href' ) ? $button_link->getAttribute( 'href' ) : '#';
$block_attributes = wp_parse_args(

View File

@@ -56,7 +56,7 @@ class Column extends Abstract_Block_Renderer {
* @param Settings_Controller $settings_controller Settings controller.
*/
private function get_block_wrapper( string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string {
$original_wrapper_classname = ( new Dom_Document_Helper( $block_content ) )->getAttributeValueByTagName( 'div', 'class' ) ?? '';
$original_wrapper_classname = ( new Dom_Document_Helper( $block_content ) )->get_attribute_value_by_tag_name( 'div', 'class' ) ?? '';
$block_attributes = wp_parse_args(
$parsed_block['attrs'] ?? array(),
array(

View File

@@ -45,7 +45,7 @@ class Columns extends Abstract_Block_Renderer {
* @param Settings_Controller $settings_controller Settings controller.
*/
private function getBlockWrapper( string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string {
$original_wrapper_classname = ( new Dom_Document_Helper( $block_content ) )->getAttributeValueByTagName( 'div', 'class' ) ?? '';
$original_wrapper_classname = ( new Dom_Document_Helper( $block_content ) )->get_attribute_value_by_tag_name( 'div', 'class' ) ?? '';
$block_attributes = wp_parse_args(
$parsed_block['attrs'] ?? array(),
array(

View File

@@ -47,7 +47,7 @@ class Group extends Abstract_Block_Renderer {
* @param Settings_Controller $settings_controller Settings controller.
*/
private function get_block_wrapper( string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string {
$original_classname = ( new Dom_Document_Helper( $block_content ) )->getAttributeValueByTagName( 'div', 'class' ) ?? '';
$original_classname = ( new Dom_Document_Helper( $block_content ) )->get_attribute_value_by_tag_name( 'div', 'class' ) ?? '';
$block_attributes = wp_parse_args(
$parsed_block['attrs'] ?? array(),
array(

View File

@@ -320,21 +320,21 @@ class Image extends Abstract_Block_Renderer {
$dom_helper = new Dom_Document_Helper( $block_content );
$figure_tag = $dom_helper->findElement( 'figure' );
$figure_tag = $dom_helper->find_element( 'figure' );
if ( ! $figure_tag ) {
return null;
}
$img_tag = $dom_helper->findElement( 'img' );
$img_tag = $dom_helper->find_element( 'img' );
if ( ! $img_tag ) {
return null;
}
$image_src = $dom_helper->getAttributeValue( $img_tag, 'src' );
$image_class = $dom_helper->getAttributeValue( $img_tag, 'class' );
$image_html = $dom_helper->getOuterHtml( $img_tag );
$figcaption = $dom_helper->findElement( 'figcaption' );
$figcaption_html = $figcaption ? $dom_helper->getOuterHtml( $figcaption ) : '';
$image_src = $dom_helper->get_attribute_value( $img_tag, 'src' );
$image_class = $dom_helper->get_attribute_value( $img_tag, 'class' );
$image_html = $dom_helper->get_outer_html( $img_tag );
$figcaption = $dom_helper->find_element( 'figcaption' );
$figcaption_html = $figcaption ? $dom_helper->get_outer_html( $figcaption ) : '';
$figcaption_html = str_replace( array( '<figcaption', '</figcaption>' ), array( '<span', '</span>' ), $figcaption_html );
return array(

View File

@@ -1,62 +1,105 @@
<?php declare(strict_types = 1);
<?php
/**
* This file is part of the MailPoet plugin.
*
* @package MailPoet\EmailEditor
*/
declare( strict_types = 1 );
namespace MailPoet\EmailEditor\Integrations\Utils;
/**
* This class should guarantee that our work with the DOMDocument is unified and safe.
*/
class Dom_Document_Helper {
/**
* Instance of the DOMDocument.
*
* @var \DOMDocument
*/
private \DOMDocument $dom;
public function __construct(
string $htmlContent
) {
$this->loadHtml( $htmlContent );
/**
* Constructor.
*
* @param string $html_content The HTML content to load.
*/
public function __construct( string $html_content ) {
$this->load_html( $html_content );
}
private function loadHtml( string $htmlContent ): void {
/**
* Loads the given HTML content into the DOMDocument.
*
* @param string $html_content The HTML content to load.
*/
private function load_html( string $html_content ): 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 );
if ( ! empty( $html_content ) ) {
// prefixing the content with the XML declaration to force the input encoding to UTF-8.
$this->dom->loadHTML( '<?xml encoding="UTF-8">' . $html_content, 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;
/**
* Searches for the first appearance of the given tag name.
*
* @param string $tag_name The tag name to search for.
*/
public function find_element( string $tag_name ): ?\DOMElement {
$elements = $this->dom->getElementsByTagName( $tag_name );
return $elements->item( 0 ) ? $elements->item( 0 ) : null;
}
public function getAttributeValue( \DOMElement $element, string $attribute ): string {
/**
* Returns the value of the given attribute from the given element.
*
* @param \DOMElement $element The element to get the attribute value from.
* @param string $attribute The attribute to get the value from.
*/
public function get_attribute_value( \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.
*
* @param string $tag_name The tag name to search for.
* @param string $attribute The attribute to get the value from.
*/
public function getAttributeValueByTagName( string $tagName, string $attribute ): ?string {
$element = $this->findElement( $tagName );
public function get_attribute_value_by_tag_name( string $tag_name, string $attribute ): ?string {
$element = $this->find_element( $tag_name );
if ( ! $element ) {
return null;
}
return $this->getAttributeValue( $element, $attribute );
return $this->get_attribute_value( $element, $attribute );
}
public function getOuterHtml( \DOMElement $element ): string {
/**
* Returns the outer HTML of the given element.
*
* @param \DOMElement $element The element to get the outer HTML from.
*/
public function get_outer_html( \DOMElement $element ): string {
return (string) $this->dom->saveHTML( $element );
}
public function getElementInnerHTML( \DOMElement $element ): string {
$innerHTML = '';
$children = $element->childNodes;
/**
* Returns the inner HTML of the given element.
*
* @param \DOMElement $element The element to get the inner HTML from.
*/
public function get_element_inner_html( \DOMElement $element ): string {
$inner_html = '';
$children = $element->childNodes; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
foreach ( $children as $child ) {
if ( ! $child instanceof \DOMNode ) {
continue;
}
$innerHTML .= $this->dom->saveHTML( $child );
$inner_html .= $this->dom->saveHTML( $child );
}
return $innerHTML;
return $inner_html;
}
}

View File

@@ -6,8 +6,8 @@ class Dom_Document_Helper_Test extends \MailPoetUnitTest {
public function testItFindsElement(): void {
$html = '<div><p>Some text</p></div>';
$domDocumentHelper = new Dom_Document_Helper($html);
$element = $domDocumentHelper->findElement('p');
$empty = $domDocumentHelper->findElement('span');
$element = $domDocumentHelper->find_element('p');
$empty = $domDocumentHelper->find_element('span');
$this->assertInstanceOf(\DOMElement::class, $element);
$this->assertEquals('p', $element->tagName);
$this->assertNull($empty);
@@ -16,30 +16,30 @@ class Dom_Document_Helper_Test extends \MailPoetUnitTest {
public function testItGetsAttributeValue(): void {
$html = '<div><p class="some-class">Some text</p></div>';
$domDocumentHelper = new Dom_Document_Helper($html);
$element = $domDocumentHelper->findElement('p');
$element = $domDocumentHelper->find_element('p');
$this->assertInstanceOf(\DOMElement::class, $element);
$this->assertEquals('some-class', $domDocumentHelper->getAttributeValue($element, 'class'));
$this->assertEquals('some-class', $domDocumentHelper->get_attribute_value($element, 'class'));
}
public function testItGetsOuterHtml(): void {
$html = '<div><span>Some <strong>text</strong></span></div>';
$domDocumentHelper = new Dom_Document_Helper($html);
$element = $domDocumentHelper->findElement('span');
$element = $domDocumentHelper->find_element('span');
$this->assertInstanceOf(\DOMElement::class, $element);
$this->assertEquals('<span>Some <strong>text</strong></span>', $domDocumentHelper->getOuterHtml($element));
$this->assertEquals('<span>Some <strong>text</strong></span>', $domDocumentHelper->get_outer_html($element));
// testings encoding of special characters
$html = '<div><img src="https://test.com/DALL·E-A®∑oecasƒ-803x1024.jpg"></div>';
$domDocumentHelper = new Dom_Document_Helper($html);
$element = $domDocumentHelper->findElement('img');
$element = $domDocumentHelper->find_element('img');
$this->assertInstanceOf(\DOMElement::class, $element);
$this->assertEquals('<img src="https://test.com/DALL%C2%B7E-A%C2%AE%E2%88%91oecas%C6%92-803x1024.jpg">', $domDocumentHelper->getOuterHtml($element));
$this->assertEquals('<img src="https://test.com/DALL%C2%B7E-A%C2%AE%E2%88%91oecas%C6%92-803x1024.jpg">', $domDocumentHelper->get_outer_html($element));
}
public function testItGetsAttributeValueByTagName(): void {
$html = '<div><p class="some-class">Some text</p><p class="second-paragraph"></p></div>';
$domDocumentHelper = new Dom_Document_Helper($html);
$this->assertEquals('some-class', $domDocumentHelper->getAttributeValueByTagName('p', 'class'));
$this->assertNull($domDocumentHelper->getAttributeValueByTagName('span', 'class'));
$this->assertEquals('some-class', $domDocumentHelper->get_attribute_value_by_tag_name('p', 'class'));
$this->assertNull($domDocumentHelper->get_attribute_value_by_tag_name('span', 'class'));
}
}