UNPKG

autolinker

Version:

Utility to automatically link the URLs, email addresses, phone numbers, hashtags, and mentions (Twitter, Instagram) in a given block of text/HTML

1 lines 15.3 kB
{"version":3,"file":"html-tag.js","sourceRoot":"","sources":["../../src/html-tag.ts"],"names":[],"mappings":";;;AAAA,iCAAyC;AAEzC,yCAAyC;AAC5B,QAAA,YAAY,GAAG,KAAK,CAAC;AAElC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyEG;AACH;IA0BI;;;OAGG;IACH,iBAAY,GAAoB;QAApB,oBAAA,EAAA,QAAoB;QA7BhC;;;;;;;WAOG;QACK,YAAO,GAAW,EAAE,CAAC,CAAC,gGAAgG;QAE9H;;;;;WAKG;QACK,UAAK,GAA8B,EAAE,CAAC,CAAC,gGAAgG;QAE/I;;;;WAIG;QACK,cAAS,GAAW,EAAE,CAAC,CAAC,gGAAgG;QAO5H,IAAI,CAAC,OAAO,GAAG,GAAG,CAAC,OAAO,IAAI,EAAE,CAAC;QACjC,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC,KAAK,IAAI,EAAE,CAAC;QAC7B,IAAI,CAAC,SAAS,GAAG,GAAG,CAAC,SAAS,IAAI,GAAG,CAAC,SAAS,IAAI,EAAE,CAAC,CAAC,mFAAmF;IAC9I,CAAC;IAED;;;;;OAKG;IACH,4BAAU,GAAV,UAAW,OAAe;QACtB,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,OAAO,IAAI,CAAC;IAChB,CAAC;IAED;;;;OAIG;IACH,4BAAU,GAAV;QACI,OAAO,IAAI,CAAC,OAAO,CAAC;IACxB,CAAC;IAED;;;;;;OAMG;IACH,yBAAO,GAAP,UAAQ,QAAgB,EAAE,SAAiB;QACvC,IAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;QACjC,QAAQ,CAAC,QAAQ,CAAC,GAAG,SAAS,CAAC;QAE/B,OAAO,IAAI,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**\n * @class Autolinker.HtmlTag\n * @extends Object\n *\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 * @cfg {String} tagName\n *\n * The tag name. Ex: 'a', 'button', etc.\n *\n * Not required at instantiation time, but should be set using {@link #setTagName} before {@link #toAnchorString}\n * is executed.\n */\n private tagName: string = ''; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n /**\n * @cfg {Object.<String, String>} attrs\n *\n * An key/value Object (map) of attributes to create the tag with. The keys are the attribute names, and the\n * values are the attribute values.\n */\n private attrs: { [key: string]: string } = {}; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n /**\n * @cfg {String} innerHTML\n *\n * The inner HTML for the tag.\n */\n private innerHTML: string = ''; // default value just to get the above doc comment in the ES5 output and documentation generator\n\n /**\n * @method constructor\n * @param {Object} [cfg] The configuration properties for this class, in an Object (map)\n */\n constructor(cfg: HtmlTagCfg = {}) {\n this.tagName = cfg.tagName || '';\n this.attrs = cfg.attrs || {};\n this.innerHTML = cfg.innerHtml || cfg.innerHTML || ''; // accept either the camelCased form or the fully capitalized acronym as in the DOM\n }\n\n /**\n * Sets the tag name that will be used to generate the tag with.\n *\n * @param {String} tagName\n * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n */\n setTagName(tagName: string): this {\n this.tagName = tagName;\n return this;\n }\n\n /**\n * Retrieves the tag name.\n *\n * @return {String}\n */\n getTagName(): string {\n return this.tagName;\n }\n\n /**\n * Sets an attribute on the HtmlTag.\n *\n * @param {String} attrName The attribute name to set.\n * @param {String} attrValue The attribute value to set.\n * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n */\n setAttr(attrName: string, attrValue: string): this {\n const tagAttrs = this.getAttrs();\n tagAttrs[attrName] = attrValue;\n\n return this;\n }\n\n /**\n * Retrieves an attribute from the HtmlTag. If the attribute does not exist, returns `undefined`.\n *\n * @param {String} attrName The attribute name to retrieve.\n * @return {String} The attribute's value, or `undefined` if it does not exist on the HtmlTag.\n */\n getAttr(attrName: string): string {\n return this.getAttrs()[attrName];\n }\n\n /**\n * Sets one or more attributes on the HtmlTag.\n *\n * @param {Object.<String, String>} attrs A key/value Object (map) of the attributes to set.\n * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n */\n setAttrs(attrs: { [attr: string]: string }): this {\n Object.assign(this.getAttrs(), attrs);\n\n return this;\n }\n\n /**\n * Retrieves the attributes Object (map) for the HtmlTag.\n *\n * @return {Object.<String, String>} A key/value object of the attributes for the HtmlTag.\n */\n getAttrs(): { [key: string]: string } {\n return this.attrs;\n }\n\n /**\n * Sets the provided `cssClass`, overwriting any current CSS classes on the HtmlTag.\n *\n * @param {String} cssClass One or more space-separated CSS classes to set (overwrite).\n * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n */\n setClass(cssClass: string): this {\n return this.setAttr('class', cssClass);\n }\n\n /**\n * Convenience method to add one or more CSS classes to the HtmlTag. Will not add duplicate CSS classes.\n *\n * @param {String} cssClass One or more space-separated CSS classes to add.\n * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n */\n addClass(cssClass: string): this {\n const classAttr = this.getClass();\n const classes = !classAttr ? [] : classAttr.split(whitespaceRe);\n const newClasses = cssClass.split(whitespaceRe);\n let newClass: string | undefined;\n\n while ((newClass = newClasses.shift())) {\n if (classes.indexOf(newClass) === -1) {\n classes.push(newClass);\n }\n }\n\n this.getAttrs()['class'] = classes.join(' ');\n return this;\n }\n\n /**\n * Convenience method to remove one or more CSS classes from the HtmlTag.\n *\n * @param {String} cssClass One or more space-separated CSS classes to remove.\n * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n */\n removeClass(cssClass: string): this {\n const classAttr = this.getClass();\n const classes = !classAttr ? [] : classAttr.split(whitespaceRe);\n const removeClasses = cssClass.split(whitespaceRe);\n let removeClass: string | undefined;\n\n while (classes.length && (removeClass = removeClasses.shift())) {\n const idx = classes.indexOf(removeClass);\n if (idx !== -1) {\n classes.splice(idx, 1);\n }\n }\n\n this.getAttrs()['class'] = classes.join(' ');\n return this;\n }\n\n /**\n * Convenience method to retrieve the CSS class(es) for the HtmlTag, which will each be separated by spaces when\n * there are multiple.\n *\n * @return {String}\n */\n getClass(): string {\n return this.getAttrs()['class'] || '';\n }\n\n /**\n * Convenience method to check if the tag has a CSS class or not.\n *\n * @param {String} cssClass The CSS class to check for.\n * @return {Boolean} `true` if the HtmlTag has the CSS class, `false` otherwise.\n */\n hasClass(cssClass: string): boolean {\n return (' ' + this.getClass() + ' ').indexOf(' ' + cssClass + ' ') !== -1;\n }\n\n /**\n * Sets the inner HTML for the tag.\n *\n * @param {String} html The inner HTML to set.\n * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n */\n setInnerHTML(html: string): this {\n this.innerHTML = html;\n\n return this;\n }\n\n /**\n * Backwards compatibility method name.\n *\n * @param {String} html The inner HTML to set.\n * @return {Autolinker.HtmlTag} This HtmlTag instance, so that method calls may be chained.\n */\n setInnerHtml(html: string): this {\n return this.setInnerHTML(html);\n }\n\n /**\n * Retrieves the inner HTML for the tag.\n *\n * @return {String}\n */\n getInnerHTML(): string {\n return this.innerHTML || '';\n }\n\n /**\n * Backward compatibility method name.\n *\n * @return {String}\n */\n getInnerHtml(): string {\n return this.getInnerHTML();\n }\n\n /**\n * Generates the HTML string for the tag.\n *\n * @return {String}\n */\n toAnchorString(): string {\n const tagName = this.getTagName();\n let attrsStr = this.buildAttrsStr();\n\n attrsStr = attrsStr ? ' ' + attrsStr : ''; // prepend a space if there are actually attributes\n\n return ['<', tagName, attrsStr, '>', this.getInnerHtml(), '</', tagName, '>'].join('');\n }\n\n /**\n * Support method for {@link #toAnchorString}, returns the string space-separated key=\"value\" pairs, used to populate\n * the stringified HtmlTag.\n *\n * @protected\n * @return {String} Example return: `attr1=\"value1\" attr2=\"value2\"`\n */\n protected buildAttrsStr(): string {\n const attrs = this.getAttrs(),\n attrsArr: string[] = [];\n\n for (const prop in attrs) {\n if (hasOwnProperty.call(attrs, prop)) {\n attrsArr.push(prop + '=\"' + attrs[prop] + '\"');\n }\n }\n return attrsArr.join(' ');\n }\n}\n\nexport interface HtmlTagCfg {\n tagName?: string;\n attrs?: { [key: string]: string };\n innerHtml?: string;\n innerHTML?: string;\n}\n"]}