Skip to content

Commit 536b947

Browse files
committed
Handle null or undefined being passed to the link() method by returning empty string.
1 parent e732bb6 commit 536b947

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/Autolinker.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ Autolinker.prototype = {
264264
* @return {String} The HTML, with matches automatically linked.
265265
*/
266266
link : function( textOrHtml ) {
267+
if( !textOrHtml ) { return ""; } // handle `null` and `undefined`
268+
267269
var htmlParser = this.getHtmlParser(),
268270
htmlNodes = htmlParser.parse( textOrHtml ),
269271
anchorTagStackCount = 0, // used to only process text around anchor tags, and any inner text/html they may have

tests/AutolinkerSpec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ describe( "Autolinker", function() {
4747
} );
4848

4949

50+
it( 'should return an empty string when provided `undefined` as its argument', function() {
51+
expect( autolinker.link( undefined ) ).toBe( '' );
52+
} );
53+
54+
it( 'should return an empty string when provided `null` as its argument', function() {
55+
expect( autolinker.link( null ) ).toBe( '' );
56+
} );
57+
58+
5059
describe( "URL linking", function() {
5160

5261
describe( "protocol-prefixed URLs (i.e. URLs starting with http:// or https://)", function() {

0 commit comments

Comments
 (0)