UNPKG

6.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.AbstractMatch = void 0;
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 */
35var AbstractMatch = /** @class */ (function () {
36 /**
37 * @member Autolinker.match.Match
38 * @method constructor
39 * @param {Object} cfg The configuration properties for the Match
40 * instance, specified in an Object (map).
41 */
42 function AbstractMatch(cfg) {
43 /**
44 * @cfg {Autolinker.AnchorTagBuilder} tagBuilder (required)
45 *
46 * Reference to the AnchorTagBuilder instance to use to generate an anchor
47 * tag for the Match.
48 */
49 // @ts-ignore
50 this._ = null; // property used just to get the above doc comment into the ES5 output and documentation generator
51 /**
52 * @cfg {String} matchedText (required)
53 *
54 * The original text that was matched by the {@link Autolinker.matcher.Matcher}.
55 */
56 this.matchedText = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
57 /**
58 * @cfg {Number} offset (required)
59 *
60 * The offset of where the match was made in the input string.
61 */
62 this.offset = 0; // default value just to get the above doc comment in the ES5 output and documentation generator
63 this.tagBuilder = cfg.tagBuilder;
64 this.matchedText = cfg.matchedText;
65 this.offset = cfg.offset;
66 }
67 /**
68 * Returns the original text that was matched.
69 *
70 * @return {String}
71 */
72 AbstractMatch.prototype.getMatchedText = function () {
73 return this.matchedText;
74 };
75 /**
76 * Sets the {@link #offset} of where the match was made in the input string.
77 *
78 * A {@link Autolinker.matcher.Matcher} will be fed only HTML text nodes,
79 * and will therefore set an original offset that is relative to the HTML
80 * text node itself. However, we want this offset to be relative to the full
81 * HTML input string, and thus if using {@link Autolinker#parse} (rather
82 * than calling a {@link Autolinker.matcher.Matcher} directly), then this
83 * offset is corrected after the Matcher itself has done its job.
84 *
85 * @private
86 * @param {Number} offset
87 */
88 AbstractMatch.prototype.setOffset = function (offset) {
89 this.offset = offset;
90 };
91 /**
92 * Returns the offset of where the match was made in the input string. This
93 * is the 0-based index of the match.
94 *
95 * @return {Number}
96 */
97 AbstractMatch.prototype.getOffset = function () {
98 return this.offset;
99 };
100 /**
101 * Returns the CSS class suffix(es) for this match.
102 *
103 * A CSS class suffix is appended to the {@link Autolinker#className} in
104 * the {@link Autolinker.AnchorTagBuilder} when a match is translated into
105 * an anchor tag.
106 *
107 * For example, if {@link Autolinker#className} was configured as 'myLink',
108 * and this method returns `[ 'url' ]`, the final class name of the element
109 * will become: 'myLink myLink-url'.
110 *
111 * The match may provide multiple CSS class suffixes to be appended to the
112 * {@link Autolinker#className} in order to facilitate better styling
113 * options for different match criteria. See {@link Autolinker.match.Mention}
114 * for an example.
115 *
116 * By default, this method returns a single array with the match's
117 * {@link #getType type} name, but may be overridden by subclasses.
118 *
119 * @return {String[]}
120 */
121 AbstractMatch.prototype.getCssClassSuffixes = function () {
122 return [this.type];
123 };
124 /**
125 * Builds and returns an {@link Autolinker.HtmlTag} instance based on the
126 * Match.
127 *
128 * This can be used to easily generate anchor tags from matches, and either
129 * return their HTML string, or modify them before doing so.
130 *
131 * Example Usage:
132 *
133 * var tag = match.buildTag();
134 * tag.addClass( 'cordova-link' );
135 * tag.setAttr( 'target', '_system' );
136 *
137 * tag.toAnchorString(); // <a href="http://google.com" class="cordova-link" target="_system">Google</a>
138 *
139 * Example Usage in {@link Autolinker#replaceFn}:
140 *
141 * var html = Autolinker.link( "Test google.com", {
142 * replaceFn : function( match ) {
143 * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance
144 * tag.setAttr( 'rel', 'nofollow' );
145 *
146 * return tag;
147 * }
148 * } );
149 *
150 * // generated html:
151 * // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a>
152 */
153 AbstractMatch.prototype.buildTag = function () {
154 return this.tagBuilder.build(this);
155 };
156 return AbstractMatch;
157}());
158exports.AbstractMatch = AbstractMatch;
159//# sourceMappingURL=abstract-match.js.map
\No newline at end of file