Skip to content

Commit fc1cae1

Browse files
committed
Better handle extraneous </a> tags in the input HTML.
1 parent 755318f commit fc1cae1

File tree

6 files changed

+45
-6
lines changed

6 files changed

+45
-6
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ autolinker.link( "Go to www.google.com" );
148148

149149
## Changelog:
150150

151+
### 0.9.1
152+
153+
- Added a patch to attempt to better handle extraneous </a> tags in the input string if any exist. This is for when the
154+
input may have some invalid markup (for instance, on sites which allow user comments, blog posts, etc.).
155+
151156
### 0.9.0
152157

153158
- Added better support for the processing of existing HTML in the input string. Now handles namespaced tags, and attribute names

dist/Autolinker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* autolinker
3-
* 0.9.0
3+
* 0.9.1
44
*
55
* Copyright(c) 2014 Gregory Jacobs <[email protected]>
66
* MIT Licensed. http://www.opensource.org/licenses/mit-license.php
@@ -299,7 +299,7 @@
299299
resultHtml.push( this.processTextNode( inBetweenTagsText ) );
300300

301301
} else { // it's the end </a> tag
302-
anchorTagStackCount--;
302+
anchorTagStackCount = Math.max( anchorTagStackCount - 1, 0 ); // attempt to handle extraneous </a> tags by making sure the stack count never goes below 0
303303
if( anchorTagStackCount === 0 ) {
304304
resultHtml.push( inBetweenTagsText ); // We hit the matching </a> tag, simply add all of the text from the start <a> tag to the end </a> tag without linking it
305305
}

dist/Autolinker.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "autolinker",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "Simple utility to automatically link the URLs, email addresses, and Twitter handles in a given block of text/HTML",
55
"main": "dist/Autolinker.js",
66
"directories": {

src/Autolinker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@
290290
resultHtml.push( this.processTextNode( inBetweenTagsText ) );
291291

292292
} else { // it's the end </a> tag
293-
anchorTagStackCount--;
293+
anchorTagStackCount = Math.max( anchorTagStackCount - 1, 0 ); // attempt to handle extraneous </a> tags by making sure the stack count never goes below 0
294294
if( anchorTagStackCount === 0 ) {
295295
resultHtml.push( inBetweenTagsText ); // We hit the matching </a> tag, simply add all of the text from the start <a> tag to the end </a> tag without linking it
296296
}

tests/AutolinkerSpec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,40 @@ describe( "Autolinker", function() {
274274
'</div>'
275275
].join( "" ) );
276276
} );
277+
278+
279+
it( "should attempt to handle some invalid HTML markup relating to <a> tags, esp if there are extraneous closing </a> tags", function() {
280+
var html = '</a><a href="http://google.com">google.com</a>';
281+
282+
var result = Autolinker.link( html, { newWindow: false } );
283+
expect( result ).toBe( html );
284+
} );
285+
286+
287+
it( "should attempt to handle some more complex invalid HTML markup relating to <a> tags, esp if there are extraneous closing </a> tags", function() {
288+
var html = [
289+
'</a>', // invalid
290+
'<a href="http://google.com">google.com</a>',
291+
'<div>google.com</div>',
292+
'</a>', // invalid
293+
'<a href="http://yahoo.com">yahoo.com</a>',
294+
'</a>', // invalid
295+
'</a>', // invalid
296+
'twitter.com'
297+
].join( "" );
298+
299+
var result = Autolinker.link( html, { newWindow: false } );
300+
expect( result ).toBe( [
301+
'</a>', // invalid - left alone
302+
'<a href="http://google.com">google.com</a>', // valid tag - left alone
303+
'<div><a href="http://google.com">google.com</a></div>', // autolinked text in <div>
304+
'</a>', // invalid - left alone
305+
'<a href="http://yahoo.com">yahoo.com</a>', // valid tag - left alone
306+
'</a>', // invalid - left alone
307+
'</a>', // invalid - left alone
308+
'<a href="http://twitter.com">twitter.com</a>' // autolinked text
309+
].join( "" ) );
310+
} );
277311

278312
} );
279313

0 commit comments

Comments
 (0)