1 | ;
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.AnchorTagBuilder = void 0;
|
4 | var html_tag_1 = require("./html-tag");
|
5 | var truncate_smart_1 = require("./truncate/truncate-smart");
|
6 | var truncate_middle_1 = require("./truncate/truncate-middle");
|
7 | var truncate_end_1 = require("./truncate/truncate-end");
|
8 | /**
|
9 | * @protected
|
10 | * @class Autolinker.AnchorTagBuilder
|
11 | * @extends Object
|
12 | *
|
13 | * Builds anchor (<a>) tags for the Autolinker utility when a match is
|
14 | * found.
|
15 | *
|
16 | * Normally this class is instantiated, configured, and used internally by an
|
17 | * {@link Autolinker} instance, but may actually be used indirectly in a
|
18 | * {@link Autolinker#replaceFn replaceFn} to create {@link Autolinker.HtmlTag HtmlTag}
|
19 | * instances which may be modified before returning from the
|
20 | * {@link Autolinker#replaceFn replaceFn}. For example:
|
21 | *
|
22 | * var html = Autolinker.link( "Test google.com", {
|
23 | * replaceFn : function( match ) {
|
24 | * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance
|
25 | * tag.setAttr( 'rel', 'nofollow' );
|
26 | *
|
27 | * return tag;
|
28 | * }
|
29 | * } );
|
30 | *
|
31 | * // generated html:
|
32 | * // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a>
|
33 | */
|
34 | var AnchorTagBuilder = /** @class */ (function () {
|
35 | /**
|
36 | * @method constructor
|
37 | * @param {Object} [cfg] The configuration options for the AnchorTagBuilder instance, specified in an Object (map).
|
38 | */
|
39 | function AnchorTagBuilder(cfg) {
|
40 | if (cfg === void 0) { cfg = {}; }
|
41 | /**
|
42 | * @cfg {Boolean} newWindow
|
43 | * @inheritdoc Autolinker#newWindow
|
44 | */
|
45 | this.newWindow = false; // default value just to get the above doc comment in the ES5 output and documentation generator
|
46 | /**
|
47 | * @cfg {Object} truncate
|
48 | * @inheritdoc Autolinker#truncate
|
49 | */
|
50 | this.truncate = {}; // default value just to get the above doc comment in the ES5 output and documentation generator
|
51 | /**
|
52 | * @cfg {String} className
|
53 | * @inheritdoc Autolinker#className
|
54 | */
|
55 | this.className = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
|
56 | this.newWindow = cfg.newWindow || false;
|
57 | this.truncate = cfg.truncate || {};
|
58 | this.className = cfg.className || '';
|
59 | }
|
60 | /**
|
61 | * Generates the actual anchor (<a>) tag to use in place of the
|
62 | * matched text, via its `match` object.
|
63 | *
|
64 | * @param match The Match instance to generate an anchor tag from.
|
65 | * @return The HtmlTag instance for the anchor tag.
|
66 | */
|
67 | AnchorTagBuilder.prototype.build = function (match) {
|
68 | return new html_tag_1.HtmlTag({
|
69 | tagName: 'a',
|
70 | attrs: this.createAttrs(match),
|
71 | innerHtml: this.processAnchorText(match.getAnchorText()),
|
72 | });
|
73 | };
|
74 | /**
|
75 | * Creates the Object (map) of the HTML attributes for the anchor (<a>)
|
76 | * tag being generated.
|
77 | *
|
78 | * @protected
|
79 | * @param match The Match instance to generate an anchor tag from.
|
80 | * @return A key/value Object (map) of the anchor tag's attributes.
|
81 | */
|
82 | AnchorTagBuilder.prototype.createAttrs = function (match) {
|
83 | var attrs = {
|
84 | href: match.getAnchorHref(), // we'll always have the `href` attribute
|
85 | };
|
86 | var cssClass = this.createCssClass(match);
|
87 | if (cssClass) {
|
88 | attrs['class'] = cssClass;
|
89 | }
|
90 | if (this.newWindow) {
|
91 | attrs['target'] = '_blank';
|
92 | attrs['rel'] = 'noopener noreferrer'; // Issue #149. See https://mathiasbynens.github.io/rel-noopener/
|
93 | }
|
94 | if (this.truncate) {
|
95 | if (this.truncate.length && this.truncate.length < match.getAnchorText().length) {
|
96 | attrs['title'] = match.getAnchorHref();
|
97 | }
|
98 | }
|
99 | return attrs;
|
100 | };
|
101 | /**
|
102 | * Creates the CSS class that will be used for a given anchor tag, based on
|
103 | * the `matchType` and the {@link #className} config.
|
104 | *
|
105 | * Example returns:
|
106 | *
|
107 | * - "" // no {@link #className}
|
108 | * - "myLink myLink-url" // url match
|
109 | * - "myLink myLink-email" // email match
|
110 | * - "myLink myLink-phone" // phone match
|
111 | * - "myLink myLink-hashtag" // hashtag match
|
112 | * - "myLink myLink-mention myLink-twitter" // mention match with Twitter service
|
113 | *
|
114 | * @protected
|
115 | * @param match The Match instance to generate an
|
116 | * anchor tag from.
|
117 | * @return The CSS class string for the link. Example return:
|
118 | * "myLink myLink-url". If no {@link #className} was configured, returns
|
119 | * an empty string.
|
120 | */
|
121 | AnchorTagBuilder.prototype.createCssClass = function (match) {
|
122 | var className = this.className;
|
123 | if (!className) {
|
124 | return '';
|
125 | }
|
126 | else {
|
127 | var returnClasses = [className], cssClassSuffixes = match.getCssClassSuffixes();
|
128 | for (var i = 0, len = cssClassSuffixes.length; i < len; i++) {
|
129 | returnClasses.push(className + '-' + cssClassSuffixes[i]);
|
130 | }
|
131 | return returnClasses.join(' ');
|
132 | }
|
133 | };
|
134 | /**
|
135 | * Processes the `anchorText` by truncating the text according to the
|
136 | * {@link #truncate} config.
|
137 | *
|
138 | * @private
|
139 | * @param anchorText The anchor tag's text (i.e. what will be
|
140 | * displayed).
|
141 | * @return The processed `anchorText`.
|
142 | */
|
143 | AnchorTagBuilder.prototype.processAnchorText = function (anchorText) {
|
144 | anchorText = this.doTruncate(anchorText);
|
145 | return anchorText;
|
146 | };
|
147 | /**
|
148 | * Performs the truncation of the `anchorText` based on the {@link #truncate}
|
149 | * option. If the `anchorText` is longer than the length specified by the
|
150 | * {@link #truncate} option, the truncation is performed based on the
|
151 | * `location` property. See {@link #truncate} for details.
|
152 | *
|
153 | * @private
|
154 | * @param anchorText The anchor tag's text (i.e. what will be
|
155 | * displayed).
|
156 | * @return The truncated anchor text.
|
157 | */
|
158 | AnchorTagBuilder.prototype.doTruncate = function (anchorText) {
|
159 | var truncate = this.truncate;
|
160 | if (!truncate || !truncate.length)
|
161 | return anchorText;
|
162 | var truncateLength = truncate.length, truncateLocation = truncate.location;
|
163 | if (truncateLocation === 'smart') {
|
164 | return (0, truncate_smart_1.truncateSmart)(anchorText, truncateLength);
|
165 | }
|
166 | else if (truncateLocation === 'middle') {
|
167 | return (0, truncate_middle_1.truncateMiddle)(anchorText, truncateLength);
|
168 | }
|
169 | else {
|
170 | return (0, truncate_end_1.truncateEnd)(anchorText, truncateLength);
|
171 | }
|
172 | };
|
173 | return AnchorTagBuilder;
|
174 | }());
|
175 | exports.AnchorTagBuilder = AnchorTagBuilder;
|
176 | //# sourceMappingURL=anchor-tag-builder.js.map |
\ | No newline at end of file |