Skip to content

Commit 043b358

Browse files
committed
#558: Content Types Output Style Attribute Removal
- Merge from `558_1_content-type-style-attribute-removal-proposed` - Code Review Feedback/Amends
1 parent 60343e5 commit 043b358

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

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

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*/
1818
class PageBuilderStripStyles implements DataConverterInterface
1919
{
20-
const BODY_ID = 'html-body';
21-
const DATA_ATTRIBUTE = 'data-pb-style';
22-
const SELECTOR = '[style]';
20+
private const BODY_ID = 'html-body';
21+
private const DATA_ATTRIBUTE = 'data-pb-style';
22+
private const SELECTOR = '[style]';
2323

2424
/**
2525
* @var HtmlDocumentFactory
@@ -35,7 +35,7 @@ public function __construct(HtmlDocumentFactory $htmlDocumentFactory)
3535
}
3636

3737
/**
38-
* Selecting all elements that contains inline styles and convert them in style blocks
38+
* Generates `mageUtils.uniqueid()` Naming Convention
3939
*
4040
* @return string
4141
*/
@@ -68,33 +68,38 @@ private function generateInternalStyles(array $styleMap): string
6868
public function convert($value): string
6969
{
7070
$document = $this->htmlDocumentFactory->create([ 'document' => $value ]);
71-
$nodes = $document->querySelectorAll(self::SELECTOR);
7271
$body = $document->querySelector('body');
73-
$styleMap = [];
72+
$nodes = $document->querySelectorAll(self::SELECTOR);
7473

75-
foreach ($nodes as $node) {
76-
/* @var ElementInterface $node */
77-
$styleAttr = $node->getAttribute('style');
74+
if ($nodes->count() > 0) {
75+
$styleMap = [];
7876

79-
if ($styleAttr) {
80-
$generatedDataAttribute = $this->generateDataAttribute();
81-
$node->setAttribute(self::DATA_ATTRIBUTE, $generatedDataAttribute);
82-
$styleMap[$generatedDataAttribute] = $styleAttr; // Amend Array for Internal Style Generation
83-
$node->removeAttribute('style');
77+
foreach ($nodes as $node) {
78+
/* @var ElementInterface $node */
79+
$styleAttr = $node->getAttribute('style');
80+
81+
if ($styleAttr) {
82+
$generatedDataAttribute = $this->generateDataAttribute();
83+
$node->setAttribute(self::DATA_ATTRIBUTE, $generatedDataAttribute);
84+
$styleMap[$generatedDataAttribute] = $styleAttr; // Amend Array for Internal Style Generation
85+
$node->removeAttribute('style');
86+
}
8487
}
85-
}
8688

87-
if (count($styleMap) > 0) {
88-
// Style Block Generation
89-
$style = $document->createElement(
90-
'style',
91-
$this->generateInternalStyles($styleMap)
92-
);
93-
$body->appendChild($style);
94-
95-
return $document->stripHtmlWrapperTags();
96-
} else {
97-
return $value;
89+
if (count($styleMap) > 0) {
90+
// Style Block Generation
91+
$style = $document->createElement(
92+
'style',
93+
$this->generateInternalStyles($styleMap)
94+
);
95+
$body->appendChild($style);
96+
97+
return $document->stripHtmlWrapperTags();
98+
} else {
99+
return $value;
100+
}
98101
}
102+
103+
return $value;
99104
}
100105
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
class PageBuilderStripStylesTest extends TestCase
1919
{
20+
private const XPATH_SELECTOR = '//*[@data-content-type][@style]|//*[@data-content-type]/*[@style]';
21+
2022
/**
2123
* @var ObjectManagerInterface
2224
*/
@@ -53,7 +55,7 @@ public function testConvert(string $htmlString, int $expectedStyleTags)
5355
\libxml_clear_errors();
5456

5557
$getInternalStyles = false;
56-
$nodesBefore = $xpathBefore->query(PageBuilderStripStyles::XPATH_SELECTOR);
58+
$nodesBefore = $xpathBefore->query(self::XPATH_SELECTOR);
5759
$styleRules = [];
5860

5961
foreach ($nodesBefore as $node) {
@@ -67,7 +69,7 @@ public function testConvert(string $htmlString, int $expectedStyleTags)
6769
}
6870

6971
// Assert Inline Styles Were Removed
70-
$this->assertEquals(0, \count($xpathAfter->query(PageBuilderStripStyles::XPATH_SELECTOR)));
72+
$this->assertEquals(0, \count($xpathAfter->query(self::XPATH_SELECTOR)));
7173

7274
// Assert Expected Internal Styles
7375
$this->assertEquals($expectedStyleTags, \count($xpathAfter->query('//style')));

0 commit comments

Comments
 (0)