Skip to content

Commit

Permalink
#558: Content Types Output Style Attribute Removal
Browse files Browse the repository at this point in the history
- Refactoring Using DOMDocument
- Adding `generateDataAttribute` Method
- Adding `generateInternalStyles` Method
- Modifying Ordering of Expected vs. Actual in PageBuilderStripStylesTest
  • Loading branch information
bluemwhitew committed Sep 9, 2020
1 parent 90f7518 commit 3dffe71
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,108 @@

namespace Magento\PageBuilder\Setup\Converters;

use DOMDocument;
use DOMElement;
use DOMException;
use DOMNode;
use DOMXPath;
use Exception;
use Magento\Framework\DB\DataConverter\DataConverterInterface;
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
use Magento\PageBuilder\Model\Dom\HtmlDocumentFactory;

/**
* ...
*/
class PageBuilderStripStyles implements DataConverterInterface
{
/**
* @var HtmlDocumentFactory
* @var DOMDocument
*/
private $htmlDocumentFactory;
private $domDocument;

/**
* @param HtmlDocumentFactory $htmlDocumentFactory
* @param DOMDocument $domDocument
*/
public function __construct(HtmlDocumentFactory $htmlDocumentFactory)
public function __construct(DOMDocument $domDocument)
{
$this->htmlDocumentFactory = $htmlDocumentFactory;
$this->domDocument = $domDocument;
}

/**
* Generates `mageUtils.uniqueid()` Naming Convention
*
* @param int $length
* @param string $prefix
* @return string
*/
private function generateDataAttribute(int $length = 7, string $prefix = 'style-'): string
{
return $prefix . substr(strtoupper(uniqid()), 0, $length); // @todo: Fix RNGesus...
}

/**
* Converts Inline Styles to Internal Styles
*
* @param array $styleMap
* @return string
*/
private function generateInternalStyles(array $styleMap): string
{
$output = '';

foreach ($styleMap as $selector => $styles) {
$output .= '[data-pb-style="' . $selector . '"] { ';
$output .= $styles;
$output .= ' }';
}

return $output;
}

/**
* @inheritDoc
*/
public function convert($value)
{
$document = $this->htmlDocumentFactory->create(['document' => $value]);
$queryPageBuilderElements = $document->querySelectorAll('[style]'); // @todo: Match Types
$document = new DOMDocument();
$document->loadHTML($value);
$xpath = new DOMXPath($document);

// Query for Inline Styles
$nodes = $xpath->query('//*[@style]');

/** @var ElementInterface $element */
foreach ($queryPageBuilderElements as $element) {
$style = $element->getAttribute('style');
$styleMap = array();

if ($style) {
// @todo: Convert to Inline
foreach ($nodes as $node) {
/* @var DOMElement $node */
$styleAttr = $node->getAttribute('style');

$element->removeAttribute('style');
if ($styleAttr) {
$generatedDataAttribute = $this->generateDataAttribute();
$node->setAttribute('data-pb-style', $this->generateDataAttribute());

// Add for Internal Style Generation
$styleMap[$generatedDataAttribute] = $styleAttr;

// Strip Inline Styles
$node->removeAttribute('style');
}
}

return $queryPageBuilderElements->count() > 0 ? $document->stripHtmlWrapperTags() : $value;
// Style Block Generation
$style = $document->createElement(
'style',
$this->generateInternalStyles($styleMap)
);
$style->setAttribute('type', 'text/css');
$xpath->query('//body')[0]->appendChild($style); // @todo: Refactor

// @todo: Refactor
preg_match(
'/<html><body>(.+)<\/body><\/html>$/si',
$document->saveHTML(),
$matches
);

return $matches && $nodes->count() > 0 ? $matches[1] : $value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function testConversion(string $htmlString, string $expectedResult)
{
$converterPageBuilderStripStyles = $this->objectManager->create(PageBuilderStripStyles::class);
$result = $converterPageBuilderStripStyles->convert($htmlString);
$this->assertEquals($result, $expectedResult);
$this->assertEquals($expectedResult, $result);
}

/**
Expand Down

0 comments on commit 3dffe71

Please sign in to comment.