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