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\n * Represents an HTML tag, which can be used to easily build/modify HTML tags programmatically.\n *\n * Autolinker uses this abstraction to create HTML tags, and then write them out as strings. You may also use\n * this class in your code, especially within a {@link Autolinker#replaceFn replaceFn}.\n *\n * ## Examples\n *\n * Example instantiation:\n *\n * var tag = new Autolinker.HtmlTag( {\n * tagName : 'a',\n * attrs : { 'href': 'http://google.com', 'class': 'external-link' },\n * innerHtml : 'Google'\n * } );\n *\n * tag.toAnchorString(); // <a href=\"http://google.com\" class=\"external-link\">Google</a>\n *\n * // Individual accessor methods\n * tag.getTagName(); // 'a'\n * tag.getAttr( 'href' ); // 'http://google.com'\n * tag.hasClass( 'external-link' ); // true\n *\n *\n * Using mutator methods (which may be used in combination with instantiation config properties):\n *\n * var tag = new Autolinker.HtmlTag();\n * tag.setTagName( 'a' );\n * tag.setAttr( 'href', 'http://google.com' );\n * tag.addClass( 'external-link' );\n * tag.setInnerHtml( 'Google' );\n *\n * tag.getTagName(); // 'a'\n * tag.getAttr( 'href' ); // 'http://google.com'\n * tag.hasClass( 'external-link' ); // true\n *\n * tag.toAnchorString(); // <a href=\"http://google.com\" class=\"external-link\">Google</a>\n *\n *\n * ## Example use within a {@link Autolinker#replaceFn replaceFn}\n *\n * var html = Autolinker.link( \"Test google.com\", {\n * replaceFn : function( match ) {\n * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance, configured with the Match's href and anchor text\n * tag.setAttr( 'rel', 'nofollow' );\n *\n * return tag;\n * }\n * } );\n *\n * // generated html:\n * // Test <a href=\"http://google.com\" target=\"_blank\" rel=\"nofollow\">google.com</a>\n *\n *\n * ## Example use with a new tag for the replacement\n *\n * var html = Autolinker.link( \"Test google.com\", {\n * replaceFn : function( match ) {\n * var tag = new Autolinker.HtmlTag( {\n * tagName : 'button',\n * attrs : { 'title': 'Load URL: ' + match.getAnchorHref() },\n * innerHtml : 'Load URL: ' + match.getAnchorText()\n * } );\n *\n * return tag;\n * }\n * } );\n *\n * // generated html:\n * // Test <button title=\"Load URL: http://google.com\">Load URL: google.com</button>\n */\nexport class HtmlTag {\n\n\t/**\n\t * @cfg {String} tagName\n\t *\n\t * The tag name. Ex: 'a', 'button', etc.\n\t *\n\t * Not required at instantiation time, but should be set using {@link #setTagName} before {@link #toAnchorString}\n\t * is executed.\n\t */\n\tprivate tagName: string = ''; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n\t/**\n\t * @cfg {Object.<String, String>} attrs\n\t *\n\t * An key/value Object (map) of attributes to create the tag with. The keys are the attribute names, and the\n\t * values are the attribute values.\n\t */\n\tprivate attrs: { [key: string]: string } = {}; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n\t/**\n\t * @cfg {String} innerHTML\n\t *\n\t * The inner HTML for the tag.\n\t */\n\tprivate innerHTML: string = ''; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n\t/**\n\t * @protected\n\t * @property {RegExp} whitespaceRegex\n\t *\n\t * Regular expression used to match whitespace in a string of CSS classes.\n\t */\n\tprotected whitespaceRegex = /\\s+/; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n\n\t/**\n\t * @method constructor\n\t * @param {Object} [cfg] The configuration properties for this class, in an Object (map)\n\t */\n\tconstructor( cfg: HtmlTagCfg = {} ) {\n\t\tthis.tagName = cfg.tagName || '';\n\t\tthis.attrs = cfg.attrs || {};\n\t\tthis.innerHTML = cfg.innerHtml || cfg.innerHTML || ''; // accept either the camelCased form or the fully capitalized acronym as in the DOM\n\t}\n\n\n\t/**\n\t * Sets the tag name that will be used to generate the tag with.\n\t *\n\t * @param {String} tagName\n\t * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n\t */\n\tsetTagName( tagName: string ) {\n\t\tthis.tagName = tagName;\n\t\treturn this;\n\t}\n\n\n\t/**\n\t * Retrieves the tag name.\n\t *\n\t * @return {String}\n\t */\n\tgetTagName() {\n\t\treturn this.tagName || '';\n\t}\n\n\n\t/**\n\t * Sets an attribute on the HtmlTag.\n\t *\n\t * @param {String} attrName The attribute name to set.\n\t * @param {String} attrValue The attribute value to set.\n\t * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n\t */\n\tsetAttr( attrName: string, attrValue: string ) {\n\t\tlet tagAttrs = this.getAttrs();\n\t\ttagAttrs[ attrName ] = attrValue;\n\n\t\treturn this;\n\t}\n\n\n\t/**\n\t * Retrieves an attribute from the HtmlTag. If the attribute does not exist, returns `undefined`.\n\t *\n\t * @param {String} attrName The attribute name to retrieve.\n\t * @return {String} The attribute's value, or `undefined` if it does not exist on the HtmlTag.\n\t */\n\tgetAttr( attrName: string ) {\n\t\treturn this.getAttrs()[ attrName ];\n\t}\n\n\n\t/**\n\t * Sets one or more attributes on the HtmlTag.\n\t *\n\t * @param {Object.<String, String>} attrs A key/value Object (map) of the attributes to set.\n\t * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n\t */\n\tsetAttrs( attrs: {[attr: string]: string} ) {\n\t\tObject.assign( this.getAttrs(), attrs );\n\n\t\treturn this;\n\t}\n\n\n\t/**\n\t * Retrieves the attributes Object (map) for the HtmlTag.\n\t *\n\t * @return {Object.<String, String>} A key/value object of the attributes for the HtmlTag.\n\t */\n\tgetAttrs() {\n\t\treturn this.attrs || ( this.attrs = {} );\n\t}\n\n\n\t/**\n\t * Sets the provided `cssClass`, overwriting any current CSS classes on the HtmlTag.\n\t *\n\t * @param {String} cssClass One or more space-separated CSS classes to set (overwrite).\n\t * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n\t */\n\tsetClass( cssClass: string ) {\n\t\treturn this.setAttr( 'class', cssClass );\n\t}\n\n\n\t/**\n\t * Convenience method to add one or more CSS classes to the HtmlTag. Will not add duplicate CSS classes.\n\t *\n\t * @param {String} cssClass One or more space-separated CSS classes to add.\n\t * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n\t */\n\taddClass( cssClass: string ) {\n\t\tlet classAttr = this.getClass(),\n\t\t whitespaceRegex = this.whitespaceRegex,\n\t\t classes = ( !classAttr ) ? [] : classAttr.split( whitespaceRegex ),\n\t\t newClasses = cssClass.split( whitespaceRegex ),\n\t\t newClass: string | undefined;\n\n\t\twhile( newClass = newClasses.shift() ) {\n\t\t\tif( indexOf( classes, newClass ) === -1 ) {\n\t\t\t\tclasses.push( newClass );\n\t\t\t}\n\t\t}\n\n\t\tthis.getAttrs()[ 'class' ] = classes.join( \" \" );\n\t\treturn this;\n\t}\n\n\n\t/**\n\t * Convenience method to remove one or more CSS classes from the HtmlTag.\n\t *\n\t * @param {String} cssClass One or more space-separated CSS classes to remove.\n\t * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n\t */\n\tremoveClass( cssClass: string ) {\n\t\tlet classAttr = this.getClass(),\n\t\t whitespaceRegex = this.whitespaceRegex,\n\t\t classes = ( !classAttr ) ? [] : classAttr.split( whitespaceRegex ),\n\t\t removeClasses = cssClass.split( whitespaceRegex ),\n\t\t removeClass: string | undefined;\n\n\t\twhile( classes.length && ( removeClass = removeClasses.shift() ) ) {\n\t\t\tlet idx = indexOf( classes, removeClass );\n\t\t\tif( idx !== -1 ) {\n\t\t\t\tclasses.splice( idx, 1 );\n\t\t\t}\n\t\t}\n\n\t\tthis.getAttrs()[ 'class' ] = classes.join( \" \" );\n\t\treturn this;\n\t}\n\n\n\t/**\n\t * Convenience method to retrieve the CSS class(es) for the HtmlTag, which will each be separated by spaces when\n\t * there are multiple.\n\t *\n\t * @return {String}\n\t */\n\tgetClass() {\n\t\treturn this.getAttrs()[ 'class' ] || \"\";\n\t}\n\n\n\t/**\n\t * Convenience method to check if the tag has a CSS class or not.\n\t *\n\t * @param {String} cssClass The CSS class to check for.\n\t * @return {Boolean} `true` if the HtmlTag has the CSS class, `false` otherwise.\n\t */\n\thasClass( cssClass: string ) {\n\t\treturn ( ' ' + this.getClass() + ' ' ).indexOf( ' ' + cssClass + ' ' ) !== -1;\n\t}\n\n\n\t/**\n\t * Sets the inner HTML for the tag.\n\t *\n\t * @param {String} html The inner HTML to set.\n\t * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n\t */\n\tsetInnerHTML( html: string ) {\n\t\tthis.innerHTML = html;\n\n\t\treturn this;\n\t}\n\n\n\t/**\n\t * Backwards compatibility method name.\n\t *\n\t * @param {String} html The inner HTML to set.\n\t * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n\t */\n\tsetInnerHtml( html: string ) {\n\t\treturn this.setInnerHTML( html );\n\t}\n\n\n\t/**\n\t * Retrieves the inner HTML for the tag.\n\t *\n\t * @return {String}\n\t */\n\tgetInnerHTML() {\n\t\treturn this.innerHTML || \"\";\n\t}\n\n\n\t/**\n\t * Backward compatibility method name.\n\t *\n\t * @return {String}\n\t */\n\tgetInnerHtml() {\n\t\treturn this.getInnerHTML();\n\t}\n\n\n\t/**\n\t * Override of superclass method used to generate the HTML string for the tag.\n\t *\n\t * @return {String}\n\t */\n\ttoAnchorString() {\n\t\tlet tagName = this.getTagName(),\n\t\t attrsStr = this.buildAttrsStr();\n\n\t\tattrsStr = ( attrsStr ) ? ' ' + attrsStr : ''; // prepend a space if there are actually attributes\n\n\t\treturn [ '<', tagName, attrsStr, '>', this.getInnerHtml(), '</', tagName, '>' ].join( \"\" );\n\t}\n\n\n\t/**\n\t * Support method for {@link #toAnchorString}, returns the string space-separated key=\"value\" pairs, used to populate\n\t * the stringified HtmlTag.\n\t *\n\t * @protected\n\t * @return {String} Example return: `attr1=\"value1\" attr2=\"value2\"`\n\t */\n\tprotected buildAttrsStr() {\n\t\tif( !this.attrs ) return \"\"; // no `attrs` Object (map) has been set, return empty string\n\n\t\tlet attrs = this.getAttrs(),\n\t\t attrsArr: string[] = [];\n\n\t\tfor( let prop in attrs ) {\n\t\t\tif( attrs.hasOwnProperty( prop ) ) {\n\t\t\t\tattrsArr.push( prop + '=\"' + attrs[ prop ] + '\"' );\n\t\t\t}\n\t\t}\n\t\treturn attrsArr.join( \" \" );\n\t}\n\n}\n\n\nexport interface HtmlTagCfg {\n\ttagName?: string;\n\tattrs?: { [key: string]: string };\n\tinnerHtml?: string;\n\tinnerHTML?: string;\n}"]}
\No newline at end of file