Skip to content

Commit 368d6fa

Browse files
committed
#558: Content Types Output Style Attribute Removal
- Code Review Feedback/Amends (Thanks, @barnyshergold/@omiroshnichenko)
1 parent ac32a27 commit 368d6fa

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
*/
1919
class PageBuilderStripStyles implements DataConverterInterface
2020
{
21-
const BODY_ID = 'html-body';
22-
const DATA_ATTRIBUTE = 'data-pb-style';
23-
const XPATH_SELECTOR = '//*[@data-content-type][@style]|//*[@data-content-type]/*[@style]';
21+
public const BODY_ID = 'html-body';
22+
public const DATA_ATTRIBUTE = 'data-pb-style';
23+
public const XPATH_SELECTOR = '//*[@data-content-type][@style]|//*[@data-content-type]/*[@style]';
2424

2525
/**
2626
* @var DOMDocument
@@ -76,34 +76,38 @@ public function convert($value): string
7676

7777
$body = $document->documentElement->lastChild;
7878
$nodes = $xpath->query(self::XPATH_SELECTOR); // Query for Inline Styles
79-
$styleMap = [];
8079

81-
foreach ($nodes as $node) {
82-
/* @var DOMElement $node */
83-
$styleAttr = $node->getAttribute('style');
80+
if ($nodes->count() > 0) {
81+
$styleMap = [];
8482

85-
if ($styleAttr) {
86-
$generatedDataAttribute = $this->generateDataAttribute();
87-
$node->setAttribute(self::DATA_ATTRIBUTE, $generatedDataAttribute);
88-
$styleMap[$generatedDataAttribute] = $styleAttr; // Amend Array for Internal Style Generation
89-
$node->removeAttribute('style');
83+
foreach ($nodes as $node) {
84+
/* @var DOMElement $node */
85+
$styleAttr = $node->getAttribute('style');
86+
87+
if ($styleAttr) {
88+
$generatedDataAttribute = $this->generateDataAttribute();
89+
$node->setAttribute(self::DATA_ATTRIBUTE, $generatedDataAttribute);
90+
$styleMap[$generatedDataAttribute] = $styleAttr; // Amend Array for Internal Style Generation
91+
$node->removeAttribute('style');
92+
}
9093
}
94+
95+
// Style Block Generation
96+
$style = $document->createElement(
97+
'style',
98+
$this->generateInternalStyles($styleMap)
99+
);
100+
$body->appendChild($style);
101+
102+
\preg_match(
103+
'/<html><body>(.+)<\/body><\/html>$/si',
104+
$document->saveHTML(),
105+
$matches
106+
);
107+
108+
return $matches ? $matches[1] : $value;
91109
}
92110

93-
// Style Block Generation
94-
$style = $document->createElement(
95-
'style',
96-
$this->generateInternalStyles($styleMap)
97-
);
98-
$body->appendChild($style);
99-
100-
// @todo: Refactor
101-
\preg_match(
102-
'/<html><body>(.+)<\/body><\/html>$/si',
103-
$document->saveHTML(),
104-
$matches
105-
);
106-
107-
return $matches && $nodes->count() > 0 ? $matches[1] : $value;
111+
return $value;
108112
}
109113
}

app/code/Magento/PageBuilder/Setup/Patch/Data/UpgradePageBuilderStripStyles.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(UpgradeContentHelper $helper)
3737
* @return void
3838
* @throws FieldDataConversionException
3939
*/
40-
public function apply()
40+
public function apply(): void
4141
{
4242
$this->helper->upgrade([
4343
PageBuilderStripStyles::class

0 commit comments

Comments
 (0)