@@ -29,16 +29,24 @@ class TemplatePlugin
29
29
*/
30
30
private $ domDocument ;
31
31
32
+ /**
33
+ * @var \Magento\Framework\Math\Random
34
+ */
35
+ private $ mathRandom ;
36
+
32
37
/**
33
38
* @param \Psr\Log\LoggerInterface $logger
34
39
* @param \Magento\Framework\View\ConfigInterface $viewConfig
40
+ * @param \Magento\Framework\Math\Random $mathRandom
35
41
*/
36
42
public function __construct (
37
43
\Psr \Log \LoggerInterface $ logger ,
38
- \Magento \Framework \View \ConfigInterface $ viewConfig
44
+ \Magento \Framework \View \ConfigInterface $ viewConfig ,
45
+ \Magento \Framework \Math \Random $ mathRandom
39
46
) {
40
47
$ this ->logger = $ logger ;
41
48
$ this ->viewConfig = $ viewConfig ;
49
+ $ this ->mathRandom = $ mathRandom ;
42
50
}
43
51
44
52
/**
@@ -150,16 +158,17 @@ private function convertEncodedHtmlContentTypesToPlaceholders(
150
158
): void {
151
159
$ xpath = new \DOMXPath ($ document );
152
160
161
+ // construct xpath query to fetch top-level ancestor html content type nodes
153
162
/** @var $htmlContentTypeNodes \DOMNode[] */
154
- $ htmlContentTypeNodes = $ xpath ->query ('//*[@data-content-type="html" and not(@data-decoded="true")] ' );
163
+ $ htmlContentTypeNodes = $ xpath ->query (
164
+ '//*[@data-content-type="html" and not(@data-decoded="true")] ' .
165
+ '[not(ancestor::*[@data-content-type="html"])] '
166
+ );
155
167
156
- // Preliminarily set decoded attribute on all encoded html content types so we don't double decode;
157
- // this needs to be done in a separate loop as contents will change throughout the subsequent loop
158
168
foreach ($ htmlContentTypeNodes as $ htmlContentTypeNode ) {
169
+ // Set decoded attribute on all encoded html content types so we don't double decode;
159
170
$ htmlContentTypeNode ->setAttribute ('data-decoded ' , 'true ' );
160
- }
161
171
162
- foreach ($ htmlContentTypeNodes as $ htmlContentTypeNode ) {
163
172
// if nothing exists inside the node, continue
164
173
if (!strlen (trim ($ htmlContentTypeNode ->nodeValue ))) {
165
174
continue ;
@@ -171,7 +180,7 @@ private function convertEncodedHtmlContentTypesToPlaceholders(
171
180
// generate unique node name element to replace with decoded html contents at end of processing;
172
181
// goal is to create a document as few times as possible to prevent inadvertent parsing of contents as html
173
182
// by the dom library
174
- $ uniqueNodeName = ' a ' . md5 ( uniqid ( '' , true ) );
183
+ $ uniqueNodeName = $ this -> mathRandom -> getRandomString ( 32 , $ this -> mathRandom :: CHARS_LOWERS );
175
184
176
185
$ uniqueNode = new \DOMElement ($ uniqueNodeName );
177
186
$ htmlContentTypeNode ->parentNode ->replaceChild ($ uniqueNode , $ htmlContentTypeNode );
0 commit comments