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

fix(Angular.js): use cookie instead of window.name to load with debug… #13036

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -1616,19 +1616,19 @@ function bootstrap(element, modules, config) {
return injector;
};

var NG_ENABLE_DEBUG_INFO = /^NG_ENABLE_DEBUG_INFO!/;
var NG_DEFER_BOOTSTRAP = /^NG_DEFER_BOOTSTRAP!/;
var NG_ENABLE_DEBUG_INFO = /\bNG_ENABLE_DEBUG_INFO!=true\b/;
var NG_DEFER_BOOTSTRAP = /\bNG_DEFER_BOOTSTRAP!=true\b/;

if (window && NG_ENABLE_DEBUG_INFO.test(window.name)) {
if (document && NG_ENABLE_DEBUG_INFO.test(document.cookie)) {
config.debugInfoEnabled = true;
window.name = window.name.replace(NG_ENABLE_DEBUG_INFO, '');
document.cookie = 'NG_ENABLE_DEBUG_INFO!=false;expires=Thu, 01 Jan 1970 00:00:01 GMT';
}

if (window && !NG_DEFER_BOOTSTRAP.test(window.name)) {
if (document && !NG_DEFER_BOOTSTRAP.test(document.cookie)) {
return doBootstrap();
}

window.name = window.name.replace(NG_DEFER_BOOTSTRAP, '');
document.cookie = 'NG_DEFER_BOOTSTRAP!=false;expires=Thu, 01 Jan 1970 00:00:01 GMT';
angular.resumeBootstrap = function(extraModules) {
forEach(extraModules, function(module) {
modules.push(module);
Expand All @@ -1652,7 +1652,7 @@ function bootstrap(element, modules, config) {
* See {@link ng.$compileProvider#debugInfoEnabled} for more.
*/
function reloadWithDebugInfo() {
window.name = 'NG_ENABLE_DEBUG_INFO!' + window.name;
document.cookie = 'NG_ENABLE_DEBUG_INFO!=true';
window.location.reload();
}

Expand Down
109 changes: 97 additions & 12 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1742,22 +1742,19 @@ describe('angular', function() {


describe('deferred bootstrap', function() {
var originalName = window.name,
element;
var element;

beforeEach(function() {
window.name = '';
element = jqLite('<div>{{1+2}}</div>');
});

afterEach(function() {
dealoc(element);
window.name = originalName;
});

it('should provide injector for deferred bootstrap', function() {
var injector;
window.name = 'NG_DEFER_BOOTSTRAP!';
document.cookie = 'NG_DEFER_BOOTSTRAP!=true';

injector = angular.bootstrap(element);
expect(injector).toBeUndefined();
Expand All @@ -1768,7 +1765,7 @@ describe('angular', function() {

it('should resume deferred bootstrap, if defined', function() {
var injector;
window.name = 'NG_DEFER_BOOTSTRAP!';
document.cookie = 'NG_DEFER_BOOTSTRAP!=true';

angular.resumeDeferredBootstrap = noop;
var spy = spyOn(angular, "resumeDeferredBootstrap");
Expand All @@ -1777,21 +1774,21 @@ describe('angular', function() {
});

it('should wait for extra modules', function() {
window.name = 'NG_DEFER_BOOTSTRAP!';
document.cookie = 'NG_DEFER_BOOTSTRAP!=true';
angular.bootstrap(element);

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

angular.resumeBootstrap();

expect(element.html()).toBe('3');
expect(window.name).toEqual('');
expect(/\bNG_DEFER_BOOTSTRAP!=true\b/.test(document.cookie)).toBeFalsy();
});


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

var bootstrapping = jasmine.createSpy('bootstrapping');
angular.bootstrap(element, [bootstrapping]);
Expand Down Expand Up @@ -1819,16 +1816,104 @@ describe('angular', function() {
});


it('should restore the original window.name after bootstrap', function() {
window.name = 'NG_DEFER_BOOTSTRAP!my custom name';
it('should remove the cookie after bootstrap', function() {
document.cookie = 'NG_DEFER_BOOTSTRAP!=true';
angular.bootstrap(element);

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

angular.resumeBootstrap();

expect(element.html()).toBe('3');
expect(window.name).toEqual('my custom name');
expect(/\bNG_DEFER_BOOTSTRAP!=true\b/.test(document.cookie)).toBeFalsy();
});
});

describe('reloadWithDebugInfo', function() {
var element;

beforeEach(function() {
element = jqLite('<div>{{1+2}}</div>');
});

afterEach(function() {
dealoc(element);
});

it('should not change the configuration when the cookie is not set', function() {
var compileProvider = null;

bootstrap(element, [disableDebugAndGetProvider]);
expect(compileProvider.debugInfoEnabled()).toBeFalsy();

function disableDebugAndGetProvider($compileProvider) {
$compileProvider.debugInfoEnabled(false);
compileProvider = $compileProvider;
}
});

it('should enable debug if the cookie is set', function() {
var compileProvider = null;

document.cookie = 'NG_ENABLE_DEBUG_INFO!=true';
bootstrap(element, [getCompileProvider]);

expect(compileProvider.debugInfoEnabled()).toBeTruthy();

function getCompileProvider($compileProvider) {
compileProvider = $compileProvider;
}
});

it('should remove the cookie', function() {
document.cookie = 'NG_ENABLE_DEBUG_INFO!=true';
bootstrap(element);

expect(document.cookie).toEqual('');
});
});

describe('reloadWithDebugInfo', function() {
var element;

beforeEach(function() {
element = jqLite('<div>{{1+2}}</div>');
});

afterEach(function() {
dealoc(element);
});

it('should not change the configuration when the cookie is not set', function() {
var compileProvider = null;

bootstrap(element, [disableDebugAndGetProvider]);
expect(compileProvider.debugInfoEnabled()).toBeFalsy();

function disableDebugAndGetProvider($compileProvider) {
$compileProvider.debugInfoEnabled(false);
compileProvider = $compileProvider;
}
});

it('should enable debug if the cookie is set', function() {
var compileProvider = null;

document.cookie = 'NG_ENABLE_DEBUG_INFO!=true';
bootstrap(element, [getCompileProvider]);

expect(compileProvider.debugInfoEnabled()).toBeTruthy();

function getCompileProvider($compileProvider) {
compileProvider = $compileProvider;
}
});

it('should remove the cookie', function() {
document.cookie = 'NG_ENABLE_DEBUG_INFO!=true';
bootstrap(element);

expect(document.cookie).toEqual('');
});
});
});
Expand Down