Skip to content

Commit

Permalink
Merge pull request #369 from churkin/i368
Browse files Browse the repository at this point in the history
The getIframeByElement function works incorrectly for the nested iframes (close #368)
  • Loading branch information
helen-dikareva committed Jan 25, 2016
2 parents 3d64dcb + 166986a commit 4d648a6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
24 changes: 3 additions & 21 deletions src/client/utils/dom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import INTERNAL_ATTRS from '../../processing/dom/internal-attributes';
import INTERNAL_PROPS from '../../processing/dom/internal-properties';
import SHADOW_UI_CLASSNAME from '../../shadow-ui/class-name';
import nativeMethods from '../sandbox/native-methods';
import * as urlUtils from './url';
Expand Down Expand Up @@ -109,27 +110,9 @@ export function getChildVisibleIndex (select, child) {
}

export function getIframeByElement (el) {
var currentDocument = el.documentElement ? el : findDocument(el);
var currentWindow = window !== window.top && isCrossDomainWindows(window.top, window) ? window : window.top;
var iframes = currentWindow.document.getElementsByTagName('iframe');
var elWindow = el[INTERNAL_PROPS.processedContext];

for (var i = 0; i < iframes.length; i++) {
if (iframes[i].contentDocument === currentDocument)
return iframes[i];
}

return null;
}

export function getIframeByWindow (win) {
var iframes = window.top.document.getElementsByTagName('iframe');

for (var i = 0; i < iframes.length; i++) {
if (iframes[i].contentWindow === win)
return iframes[i];
}

return null;
return getFrameElement(elWindow);
}

export function getIframeLocation (iframe) {
Expand Down Expand Up @@ -637,4 +620,3 @@ export function getParents (el, selector) {

return parents;
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@

hammerhead.get('./utils/destination-location').forceLocation('http://localhost/sessionId/http://target_url');
hammerhead.start({
crossDomainProxyPort: 2000,
serviceMsgUrl: '/service-msg/"window[\'' + INTERNAL_PROPS.overrideDomMethodName + '\'] = function() {}"'
crossDomainProxyPort: 2000,
serviceMsgUrl: '/service-msg/"window[\'' + INTERNAL_PROPS.overrideDomMethodName +
'\'] = function() {}"',
iframeTaskScriptTemplate: [
'window["%hammerhead%"].get("./utils/destination-location").forceLocation("http://localhost/sessionId/http://target_url");',
'window["%hammerhead%"].start({',
' referer : "",',
' cookie: "",',
' serviceMsgUrl : "/service-msg/100",',
' sessionId : "sessionId",',
' iframeTaskScriptTemplate: {{{iframeTaskScriptTemplate}}}',
'});'].join('')
});
</script>
<h1>Iframe content</h1>
Expand Down
44 changes: 40 additions & 4 deletions test/client/fixtures/utils/dom-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var INTERNAL_PROPS = hammerhead.get('../processing/dom/internal-properties');

var domUtils = hammerhead.utils.dom;
var browserUtils = hammerhead.utils.browser;
var iframeSandbox = hammerhead.sandbox.iframe;

QUnit.testStart(function () {
Expand All @@ -17,15 +18,15 @@ asyncTest('isCrossDomainWindows', function () {

var iframeWithEmptySrc = document.createElement('iframe');

iframeWithEmptySrc.id = 'test2';
iframeWithEmptySrc.id = 'test2';
iframeWithEmptySrc.src = '';
document.body.appendChild(iframeWithEmptySrc);
ok(!domUtils.isCrossDomainWindows(window, iframeWithEmptySrc.contentWindow));
iframeWithEmptySrc.parentNode.removeChild(iframeWithEmptySrc);

var iframeAboutBlank = document.createElement('iframe');

iframeAboutBlank.id = 'test3';
iframeAboutBlank.id = 'test3';
iframeAboutBlank.src = 'about:blank';
document.body.appendChild(iframeAboutBlank);
ok(!domUtils.isCrossDomainWindows(window, iframeAboutBlank.contentWindow));
Expand All @@ -39,7 +40,7 @@ asyncTest('isCrossDomainWindows', function () {
crossDomainIframe.parentNode.removeChild(crossDomainIframe);
start();
});
crossDomainIframe.id = 'test1';
crossDomainIframe.id = 'test1';
crossDomainIframe.src = window.getCrossDomainPageUrl('../../data/cross-domain/get-message.html');

document.body.appendChild(crossDomainIframe);
Expand Down Expand Up @@ -194,7 +195,7 @@ asyncTest('changed location 2', function () {
this.contentWindow.location = 'http://' + location.host + '/';
};

iframe.id = 'test7';
iframe.id = 'test7';
iframe.setAttribute('src', 'about:blank');
iframe.addEventListener('load', handler);
document.body.appendChild(iframe);
Expand Down Expand Up @@ -441,3 +442,38 @@ asyncTest('cross domain iframe that contains iframe without src should not throw

document.body.appendChild(iframe);
});

if (!browserUtils.isFirefox) {
asyncTest('getIframeByElement', function () {
var parentIframe = document.createElement('iframe');

parentIframe.id = 'test_parent';

window.QUnitGlobals.waitForIframe(parentIframe)
.then(function () {
var parentIframeUtils = parentIframe.contentWindow['%hammerhead%'].utils.dom;
var childIframe = parentIframe.contentDocument.createElement('iframe');

childIframe.id = 'test_child';

window.QUnitGlobals.waitForIframe(childIframe)
.then(function () {
var childIframeUtils = childIframe.contentWindow['%hammerhead%'].utils.dom;

strictEqual(domUtils.getIframeByElement(parentIframe.contentDocument.body), parentIframe);
strictEqual(domUtils.getIframeByElement(childIframe.contentDocument.body), childIframe);
strictEqual(childIframeUtils.getIframeByElement(parentIframe.contentDocument.body), parentIframe);
strictEqual(childIframeUtils.getIframeByElement(childIframe.contentDocument.body), childIframe);
strictEqual(parentIframeUtils.getIframeByElement(parentIframe.contentDocument.body), parentIframe);
strictEqual(parentIframeUtils.getIframeByElement(childIframe.contentDocument.body), childIframe);

childIframe.parentNode.removeChild(childIframe);
start();
});

parentIframe.contentDocument.body.appendChild(childIframe);
});

document.body.appendChild(parentIframe);
});
}

0 comments on commit 4d648a6

Please sign in to comment.