Debian Codegraph @

src/match/Match.js

raw symbols SCIP
1 /*global Autolinker */
2 /**
3 * @abstract
4 * @class Autolinker.match.Match
5 *
6 * Represents a match found in an input string which should be Autolinked. A Match object is what is provided in a
7 * {@link Autolinker#replaceFn replaceFn}, and may be used to query for details about the match.
8 *
9 * For example:
10 *
11 * var input = "..."; // string with URLs, Email Addresses, and Mentions (Twitter, Instagram, Soundcloud)
12 *
13 * var linkedText = Autolinker.link( input, {
14 * replaceFn : function( match ) {
15 * console.log( "href = ", match.getAnchorHref() );
16 * console.log( "text = ", match.getAnchorText() );
17 *
18 * switch( match.getType() ) {
19 * case 'url' :
20 * console.log( "url: ", match.getUrl() );
21 *
22 * case 'email' :
23 * console.log( "email: ", match.getEmail() );
24 *
25 * case 'mention' :
26 * console.log( "mention: ", match.getMention() );
27 * }
28 * }
29 * } );
30 *
31 * See the {@link Autolinker} class for more details on using the {@link Autolinker#replaceFn replaceFn}.
32 */
33 Autolinker.match.Match = Autolinker.Util.extend( Object, {
34
35 /**
36 * @cfg {Autolinker.AnchorTagBuilder} tagBuilder (required)
37 *
38 * Reference to the AnchorTagBuilder instance to use to generate an anchor
39 * tag for the Match.
40 */
41
42 /**
43 * @cfg {String} matchedText (required)
44 *
45 * The original text that was matched by the {@link Autolinker.matcher.Matcher}.
46 */
47
48 /**
49 * @cfg {Number} offset (required)
50 *
51 * The offset of where the match was made in the input string.
52 */
53
54
55 /**
56 * @constructor
57 * @param {Object} cfg The configuration properties for the Match
58 * instance, specified in an Object (map).
59 */
60 constructor : function( cfg ) {
61 // @if DEBUG
62 if( cfg.tagBuilder == null ) throw new Error( '`tagBuilder` cfg required' );
63 if( cfg.matchedText == null ) throw new Error( '`matchedText` cfg required' );
64 if( cfg.offset == null ) throw new Error( '`offset` cfg required' );
65 // @endif
66
67 this.tagBuilder = cfg.tagBuilder;
68 this.matchedText = cfg.matchedText;
69 this.offset = cfg.offset;
70 },
71
72
73 /**
74 * Returns a string name for the type of match that this class represents.
75 *
76 * @abstract
77 * @return {String}
78 */
79 getType : Autolinker.Util.abstractMethod,
80
81
82 /**
83 * Returns the original text that was matched.
84 *
85 * @return {String}
86 */
87 getMatchedText : function() {
88 return this.matchedText;
89 },
90
91
92 /**
93 * Sets the {@link #offset} of where the match was made in the input string.
94 *
95 * A {@link Autolinker.matcher.Matcher} will be fed only HTML text nodes,
96 * and will therefore set an original offset that is relative to the HTML
97 * text node itself. However, we want this offset to be relative to the full
98 * HTML input string, and thus if using {@link Autolinker#parse} (rather
99 * than calling a {@link Autolinker.matcher.Matcher} directly), then this
100 * offset is corrected after the Matcher itself has done its job.
101 *
102 * @param {Number} offset
103 */
104 setOffset : function( offset ) {
105 this.offset = offset;
106 },
107
108
109 /**
110 * Returns the offset of where the match was made in the input string. This
111 * is the 0-based index of the match.
112 *
113 * @return {Number}
114 */
115 getOffset : function() {
116 return this.offset;
117 },
118
119
120 /**
121 * Returns the anchor href that should be generated for the match.
122 *
123 * @abstract
124 * @return {String}
125 */
126 getAnchorHref : Autolinker.Util.abstractMethod,
127
128
129 /**
130 * Returns the anchor text that should be generated for the match.
131 *
132 * @abstract
133 * @return {String}
134 */
135 getAnchorText : Autolinker.Util.abstractMethod,
136
137
138 /**
139 * Returns the CSS class suffix(es) for this match.
140 *
141 * A CSS class suffix is appended to the {@link Autolinker#className} in
142 * the {@link Autolinker.AnchorTagBuilder} when a match is translated into
143 * an anchor tag.
144 *
145 * For example, if {@link Autolinker#className} was configured as 'myLink',
146 * and this method returns `[ 'url' ]`, the final class name of the element
147 * will become: 'myLink myLink-url'.
148 *
149 * The match may provide multiple CSS class suffixes to be appended to the
150 * {@link Autolinker#className} in order to facilitate better styling
151 * options for different match criteria. See {@link Autolinker.match.Mention}
152 * for an example.
153 *
154 * By default, this method returns a single array with the match's
155 * {@link #getType type} name, but may be overridden by subclasses.
156 *
157 * @return {String[]}
158 */
159 getCssClassSuffixes : function() {
160 return [ this.getType() ];
161 },
162
163
164 /**
165 * Builds and returns an {@link Autolinker.HtmlTag} instance based on the
166 * Match.
167 *
168 * This can be used to easily generate anchor tags from matches, and either
169 * return their HTML string, or modify them before doing so.
170 *
171 * Example Usage:
172 *
173 * var tag = match.buildTag();
174 * tag.addClass( 'cordova-link' );
175 * tag.setAttr( 'target', '_system' );
176 *
177 * tag.toAnchorString(); // <a href="http://google.com" class="cordova-link" target="_system">Google</a>
178 */
179 buildTag : function() {
180 return this.tagBuilder.build( this );
181 }
182
183 } );