UNPKG

4.26 kBTypeScriptView Raw
1import { HtmlTag } from './html-tag';
2import { TruncateConfigObj } from './autolinker';
3import { AbstractMatch } from './match/abstract-match';
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 */
30export 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 (&lt;a&gt;) tag to use in place of the
53 * matched text, via its `match` object.
54 *
55 * @param match The Match instance to generate an anchor tag from.
56 * @return The HtmlTag instance for the anchor tag.
57 */
58 build(match: AbstractMatch): HtmlTag;
59 /**
60 * Creates the Object (map) of the HTML attributes for the anchor (&lt;a&gt;)
61 * tag being generated.
62 *
63 * @protected
64 * @param match The Match instance to generate an anchor tag from.
65 * @return A key/value Object (map) of the anchor tag's attributes.
66 */
67 protected createAttrs(match: AbstractMatch): {
68 [attrName: string]: string;
69 };
70 /**
71 * Creates the CSS class that will be used for a given anchor tag, based on
72 * the `matchType` and the {@link #className} config.
73 *
74 * Example returns:
75 *
76 * - "" // no {@link #className}
77 * - "myLink myLink-url" // url match
78 * - "myLink myLink-email" // email match
79 * - "myLink myLink-phone" // phone match
80 * - "myLink myLink-hashtag" // hashtag match
81 * - "myLink myLink-mention myLink-twitter" // mention match with Twitter service
82 *
83 * @protected
84 * @param match The Match instance to generate an
85 * anchor tag from.
86 * @return The CSS class string for the link. Example return:
87 * "myLink myLink-url". If no {@link #className} was configured, returns
88 * an empty string.
89 */
90 protected createCssClass(match: AbstractMatch): string;
91 /**
92 * Processes the `anchorText` by truncating the text according to the
93 * {@link #truncate} config.
94 *
95 * @private
96 * @param anchorText The anchor tag's text (i.e. what will be
97 * displayed).
98 * @return The processed `anchorText`.
99 */
100 private processAnchorText;
101 /**
102 * Performs the truncation of the `anchorText` based on the {@link #truncate}
103 * option. If the `anchorText` is longer than the length specified by the
104 * {@link #truncate} option, the truncation is performed based on the
105 * `location` property. See {@link #truncate} for details.
106 *
107 * @private
108 * @param anchorText The anchor tag's text (i.e. what will be
109 * displayed).
110 * @return The truncated anchor text.
111 */
112 private doTruncate;
113}
114export interface AnchorTagBuilderCfg {
115 newWindow?: boolean;
116 truncate?: TruncateConfigObj;
117 className?: string;
118}