Skip to content

Commit 7532a7b

Browse files
committed
Fix for handling self-closing tags.
1 parent 67a0908 commit 7532a7b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Autolinker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@
243243
'(?:\\s*=\\s*' + attrValueRegex.source + ')?', // optional '=[value]'
244244
')*',
245245

246-
'\\s*', // any trailing spaces before the closing '>'
246+
'\\s*/?', // any trailing spaces and optional '/' before the closing '>'
247247
'>'
248248
].join( "" ), 'g' );
249249
} )(),

tests/AutolinkerSpec.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,26 @@ describe( "Autolinker", function() {
518518
} );
519519

520520

521-
it( "should NOT automatically link URLs within existing HTML tags", function() {
521+
it( "should NOT automatically link URLs within the attributes of existing HTML tags", function() {
522522
var result = autolinker.link( '<p>Joe went to <a href="http://www.yahoo.com">yahoo</a></p>' );
523523
expect( result ).toBe( '<p>Joe went to <a href="http://www.yahoo.com">yahoo</a></p>' );
524524
} );
525525

526+
527+
it( "should NOT automatically link URLs within the attributes of existing HTML tags when there are prefixed or suffixed spaces in the attribute values", function() {
528+
var result = autolinker.link( '<p>Joe went to <a href=" http://www.yahoo.com">yahoo</a></p>' );
529+
expect( result ).toBe( '<p>Joe went to <a href=" http://www.yahoo.com">yahoo</a></p>' );
530+
531+
var result2 = autolinker.link( '<p>Joe went to <a href="http://www.yahoo.com ">yahoo</a></p>' );
532+
expect( result2 ).toBe( '<p>Joe went to <a href="http://www.yahoo.com ">yahoo</a></p>' );
533+
} );
534+
535+
536+
it( "should NOT automatically link URLs within self-closing tags", function() {
537+
var result = autolinker.link( 'Just a flower image <img src="https://farm9.staticflickr.com/8378/8578790632_83c6471f3f_b.jpg" />' );
538+
expect( result ).toBe( 'Just a flower image <img src="https://farm9.staticflickr.com/8378/8578790632_83c6471f3f_b.jpg" />' );
539+
} );
540+
526541

527542
it( "should NOT automatically link a URL found within the inner text of a pre-existing anchor tag", function() {
528543
var result = autolinker.link( '<p>Joe went to <a href="http://www.yahoo.com">yahoo.com</a></p> yesterday.' );
@@ -548,6 +563,12 @@ describe( "Autolinker", function() {
548563
} );
549564

550565

566+
it( "should NOT automatically link an image tag with a URL inside of it, when it has another attribute which has extraneous spaces surround its value (Issue #45)", function() {
567+
var result = autolinker.link( "Testing <img src='http://terryshoemaker.files.wordpress.com/2013/03/placeholder1.jpg' style=' height: 22px; background-color: rgb(0, 188, 204); border-radius: 7px; padding: 2px; margin: 0px 2px;'>" );
568+
expect( result ).toBe( "Testing <img src='http://terryshoemaker.files.wordpress.com/2013/03/placeholder1.jpg' style=' height: 22px; background-color: rgb(0, 188, 204); border-radius: 7px; padding: 2px; margin: 0px 2px;'>" );
569+
} );
570+
571+
551572
it( "should allow the full range of HTML attribute name characters as specified in the W3C HTML syntax document (http://www.w3.org/TR/html-markup/syntax.html)", function() {
552573
// Note: We aren't actually expecting the HTML to be modified by this test
553574
var inAndOutHtml = '<ns:p>Foo <a data-qux-="" href="http://www.example.com">Bar<\/a> Baz<\/ns:p>';

0 commit comments

Comments
 (0)