abstract-match.js 5.8 KB

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