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

fix(angular-loader): do not depend on "closure" globals that may not be available #15881

Closed
wants to merge 2 commits into from
Closed
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
44 changes: 0 additions & 44 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,50 +129,6 @@ var VALIDITY_STATE_PROPERTY = 'validity';

var hasOwnProperty = Object.prototype.hasOwnProperty;

var minErrConfig = {
objectMaxDepth: 5
};

/**
* @ngdoc function
* @name angular.errorHandlingConfig
* @module ng
* @kind function
*
* @description
* Configure several aspects of error handling in AngularJS if used as a setter or return the
* current configuration if used as a getter. The following options are supported:
*
* - **objectMaxDepth**: The maximum depth to which objects are traversed when stringified for error messages.
*
* Omitted or undefined options will leave the corresponding configuration values unchanged.
*
* @param {Object=} config - The configuration object. May only contain the options that need to be
* updated. Supported keys:
*
* * `objectMaxDepth` **{Number}** - The max depth for stringifying objects. Setting to a
* non-positive or non-numeric value, removes the max depth limit.
* Default: 5
*/
function errorHandlingConfig(config) {
if (isObject(config)) {
if (isDefined(config.objectMaxDepth)) {
minErrConfig.objectMaxDepth = isValidObjectMaxDepth(config.objectMaxDepth) ? config.objectMaxDepth : NaN;
}
} else {
return minErrConfig;
}
}

/**
* @private
* @param {Number} maxDepth
* @return {boolean}
*/
function isValidObjectMaxDepth(maxDepth) {
return isNumber(maxDepth) && maxDepth > 0;
}

/**
* @private
*
Expand Down
24 changes: 24 additions & 0 deletions src/loader.prefix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,31 @@
*/
'use strict';
(function() {
// NOTE:
// These functions are copied here from `src/Angular.js`, because they are needed inside the
// `angular-loader.js` closure and need to be available before the main `angular.js` script has
// been loaded.
function isFunction(value) {return typeof value === 'function';}
function isDefined(value) {return typeof value !== 'undefined';}
function isNumber(value) {return typeof value === 'number';}
function isObject(value) {return value !== null && typeof value === 'object';}
function isScope(obj) {return obj && obj.$evalAsync && obj.$watch;}
function isUndefined(value) {return typeof value === 'undefined';}
function isWindow(obj) {return obj && obj.window === obj;}
function sliceArgs(args, startIndex) {return Array.prototype.slice.call(args, startIndex || 0);}
function toJsonReplacer(key, value) {
var val = value;

if (typeof key === 'string' && key.charAt(0) === '$' && key.charAt(1) === '$') {
val = undefined;
} else if (isWindow(value)) {
val = '$WINDOW';
} else if (value && window.document === value) {
val = '$DOCUMENT';
} else if (isScope(value)) {
val = '$SCOPE';
}

return val;
}

50 changes: 50 additions & 0 deletions src/minErr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
'use strict';

/* exported
minErrConfig,
errorHandlingConfig,
isValidObjectMaxDepth
*/

var minErrConfig = {
objectMaxDepth: 5
};

/**
* @ngdoc function
* @name angular.errorHandlingConfig
* @module ng
* @kind function
*
* @description
* Configure several aspects of error handling in AngularJS if used as a setter or return the
* current configuration if used as a getter. The following options are supported:
*
* - **objectMaxDepth**: The maximum depth to which objects are traversed when stringified for error messages.
*
* Omitted or undefined options will leave the corresponding configuration values unchanged.
*
* @param {Object=} config - The configuration object. May only contain the options that need to be
* updated. Supported keys:
*
* * `objectMaxDepth` **{Number}** - The max depth for stringifying objects. Setting to a
* non-positive or non-numeric value, removes the max depth limit.
* Default: 5
*/
function errorHandlingConfig(config) {
if (isObject(config)) {
if (isDefined(config.objectMaxDepth)) {
minErrConfig.objectMaxDepth = isValidObjectMaxDepth(config.objectMaxDepth) ? config.objectMaxDepth : NaN;
}
} else {
return minErrConfig;
}
}

/**
* @private
* @param {Number} maxDepth
* @return {boolean}
*/
function isValidObjectMaxDepth(maxDepth) {
return isNumber(maxDepth) && maxDepth > 0;
}

/**
* @description
*
Expand Down
6 changes: 4 additions & 2 deletions src/stringify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

/* global toDebugString: true */
/* exported toDebugString */

function serializeObject(obj, maxDepth) {
var seen = [];
Expand All @@ -9,7 +9,9 @@ function serializeObject(obj, maxDepth) {
// and a very deep object can cause a performance issue, so we copy the object
// based on this specific depth and then stringify it.
if (isValidObjectMaxDepth(maxDepth)) {
obj = copy(obj, null, maxDepth);
// This file is also included in `angular-loader`, so `copy()` might not always be available in
// the closure. Therefore, it is lazily retrieved as `angular.copy()` when needed.
obj = angular.copy(obj, null, maxDepth);
}
return JSON.stringify(obj, function(key, val) {
val = toJsonReplacer(key, val);
Expand Down
25 changes: 0 additions & 25 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,13 @@ Float32Array, Float64Array, */

describe('angular', function() {
var element, document;
var originalObjectMaxDepthInErrorMessage = minErrConfig.objectMaxDepth;

beforeEach(function() {
document = window.document;
});

afterEach(function() {
dealoc(element);
minErrConfig.objectMaxDepth = originalObjectMaxDepthInErrorMessage;
});

describe('errorHandlingConfig', function() {
it('should get default objectMaxDepth', function() {
expect(errorHandlingConfig().objectMaxDepth).toBe(5);
});

it('should set objectMaxDepth', function() {
errorHandlingConfig({objectMaxDepth: 3});
expect(errorHandlingConfig().objectMaxDepth).toBe(3);
});

it('should not change objectMaxDepth when undefined is supplied', function() {
errorHandlingConfig({objectMaxDepth: undefined});
expect(errorHandlingConfig().objectMaxDepth).toBe(originalObjectMaxDepthInErrorMessage);
});

they('should set objectMaxDepth to NaN when $prop is supplied',
[NaN, null, true, false, -1, 0], function(maxDepth) {
errorHandlingConfig({objectMaxDepth: maxDepth});
expect(errorHandlingConfig().objectMaxDepth).toBeNaN();
}
);
});

describe('case', function() {
Expand Down
Loading