',
@@ -1393,7 +1548,7 @@ describe( "Autolinker", function() {
it( "should properly skip over attribute names and values that could be interpreted as urls/emails/twitter accts, while still autolinking urls in their inner text", function() {
var html = '
';
- var result = autolinker.link( html );
+ var result = twitterAutolinker.link( html );
expect( result ).toBe( [
'
',
'
google.com ',
@@ -1662,8 +1817,16 @@ describe( "Autolinker", function() {
it( "should add className to twitter links", function() {
- var result = Autolinker.link( "hi from @iggypopschest", { newWindow: false, twitter: true, className: 'myLink' } );
- expect( result ).toBe( 'hi from ' );
+ var result = Autolinker.link( "hi from @iggypopschest", { newWindow: false, mention: 'twitter', className: 'myLink' } );
+ expect( result ).toBe( 'hi from ' );
+ } );
+
+ it( "should add className to mention links", function() {
+ var result = Autolinker.link( "hi from @iggypopschest", { newWindow: false, mention: 'twitter', className: 'myLink' } );
+ expect( result ).toBe( 'hi from ' );
+
+ result = Autolinker.link( "hi from @iggypopschest", { newWindow: false, mention: 'instagram', className: 'myLink' } );
+ expect( result ).toBe( 'hi from
@iggypopschest' );
} );
} );
@@ -1757,83 +1920,157 @@ describe( "Autolinker", function() {
} );
- describe( "`urls` (as boolean), `email`, `phone`, and `twitter` options", function() {
+ describe( "`urls` (as boolean), `email`, `phone`, `twitter`, and `mention` options", function() {
var inputStr = [
"Website: asdf.com",
"Email: asdf@asdf.com",
"Phone: (123) 456-7890",
- "Twitter: @asdf",
+ "Mention: @asdf",
"Hashtag: #asdf"
].join( ", " );
- it( "should link all 5 types if all 5 urls/email/phone/twitter/hashtag options are enabled", function() {
- var result = Autolinker.link( inputStr, { newWindow: false, hashtag: 'twitter' } );
+ it( "should link all 5 types if all 5 urls/email/phone/mention/hashtag options are enabled", function() {
+ var result = Autolinker.link( inputStr, {
+ hashtag: 'twitter',
+ mention: 'twitter',
+ newWindow: false
+ } );
+ expect( result ).toBe( [
+ 'Website:
asdf.com',
+ 'Email:
asdf@asdf.com',
+ 'Phone:
(123) 456-7890',
+ 'Mention:
@asdf',
+ 'Hashtag:
#asdf'
+ ].join( ", " ) );
+ } );
+
+
+ it( "should link mentions based on value provided to mention option", function() {
+ var result = Autolinker.link( inputStr, { newWindow: false, hashtag: 'twitter', mention: 'twitter' } );
+ expect( result ).toBe( [
+ 'Website:
asdf.com',
+ 'Email:
asdf@asdf.com',
+ 'Phone:
(123) 456-7890',
+ 'Mention:
@asdf',
+ 'Hashtag:
#asdf'
+ ].join( ", " ) );
+
+ result = Autolinker.link( inputStr, { newWindow: false, hashtag: 'twitter', mention: 'instagram' } );
+ expect( result ).toBe( [
+ 'Website:
asdf.com',
+ 'Email:
asdf@asdf.com',
+ 'Phone:
(123) 456-7890',
+ 'Mention:
@asdf',
+ 'Hashtag:
#asdf'
+ ].join( ", " ) );
+ } );
+
+
+ it( "should ignore twitter option if mention option is set", function() {
+ var result = Autolinker.link( inputStr, { newWindow: false, hashtag: 'twitter', twitter: false, mention: 'twitter' } );
+ expect( result ).toBe( [
+ 'Website:
asdf.com',
+ 'Email:
asdf@asdf.com',
+ 'Phone:
(123) 456-7890',
+ 'Mention:
@asdf',
+ 'Hashtag:
#asdf'
+ ].join( ", " ) );
+
+ result = Autolinker.link( inputStr, { newWindow: false, hashtag: 'twitter', twitter: true, mention: 'instagram' } );
expect( result ).toBe( [
'Website:
asdf.com',
'Email:
asdf@asdf.com',
'Phone:
(123) 456-7890',
- 'Twitter:
@asdf',
+ 'Mention:
@asdf',
'Hashtag:
#asdf'
].join( ", " ) );
} );
it( "should not link urls when they are disabled", function() {
- var result = Autolinker.link( inputStr, { newWindow: false, hashtag: 'twitter', urls: false } );
+ var result = Autolinker.link( inputStr, {
+ mention: 'twitter',
+ hashtag: 'twitter',
+ urls: false,
+ newWindow: false
+ } );
+
expect( result ).toBe( [
'Website: asdf.com',
'Email:
asdf@asdf.com',
'Phone:
(123) 456-7890',
- 'Twitter:
@asdf',
+ 'Mention:
@asdf',
'Hashtag:
#asdf'
].join( ", " ) );
} );
it( "should not link email addresses when they are disabled", function() {
- var result = Autolinker.link( inputStr, { newWindow: false, hashtag: 'twitter', email: false } );
+ var result = Autolinker.link( inputStr, {
+ mention: 'twitter',
+ hashtag: 'twitter',
+ email: false,
+ newWindow: false
+ } );
+
expect( result ).toBe( [
'Website:
asdf.com',
'Email: asdf@asdf.com',
'Phone:
(123) 456-7890',
- 'Twitter:
@asdf',
+ 'Mention:
@asdf',
'Hashtag:
#asdf'
].join( ", " ) );
} );
it( "should not link phone numbers when they are disabled", function() {
- var result = Autolinker.link( inputStr, { newWindow: false, hashtag: 'twitter', phone: false } );
+ var result = Autolinker.link( inputStr, {
+ hashtag : 'twitter',
+ mention : 'twitter',
+ phone : false,
+ newWindow : false
+ } );
+
expect( result ).toBe( [
'Website:
asdf.com',
'Email:
asdf@asdf.com',
'Phone: (123) 456-7890',
- 'Twitter:
@asdf',
+ 'Mention:
@asdf',
'Hashtag:
#asdf'
].join( ", " ) );
} );
- it( "should not link Twitter handles when they are disabled", function() {
- var result = Autolinker.link( inputStr, { newWindow: false, hashtag: 'twitter', twitter: false } );
+ it( "should not link mention handles when they are disabled", function() {
+ var result = Autolinker.link( inputStr, {
+ newWindow: false,
+ hashtag: 'twitter',
+ mention: false
+ } );
+
expect( result ).toBe( [
'Website:
asdf.com',
'Email:
asdf@asdf.com',
'Phone:
(123) 456-7890',
- 'Twitter: @asdf',
+ 'Mention: @asdf',
'Hashtag:
#asdf'
].join( ", " ) );
} );
it( "should not link Hashtags when they are disabled", function() {
- var result = Autolinker.link( inputStr, { newWindow: false, hashtag: false } );
+ var result = Autolinker.link( inputStr, {
+ mention : 'twitter',
+ hashtag : false,
+ newWindow : false
+ } );
+
expect( result ).toBe( [
'Website:
asdf.com',
'Email:
asdf@asdf.com',
'Phone:
(123) 456-7890',
- 'Twitter:
@asdf',
+ 'Mention:
@asdf',
'Hashtag: #asdf'
].join( ", " ) );
} );
@@ -1843,13 +2080,46 @@ describe( "Autolinker", function() {
describe( "`replaceFn` option", function() {
var returnTrueFn = function() { return true; },
- returnFalseFn = function() { return false; };
+ returnFalseFn = function() { return false; },
+ replaceFnSpy;
+
+ beforeEach( function() {
+ replaceFnSpy = jasmine.createSpy( 'replaceFnSpy' );
+ } );
+
+
+ it( "by default, should be called with with the `Autolinker` instance " +
+ "as the context object (`this` reference)",
+ function() {
+ var replaceFnAutolinker = new Autolinker( {
+ replaceFn: replaceFnSpy
+ } );
+ replaceFnAutolinker.link( "asdf.com" ); // will call the `replaceFn`
+
+ expect( replaceFnSpy ).toHaveBeenCalled();
+ expect( replaceFnSpy.calls.first().object ).toBe( replaceFnAutolinker );
+ } );
+
+
+ it( "when provided a `context` option, should be called with with " +
+ "that object as the context object (`this` reference)",
+ function() {
+ var contextObj = { prop: 'value' };
+ var replaceFnAutolinker = new Autolinker( {
+ replaceFn : replaceFnSpy,
+ context : contextObj
+ } );
+ replaceFnAutolinker.link( "asdf.com" ); // will call the `replaceFn`
+
+ expect( replaceFnSpy ).toHaveBeenCalled();
+ expect( replaceFnSpy.calls.first().object ).toBe( contextObj );
+ } );
it( "should populate a UrlMatch object with the appropriate properties", function() {
var replaceFnCallCount = 0;
var result = Autolinker.link( "Website: asdf.com ", { // purposeful trailing space
- replaceFn : function( autolinker, match ) {
+ replaceFn : function( match ) {
replaceFnCallCount++;
expect( match.getMatchedText() ).toBe( 'asdf.com' );
@@ -1864,7 +2134,7 @@ describe( "Autolinker", function() {
it( "should populate an EmailMatch object with the appropriate properties", function() {
var replaceFnCallCount = 0;
var result = Autolinker.link( "Email: asdf@asdf.com ", { // purposeful trailing space
- replaceFn : function( autolinker, match ) {
+ replaceFn : function( match ) {
replaceFnCallCount++;
expect( match.getMatchedText() ).toBe( 'asdf@asdf.com' );
@@ -1880,7 +2150,7 @@ describe( "Autolinker", function() {
var replaceFnCallCount = 0;
var result = Autolinker.link( "Hashtag: #myHashtag ", { // purposeful trailing space
hashtag: 'twitter',
- replaceFn : function( autolinker, match ) {
+ replaceFn : function( match ) {
replaceFnCallCount++;
expect( match.getType() ).toBe( 'hashtag' );
@@ -1896,11 +2166,28 @@ describe( "Autolinker", function() {
it( "should populate a TwitterMatch object with the appropriate properties", function() {
var replaceFnCallCount = 0;
var result = Autolinker.link( "Twitter: @myTwitter ", { // purposeful trailing space
- replaceFn : function( autolinker, match ) {
+ mention: 'twitter',
+ replaceFn : function( match ) {
+ replaceFnCallCount++;
+
+ expect( match.getMatchedText() ).toBe( '@myTwitter' );
+ expect( match.getMention() ).toBe( 'myTwitter' );
+ }
+ } );
+
+ expect( replaceFnCallCount ).toBe( 1 ); // make sure the replaceFn was called
+ } );
+
+
+ it( "should populate a MentionMatch object with the appropriate properties", function() {
+ var replaceFnCallCount = 0;
+ var result = Autolinker.link( "Mention: @myTwitter ", { // purposeful trailing space
+ mention: 'twitter',
+ replaceFn : function( match ) {
replaceFnCallCount++;
expect( match.getMatchedText() ).toBe( '@myTwitter' );
- expect( match.getTwitterHandle() ).toBe( 'myTwitter' );
+ expect( match.getMention() ).toBe( 'myTwitter' );
}
} );
@@ -1910,6 +2197,7 @@ describe( "Autolinker", function() {
it( "should replace the match as Autolinker would normally do when `true` is returned from the `replaceFn`", function() {
var result = Autolinker.link( "Website: asdf.com, Email: asdf@asdf.com, Twitter: @asdf", {
+ mention : 'twitter',
newWindow : false, // just to suppress the target="_blank" from the output for this test
replaceFn : returnTrueFn
} );
@@ -1924,6 +2212,7 @@ describe( "Autolinker", function() {
it( "should replace the match as Autolinker would normally do when there is no return value (i.e. `undefined` is returned) from the `replaceFn`", function() {
var result = Autolinker.link( "Website: asdf.com, Email: asdf@asdf.com, Twitter: @asdf", {
+ mention : 'twitter',
newWindow : false, // just to suppress the target="_blank" from the output for this test
replaceFn : function() {} // no return value (`undefined` is returned)
} );
@@ -1938,6 +2227,7 @@ describe( "Autolinker", function() {
it( "should leave the match as-is when `false` is returned from the `replaceFn`", function() {
var result = Autolinker.link( "Website: asdf.com, Email: asdf@asdf.com, Twitter: @asdf", {
+ mention : 'twitter',
replaceFn : returnFalseFn
} );
@@ -1951,6 +2241,7 @@ describe( "Autolinker", function() {
it( "should use a string returned from the `replaceFn` as the HTML that is replaced in the input", function() {
var result = Autolinker.link( "Website: asdf.com, Email: asdf@asdf.com, Twitter: @asdf", {
+ mention : 'twitter',
replaceFn : function() { return "test"; }
} );
@@ -1962,7 +2253,7 @@ describe( "Autolinker", function() {
var result = Autolinker.link( "Website: asdf.com", {
newWindow : false,
- replaceFn : function( autolinker, match ) {
+ replaceFn : function( match ) {
var tag = match.buildTag();
tag.setInnerHtml( 'asdf!' ); // just to check that we're replacing with the returned `tag` instance
return tag;
@@ -1977,7 +2268,7 @@ describe( "Autolinker", function() {
var result = Autolinker.link( "Website: asdf.com", {
newWindow : false,
- replaceFn : function( autolinker, match ) {
+ replaceFn : function( match ) {
var tag = match.buildTag();
tag.addClass( 'test' );
tag.addClass( 'test2' );
@@ -2006,7 +2297,7 @@ describe( "Autolinker", function() {
var result = Autolinker.link( "@asdf", { replaceFn: returnFalseFn } );
expect( result ).toBe( "@asdf" );
- var result2 = Autolinker.link( "Twitter: @asdf", { replaceFn: returnFalseFn } );
+ var result2 = Autolinker.link( "Twitter: @asdf", { mention: 'twitter', replaceFn: returnFalseFn } );
expect( result2 ).toBe( "Twitter: @asdf" );
} );
@@ -2053,7 +2344,7 @@ describe( "Autolinker", function() {
unlinked : 'asdf@asdf.com',
linked : '
asdf@asdf.com'
},
- twitter : {
+ mention : {
unlinked : '@asdf',
linked : '
@asdf'
},
@@ -2074,7 +2365,7 @@ describe( "Autolinker", function() {
'Check link 2: <%= wwwUrl %>.',
'Check link 3: <%= tldUrl %>.',
'My email is: <%= email %>.',
- 'My twitter username is <%= twitter %>.',
+ 'My mention (twitter) username is <%= mention %>.',
'My phone number is <%= phone %>.',
'Hashtag <%= hashtag %>.'
].join( '\n' ) );
@@ -2084,7 +2375,7 @@ describe( "Autolinker", function() {
wwwUrl : testCases.wwwUrl.unlinked,
tldUrl : testCases.tldUrl.unlinked,
email : testCases.email.unlinked,
- twitter : testCases.twitter.unlinked,
+ mention : testCases.mention.unlinked,
phone : testCases.phone.unlinked,
hashtag : testCases.hashtag.unlinked
} );
@@ -2102,7 +2393,7 @@ describe( "Autolinker", function() {
wwwMatches : !!( i & parseInt( '00000010', 2 ) ),
tldMatches : !!( i & parseInt( '00000100', 2 ) ),
email : !!( i & parseInt( '00001000', 2 ) ),
- twitter : !!( i & parseInt( '00010000', 2 ) ),
+ mention : !!( i & parseInt( '00010000', 2 ) ) ? 'twitter' : false,
phone : !!( i & parseInt( '00100000', 2 ) ),
hashtag : !!( i & parseInt( '01000000', 2 ) ) ? 'twitter' : false
};
@@ -2114,7 +2405,7 @@ describe( "Autolinker", function() {
tldMatches : cfg.tldMatches
},
email : cfg.email,
- twitter : cfg.twitter,
+ mention : cfg.mention,
phone : cfg.phone,
hashtag : cfg.hashtag,
@@ -2143,7 +2434,7 @@ describe( "Autolinker", function() {
wwwUrl : cfg.wwwMatches ? testCases.wwwUrl.linked : testCases.wwwUrl.unlinked,
tldUrl : cfg.tldMatches ? testCases.tldUrl.linked : testCases.tldUrl.unlinked,
email : cfg.email ? testCases.email.linked : testCases.email.unlinked,
- twitter : cfg.twitter ? testCases.twitter.linked : testCases.twitter.unlinked,
+ mention : cfg.mention ? testCases.mention.linked : testCases.mention.unlinked,
phone : cfg.phone ? testCases.phone.linked : testCases.phone.unlinked,
hashtag : cfg.hashtag ? testCases.hashtag.linked : testCases.hashtag.unlinked
} );
diff --git a/tests/index.html b/tests/index.html
index 9780740d..edf8a876 100644
--- a/tests/index.html
+++ b/tests/index.html
@@ -26,13 +26,13 @@
-
+
-
+
@@ -50,7 +50,7 @@
-
+
diff --git a/tests/match/MatchChecker.js b/tests/match/MatchChecker.js
index 416432ab..ee657ce6 100644
--- a/tests/match/MatchChecker.js
+++ b/tests/match/MatchChecker.js
@@ -59,21 +59,24 @@ Autolinker.match.MatchChecker = {
expect( match.getNumber() ).toBe( number );
expect( match.getOffset() ).toBe( offset );
},
-
+
/**
- * Expects a {@link Autolinker.match.Twitter Twitter} match.
+ * Expects a {@link Autolinker.match.Mention Mention} match.
*
- * @param {Autolinker.match.Twitter} match The Match object to check.
- * @param {String} twitterHandle The Twitter handle to expect, without the
+ * @param {Autolinker.match.Mention} match The Match object to check.
+ * @param {String} serviceName The service name to expect of where to direct
+ * clicks to the mention to. Ex: 'twitter', 'instagram'.
+ * @param {String} mention The mention to expect, without the
* prefixed '@' character.
* @param {Number} offset The offset for the match in the original string to
* expect.
*/
- expectTwitterMatch : function( match, twitterHandle, offset ) {
- this.expectMatchType( match, 'Twitter' );
+ expectMentionMatch : function( match, serviceName, mention, offset ) {
+ this.expectMatchType( match, 'Mention' );
- expect( match.getTwitterHandle() ).toBe( twitterHandle );
+ expect( match.getServiceName() ).toBe( serviceName );
+ expect( match.getMention() ).toBe( mention );
expect( match.getOffset() ).toBe( offset );
},
@@ -113,4 +116,4 @@ Autolinker.match.MatchChecker = {
}
}
-};
\ No newline at end of file
+};
diff --git a/tests/matcher/TwitterSpec.js b/tests/matcher/MentionSpec.js
similarity index 54%
rename from tests/matcher/TwitterSpec.js
rename to tests/matcher/MentionSpec.js
index 8127c9cc..9d341e13 100644
--- a/tests/matcher/TwitterSpec.js
+++ b/tests/matcher/MentionSpec.js
@@ -1,11 +1,12 @@
/*global Autolinker, _, describe, beforeEach, afterEach, it, expect, jasmine */
-describe( "Autolinker.matcher.Twitter", function() {
+describe( "Autolinker.matcher.Mention", function() {
var MatchChecker = Autolinker.match.MatchChecker,
matcher;
beforeEach( function() {
- matcher = new Autolinker.matcher.Twitter( {
- tagBuilder : new Autolinker.AnchorTagBuilder()
+ matcher = new Autolinker.matcher.Mention( {
+ tagBuilder : new Autolinker.AnchorTagBuilder(),
+ serviceName: 'twitter'
} );
} );
@@ -24,7 +25,7 @@ describe( "Autolinker.matcher.Twitter", function() {
var matches = matcher.parseMatches( '@asdf' );
expect( matches.length ).toBe( 1 );
- MatchChecker.expectTwitterMatch( matches[ 0 ], 'asdf', 0 );
+ MatchChecker.expectMentionMatch( matches[ 0 ], 'twitter', 'asdf', 0 );
} );
@@ -32,7 +33,7 @@ describe( "Autolinker.matcher.Twitter", function() {
var matches = matcher.parseMatches( 'Hello @asdf my good friend' );
expect( matches.length ).toBe( 1 );
- MatchChecker.expectTwitterMatch( matches[ 0 ], 'asdf', 6 );
+ MatchChecker.expectMentionMatch( matches[ 0 ], 'twitter', 'asdf', 6 );
} );
@@ -40,7 +41,7 @@ describe( "Autolinker.matcher.Twitter", function() {
var matches = matcher.parseMatches( 'Hello @asdf' );
expect( matches.length ).toBe( 1 );
- MatchChecker.expectTwitterMatch( matches[ 0 ], 'asdf', 6 );
+ MatchChecker.expectMentionMatch( matches[ 0 ], 'twitter', 'asdf', 6 );
} );
@@ -48,8 +49,8 @@ describe( "Autolinker.matcher.Twitter", function() {
var matches = matcher.parseMatches( 'Talk to @asdf or @fdsa' );
expect( matches.length ).toBe( 2 );
- MatchChecker.expectTwitterMatch( matches[ 0 ], 'asdf', 8 );
- MatchChecker.expectTwitterMatch( matches[ 1 ], 'fdsa', 17 );
+ MatchChecker.expectMentionMatch( matches[ 0 ], 'twitter', 'asdf', 8 );
+ MatchChecker.expectMentionMatch( matches[ 1 ], 'twitter', 'fdsa', 17 );
} );
@@ -57,9 +58,33 @@ describe( "Autolinker.matcher.Twitter", function() {
var matches = matcher.parseMatches( 'Hello (@asdf)' );
expect( matches.length ).toBe( 1 );
- MatchChecker.expectTwitterMatch( matches[ 0 ], 'asdf', 7 );
+ MatchChecker.expectMentionMatch( matches[ 0 ], 'twitter', 'asdf', 7 );
+ } );
+
+
+ it( 'an Instagram username with period not at boundaries should be parsed correctly', function() {
+ var instagramMatcher = new Autolinker.matcher.Mention( {
+ tagBuilder : new Autolinker.AnchorTagBuilder(),
+ serviceName: 'instagram'
+ } );
+ var matches = instagramMatcher.parseMatches( 'Hello (@as.df)' );
+
+ expect( matches.length ).toBe( 1 );
+ MatchChecker.expectMentionMatch( matches[ 0 ], 'instagram', 'as.df', 7 );
+ } );
+
+
+ it( 'an Instagram username with period at end of string should ignore period', function() {
+ var instagramMatcher = new Autolinker.matcher.Mention( {
+ tagBuilder : new Autolinker.AnchorTagBuilder(),
+ serviceName: 'instagram'
+ } );
+ var matches = instagramMatcher.parseMatches( 'Hello (@asdf.)' );
+
+ expect( matches.length ).toBe( 1 );
+ MatchChecker.expectMentionMatch( matches[ 0 ], 'instagram', 'asdf', 7 );
} );
} );
-} );
\ No newline at end of file
+} );