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

Commit 9ee0755

Browse files
committed
fix(ngSanitize): ensure html is a string in htmlParser()
Previously, $sanitize(nonString) would throw. Now, the type is converted to a string before any work is done. Closes #8417 Closes #8416
1 parent afe93ea commit 9ee0755

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/ngSanitize/sanitize.js

+7
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,13 @@ function makeMap(str) {
232232
* @param {object} handler
233233
*/
234234
function htmlParser( html, handler ) {
235+
if (typeof html !== 'string') {
236+
if (html === null || typeof html === 'undefined') {
237+
html = '';
238+
} else {
239+
html = '' + html;
240+
}
241+
}
235242
var index, chars, match, stack = [], last = html, text;
236243
stack.last = function() { return stack[ stack.length - 1 ]; };
237244

test/ngSanitize/sanitizeSpec.js

+10
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ describe('HTML', function() {
228228
.toEqual('<p> 10 &lt; <span>100</span> </p>');
229229
});
230230

231+
it('should accept non-string arguments', function() {
232+
expectHTML(null).toBe('');
233+
expectHTML(undefined).toBe('');
234+
expectHTML(42).toBe('42');
235+
expectHTML({}).toBe('[object Object]');
236+
expectHTML([1, 2, 3]).toBe('1,2,3');
237+
expectHTML(true).toBe('true');
238+
expectHTML(false).toBe('false');
239+
});
240+
231241
describe('htmlSanitizerWriter', function() {
232242
/* global htmlSanitizeWriter: false */
233243
if (angular.isUndefined(window.htmlSanitizeWriter)) return;

0 commit comments

Comments
 (0)