Skip to content

Commit 338979d

Browse files
BobChao87petebacondarwin
authored andcommitted
fix($sanitize): reduce stack height in IE <= 11
Update Internet Explorer-only helper function stripCustomNsAttrs to be less recursive. Reduce stack height of function causing out of stack space error. Closes angular#14928 Closes angular#15030
1 parent ee594c7 commit 338979d

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

src/ngSanitize/sanitize.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -499,27 +499,26 @@ function $SanitizeProvider() {
499499
* @param node Root element to process
500500
*/
501501
function stripCustomNsAttrs(node) {
502-
if (node.nodeType === window.Node.ELEMENT_NODE) {
503-
var attrs = node.attributes;
504-
for (var i = 0, l = attrs.length; i < l; i++) {
505-
var attrNode = attrs[i];
506-
var attrName = attrNode.name.toLowerCase();
507-
if (attrName === 'xmlns:ns1' || attrName.lastIndexOf('ns1:', 0) === 0) {
508-
node.removeAttributeNode(attrNode);
509-
i--;
510-
l--;
502+
while (node) {
503+
if (node.nodeType === window.Node.ELEMENT_NODE) {
504+
var attrs = node.attributes;
505+
for (var i = 0, l = attrs.length; i < l; i++) {
506+
var attrNode = attrs[i];
507+
var attrName = attrNode.name.toLowerCase();
508+
if (attrName === 'xmlns:ns1' || attrName.lastIndexOf('ns1:', 0) === 0) {
509+
node.removeAttributeNode(attrNode);
510+
i--;
511+
l--;
512+
}
511513
}
512514
}
513-
}
514515

515-
var nextNode = node.firstChild;
516-
if (nextNode) {
517-
stripCustomNsAttrs(nextNode);
518-
}
516+
var nextNode = node.firstChild;
517+
if (nextNode) {
518+
stripCustomNsAttrs(nextNode);
519+
}
519520

520-
nextNode = node.nextSibling;
521-
if (nextNode) {
522-
stripCustomNsAttrs(nextNode);
521+
node = node.nextSibling;
523522
}
524523
}
525524
}

test/ngSanitize/sanitizeSpec.js

+11
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,17 @@ describe('HTML', function() {
130130
expectHTML('a<div style="abc">b</div>c').toEqual('a<div>b</div>c');
131131
});
132132

133+
it('should handle large datasets', function() {
134+
// Large is non-trivial to quantify, but handling ~100,000 should be sufficient for most purposes.
135+
var largeNumber = 17; // 2^17 = 131,072
136+
var result = '<div>b</div>';
137+
// Ideally we would use repeat, but that isn't supported in IE.
138+
for (var i = 0; i < largeNumber; i++) {
139+
result += result;
140+
}
141+
expectHTML('a' + result + 'c').toEqual('a' + result + 'c');
142+
});
143+
133144
it('should remove style', function() {
134145
expectHTML('a<STyle>evil</stYle>c.').toEqual('ac.');
135146
});

0 commit comments

Comments
 (0)