17
17
*/
18
18
class PageBuilderStripStyles implements DataConverterInterface
19
19
{
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] ' ;
23
23
24
24
/**
25
25
* @var HtmlDocumentFactory
@@ -35,7 +35,7 @@ public function __construct(HtmlDocumentFactory $htmlDocumentFactory)
35
35
}
36
36
37
37
/**
38
- * Selecting all elements that contains inline styles and convert them in style blocks
38
+ * Generates `mageUtils.uniqueid()` Naming Convention
39
39
*
40
40
* @return string
41
41
*/
@@ -68,33 +68,38 @@ private function generateInternalStyles(array $styleMap): string
68
68
public function convert ($ value ): string
69
69
{
70
70
$ document = $ this ->htmlDocumentFactory ->create ([ 'document ' => $ value ]);
71
- $ nodes = $ document ->querySelectorAll (self ::SELECTOR );
72
71
$ body = $ document ->querySelector ('body ' );
73
- $ styleMap = [] ;
72
+ $ nodes = $ document -> querySelectorAll ( self :: SELECTOR ) ;
74
73
75
- foreach ($ nodes as $ node ) {
76
- /* @var ElementInterface $node */
77
- $ styleAttr = $ node ->getAttribute ('style ' );
74
+ if ($ nodes ->count () > 0 ) {
75
+ $ styleMap = [];
78
76
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
+ }
84
87
}
85
- }
86
88
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
+ }
98
101
}
102
+
103
+ return $ value ;
99
104
}
100
105
}
0 commit comments