Skip to content

Commit 47dab93

Browse files
committed
MAGETWO-32485: Merge initialization of all components on body element to one initialization file
- Add test.
1 parent c27121a commit 47dab93

File tree

2 files changed

+372
-21
lines changed

2 files changed

+372
-21
lines changed

app/code/Magento/PageCache/view/frontend/web/js/page-cache.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,26 @@ define([
2424
* @param {jQuery} element - Comment holder
2525
*/
2626
(function lookup(element) {
27-
if (element.is('iframe')) {
28-
var hostName = window.location.hostname,
29-
iFrameHostName = $('<a>').prop('href', element.prop('src')).prop('hostname');
30-
31-
if (hostName != iFrameHostName) {
32-
return;
33-
}
34-
}
35-
element.contents().each(function (i, el) {
36-
if (el.nodeType == 8) {
37-
elements.push(el);
38-
} else if (el.nodeType == 1) {
39-
lookup($(el));
27+
$(element).contents().each(function (index, el) {
28+
switch (el.nodeType) {
29+
case 1: // ELEMENT_NODE
30+
lookup(el);
31+
break;
32+
33+
case 8: // COMMENT_NODE
34+
elements.push(el);
35+
break;
36+
37+
case 9: // DOCUMENT_NODE
38+
var hostName = window.location.hostname,
39+
iFrameHostName = $('<a>')
40+
.prop('href', element.prop('src'))
41+
.prop('hostname');
42+
43+
if (hostName === iFrameHostName) {
44+
lookup($(el).find('body'));
45+
}
46+
break;
4047
}
4148
});
4249
})(this);
@@ -59,10 +66,7 @@ define([
5966
*/
6067
_create: function () {
6168
if ($.mage.cookies.get(this.options.msgBoxCookieName)) {
62-
$.mage.cookies.set(this.options.msgBoxCookieName, null, {
63-
expires: new Date(),
64-
path: '/'
65-
});
69+
$.mage.cookies.clear(this.options.msgBoxCookieName);
6670
} else {
6771
$(this.options.msgBoxSelector).hide();
6872
}
@@ -125,7 +129,7 @@ define([
125129
}
126130
placeholders = this._searchPlaceholders(this.element.comments());
127131

128-
if (placeholders.length) {
132+
if (placeholders && placeholders.length) {
129133
this._ajax(placeholders, version);
130134
}
131135
},
@@ -140,13 +144,14 @@ define([
140144
var placeholders = [],
141145
tmp = {},
142146
ii,
147+
len,
143148
el, matches, name;
144149

145-
if (!elements.length) {
150+
if (!(elements && elements.length)) {
146151
return placeholders;
147152
}
148153

149-
for (ii = 0; ii < elements.length; ii++) {
154+
for (ii = 0, len = elements.length; ii < len; ii++) {
150155
el = elements[ii];
151156
matches = this.options.patternPlaceholderOpen.exec(el.nodeValue);
152157
name = null;
@@ -182,14 +187,19 @@ define([
182187
* @protected
183188
*/
184189
_replacePlaceholder: function (placeholder, html) {
190+
if (!placeholder || !html) {
191+
return;
192+
}
193+
185194
var parent = $(placeholder.openElement).parent(),
186195
contents = parent.contents(),
187196
startReplacing = false,
188197
prevSibling = null,
189198
yy,
199+
len,
190200
element;
191201

192-
for (yy = 0; yy < contents.length; yy++) {
202+
for (yy = 0, len = contents.length; yy < len; yy++) {
193203
element = contents[yy];
194204

195205
if (element == placeholder.openElement) {
@@ -286,6 +296,7 @@ define([
286296
*/
287297
function generateRandomString(chars, length) {
288298
var result = '';
299+
length = length > 0 && Number.isFinite(length) ? length : 1;
289300

290301
while (length--) {
291302
result += chars[Math.round(Math.random() * (chars.length - 1))];

0 commit comments

Comments
 (0)