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