Skip to content

Commit 94b824d

Browse files
committed
Merge pull request #101 from dgrad/master
Allow single-letter schemes (fixes #100)
2 parents ad63dc9 + 9379db7 commit 94b824d

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
lines changed

src/matchParser/MatchParser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Autolinker.matchParser.MatchParser = Autolinker.Util.extend( Object, {
106106

107107
emailRegex = /(?:[\-;:&=\+\$,\w\.]+@)/, // something@ for email addresses (a.k.a. local-part)
108108
phoneRegex = /(?:\+?\d{1,3}[-\s.])?\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/, // ex: (123) 456-7890, 123 456 7890, 123-456-7890, etc.
109-
protocolRegex = /(?:[A-Za-z][-.+A-Za-z0-9]+:(?![A-Za-z][-.+A-Za-z0-9]+:\/\/)(?!\d+\/?)(?:\/\/)?)/, // match protocol, allow in format "http://" or "mailto:". However, do not match the first part of something like 'link:http://www.google.com' (i.e. don't match "link:"). Also, make sure we don't interpret 'google.com:8000' as if 'google.com' was a protocol here (i.e. ignore a trailing port number in this regex)
109+
protocolRegex = /(?:[A-Za-z][-.+A-Za-z0-9]*:(?![A-Za-z][-.+A-Za-z0-9]*:\/\/)(?!\d+\/?)(?:\/\/)?)/, // match protocol, allow in format "http://" or "mailto:". However, do not match the first part of something like 'link:http://www.google.com' (i.e. don't match "link:"). Also, make sure we don't interpret 'google.com:8000' as if 'google.com' was a protocol here (i.e. ignore a trailing port number in this regex)
110110
wwwRegex = /(?:www\.)/, // starting with 'www.'
111111
domainNameRegex = /[A-Za-z0-9\.\-]*[A-Za-z0-9\-]/, // anything looking at all like a domain, non-unicode domains, not ending in a period
112112
tldRegex = /\.(?:international|construction|contractors|enterprises|photography|productions|foundation|immobilien|industries|management|properties|technology|christmas|community|directory|education|equipment|institute|marketing|solutions|vacations|bargains|boutique|builders|catering|cleaning|clothing|computer|democrat|diamonds|graphics|holdings|lighting|partners|plumbing|supplies|training|ventures|academy|careers|company|cruises|domains|exposed|flights|florist|gallery|guitars|holiday|kitchen|neustar|okinawa|recipes|rentals|reviews|shiksha|singles|support|systems|agency|berlin|camera|center|coffee|condos|dating|estate|events|expert|futbol|kaufen|luxury|maison|monash|museum|nagoya|photos|repair|report|social|supply|tattoo|tienda|travel|viajes|villas|vision|voting|voyage|actor|build|cards|cheap|codes|dance|email|glass|house|mango|ninja|parts|photo|press|shoes|solar|today|tokyo|tools|watch|works|aero|arpa|asia|best|bike|blue|buzz|camp|club|cool|coop|farm|fish|gift|guru|info|jobs|kiwi|kred|land|limo|link|menu|mobi|moda|name|pics|pink|post|qpon|rich|ruhr|sexy|tips|vote|voto|wang|wien|wiki|zone|bar|bid|biz|cab|cat|ceo|com|edu|gov|int|kim|mil|net|onl|org|pro|pub|red|tel|uno|wed|xxx|xyz|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cw|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw)\b/, // match our known top level domains (TLDs)

src/matchParser/MatchValidator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Autolinker.MatchValidator = Autolinker.Util.extend( Object, {
4040
* @private
4141
* @property {RegExp} hasFullProtocolRegex
4242
*/
43-
hasFullProtocolRegex : /^[A-Za-z][-.+A-Za-z0-9]+:\/\//,
43+
hasFullProtocolRegex : /^[A-Za-z][-.+A-Za-z0-9]*:\/\//,
4444

4545
/**
4646
* Regex to find the URI scheme, such as 'mailto:'.
@@ -50,7 +50,7 @@ Autolinker.MatchValidator = Autolinker.Util.extend( Object, {
5050
* @private
5151
* @property {RegExp} uriSchemeRegex
5252
*/
53-
uriSchemeRegex : /^[A-Za-z][-.+A-Za-z0-9]+:/,
53+
uriSchemeRegex : /^[A-Za-z][-.+A-Za-z0-9]*:/,
5454

5555
/**
5656
* Regex to determine if at least one word char exists after the protocol (i.e. after the ':')
@@ -190,4 +190,4 @@ Autolinker.MatchValidator = Autolinker.Util.extend( Object, {
190190
return ( !!protocolRelativeMatch && this.invalidProtocolRelMatchRegex.test( protocolRelativeMatch ) );
191191
}
192192

193-
} );
193+
} );

tests/AutolinkerSpec.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ describe( "Autolinker", function() {
214214
} );
215215

216216

217-
it( "should autolink protocols with at least two characters", function() {
218-
var result = autolinker.link( 'link this: gg://example.com/' );
219-
expect( result ).toBe( 'link this: <a href="gg://example.com/">gg://example.com</a>' );
217+
it( "should autolink protocols with at least one character", function() {
218+
var result = autolinker.link( 'link this: g://example.com/' );
219+
expect( result ).toBe( 'link this: <a href="g://example.com/">g://example.com</a>' );
220220
} );
221221

222222

@@ -226,12 +226,6 @@ describe( "Autolinker", function() {
226226
} );
227227

228228

229-
it( "should NOT autolink a protocol with only one character", function() {
230-
var result = autolinker.link( 'do not link this: a://example' );
231-
expect( result ).toBe( 'do not link this: a://example' );
232-
} );
233-
234-
235229
it( "should autolink protocols with digits, dashes, dots, and plus signs in their names", function() {
236230
var result1 = autolinker.link( 'link this: a1://example' );
237231
expect( result1 ).toBe( 'link this: <a href="a1://example">a1://example</a>' );
@@ -252,17 +246,20 @@ describe( "Autolinker", function() {
252246

253247

254248
it( "should NOT autolink protocols that start with a digit, dash, plus sign, or dot, as per http://tools.ietf.org/html/rfc3986#section-3.1", function() {
255-
var result = autolinker.link( 'do not link this: 1a://example' );
256-
expect( result ).toBe( 'do not link this: 1a://example' );
249+
var result = autolinker.link( 'do not link first char: 1a://example' );
250+
expect( result ).toBe( 'do not link first char: 1<a href="a://example">a://example</a>' );
251+
252+
var result2 = autolinker.link( 'do not link first char: -a://example' );
253+
expect( result2 ).toBe( 'do not link first char: -<a href="a://example">a://example</a>' );
257254

258-
var result2 = autolinker.link( 'do not link this: -a://example' );
259-
expect( result2 ).toBe( 'do not link this: -a://example' );
255+
var result3 = autolinker.link( 'do not link first char: +a://example' );
256+
expect( result3 ).toBe( 'do not link first char: +<a href="a://example">a://example</a>' );
260257

261-
var result3 = autolinker.link( 'do not link this: +a://example' );
262-
expect( result3 ).toBe( 'do not link this: +a://example' );
258+
var result4 = autolinker.link( 'do not link first char: .a://example' );
259+
expect( result4 ).toBe( 'do not link first char: .<a href="a://example">a://example</a>' );
263260

264-
var result4 = autolinker.link( 'do not link this: .a://example' );
265-
expect( result4 ).toBe( 'do not link this: .a://example' );
261+
var result5 = autolinker.link( 'do not link first char: .aa://example' );
262+
expect( result5 ).toBe( 'do not link first char: .<a href="aa://example">aa://example</a>' );
266263
} );
267264

268265

0 commit comments

Comments
 (0)