| 1 | /*global Autolinker */ |
| 2 | /*jshint boss:true */ |
| 3 | /** |
| 4 | * @class Autolinker.HtmlTag |
| 5 | * @extends Object |
| 6 | * |
| 7 | * Represents an HTML tag, which can be used to easily build/modify HTML tags programmatically. |
| 8 | * |
| 9 | * Autolinker uses this abstraction to create HTML tags, and then write them out as strings. You may also use |
| 10 | * this class in your code, especially within a {@link Autolinker#replaceFn replaceFn}. |
| 11 | * |
| 12 | * ## Examples |
| 13 | * |
| 14 | * Example instantiation: |
| 15 | * |
| 16 | * var tag = new Autolinker.HtmlTag( { |
| 17 | * tagName : 'a', |
| 18 | * attrs : { 'href': 'http://google.com', 'class': 'external-link' }, |
| 19 | * innerHtml : 'Google' |
| 20 | * } ); |
| 21 | * |
| 22 | * tag.toAnchorString(); // <a href="http://google.com" class="external-link">Google</a> |
| 23 | * |
| 24 | * // Individual accessor methods |
| 25 | * tag.getTagName(); // 'a' |
| 26 | * tag.getAttr( 'href' ); // 'http://google.com' |
| 27 | * tag.hasClass( 'external-link' ); // true |
| 28 | * |
| 29 | * |
| 30 | * Using mutator methods (which may be used in combination with instantiation config properties): |
| 31 | * |
| 32 | * var tag = new Autolinker.HtmlTag(); |
| 33 | * tag.setTagName( 'a' ); |
| 34 | * tag.setAttr( 'href', 'http://google.com' ); |
| 35 | * tag.addClass( 'external-link' ); |
| 36 | * tag.setInnerHtml( 'Google' ); |
| 37 | * |
| 38 | * tag.getTagName(); // 'a' |
| 39 | * tag.getAttr( 'href' ); // 'http://google.com' |
| 40 | * tag.hasClass( 'external-link' ); // true |
| 41 | * |
| 42 | * tag.toAnchorString(); // <a href="http://google.com" class="external-link">Google</a> |
| 43 | * |
| 44 | * |
| 45 | * ## Example use within a {@link Autolinker#replaceFn replaceFn} |
| 46 | * |
| 47 | * var html = Autolinker.link( "Test google.com", { |
| 48 | * replaceFn : function( match ) { |
| 49 | * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance, configured with the Match's href and anchor text |
| 50 | * tag.setAttr( 'rel', 'nofollow' ); |
| 51 | * |
| 52 | * return tag; |
| 53 | * } |
| 54 | * } ); |
| 55 | * |
| 56 | * // generated html: |
| 57 | * // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a> |
| 58 | * |
| 59 | * |
| 60 | * ## Example use with a new tag for the replacement |
| 61 | * |
| 62 | * var html = Autolinker.link( "Test google.com", { |
| 63 | * replaceFn : function( match ) { |
| 64 | * var tag = new Autolinker.HtmlTag( { |
| 65 | * tagName : 'button', |
| 66 | * attrs : { 'title': 'Load URL: ' + match.getAnchorHref() }, |
| 67 | * innerHtml : 'Load URL: ' + match.getAnchorText() |
| 68 | * } ); |
| 69 | * |
| 70 | * return tag; |
| 71 | * } |
| 72 | * } ); |
| 73 | * |
| 74 | * // generated html: |
| 75 | * // Test <button title="Load URL: http://google.com">Load URL: google.com</button> |
| 76 | */ |
| 77 | Autolinker.HtmlTag = Autolinker.Util.extend( Object, { |
| 78 | |
| 79 | /** |
| 80 | * @cfg {String} tagName |
| 81 | * |
| 82 | * The tag name. Ex: 'a', 'button', etc. |
| 83 | * |
| 84 | * Not required at instantiation time, but should be set using {@link #setTagName} before {@link #toAnchorString} |
| 85 | * is executed. |
| 86 | */ |
| 87 | |
| 88 | /** |
| 89 | * @cfg {Object.<String, String>} attrs |
| 90 | * |
| 91 | * An key/value Object (map) of attributes to create the tag with. The keys are the attribute names, and the |
| 92 | * values are the attribute values. |
| 93 | */ |
| 94 | |
| 95 | /** |
| 96 | * @cfg {String} innerHtml |
| 97 | * |
| 98 | * The inner HTML for the tag. |
| 99 | * |
| 100 | * Note the camel case name on `innerHtml`. Acronyms are camelCased in this utility (such as not to run into the acronym |
| 101 | * naming inconsistency that the DOM developers created with `XMLHttpRequest`). You may alternatively use {@link #innerHTML} |
| 102 | * if you prefer, but this one is recommended. |
| 103 | */ |
| 104 | |
| 105 | /** |
| 106 | * @cfg {String} innerHTML |
| 107 | * |
| 108 | * Alias of {@link #innerHtml}, accepted for consistency with the browser DOM api, but prefer the camelCased version |
| 109 | * for acronym names. |
| 110 | */ |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * @protected |
| 115 | * @property {RegExp} whitespaceRegex |
| 116 | * |
| 117 | * Regular expression used to match whitespace in a string of CSS classes. |
| 118 | */ |
| 119 | whitespaceRegex : /\s+/, |
| 120 | |
| 121 | |
| 122 | /** |
| 123 | * @constructor |
| 124 | * @param {Object} [cfg] The configuration properties for this class, in an Object (map) |
| 125 | */ |
| 126 | constructor : function( cfg ) { |
| 127 | Autolinker.Util.assign( this, cfg ); |
| 128 | |
| 129 | this.innerHtml = this.innerHtml || this.innerHTML; // accept either the camelCased form or the fully capitalized acronym |
| 130 | }, |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Sets the tag name that will be used to generate the tag with. |
| 135 | * |
| 136 | * @param {String} tagName |
| 137 | * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained. |
| 138 | */ |
| 139 | setTagName : function( tagName ) { |
| 140 | this.tagName = tagName; |
| 141 | return this; |
| 142 | }, |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Retrieves the tag name. |
| 147 | * |
| 148 | * @return {String} |
| 149 | */ |
| 150 | getTagName : function() { |
| 151 | return this.tagName || ""; |
| 152 | }, |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * Sets an attribute on the HtmlTag. |
| 157 | * |
| 158 | * @param {String} attrName The attribute name to set. |
| 159 | * @param {String} attrValue The attribute value to set. |
| 160 | * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained. |
| 161 | */ |
| 162 | setAttr : function( attrName, attrValue ) { |
| 163 | var tagAttrs = this.getAttrs(); |
| 164 | tagAttrs[ attrName ] = attrValue; |
| 165 | |
| 166 | return this; |
| 167 | }, |
| 168 | |
| 169 | |
| 170 | /** |
| 171 | * Retrieves an attribute from the HtmlTag. If the attribute does not exist, returns `undefined`. |
| 172 | * |
| 173 | * @param {String} attrName The attribute name to retrieve. |
| 174 | * @return {String} The attribute's value, or `undefined` if it does not exist on the HtmlTag. |
| 175 | */ |
| 176 | getAttr : function( attrName ) { |
| 177 | return this.getAttrs()[ attrName ]; |
| 178 | }, |
| 179 | |
| 180 | |
| 181 | /** |
| 182 | * Sets one or more attributes on the HtmlTag. |
| 183 | * |
| 184 | * @param {Object.<String, String>} attrs A key/value Object (map) of the attributes to set. |
| 185 | * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained. |
| 186 | */ |
| 187 | setAttrs : function( attrs ) { |
| 188 | var tagAttrs = this.getAttrs(); |
| 189 | Autolinker.Util.assign( tagAttrs, attrs ); |
| 190 | |
| 191 | return this; |
| 192 | }, |
| 193 | |
| 194 | |
| 195 | /** |
| 196 | * Retrieves the attributes Object (map) for the HtmlTag. |
| 197 | * |
| 198 | * @return {Object.<String, String>} A key/value object of the attributes for the HtmlTag. |
| 199 | */ |
| 200 | getAttrs : function() { |
| 201 | return this.attrs || ( this.attrs = {} ); |
| 202 | }, |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * Sets the provided `cssClass`, overwriting any current CSS classes on the HtmlTag. |
| 207 | * |
| 208 | * @param {String} cssClass One or more space-separated CSS classes to set (overwrite). |
| 209 | * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained. |
| 210 | */ |
| 211 | setClass : function( cssClass ) { |
| 212 | return this.setAttr( 'class', cssClass ); |
| 213 | }, |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * Convenience method to add one or more CSS classes to the HtmlTag. Will not add duplicate CSS classes. |
| 218 | * |
| 219 | * @param {String} cssClass One or more space-separated CSS classes to add. |
| 220 | * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained. |
| 221 | */ |
| 222 | addClass : function( cssClass ) { |
| 223 | var classAttr = this.getClass(), |
| 224 | whitespaceRegex = this.whitespaceRegex, |
| 225 | indexOf = Autolinker.Util.indexOf, // to support IE8 and below |
| 226 | classes = ( !classAttr ) ? [] : classAttr.split( whitespaceRegex ), |
| 227 | newClasses = cssClass.split( whitespaceRegex ), |
| 228 | newClass; |
| 229 | |
| 230 | while( newClass = newClasses.shift() ) { |
| 231 | if( indexOf( classes, newClass ) === -1 ) { |
| 232 | classes.push( newClass ); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | this.getAttrs()[ 'class' ] = classes.join( " " ); |
| 237 | return this; |
| 238 | }, |
| 239 | |
| 240 | |
| 241 | /** |
| 242 | * Convenience method to remove one or more CSS classes from the HtmlTag. |
| 243 | * |
| 244 | * @param {String} cssClass One or more space-separated CSS classes to remove. |
| 245 | * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained. |
| 246 | */ |
| 247 | removeClass : function( cssClass ) { |
| 248 | var classAttr = this.getClass(), |
| 249 | whitespaceRegex = this.whitespaceRegex, |
| 250 | indexOf = Autolinker.Util.indexOf, // to support IE8 and below |
| 251 | classes = ( !classAttr ) ? [] : classAttr.split( whitespaceRegex ), |
| 252 | removeClasses = cssClass.split( whitespaceRegex ), |
| 253 | removeClass; |
| 254 | |
| 255 | while( classes.length && ( removeClass = removeClasses.shift() ) ) { |
| 256 | var idx = indexOf( classes, removeClass ); |
| 257 | if( idx !== -1 ) { |
| 258 | classes.splice( idx, 1 ); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | this.getAttrs()[ 'class' ] = classes.join( " " ); |
| 263 | return this; |
| 264 | }, |
| 265 | |
| 266 | |
| 267 | /** |
| 268 | * Convenience method to retrieve the CSS class(es) for the HtmlTag, which will each be separated by spaces when |
| 269 | * there are multiple. |
| 270 | * |
| 271 | * @return {String} |
| 272 | */ |
| 273 | getClass : function() { |
| 274 | return this.getAttrs()[ 'class' ] || ""; |
| 275 | }, |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * Convenience method to check if the tag has a CSS class or not. |
| 280 | * |
| 281 | * @param {String} cssClass The CSS class to check for. |
| 282 | * @return {Boolean} `true` if the HtmlTag has the CSS class, `false` otherwise. |
| 283 | */ |
| 284 | hasClass : function( cssClass ) { |
| 285 | return ( ' ' + this.getClass() + ' ' ).indexOf( ' ' + cssClass + ' ' ) !== -1; |
| 286 | }, |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * Sets the inner HTML for the tag. |
| 291 | * |
| 292 | * @param {String} html The inner HTML to set. |
| 293 | * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained. |
| 294 | */ |
| 295 | setInnerHtml : function( html ) { |
| 296 | this.innerHtml = html; |
| 297 | |
| 298 | return this; |
| 299 | }, |
| 300 | |
| 301 | |
| 302 | /** |
| 303 | * Retrieves the inner HTML for the tag. |
| 304 | * |
| 305 | * @return {String} |
| 306 | */ |
| 307 | getInnerHtml : function() { |
| 308 | return this.innerHtml || ""; |
| 309 | }, |
| 310 | |
| 311 | |
| 312 | /** |
| 313 | * Override of superclass method used to generate the HTML string for the tag. |
| 314 | * |
| 315 | * @return {String} |
| 316 | */ |
| 317 | toAnchorString : function() { |
| 318 | var tagName = this.getTagName(), |
| 319 | attrsStr = this.buildAttrsStr(); |
| 320 | |
| 321 | attrsStr = ( attrsStr ) ? ' ' + attrsStr : ''; // prepend a space if there are actually attributes |
| 322 | |
| 323 | return [ '<', tagName, attrsStr, '>', this.getInnerHtml(), '</', tagName, '>' ].join( "" ); |
| 324 | }, |
| 325 | |
| 326 | |
| 327 | /** |
| 328 | * Support method for {@link #toAnchorString}, returns the string space-separated key="value" pairs, used to populate |
| 329 | * the stringified HtmlTag. |
| 330 | * |
| 331 | * @protected |
| 332 | * @return {String} Example return: `attr1="value1" attr2="value2"` |
| 333 | */ |
| 334 | buildAttrsStr : function() { |
| 335 | if( !this.attrs ) return ""; // no `attrs` Object (map) has been set, return empty string |
| 336 | |
| 337 | var attrs = this.getAttrs(), |
| 338 | attrsArr = []; |
| 339 | |
| 340 | for( var prop in attrs ) { |
| 341 | if( attrs.hasOwnProperty( prop ) ) { |
| 342 | attrsArr.push( prop + '="' + attrs[ prop ] + '"' ); |
| 343 | } |
| 344 | } |
| 345 | return attrsArr.join( " " ); |
| 346 | } |
| 347 | |
| 348 | } ); |