|
| 1 | +import { Match } from "./match/match"; |
| 2 | +import { HtmlTag } from "./html-tag"; |
| 3 | +import { TruncateConfigObj } from "./autolinker"; |
| 4 | +/** |
| 5 | + * @protected |
| 6 | + * @class Autolinker.AnchorTagBuilder |
| 7 | + * @extends Object |
| 8 | + * |
| 9 | + * Builds anchor (<a>) tags for the Autolinker utility when a match is |
| 10 | + * found. |
| 11 | + * |
| 12 | + * Normally this class is instantiated, configured, and used internally by an |
| 13 | + * {@link Autolinker} instance, but may actually be used indirectly in a |
| 14 | + * {@link Autolinker#replaceFn replaceFn} to create {@link Autolinker.HtmlTag HtmlTag} |
| 15 | + * instances which may be modified before returning from the |
| 16 | + * {@link Autolinker#replaceFn replaceFn}. For example: |
| 17 | + * |
| 18 | + * var html = Autolinker.link( "Test google.com", { |
| 19 | + * replaceFn : function( match ) { |
| 20 | + * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance |
| 21 | + * tag.setAttr( 'rel', 'nofollow' ); |
| 22 | + * |
| 23 | + * return tag; |
| 24 | + * } |
| 25 | + * } ); |
| 26 | + * |
| 27 | + * // generated html: |
| 28 | + * // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a> |
| 29 | + */ |
| 30 | +export declare class AnchorTagBuilder { |
| 31 | + /** |
| 32 | + * @cfg {Boolean} newWindow |
| 33 | + * @inheritdoc Autolinker#newWindow |
| 34 | + */ |
| 35 | + private readonly newWindow; |
| 36 | + /** |
| 37 | + * @cfg {Object} truncate |
| 38 | + * @inheritdoc Autolinker#truncate |
| 39 | + */ |
| 40 | + private readonly truncate; |
| 41 | + /** |
| 42 | + * @cfg {String} className |
| 43 | + * @inheritdoc Autolinker#className |
| 44 | + */ |
| 45 | + private readonly className; |
| 46 | + /** |
| 47 | + * @method constructor |
| 48 | + * @param {Object} [cfg] The configuration options for the AnchorTagBuilder instance, specified in an Object (map). |
| 49 | + */ |
| 50 | + constructor(cfg?: AnchorTagBuilderCfg); |
| 51 | + /** |
| 52 | + * Generates the actual anchor (<a>) tag to use in place of the |
| 53 | + * matched text, via its `match` object. |
| 54 | + * |
| 55 | + * @param {Autolinker.match.Match} match The Match instance to generate an |
| 56 | + * anchor tag from. |
| 57 | + * @return {Autolinker.HtmlTag} The HtmlTag instance for the anchor tag. |
| 58 | + */ |
| 59 | + build(match: Match): HtmlTag; |
| 60 | + /** |
| 61 | + * Creates the Object (map) of the HTML attributes for the anchor (<a>) |
| 62 | + * tag being generated. |
| 63 | + * |
| 64 | + * @protected |
| 65 | + * @param {Autolinker.match.Match} match The Match instance to generate an |
| 66 | + * anchor tag from. |
| 67 | + * @return {Object} A key/value Object (map) of the anchor tag's attributes. |
| 68 | + */ |
| 69 | + protected createAttrs(match: Match): { |
| 70 | + [attrName: string]: string; |
| 71 | + }; |
| 72 | + /** |
| 73 | + * Creates the CSS class that will be used for a given anchor tag, based on |
| 74 | + * the `matchType` and the {@link #className} config. |
| 75 | + * |
| 76 | + * Example returns: |
| 77 | + * |
| 78 | + * - "" // no {@link #className} |
| 79 | + * - "myLink myLink-url" // url match |
| 80 | + * - "myLink myLink-email" // email match |
| 81 | + * - "myLink myLink-phone" // phone match |
| 82 | + * - "myLink myLink-hashtag" // hashtag match |
| 83 | + * - "myLink myLink-mention myLink-twitter" // mention match with Twitter service |
| 84 | + * |
| 85 | + * @protected |
| 86 | + * @param {Autolinker.match.Match} match The Match instance to generate an |
| 87 | + * anchor tag from. |
| 88 | + * @return {String} The CSS class string for the link. Example return: |
| 89 | + * "myLink myLink-url". If no {@link #className} was configured, returns |
| 90 | + * an empty string. |
| 91 | + */ |
| 92 | + protected createCssClass(match: Match): string; |
| 93 | + /** |
| 94 | + * Processes the `anchorText` by truncating the text according to the |
| 95 | + * {@link #truncate} config. |
| 96 | + * |
| 97 | + * @private |
| 98 | + * @param {String} anchorText The anchor tag's text (i.e. what will be |
| 99 | + * displayed). |
| 100 | + * @return {String} The processed `anchorText`. |
| 101 | + */ |
| 102 | + private processAnchorText; |
| 103 | + /** |
| 104 | + * Performs the truncation of the `anchorText` based on the {@link #truncate} |
| 105 | + * option. If the `anchorText` is longer than the length specified by the |
| 106 | + * {@link #truncate} option, the truncation is performed based on the |
| 107 | + * `location` property. See {@link #truncate} for details. |
| 108 | + * |
| 109 | + * @private |
| 110 | + * @param {String} anchorText The anchor tag's text (i.e. what will be |
| 111 | + * displayed). |
| 112 | + * @return {String} The truncated anchor text. |
| 113 | + */ |
| 114 | + private doTruncate; |
| 115 | +} |
| 116 | +export interface AnchorTagBuilderCfg { |
| 117 | + newWindow?: boolean; |
| 118 | + truncate?: TruncateConfigObj; |
| 119 | + className?: string; |
| 120 | +} |
0 commit comments