UNPKG

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