Skip to content

Commit 3dffe71

Browse files
committed
#558: Content Types Output Style Attribute Removal
- Refactoring Using DOMDocument - Adding `generateDataAttribute` Method - Adding `generateInternalStyles` Method - Modifying Ordering of Expected vs. Actual in PageBuilderStripStylesTest
1 parent 90f7518 commit 3dffe71

File tree

2 files changed

+78
-17
lines changed

2 files changed

+78
-17
lines changed

app/code/Magento/PageBuilder/Setup/Converters/PageBuilderStripStyles.php

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,47 +8,108 @@
88

99
namespace Magento\PageBuilder\Setup\Converters;
1010

11+
use DOMDocument;
12+
use DOMElement;
13+
use DOMException;
14+
use DOMNode;
15+
use DOMXPath;
16+
use Exception;
1117
use Magento\Framework\DB\DataConverter\DataConverterInterface;
12-
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
13-
use Magento\PageBuilder\Model\Dom\HtmlDocumentFactory;
1418

1519
/**
1620
* ...
1721
*/
1822
class PageBuilderStripStyles implements DataConverterInterface
1923
{
2024
/**
21-
* @var HtmlDocumentFactory
25+
* @var DOMDocument
2226
*/
23-
private $htmlDocumentFactory;
27+
private $domDocument;
2428

2529
/**
26-
* @param HtmlDocumentFactory $htmlDocumentFactory
30+
* @param DOMDocument $domDocument
2731
*/
28-
public function __construct(HtmlDocumentFactory $htmlDocumentFactory)
32+
public function __construct(DOMDocument $domDocument)
2933
{
30-
$this->htmlDocumentFactory = $htmlDocumentFactory;
34+
$this->domDocument = $domDocument;
35+
}
36+
37+
/**
38+
* Generates `mageUtils.uniqueid()` Naming Convention
39+
*
40+
* @param int $length
41+
* @param string $prefix
42+
* @return string
43+
*/
44+
private function generateDataAttribute(int $length = 7, string $prefix = 'style-'): string
45+
{
46+
return $prefix . substr(strtoupper(uniqid()), 0, $length); // @todo: Fix RNGesus...
47+
}
48+
49+
/**
50+
* Converts Inline Styles to Internal Styles
51+
*
52+
* @param array $styleMap
53+
* @return string
54+
*/
55+
private function generateInternalStyles(array $styleMap): string
56+
{
57+
$output = '';
58+
59+
foreach ($styleMap as $selector => $styles) {
60+
$output .= '[data-pb-style="' . $selector . '"] { ';
61+
$output .= $styles;
62+
$output .= ' }';
63+
}
64+
65+
return $output;
3166
}
3267

3368
/**
3469
* @inheritDoc
3570
*/
3671
public function convert($value)
3772
{
38-
$document = $this->htmlDocumentFactory->create(['document' => $value]);
39-
$queryPageBuilderElements = $document->querySelectorAll('[style]'); // @todo: Match Types
73+
$document = new DOMDocument();
74+
$document->loadHTML($value);
75+
$xpath = new DOMXPath($document);
76+
77+
// Query for Inline Styles
78+
$nodes = $xpath->query('//*[@style]');
4079

41-
/** @var ElementInterface $element */
42-
foreach ($queryPageBuilderElements as $element) {
43-
$style = $element->getAttribute('style');
80+
$styleMap = array();
4481

45-
if ($style) {
46-
// @todo: Convert to Inline
82+
foreach ($nodes as $node) {
83+
/* @var DOMElement $node */
84+
$styleAttr = $node->getAttribute('style');
4785

48-
$element->removeAttribute('style');
86+
if ($styleAttr) {
87+
$generatedDataAttribute = $this->generateDataAttribute();
88+
$node->setAttribute('data-pb-style', $this->generateDataAttribute());
89+
90+
// Add for Internal Style Generation
91+
$styleMap[$generatedDataAttribute] = $styleAttr;
92+
93+
// Strip Inline Styles
94+
$node->removeAttribute('style');
4995
}
5096
}
5197

52-
return $queryPageBuilderElements->count() > 0 ? $document->stripHtmlWrapperTags() : $value;
98+
// Style Block Generation
99+
$style = $document->createElement(
100+
'style',
101+
$this->generateInternalStyles($styleMap)
102+
);
103+
$style->setAttribute('type', 'text/css');
104+
$xpath->query('//body')[0]->appendChild($style); // @todo: Refactor
105+
106+
// @todo: Refactor
107+
preg_match(
108+
'/<html><body>(.+)<\/body><\/html>$/si',
109+
$document->saveHTML(),
110+
$matches
111+
);
112+
113+
return $matches && $nodes->count() > 0 ? $matches[1] : $value;
53114
}
54115
}

dev/tests/integration/testsuite/Magento/PageBuilder/Setup/Converters/PageBuilderStripStylesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testConversion(string $htmlString, string $expectedResult)
3333
{
3434
$converterPageBuilderStripStyles = $this->objectManager->create(PageBuilderStripStyles::class);
3535
$result = $converterPageBuilderStripStyles->convert($htmlString);
36-
$this->assertEquals($result, $expectedResult);
36+
$this->assertEquals($expectedResult, $result);
3737
}
3838

3939
/**

0 commit comments

Comments
 (0)