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