Skip to content

Commit 2738305

Browse files
authored
Merge pull request #253 from examan/master
skip parsing style and script tag
2 parents 4601795 + 6030e17 commit 2738305

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Autolinker.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -605,19 +605,19 @@ Autolinker.prototype = {
605605
matches = [];
606606

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

613-
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
614-
if( !node.isClosing() ) { // it's the start <a> tag
613+
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
614+
if( !node.isClosing() ) { // it's the start <a>, <style> or <script> tag
615615
anchorTagStackCount++;
616-
} else { // it's the end </a> tag
616+
} else { // it's the end </a>, </style> or </script> tag
617617
anchorTagStackCount = Math.max( anchorTagStackCount - 1, 0 ); // attempt to handle extraneous </a> tags by making sure the stack count never goes below 0
618618
}
619619

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

623623
matches.push.apply( matches, textNodeMatches );

tests/AutolinkerSpec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,18 @@ describe( "Autolinker", function() {
14221422
} );
14231423

14241424

1425+
it( "should NOT automatically link a URL found within the inner text of a style tag", function() {
1426+
var result = autolinker.link( 'Testing with <style> .class { background-image: url("http://www.example.com/image.png"); } </style> tags' );
1427+
expect( result ).toBe( 'Testing with <style> .class { background-image: url("http://www.example.com/image.png"); } </style> tags' );
1428+
} );
1429+
1430+
1431+
it( "should NOT automatically link a URL found within the inner text of a script tag", function() {
1432+
var result = autolinker.link( 'Testing with <script> alert("http://google.com"); </script> tags' );
1433+
expect( result ).toBe( 'Testing with <script> alert("http://google.com"); </script> tags' );
1434+
} );
1435+
1436+
14251437
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() {
14261438
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;'>" );
14271439
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;'>" );

0 commit comments

Comments
 (0)