|
18 | 18 | */
|
19 | 19 | class PageBuilderStripStyles implements DataConverterInterface
|
20 | 20 | {
|
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]'; |
24 | 24 |
|
25 | 25 | /**
|
26 | 26 | * @var DOMDocument
|
@@ -76,34 +76,38 @@ public function convert($value): string
|
76 | 76 |
|
77 | 77 | $body = $document->documentElement->lastChild;
|
78 | 78 | $nodes = $xpath->query(self::XPATH_SELECTOR); // Query for Inline Styles
|
79 |
| - $styleMap = []; |
80 | 79 |
|
81 |
| - foreach ($nodes as $node) { |
82 |
| - /* @var DOMElement $node */ |
83 |
| - $styleAttr = $node->getAttribute('style'); |
| 80 | + if ($nodes->count() > 0) { |
| 81 | + $styleMap = []; |
84 | 82 |
|
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 | + } |
90 | 93 | }
|
| 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; |
91 | 109 | }
|
92 | 110 |
|
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; |
108 | 112 | }
|
109 | 113 | }
|
0 commit comments