Debian Codegraph @

src/matcher/Mention.js

raw symbols SCIP
1 /*global Autolinker */
2 /**
3 * @class Autolinker.matcher.Mention
4 * @extends Autolinker.matcher.Matcher
5 *
6 * Matcher to find/replace username matches in an input string.
7 */
8 Autolinker.matcher.Mention = Autolinker.Util.extend( Autolinker.matcher.Matcher, {
9
10 /**
11 * Hash of regular expression to match username handles. Example match:
12 *
13 * @asdf
14 *
15 * @private
16 * @property {Object} matcherRegexes
17 */
18 matcherRegexes : {
19 "twitter": new RegExp( '@[_' + Autolinker.RegexLib.alphaNumericCharsStr + ']{1,20}', 'g' ),
20 "instagram": new RegExp( '@[_.' + Autolinker.RegexLib.alphaNumericCharsStr + ']{1,50}', 'g' ),
21 "soundcloud": new RegExp( '@[_.' + Autolinker.RegexLib.alphaNumericCharsStr + "\-" + ']{1,50}', 'g' )
22 },
23
24 /**
25 * The regular expression to use to check the character before a username match to
26 * make sure we didn't accidentally match an email address.
27 *
28 * For example, the string "[email protected]" should not match "@asdf" as a username.
29 *
30 * @private
31 * @property {RegExp} nonWordCharRegex
32 */
33 nonWordCharRegex : new RegExp( '[^' + Autolinker.RegexLib.alphaNumericCharsStr + ']' ),
34
35
36 /**
37 * @constructor
38 * @param {Object} cfg The configuration properties for the Match instance,
39 * specified in an Object (map).
40 */
41 constructor : function( cfg ) {
42 Autolinker.matcher.Matcher.prototype.constructor.call( this, cfg );
43
44 this.serviceName = cfg.serviceName;
45 },
46
47
48 /**
49 * @inheritdoc
50 */
51 parseMatches : function( text ) {
52 var matcherRegex = this.matcherRegexes[this.serviceName],
53 nonWordCharRegex = this.nonWordCharRegex,
54 serviceName = this.serviceName,
55 tagBuilder = this.tagBuilder,
56 matches = [],
57 match;
58
59 if (!matcherRegex) {
60 return matches;
61 }
62
63 while( ( match = matcherRegex.exec( text ) ) !== null ) {
64 var offset = match.index,
65 prevChar = text.charAt( offset - 1 );
66
67 // If we found the match at the beginning of the string, or we found the match
68 // and there is a whitespace char in front of it (meaning it is not an email
69 // address), then it is a username match.
70 if( offset === 0 || nonWordCharRegex.test( prevChar ) ) {
71 var matchedText = match[ 0 ].replace(/\.+$/g, ''), // strip off trailing .
72 mention = matchedText.slice( 1 ); // strip off the '@' character at the beginning
73
74 matches.push( new Autolinker.match.Mention( {
75 tagBuilder : tagBuilder,
76 matchedText : matchedText,
77 offset : offset,
78 serviceName : serviceName,
79 mention : mention
80 } ) );
81 }
82 }
83
84 return matches;
85 }
86
87 } );