Skip to content

Commit 0ce3ff8

Browse files
committed
Add Autolinker.match.Match#getMatchedText method.
1 parent 777d9fb commit 0ce3ff8

File tree

7 files changed

+108
-8
lines changed

7 files changed

+108
-8
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ The full API docs for Autolinker may be referenced at: [http://gregjacobs.github
227227

228228
## Changelog:
229229

230+
### 0.12.3
231+
232+
- Add `Autolinker.match.Match#getMatchedText` method
233+
234+
### 0.12.2
235+
236+
- Add documentation generation, and update inline documentation.
237+
230238
### 0.12.1
231239

232240
- Expose the `Autolinker.HtmlTag` class, and allow it to be used in the `replaceFn`

dist/Autolinker.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@
487487

488488

489489
if( emailAddressMatch ) {
490-
match = new Autolinker.match.Email( { email: emailAddressMatch } );
490+
match = new Autolinker.match.Email( { matchedText: matchStr, email: emailAddressMatch } );
491491

492492
} else if( twitterMatch ) {
493493
// fix up the `matchStr` if there was a preceding whitespace char, which was needed to determine the match
@@ -496,7 +496,7 @@
496496
prefixStr = twitterHandlePrefixWhitespaceChar;
497497
matchStr = matchStr.slice( 1 ); // remove the prefixed whitespace char from the match
498498
}
499-
match = new Autolinker.match.Twitter( { twitterHandle: twitterHandle } );
499+
match = new Autolinker.match.Twitter( { matchedText: matchStr, twitterHandle: twitterHandle } );
500500

501501
} else { // url match
502502
// If it's a protocol-relative '//' match, remove the character before the '//' (which the matcherRegex needed
@@ -509,7 +509,13 @@
509509
matchStr = matchStr.slice( 1 ); // remove the prefixed char from the match
510510
}
511511
}
512-
match = new Autolinker.match.Url( { url: matchStr, protocolRelativeMatch: protocolRelativeMatch, stripPrefix: me.stripPrefix } );
512+
513+
match = new Autolinker.match.Url( {
514+
matchedText : matchStr,
515+
url : matchStr,
516+
protocolRelativeMatch : protocolRelativeMatch,
517+
stripPrefix : me.stripPrefix
518+
} );
513519
}
514520

515521
// Generate the replacement text for the match
@@ -1436,6 +1442,13 @@
14361442
*/
14371443
Autolinker.match.Match = Autolinker.Util.extend( Object, {
14381444

1445+
/**
1446+
* @cfg {String} matchedText (required)
1447+
*
1448+
* The original text that was matched.
1449+
*/
1450+
1451+
14391452
/**
14401453
* @constructor
14411454
* @param {Object} cfg The configuration properties for the Match instance, specified in an Object (map).
@@ -1453,6 +1466,16 @@
14531466
*/
14541467
getType : Autolinker.Util.abstractMethod,
14551468

1469+
1470+
/**
1471+
* Returns the original text that was matched.
1472+
*
1473+
* @return {String}
1474+
*/
1475+
getMatchedText : function() {
1476+
return this.matchedText;
1477+
},
1478+
14561479

14571480
/**
14581481
* Returns the anchor href that should be generated for the match.

dist/Autolinker.min.js

Lines changed: 1 addition & 1 deletion
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.12.2",
3+
"version": "0.12.3",
44
"description": "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: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ Autolinker.prototype = {
466466

467467

468468
if( emailAddressMatch ) {
469-
match = new Autolinker.match.Email( { email: emailAddressMatch } );
469+
match = new Autolinker.match.Email( { matchedText: matchStr, email: emailAddressMatch } );
470470

471471
} else if( twitterMatch ) {
472472
// fix up the `matchStr` if there was a preceding whitespace char, which was needed to determine the match
@@ -475,7 +475,7 @@ Autolinker.prototype = {
475475
prefixStr = twitterHandlePrefixWhitespaceChar;
476476
matchStr = matchStr.slice( 1 ); // remove the prefixed whitespace char from the match
477477
}
478-
match = new Autolinker.match.Twitter( { twitterHandle: twitterHandle } );
478+
match = new Autolinker.match.Twitter( { matchedText: matchStr, twitterHandle: twitterHandle } );
479479

480480
} else { // url match
481481
// If it's a protocol-relative '//' match, remove the character before the '//' (which the matcherRegex needed
@@ -488,7 +488,13 @@ Autolinker.prototype = {
488488
matchStr = matchStr.slice( 1 ); // remove the prefixed char from the match
489489
}
490490
}
491-
match = new Autolinker.match.Url( { url: matchStr, protocolRelativeMatch: protocolRelativeMatch, stripPrefix: me.stripPrefix } );
491+
492+
match = new Autolinker.match.Url( {
493+
matchedText : matchStr,
494+
url : matchStr,
495+
protocolRelativeMatch : protocolRelativeMatch,
496+
stripPrefix : me.stripPrefix
497+
} );
492498
}
493499

494500
// Generate the replacement text for the match

src/match/Match.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
*/
3333
Autolinker.match.Match = Autolinker.Util.extend( Object, {
3434

35+
/**
36+
* @cfg {String} matchedText (required)
37+
*
38+
* The original text that was matched.
39+
*/
40+
41+
3542
/**
3643
* @constructor
3744
* @param {Object} cfg The configuration properties for the Match instance, specified in an Object (map).
@@ -49,6 +56,16 @@ Autolinker.match.Match = Autolinker.Util.extend( Object, {
4956
*/
5057
getType : Autolinker.Util.abstractMethod,
5158

59+
60+
/**
61+
* Returns the original text that was matched.
62+
*
63+
* @return {String}
64+
*/
65+
getMatchedText : function() {
66+
return this.matchedText;
67+
},
68+
5269

5370
/**
5471
* Returns the anchor href that should be generated for the match.

tests/AutolinkerSpec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -921,6 +921,52 @@ describe( "Autolinker", function() {
921921
var returnTrueFn = function() { return true; },
922922
returnFalseFn = function() { return false; };
923923

924+
925+
it( "should populate a UrlMatch object with the appropriate properties", function() {
926+
var replaceFnCallCount = 0;
927+
var result = Autolinker.link( "Website: asdf.com ", { // purposeful trailing space
928+
replaceFn : function( autolinker, match ) {
929+
replaceFnCallCount++;
930+
931+
expect( match.getMatchedText() ).toBe( 'asdf.com' );
932+
expect( match.getUrl() ).toBe( 'http://asdf.com' );
933+
}
934+
} );
935+
936+
expect( replaceFnCallCount ).toBe( 1 ); // make sure the replaceFn was called
937+
} );
938+
939+
940+
it( "should populate an EmailMatch object with the appropriate properties", function() {
941+
var replaceFnCallCount = 0;
942+
var result = Autolinker.link( "Email: [email protected] ", { // purposeful trailing space
943+
replaceFn : function( autolinker, match ) {
944+
replaceFnCallCount++;
945+
946+
expect( match.getMatchedText() ).toBe( '[email protected]' );
947+
expect( match.getEmail() ).toBe( '[email protected]' );
948+
}
949+
} );
950+
951+
expect( replaceFnCallCount ).toBe( 1 ); // make sure the replaceFn was called
952+
} );
953+
954+
955+
it( "should populate a TwitterMatch object with the appropriate properties", function() {
956+
var replaceFnCallCount = 0;
957+
var result = Autolinker.link( "Twitter: @myTwitter ", { // purposeful trailing space
958+
replaceFn : function( autolinker, match ) {
959+
replaceFnCallCount++;
960+
961+
expect( match.getMatchedText() ).toBe( '@myTwitter' );
962+
expect( match.getTwitterHandle() ).toBe( 'myTwitter' );
963+
}
964+
} );
965+
966+
expect( replaceFnCallCount ).toBe( 1 ); // make sure the replaceFn was called
967+
} );
968+
969+
924970
it( "should replace the match as Autolinker would normally do when `true` is returned from the `replaceFn`", function() {
925971
var result = Autolinker.link( "Website: asdf.com, Email: [email protected], Twitter: @asdf", {
926972
newWindow : false, // just to suppress the target="_blank" from the output for this test

0 commit comments

Comments
 (0)