Skip to content

Commit 3f1a669

Browse files
#558: Content Types Output Style Attribute Removal
- use Dom adapter - replace all style
1 parent ac32a27 commit 3f1a669

File tree

5 files changed

+80
-34
lines changed

5 files changed

+80
-34
lines changed

app/code/Magento/PageBuilder/Model/Dom/Adapter/DocumentInterface.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public function __toString(): string;
2929
*/
3030
public function createDocumentFragment(): DocumentFragmentInterface;
3131

32+
/**
33+
* Create new document element
34+
*
35+
* @param string $name
36+
* @param string $value [optional]
37+
* @return ElementInterface
38+
*/
39+
public function createElement(string $name, string $value = null);
40+
3241
/**
3342
* Returns the first element matching the specified selector.
3443
*

app/code/Magento/PageBuilder/Model/Dom/Adapter/ElementInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,26 @@
77

88
namespace Magento\PageBuilder\Model\Dom\Adapter;
99

10+
use Gt\Dom\Element as GtDomElement;
11+
1012
/**
1113
* Interface for Element wrappers
1214
*/
1315
interface ElementInterface
1416
{
17+
/**
18+
* Return original element.
19+
* @return GtDomElement
20+
*/
21+
public function getOriginalElement(): GtDomElement;
22+
23+
/**
24+
* Adds new child at the end of the children
25+
* @param ElementInterface $newnode
26+
* @return ElementInterface
27+
*/
28+
public function appendChild(ElementInterface $element): ElementInterface;
29+
1530
/**
1631
* Returns true if the element would be selected by the specified selector string; otherwise, returns false.
1732
*

app/code/Magento/PageBuilder/Model/Dom/Document.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ public function createDocumentFragment(): DocumentFragmentInterface
6262
);
6363
}
6464

65+
/**
66+
* @inheritDoc
67+
*/
68+
public function createElement(string $name, string $value = null): ElementInterface
69+
{
70+
return $this->objectManager->create(
71+
ElementInterface::class,
72+
[ 'element' => $this->document->createElement($name, $value) ]
73+
);
74+
}
75+
6576
/**
6677
* @inheritDoc
6778
*/

app/code/Magento/PageBuilder/Model/Dom/Element.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ public function __construct(
4141
$this->element = $element;
4242
}
4343

44+
/**
45+
* @inheritDoc
46+
*/
47+
public function getOriginalElement(): GtDomElement
48+
{
49+
return $this->element;
50+
}
51+
52+
/**
53+
* @inheritDoc
54+
*/
55+
public function appendChild(ElementInterface $element): ElementInterface
56+
{
57+
return $this->objectManager->create(
58+
ElementInterface::class,
59+
[ 'element' => $this->element->appendChild($element->getOriginalElement()) ]
60+
);
61+
}
62+
63+
4464
/**
4565
* @inheritDoc
4666
*/

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

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88

99
namespace Magento\PageBuilder\Setup\Converters;
1010

11-
use DOMDocument;
12-
use DOMElement;
13-
use DOMXPath;
1411
use Magento\Framework\DB\DataConverter\DataConverterInterface;
12+
use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface;
13+
use Magento\PageBuilder\Model\Dom\HtmlDocumentFactory;
1514

1615
/**
1716
* Convert Inline Styles to Internal
@@ -20,23 +19,23 @@ class PageBuilderStripStyles implements DataConverterInterface
2019
{
2120
const BODY_ID = 'html-body';
2221
const DATA_ATTRIBUTE = 'data-pb-style';
23-
const XPATH_SELECTOR = '//*[@data-content-type][@style]|//*[@data-content-type]/*[@style]';
22+
const SELECTOR = '[style]';
2423

2524
/**
26-
* @var DOMDocument
25+
* @var HtmlDocumentFactory
2726
*/
28-
private $domDocument;
27+
private $htmlDocumentFactory;
2928

3029
/**
31-
* @param DOMDocument $domDocument
30+
* @param HtmlDocumentFactory $htmlDocumentFactory
3231
*/
33-
public function __construct(DOMDocument $domDocument)
32+
public function __construct(HtmlDocumentFactory $htmlDocumentFactory)
3433
{
35-
$this->domDocument = $domDocument;
34+
$this->htmlDocumentFactory = $htmlDocumentFactory;
3635
}
3736

3837
/**
39-
* Generates `mageUtils.uniqueid()` Naming Convention
38+
* Selecting all elements that contains inline styles and convert them in style blocks
4039
*
4140
* @return string
4241
*/
@@ -68,18 +67,13 @@ private function generateInternalStyles(array $styleMap): string
6867
*/
6968
public function convert($value): string
7069
{
71-
\libxml_use_internal_errors(true);
72-
$document = new DOMDocument();
73-
$document->loadHTML($value);
74-
$xpath = new DOMXPath($document);
75-
\libxml_clear_errors();
76-
77-
$body = $document->documentElement->lastChild;
78-
$nodes = $xpath->query(self::XPATH_SELECTOR); // Query for Inline Styles
70+
$document = $this->htmlDocumentFactory->create([ 'document' => $value ]);
71+
$nodes = $document->querySelectorAll(self::SELECTOR);
72+
$body = $document->querySelector('body');
7973
$styleMap = [];
8074

8175
foreach ($nodes as $node) {
82-
/* @var DOMElement $node */
76+
/* @var ElementInterface $node */
8377
$styleAttr = $node->getAttribute('style');
8478

8579
if ($styleAttr) {
@@ -90,20 +84,17 @@ public function convert($value): string
9084
}
9185
}
9286

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;
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;
98+
}
10899
}
109100
}

0 commit comments

Comments
 (0)