Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0694af8

Browse files
committed
fix(angularInit): allow auto-bootstraping from inline script
Some browsers (e.g. Safari 9.x, PhantomJS) do not set `link.origin/protocol` correctly, when setting `link.href` to `null`, which prevented auto-bootstraping Angular from scripts without a `src` attribute (i.e. inline scripts). Inline scripts are on the same origin as the loading page, so auto-bootstraping should be allowed. Fixes #15567 Closes #15571
1 parent 090a839 commit 0694af8

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/Angular.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,12 +1479,16 @@ function getNgAttribute(element, ngAttr) {
14791479
}
14801480

14811481
function allowAutoBootstrap(document) {
1482-
if (!document.currentScript) {
1482+
var script = document.currentScript;
1483+
var src = script && script.getAttribute('src');
1484+
1485+
if (!src) {
14831486
return true;
14841487
}
1485-
var src = document.currentScript.getAttribute('src');
1488+
14861489
var link = document.createElement('a');
14871490
link.href = src;
1491+
14881492
if (document.location.origin === link.origin) {
14891493
// Same-origin resources are always allowed, even for non-whitelisted schemes.
14901494
return true;

test/AngularSpec.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,8 @@ describe('angular', function() {
16841684
});
16851685

16861686
it('should bootstrap from an extension into an extension document for same-origin documents only', function() {
1687-
if (msie) return; // IE does not support document.currentScript (nor extensions with protocol), so skip test.
1687+
// IE does not support `document.currentScript` (nor extensions with protocol), so skip test.
1688+
if (msie) return;
16881689

16891690
// Extension URLs are browser-specific, so we must choose a scheme that is supported by the browser to make
16901691
// sure that the URL is properly parsed.
@@ -1715,8 +1716,28 @@ describe('angular', function() {
17151716
expect(allowAutoBootstrap(fakeDoc)).toBe(false);
17161717
});
17171718

1719+
it('should bootstrap from a script with an empty or missing `src` attribute', function() {
1720+
// IE does not support `document.currentScript` (nor extensions with protocol), so skip test.
1721+
if (msie) return;
1722+
1723+
// Fake a minimal document object (the actual document.currentScript is readonly).
1724+
var src;
1725+
var fakeDoc = {
1726+
createElement: document.createElement.bind(document),
1727+
currentScript: {getAttribute: function() { return src; }},
1728+
location: {origin: 'some-value', protocol: 'http:'}
1729+
};
1730+
1731+
src = null;
1732+
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1733+
1734+
src = '';
1735+
expect(allowAutoBootstrap(fakeDoc)).toBe(true);
1736+
});
1737+
17181738
it('should not bootstrap from an extension into a non-extension document', function() {
1719-
if (msie) return; // IE does not support document.currentScript (nor extensions with protocol), so skip test.
1739+
// IE does not support `document.currentScript` (nor extensions with protocol), so skip test.
1740+
if (msie) return;
17201741

17211742
var src = 'resource://something';
17221743
// Fake a minimal document object (the actual document.currentScript is readonly).

0 commit comments

Comments
 (0)