Skip to content

Commit deef0e3

Browse files
committed
Fix to not autolink URLs inside a <!DOCTYPE> tag
1 parent 609fbd6 commit deef0e3

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

dist/Autolinker.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* Autolinker.js
3-
* 0.11.0
3+
* 0.11.2
44
*
55
* Copyright(c) 2014 Gregory Jacobs <[email protected]>
66
* MIT Licensed. http://www.opensource.org/licenses/mit-license.php
@@ -237,22 +237,26 @@
237237
htmlRegex : (function() {
238238
var tagNameRegex = /[0-9a-zA-Z:]+/,
239239
attrNameRegex = /[^\s\0"'>\/=\x01-\x1F\x7F]+/, // the unicode range accounts for excluding control chars, and the delete char
240-
attrValueRegex = /(?:".*?"|'.*?'|[^'"=<>`\s]+)/; // double quoted, single quoted, or unquoted attribute values
240+
attrValueRegex = /(?:".*?"|'.*?'|[^'"=<>`\s]+)/, // double quoted, single quoted, or unquoted attribute values
241+
nameEqualsValueRegex = attrNameRegex.source + '(?:\\s*=\\s*' + attrValueRegex.source + ')?'; // optional '=[value]'
241242

242243
return new RegExp( [
243-
'<(/)?', // Beginning of a tag. Either '<' for a start tag, or '</' for an end tag. The slash or an empty string is Capturing Group 1.
244+
'<(?:!|(/))?', // Beginning of a tag. Either '<' for a start tag, '</' for an end tag, or <! for the <!DOCTYPE ...> tag. The slash or an empty string is Capturing Group 1.
244245

245246
// The tag name (Capturing Group 2)
246247
'(' + tagNameRegex.source + ')',
247248

248249
// Zero or more attributes following the tag name
249250
'(?:',
250251
'\\s+', // one or more whitespace chars before an attribute
251-
attrNameRegex.source,
252-
'(?:\\s*=\\s*' + attrValueRegex.source + ')?', // optional '=[value]'
252+
253+
// Either:
254+
// A. tag="value", or
255+
// B. "value" alone (for <!DOCTYPE> tag. Ex: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">)
256+
'(?:', nameEqualsValueRegex, '|', attrValueRegex.source + ')',
253257
')*',
254258

255-
'\\s*', // any trailing spaces before the closing '>'
259+
'\\s*/?', // any trailing spaces and optional '/' before the closing '>'
256260
'>'
257261
].join( "" ), 'g' );
258262
} )(),

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.11.0",
3+
"version": "0.11.2",
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: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,23 @@
228228
htmlRegex : (function() {
229229
var tagNameRegex = /[0-9a-zA-Z:]+/,
230230
attrNameRegex = /[^\s\0"'>\/=\x01-\x1F\x7F]+/, // the unicode range accounts for excluding control chars, and the delete char
231-
attrValueRegex = /(?:".*?"|'.*?'|[^'"=<>`\s]+)/; // double quoted, single quoted, or unquoted attribute values
231+
attrValueRegex = /(?:".*?"|'.*?'|[^'"=<>`\s]+)/, // double quoted, single quoted, or unquoted attribute values
232+
nameEqualsValueRegex = attrNameRegex.source + '(?:\\s*=\\s*' + attrValueRegex.source + ')?'; // optional '=[value]'
232233

233234
return new RegExp( [
234-
'<(/)?', // Beginning of a tag. Either '<' for a start tag, or '</' for an end tag. The slash or an empty string is Capturing Group 1.
235+
'<(?:!|(/))?', // Beginning of a tag. Either '<' for a start tag, '</' for an end tag, or <! for the <!DOCTYPE ...> tag. The slash or an empty string is Capturing Group 1.
235236

236237
// The tag name (Capturing Group 2)
237238
'(' + tagNameRegex.source + ')',
238239

239240
// Zero or more attributes following the tag name
240241
'(?:',
241242
'\\s+', // one or more whitespace chars before an attribute
242-
attrNameRegex.source,
243-
'(?:\\s*=\\s*' + attrValueRegex.source + ')?', // optional '=[value]'
243+
244+
// Either:
245+
// A. tag="value", or
246+
// B. "value" alone (for <!DOCTYPE> tag. Ex: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">)
247+
'(?:', nameEqualsValueRegex, '|', attrValueRegex.source + ')',
244248
')*',
245249

246250
'\\s*/?', // any trailing spaces and optional '/' before the closing '>'

tests/AutolinkerSpec.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,36 @@ describe( "Autolinker", function() {
581581
} );
582582

583583

584+
it( "should NOT automatically link anything in a !DOCTYPE tag (Issue #53)", function() {
585+
var input = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
586+
587+
var result = autolinker.link( input );
588+
expect( result ).toBe( input );
589+
} );
590+
591+
592+
it( "should automatically link tags after a !DOCTYPE tag", function() {
593+
var input = [
594+
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
595+
'<html>',
596+
'<body>',
597+
'Welcome to mysite.com',
598+
'</body>',
599+
'</html>'
600+
].join( "" );
601+
602+
var result = autolinker.link( input );
603+
expect( result ).toBe( [
604+
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
605+
'<html>',
606+
'<body>',
607+
'Welcome to <a href="http://mysite.com">mysite.com</a>',
608+
'</body>',
609+
'</html>'
610+
].join( "" ) );
611+
} );
612+
613+
584614
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() {
585615
// Note: We aren't actually expecting the HTML to be modified by this test
586616
var inAndOutHtml = '<ns:p>Foo <a data-qux-="" href="http://www.example.com">Bar<\/a> Baz<\/ns:p>';

0 commit comments

Comments
 (0)