Debian Codegraph @

src/AnchorTagBuilder.js

raw symbols SCIP
1 /*global Autolinker */
2 /*jshint sub:true */
3 /**
4 * @protected
5 * @class Autolinker.AnchorTagBuilder
6 * @extends Object
7 *
8 * Builds anchor (<a>) tags for the Autolinker utility when a match is
9 * found.
10 *
11 * Normally this class is instantiated, configured, and used internally by an
12 * {@link Autolinker} instance, but may actually be used indirectly in a
13 * {@link Autolinker#replaceFn replaceFn} to create {@link Autolinker.HtmlTag HtmlTag}
14 * instances which may be modified before returning from the
15 * {@link Autolinker#replaceFn replaceFn}. For example:
16 *
17 * var html = Autolinker.link( "Test google.com", {
18 * replaceFn : function( match ) {
19 * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance
20 * tag.setAttr( 'rel', 'nofollow' );
21 *
22 * return tag;
23 * }
24 * } );
25 *
26 * // generated html:
27 * // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a>
28 */
29 Autolinker.AnchorTagBuilder = Autolinker.Util.extend( Object, {
30
31 /**
32 * @cfg {Boolean} newWindow
33 * @inheritdoc Autolinker#newWindow
34 */
35
36 /**
37 * @cfg {Object} truncate
38 * @inheritdoc Autolinker#truncate
39 */
40
41 /**
42 * @cfg {String} className
43 * @inheritdoc Autolinker#className
44 */
45
46
47 /**
48 * @constructor
49 * @param {Object} [cfg] The configuration options for the AnchorTagBuilder instance, specified in an Object (map).
50 */
51 constructor : function( cfg ) {
52 cfg = cfg || {};
53
54 this.newWindow = cfg.newWindow;
55 this.truncate = cfg.truncate;
56 this.className = cfg.className;
57 },
58
59
60 /**
61 * Generates the actual anchor (&lt;a&gt;) tag to use in place of the
62 * matched text, via its `match` object.
63 *
64 * @param {Autolinker.match.Match} match The Match instance to generate an
65 * anchor tag from.
66 * @return {Autolinker.HtmlTag} The HtmlTag instance for the anchor tag.
67 */
68 build : function( match ) {
69 return new Autolinker.HtmlTag( {
70 tagName : 'a',
71 attrs : this.createAttrs( match ),
72 innerHtml : this.processAnchorText( match.getAnchorText() )
73 } );
74 },
75
76
77 /**
78 * Creates the Object (map) of the HTML attributes for the anchor (&lt;a&gt;)
79 * tag being generated.
80 *
81 * @protected
82 * @param {Autolinker.match.Match} match The Match instance to generate an
83 * anchor tag from.
84 * @return {Object} A key/value Object (map) of the anchor tag's attributes.
85 */
86 createAttrs : function( match ) {
87 var attrs = {
88 'href' : match.getAnchorHref() // we'll always have the `href` attribute
89 };
90
91 var cssClass = this.createCssClass( match );
92 if( cssClass ) {
93 attrs[ 'class' ] = cssClass;
94 }
95 if( this.newWindow ) {
96 attrs[ 'target' ] = "_blank";
97 attrs[ 'rel' ] = "noopener noreferrer";
98 }
99
100 if( this.truncate ) {
101 if( this.truncate.length && this.truncate.length < match.getAnchorText().length ) {
102 attrs[ 'title' ] = match.getAnchorHref();
103 }
104 }
105
106 return attrs;
107 },
108
109
110 /**
111 * Creates the CSS class that will be used for a given anchor tag, based on
112 * the `matchType` and the {@link #className} config.
113 *
114 * Example returns:
115 *
116 * - "" // no {@link #className}
117 * - "myLink myLink-url" // url match
118 * - "myLink myLink-email" // email match
119 * - "myLink myLink-phone" // phone match
120 * - "myLink myLink-hashtag" // hashtag match
121 * - "myLink myLink-mention myLink-twitter" // mention match with Twitter service
122 *
123 * @private
124 * @param {Autolinker.match.Match} match The Match instance to generate an
125 * anchor tag from.
126 * @return {String} The CSS class string for the link. Example return:
127 * "myLink myLink-url". If no {@link #className} was configured, returns
128 * an empty string.
129 */
130 createCssClass : function( match ) {
131 var className = this.className;
132
133 if( !className ) {
134 return "";
135
136 } else {
137 var returnClasses = [ className ],
138 cssClassSuffixes = match.getCssClassSuffixes();
139
140 for( var i = 0, len = cssClassSuffixes.length; i < len; i++ ) {
141 returnClasses.push( className + '-' + cssClassSuffixes[ i ] );
142 }
143 return returnClasses.join( ' ' );
144 }
145 },
146
147
148 /**
149 * Processes the `anchorText` by truncating the text according to the
150 * {@link #truncate} config.
151 *
152 * @private
153 * @param {String} anchorText The anchor tag's text (i.e. what will be
154 * displayed).
155 * @return {String} The processed `anchorText`.
156 */
157 processAnchorText : function( anchorText ) {
158 anchorText = this.doTruncate( anchorText );
159
160 return anchorText;
161 },
162
163
164 /**
165 * Performs the truncation of the `anchorText` based on the {@link #truncate}
166 * option. If the `anchorText` is longer than the length specified by the
167 * {@link #truncate} option, the truncation is performed based on the
168 * `location` property. See {@link #truncate} for details.
169 *
170 * @private
171 * @param {String} anchorText The anchor tag's text (i.e. what will be
172 * displayed).
173 * @return {String} The truncated anchor text.
174 */
175 doTruncate : function( anchorText ) {
176 var truncate = this.truncate;
177 if( !truncate || !truncate.length ) return anchorText;
178
179 var truncateLength = truncate.length,
180 truncateLocation = truncate.location;
181
182 if( truncateLocation === 'smart' ) {
183 return Autolinker.truncate.TruncateSmart( anchorText, truncateLength );
184
185 } else if( truncateLocation === 'middle' ) {
186 return Autolinker.truncate.TruncateMiddle( anchorText, truncateLength );
187
188 } else {
189 return Autolinker.truncate.TruncateEnd( anchorText, truncateLength );
190 }
191 }
192
193 } );