source: node_modules/autolinker/dist/commonjs/match/match.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.8 KB
RevLine 
[d24f17c]1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Match = void 0;
4/**
5 * @abstract
6 * @class Autolinker.match.Match
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 Match = /** @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 Match(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.__jsduckDummyDocProp = 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 Match.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 * @param {Number} offset
86 */
87 Match.prototype.setOffset = function (offset) {
88 this.offset = offset;
89 };
90 /**
91 * Returns the offset of where the match was made in the input string. This
92 * is the 0-based index of the match.
93 *
94 * @return {Number}
95 */
96 Match.prototype.getOffset = function () {
97 return this.offset;
98 };
99 /**
100 * Returns the CSS class suffix(es) for this match.
101 *
102 * A CSS class suffix is appended to the {@link Autolinker#className} in
103 * the {@link Autolinker.AnchorTagBuilder} when a match is translated into
104 * an anchor tag.
105 *
106 * For example, if {@link Autolinker#className} was configured as 'myLink',
107 * and this method returns `[ 'url' ]`, the final class name of the element
108 * will become: 'myLink myLink-url'.
109 *
110 * The match may provide multiple CSS class suffixes to be appended to the
111 * {@link Autolinker#className} in order to facilitate better styling
112 * options for different match criteria. See {@link Autolinker.match.Mention}
113 * for an example.
114 *
115 * By default, this method returns a single array with the match's
116 * {@link #getType type} name, but may be overridden by subclasses.
117 *
118 * @return {String[]}
119 */
120 Match.prototype.getCssClassSuffixes = function () {
121 return [this.getType()];
122 };
123 /**
124 * Builds and returns an {@link Autolinker.HtmlTag} instance based on the
125 * Match.
126 *
127 * This can be used to easily generate anchor tags from matches, and either
128 * return their HTML string, or modify them before doing so.
129 *
130 * Example Usage:
131 *
132 * var tag = match.buildTag();
133 * tag.addClass( 'cordova-link' );
134 * tag.setAttr( 'target', '_system' );
135 *
136 * tag.toAnchorString(); // <a href="http://google.com" class="cordova-link" target="_system">Google</a>
137 *
138 * Example Usage in {@link Autolinker#replaceFn}:
139 *
140 * var html = Autolinker.link( "Test google.com", {
141 * replaceFn : function( match ) {
142 * var tag = match.buildTag(); // returns an {@link Autolinker.HtmlTag} instance
143 * tag.setAttr( 'rel', 'nofollow' );
144 *
145 * return tag;
146 * }
147 * } );
148 *
149 * // generated html:
150 * // Test <a href="http://google.com" target="_blank" rel="nofollow">google.com</a>
151 */
152 Match.prototype.buildTag = function () {
153 return this.tagBuilder.build(this);
154 };
155 return Match;
156}());
157exports.Match = Match;
158//# sourceMappingURL=match.js.map
Note: See TracBrowser for help on using the repository browser.