UNPKG

6.24 kBTypeScriptView Raw
1import { AnchorTagBuilder } from '../anchor-tag-builder';
2import { HtmlTag } from '../html-tag';
3import { MatchType } from './match';
4/**
5 * @abstract
6 * @class Autolinker.match.AbstractMatch
7 *
8 * Represents a match found in an input string which should be Autolinked. A Match object is what is provided in a
9 * {@link Autolinker#replaceFn replaceFn}, and may be used to query for details about the match.
10 *
11 * For example:
12 *
13 * var input = "..."; // string with URLs, Email Addresses, and Mentions (Twitter, Instagram, Soundcloud)
14 *
15 * var linkedText = Autolinker.link( input, {
16 * replaceFn : function( match ) {
17 * console.log( "href = ", match.getAnchorHref() );
18 * console.log( "text = ", match.getAnchorText() );
19 *
20 * switch( match.getType() ) {
21 * case 'url' :
22 * console.log( "url: ", match.getUrl() );
23 *
24 * case 'email' :
25 * console.log( "email: ", match.getEmail() );
26 *
27 * case 'mention' :
28 * console.log( "mention: ", match.getMention() );
29 * }
30 * }
31 * } );
32 *
33 * See the {@link Autolinker} class for more details on using the {@link Autolinker#replaceFn replaceFn}.
34 */
35export declare abstract class AbstractMatch {
36 /**
37 * @public
38 * @property {'url'/'email'/'hashtag'/'mention'/'phone'} type
39 *
40 * A string name for the type of match that this class represents. Can be
41 * used in a TypeScript discriminating union to type-narrow from the
42 * `Match` type.
43 */
44 abstract readonly type: MatchType;
45 /**
46 * @cfg {Autolinker.AnchorTagBuilder} tagBuilder (required)
47 *
48 * Reference to the AnchorTagBuilder instance to use to generate an anchor
49 * tag for the Match.
50 */
51 private _;
52 private readonly tagBuilder;
53 /**
54 * @cfg {String} matchedText (required)
55 *
56 * The original text that was matched by the {@link Autolinker.matcher.Matcher}.
57 */
58 protected readonly matchedText: string;
59 /**
60 * @cfg {Number} offset (required)
61 *
62 * The offset of where the match was made in the input string.
63 */
64 private offset;
65 /**
66 * @member Autolinker.match.Match
67 * @method constructor
68 * @param {Object} cfg The configuration properties for the Match
69 * instance, specified in an Object (map).
70 */
71 constructor(cfg: AbstractMatchConfig);
72 /**
73 * Returns a string name for the type of match that this class represents.
74 *
75 * @deprecated Use {@link #type} instead which can assist in type-narrowing
76 * for TypeScript.
77 * @abstract
78 * @return {String}
79 */
80 abstract getType(): MatchType;
81 /**
82 * Returns the original text that was matched.
83 *
84 * @return {String}
85 */
86 getMatchedText(): string;
87 /**
88 * Sets the {@link #offset} of where the match was made in the input string.
89 *
90 * A {@link Autolinker.matcher.Matcher} will be fed only HTML text nodes,
91 * and will therefore set an original offset that is relative to the HTML
92 * text node itself. However, we want this offset to be relative to the full
93 * HTML input string, and thus if using {@link Autolinker#parse} (rather
94 * than calling a {@link Autolinker.matcher.Matcher} directly), then this
95 * offset is corrected after the Matcher itself has done its job.
96 *
97 * @private
98 * @param {Number} offset
99 */
100 setOffset(offset: number): void;
101 /**
102 * Returns the offset of where the match was made in the input string. This
103 * is the 0-based index of the match.
104 *
105 * @return {Number}
106 */
107 getOffset(): number;
108 /**
109 * Returns the anchor href that should be generated for the match.
110 *
111 * @abstract
112 * @return {String}
113 */
114 abstract getAnchorHref(): string;
115 /**
116 * Returns the anchor text that should be generated for the match.
117 *
118 * @abstract
119 * @return {String}
120 */
121 abstract getAnchorText(): string;
122 /**
123 * Returns the CSS class suffix(es) for this match.
124 *
125 * A CSS class suffix is appended to the {@link Autolinker#className} in
126 * the {@link Autolinker.AnchorTagBuilder} when a match is translated into
127 * an anchor tag.
128 *
129 * For example, if {@link Autolinker#className} was configured as 'myLink',
130 * and this method returns `[ 'url' ]`, the final class name of the element
131 * will become: 'myLink myLink-url'.
132 *
133 * The match may provide multiple CSS class suffixes to be appended to the
134 * {@link Autolinker#className} in order to facilitate better styling
135 * options for different match criteria. See {@link Autolinker.match.Mention}
136 * for an example.
137 *
138 * By default, this method returns a single array with the match's
139 * {@link #getType type} name, but may be overridden by subclasses.
140 *
141 * @return {String[]}
142 */
143 getCssClassSuffixes(): string[];
144 /**
145 * Builds and returns an {@link Autolinker.HtmlTag} instance based on the
146 * Match.
147 *
148 * This can be used to easily generate anchor tags from matches, and either
149 * return their HTML string, or modify them before doing so.
150 *
151 * Example Usage:
152 *
153 * var tag = match.buildTag();
154 * tag.addClass( 'cordova-link' );
155 * tag.setAttr( 'target', '_system' );
156 *
157 * tag.toAnchorString(); // <a href="http://google.com" class="cordova-link" target="_system">Google</a>
158 *
159 * Example Usage in {@link Autolinker#replaceFn}:
160 *
161 * var html = Autolinker.link( "Test google.com", {
162 * replaceFn : function( match ) {
163 * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance
164 * tag.setAttr( 'rel', 'nofollow' );
165 *
166 * return tag;
167 * }
168 * } );
169 *
170 * // generated html:
171 * // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a>
172 */
173 buildTag(): HtmlTag;
174}
175export interface AbstractMatchConfig {
176 tagBuilder: AnchorTagBuilder;
177 matchedText: string;
178 offset: number;
179}