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

Commit 624c000

Browse files
committed
fix(Angular.js): use cookie instead of window.name to load with debug info
Use cookie to store a check if the application needs to be loaded with debug info, instead of using window.name. As window.name can be used alread (and be overwritten on page load) Fixes issue #13031
1 parent 74ed286 commit 624c000

File tree

2 files changed

+70
-19
lines changed

2 files changed

+70
-19
lines changed

src/Angular.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,7 @@ function bootstrap(element, modules, config) {
15591559
var defaultConfig = {
15601560
strictDi: false
15611561
};
1562+
15621563
config = extend(defaultConfig, config);
15631564
var doBootstrap = function() {
15641565
element = jqLite(element);
@@ -1597,19 +1598,19 @@ function bootstrap(element, modules, config) {
15971598
return injector;
15981599
};
15991600

1600-
var NG_ENABLE_DEBUG_INFO = /^NG_ENABLE_DEBUG_INFO!/;
1601-
var NG_DEFER_BOOTSTRAP = /^NG_DEFER_BOOTSTRAP!/;
1601+
var NG_ENABLE_DEBUG_INFO = /\bNG_ENABLE_DEBUG_INFO!=true\b/;
1602+
var NG_DEFER_BOOTSTRAP = /\bNG_DEFER_BOOTSTRAP!=true\b/;
16021603

1603-
if (window && NG_ENABLE_DEBUG_INFO.test(window.name)) {
1604+
if (document && NG_ENABLE_DEBUG_INFO.test(document.cookie)) {
16041605
config.debugInfoEnabled = true;
1605-
window.name = window.name.replace(NG_ENABLE_DEBUG_INFO, '');
1606+
document.cookie = 'NG_ENABLE_DEBUG_INFO!=false;expires=Thu, 01 Jan 1970 00:00:01 GMT';
16061607
}
16071608

1608-
if (window && !NG_DEFER_BOOTSTRAP.test(window.name)) {
1609+
if (document && !NG_DEFER_BOOTSTRAP.test(document.cookie)) {
16091610
return doBootstrap();
16101611
}
16111612

1612-
window.name = window.name.replace(NG_DEFER_BOOTSTRAP, '');
1613+
document.cookie = 'NG_DEFER_BOOTSTRAP!=false;expires=Thu, 01 Jan 1970 00:00:01 GMT';
16131614
angular.resumeBootstrap = function(extraModules) {
16141615
forEach(extraModules, function(module) {
16151616
modules.push(module);
@@ -1633,7 +1634,7 @@ function bootstrap(element, modules, config) {
16331634
* See {@link ng.$compileProvider#debugInfoEnabled} for more.
16341635
*/
16351636
function reloadWithDebugInfo() {
1636-
window.name = 'NG_ENABLE_DEBUG_INFO!' + window.name;
1637+
document.cookie = 'NG_ENABLE_DEBUG_INFO!=true';
16371638
window.location.reload();
16381639
}
16391640

test/AngularSpec.js

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,22 +1639,19 @@ describe('angular', function() {
16391639

16401640

16411641
describe('deferred bootstrap', function() {
1642-
var originalName = window.name,
1643-
element;
1642+
var element;
16441643

16451644
beforeEach(function() {
1646-
window.name = '';
16471645
element = jqLite('<div>{{1+2}}</div>');
16481646
});
16491647

16501648
afterEach(function() {
16511649
dealoc(element);
1652-
window.name = originalName;
16531650
});
16541651

16551652
it('should provide injector for deferred bootstrap', function() {
16561653
var injector;
1657-
window.name = 'NG_DEFER_BOOTSTRAP!';
1654+
document.cookie = 'NG_DEFER_BOOTSTRAP!=true';
16581655

16591656
injector = angular.bootstrap(element);
16601657
expect(injector).toBeUndefined();
@@ -1665,7 +1662,7 @@ describe('angular', function() {
16651662

16661663
it('should resume deferred bootstrap, if defined', function() {
16671664
var injector;
1668-
window.name = 'NG_DEFER_BOOTSTRAP!';
1665+
document.cookie = 'NG_DEFER_BOOTSTRAP!=true';
16691666

16701667
angular.resumeDeferredBootstrap = noop;
16711668
var spy = spyOn(angular, "resumeDeferredBootstrap");
@@ -1674,21 +1671,21 @@ describe('angular', function() {
16741671
});
16751672

16761673
it('should wait for extra modules', function() {
1677-
window.name = 'NG_DEFER_BOOTSTRAP!';
1674+
document.cookie = 'NG_DEFER_BOOTSTRAP!=true';
16781675
angular.bootstrap(element);
16791676

16801677
expect(element.html()).toBe('{{1+2}}');
16811678

16821679
angular.resumeBootstrap();
16831680

16841681
expect(element.html()).toBe('3');
1685-
expect(window.name).toEqual('');
1682+
expect(/\bNG_DEFER_BOOTSTRAP!=true\b/.test(document.cookie)).toBeFalsy();
16861683
});
16871684

16881685

16891686
it('should load extra modules', function() {
16901687
element = jqLite('<div>{{1+2}}</div>');
1691-
window.name = 'NG_DEFER_BOOTSTRAP!';
1688+
document.cookie = 'NG_DEFER_BOOTSTRAP!=true';
16921689

16931690
var bootstrapping = jasmine.createSpy('bootstrapping');
16941691
angular.bootstrap(element, [bootstrapping]);
@@ -1716,18 +1713,71 @@ describe('angular', function() {
17161713
});
17171714

17181715

1719-
it('should restore the original window.name after bootstrap', function() {
1720-
window.name = 'NG_DEFER_BOOTSTRAP!my custom name';
1716+
it('should remove the cookie after bootstrap', function() {
1717+
document.cookie = 'NG_DEFER_BOOTSTRAP!=true';
17211718
angular.bootstrap(element);
17221719

17231720
expect(element.html()).toBe('{{1+2}}');
17241721

17251722
angular.resumeBootstrap();
17261723

17271724
expect(element.html()).toBe('3');
1728-
expect(window.name).toEqual('my custom name');
1725+
expect(/\bNG_DEFER_BOOTSTRAP!=true\b/.test(document.cookie)).toBeFalsy();
17291726
});
17301727
});
1728+
1729+
describe('reloadWithDebugInfo', function() {
1730+
var element;
1731+
1732+
beforeEach(function() {
1733+
element = jqLite('<div>{{1+2}}</div>');
1734+
});
1735+
1736+
afterEach(function() {
1737+
dealoc(element);
1738+
});
1739+
1740+
it('should not change the configuration when the cookie is not set', function() {
1741+
var compileProvider = null;
1742+
1743+
bootstrap(element, [disableDebugAndGetProvider]);
1744+
expect(compileProvider.debugInfoEnabled()).toBeFalsy();
1745+
1746+
function disableDebugAndGetProvider($compileProvider) {
1747+
$compileProvider.debugInfoEnabled(false);
1748+
compileProvider = $compileProvider;
1749+
}
1750+
});
1751+
1752+
it('should enable debug if the cookie is set', function() {
1753+
var compileProvider = null;
1754+
1755+
document.cookie = 'NG_ENABLE_DEBUG_INFO!=true';
1756+
bootstrap(element, [getCompileProvider]);
1757+
1758+
expect(compileProvider.debugInfoEnabled()).toBeTruthy();
1759+
1760+
function getCompileProvider($compileProvider) {
1761+
compileProvider = $compileProvider;
1762+
}
1763+
});
1764+
1765+
it('should remove the cookie', function() {
1766+
document.cookie = 'NG_ENABLE_DEBUG_INFO!=true';
1767+
bootstrap(element);
1768+
expect(document.cookie).toEqual('');
1769+
});
1770+
});
1771+
});
1772+
1773+
describe('reloadWithDebugInfo', function() {
1774+
1775+
it('should create a cookie', function() {
1776+
angular.reloadWithDebugInfo();
1777+
expect(/\bNG_ENABLE_DEBUG_INFO!=true\b/.test(document.cookie)).toBeTruthy();
1778+
1779+
// it should also reload the window, but we can not test that, as window.location can not be spied upon
1780+
});
17311781
});
17321782

17331783

0 commit comments

Comments
 (0)