Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Autolinker.js
Original file line number Diff line number Diff line change
Expand Up @@ -605,19 +605,19 @@ Autolinker.prototype = {
matches = [];

// Find all matches within the `textOrHtml` (but not matches that are
// already nested within <a> tags)
// already nested within <a>, <style> and <script> tags)
for( var i = 0, len = htmlNodes.length; i < len; i++ ) {
var node = htmlNodes[ i ],
nodeType = node.getType();

if( nodeType === 'element' && node.getTagName() === 'a' ) { // Process HTML anchor element nodes in the input `textOrHtml` to find out when we're within an <a> tag
if( !node.isClosing() ) { // it's the start <a> tag
if( nodeType === 'element' && ['a', 'style', 'script'].indexOf(node.getTagName()) !== -1 ) { // Process HTML anchor, style and script element nodes in the input `textOrHtml` to find out when we're within an <a>, <style> or <script> tag
if( !node.isClosing() ) { // it's the start <a>, <style> or <script> tag
anchorTagStackCount++;
} else { // it's the end </a> tag
} else { // it's the end </a>, </style> or </script> tag
anchorTagStackCount = Math.max( anchorTagStackCount - 1, 0 ); // attempt to handle extraneous </a> tags by making sure the stack count never goes below 0
}

} else if( nodeType === 'text' && anchorTagStackCount === 0 ) { // Process text nodes that are not within an <a> tag
} else if( nodeType === 'text' && anchorTagStackCount === 0 ) { // Process text nodes that are not within an <a>, <style> and <script> tag
var textNodeMatches = this.parseText( node.getText(), node.getOffset() );

matches.push.apply( matches, textNodeMatches );
Expand Down
12 changes: 12 additions & 0 deletions tests/AutolinkerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,18 @@ describe( "Autolinker", function() {
} );


it( "should NOT automatically link a URL found within the inner text of a style tag", function() {
var result = autolinker.link( 'Testing with <style> .class { background-image: url("http://www.example.com/image.png"); } </style> tags' );
expect( result ).toBe( 'Testing with <style> .class { background-image: url("http://www.example.com/image.png"); } </style> tags' );
} );


it( "should NOT automatically link a URL found within the inner text of a script tag", function() {
var result = autolinker.link( 'Testing with <script> alert("http://google.com"); </script> tags' );
expect( result ).toBe( 'Testing with <script> alert("http://google.com"); </script> tags' );
} );


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() {
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;'>" );
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;'>" );
Expand Down