|
8 | 8 |
|
9 | 9 | namespace Magento\PageBuilder\Setup\Converters;
|
10 | 10 |
|
| 11 | +use DOMDocument; |
| 12 | +use DOMElement; |
| 13 | +use DOMException; |
| 14 | +use DOMNode; |
| 15 | +use DOMXPath; |
| 16 | +use Exception; |
11 | 17 | use Magento\Framework\DB\DataConverter\DataConverterInterface;
|
12 |
| -use Magento\PageBuilder\Model\Dom\Adapter\ElementInterface; |
13 |
| -use Magento\PageBuilder\Model\Dom\HtmlDocumentFactory; |
14 | 18 |
|
15 | 19 | /**
|
16 | 20 | * ...
|
17 | 21 | */
|
18 | 22 | class PageBuilderStripStyles implements DataConverterInterface
|
19 | 23 | {
|
20 | 24 | /**
|
21 |
| - * @var HtmlDocumentFactory |
| 25 | + * @var DOMDocument |
22 | 26 | */
|
23 |
| - private $htmlDocumentFactory; |
| 27 | + private $domDocument; |
24 | 28 |
|
25 | 29 | /**
|
26 |
| - * @param HtmlDocumentFactory $htmlDocumentFactory |
| 30 | + * @param DOMDocument $domDocument |
27 | 31 | */
|
28 |
| - public function __construct(HtmlDocumentFactory $htmlDocumentFactory) |
| 32 | + public function __construct(DOMDocument $domDocument) |
29 | 33 | {
|
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; |
31 | 66 | }
|
32 | 67 |
|
33 | 68 | /**
|
34 | 69 | * @inheritDoc
|
35 | 70 | */
|
36 | 71 | public function convert($value)
|
37 | 72 | {
|
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]'); |
40 | 79 |
|
41 |
| - /** @var ElementInterface $element */ |
42 |
| - foreach ($queryPageBuilderElements as $element) { |
43 |
| - $style = $element->getAttribute('style'); |
| 80 | + $styleMap = array(); |
44 | 81 |
|
45 |
| - if ($style) { |
46 |
| - // @todo: Convert to Inline |
| 82 | + foreach ($nodes as $node) { |
| 83 | + /* @var DOMElement $node */ |
| 84 | + $styleAttr = $node->getAttribute('style'); |
47 | 85 |
|
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'); |
49 | 95 | }
|
50 | 96 | }
|
51 | 97 |
|
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; |
53 | 114 | }
|
54 | 115 | }
|
0 commit comments