Skip to content

Commit 2a478a7

Browse files
committed
Fix for Array.prototype.push encountering a "Maximum Call Stack Exceeded" error when the input is a large number of HTML character entities
Fixes #171
1 parent 4d86ea3 commit 2a478a7

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/htmlParser/HtmlParser.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,14 @@ Autolinker.htmlParser.HtmlParser = Autolinker.Util.extend( Object, {
156156
// Push TextNodes and EntityNodes for any text found between tags
157157
if( text ) {
158158
textAndEntityNodes = this.parseTextAndEntityNodes( lastIndex, text );
159-
nodes.push.apply( nodes, textAndEntityNodes );
159+
160+
// Note: the following 3 lines were previously:
161+
// nodes.push.apply( nodes, textAndEntityNodes );
162+
// but this was causing a "Maximum Call Stack Size Exceeded"
163+
// error on inputs with a large number of html entities.
164+
textAndEntityNodes.forEach( function( node ) {
165+
nodes.push( node );
166+
} );
160167
}
161168
}
162169

tests/AutolinkerSpec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,22 @@ describe( "Autolinker", function() {
14891489
} );
14901490

14911491

1492+
it( "should not fail with a Maximum Call Stack Size Exceeded for an " +
1493+
"input with a large number of html entities (Issue #171)",
1494+
function() {
1495+
var testStr = (function() {
1496+
var t = [];
1497+
for (var i = 0; i < 50000; i++) {
1498+
t.push( ' /&gt;&lt;br' );
1499+
}
1500+
return t.join( '' );
1501+
})();
1502+
1503+
var result = autolinker.link( testStr );
1504+
expect( result ).toBe( testStr );
1505+
} );
1506+
1507+
14921508
it( "should NOT modify the email address with other tags when inside another anchor", function() {
14931509
var input = [
14941510
'<div>First name: Subin</div>',

0 commit comments

Comments
 (0)