diff --git a/src/ngSanitize/sanitize.js b/src/ngSanitize/sanitize.js index de6dd3dd135d..5d7a6325af0c 100644 --- a/src/ngSanitize/sanitize.js +++ b/src/ngSanitize/sanitize.js @@ -232,6 +232,13 @@ function makeMap(str) { * @param {object} handler */ function htmlParser( html, handler ) { + if (typeof html !== 'string') { + if (html === null || typeof html === 'undefined') { + html = ''; + } else { + html = '' + html; + } + } var index, chars, match, stack = [], last = html, text; stack.last = function() { return stack[ stack.length - 1 ]; }; diff --git a/test/ngSanitize/sanitizeSpec.js b/test/ngSanitize/sanitizeSpec.js index 5f2594397657..bad56167649b 100644 --- a/test/ngSanitize/sanitizeSpec.js +++ b/test/ngSanitize/sanitizeSpec.js @@ -228,6 +228,16 @@ describe('HTML', function() { .toEqual('

10 < 100

'); }); + it('should accept non-string arguments', function() { + expectHTML(null).toBe(''); + expectHTML(undefined).toBe(''); + expectHTML(42).toBe('42'); + expectHTML({}).toBe('[object Object]'); + expectHTML([1, 2, 3]).toBe('1,2,3'); + expectHTML(true).toBe('true'); + expectHTML(false).toBe('false'); + }); + describe('htmlSanitizerWriter', function() { /* global htmlSanitizeWriter: false */ if (angular.isUndefined(window.htmlSanitizeWriter)) return;