Skip to content

Commit 619cb39

Browse files
committed
MOBILE-1423 iframe: Handle _parent, _top and _blank in iframes
1 parent 329f032 commit 619cb39

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

www/core/directives/iframe.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,22 @@ angular.module('mm.core')
3434

3535
/**
3636
* Intercept window.open in a frame and its subframes, shows an error modal instead.
37+
* Search links (<a>) and open them in browser or InAppBrowser if needed.
3738
*
3839
* @param {DOMElement} element Element to treat.
3940
* @return {Void}
4041
*/
41-
function interceptPopups(element) {
42+
function treatFrame(element) {
4243
if (element) {
4344
// Redefine window.open in this element and sub frames, it might have been loaded already.
4445
redefineWindowOpen(element);
46+
// Treat links.
47+
treatLinks(element);
4548

4649
element.on('load', function() {
47-
// Element loaded, redefine window.open again.
50+
// Element loaded, redefine window.open and treat links again.
4851
redefineWindowOpen(element);
52+
treatLinks(element);
4953
});
5054
}
5155
}
@@ -96,11 +100,41 @@ angular.module('mm.core')
96100
// Search sub frames.
97101
angular.forEach(tags, function(tag) {
98102
angular.forEach(contents.find(tag), function(subelement) {
99-
interceptPopups(angular.element(subelement));
103+
treatFrame(angular.element(subelement));
100104
});
101105
});
102106
}
103107

108+
/**
109+
* Search links (<a>) and open them in browser or InAppBrowser if needed.
110+
*
111+
* @param {DOMElement} element Element to treat.
112+
* @return {Void}
113+
*/
114+
function treatLinks(element) {
115+
var links = element.contents().find('a');
116+
angular.forEach(links, function(el) {
117+
var href = el.href;
118+
119+
// Check that href is not null.
120+
if (href) {
121+
if (href.indexOf('http') === 0) {
122+
// Link has protocol http(s), open it in browser.
123+
angular.element(el).on('click', function(e) {
124+
e.preventDefault();
125+
$mmUtil.openInBrowser(href);
126+
});
127+
} else if (el.target == '_parent' || el.target == '_top' || el.target == '_blank') {
128+
// Opening links with _parent, _top or _blank can break the app. We'll open it in InAppBrowser.
129+
angular.element(el).on('click', function(e) {
130+
e.preventDefault();
131+
$mmUtil.openInApp(href);
132+
});
133+
}
134+
}
135+
});
136+
}
137+
104138
return {
105139
restrict: 'E',
106140
template: '<div class="iframe-wrapper"><iframe class="mm-iframe" ng-style="{\'width\': width, \'height\': height}" ng-src="{{src}}"></iframe></div>',
@@ -112,18 +146,7 @@ angular.module('mm.core')
112146
scope.height = $mmUtil.formatPixelsSize(attrs.iframeHeight) || '100%';
113147

114148
var iframe = angular.element(element.find('iframe')[0]);
115-
interceptPopups(iframe);
116-
iframe.on('load', function() {
117-
angular.forEach(iframe.contents().find('a'), function(el) {
118-
var href = el.getAttribute('href');
119-
if (href && href.indexOf('http') === 0) { // Check that href is not null.
120-
angular.element(el).on('click', function(e) {
121-
$mmUtil.openInBrowser(href);
122-
e.preventDefault();
123-
});
124-
}
125-
});
126-
});
149+
treatFrame(iframe);
127150

128151
}
129152
};

0 commit comments

Comments
 (0)