智慧水务管理系统 - 精河县供水工程综合管理平台

autolinker.js 40KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. import { __assign, __read, __spreadArray } from "tslib";
  2. import { version } from './version';
  3. import { isBoolean, removeWithPredicate } from './utils';
  4. import { AnchorTagBuilder } from './anchor-tag-builder';
  5. import { HtmlTag } from './html-tag';
  6. import { parseMatches } from './parser/parse-matches';
  7. import { parseHtml } from './htmlParser/parse-html';
  8. import { mentionServices } from './parser/mention-utils';
  9. import { hashtagServices } from './parser/hashtag-utils';
  10. /**
  11. * @class Autolinker
  12. * @extends Object
  13. *
  14. * Utility class used to process a given string of text, and wrap the matches in
  15. * the appropriate anchor (<a>) tags to turn them into links.
  16. *
  17. * Any of the configuration options may be provided in an Object provided
  18. * to the Autolinker constructor, which will configure how the {@link #link link()}
  19. * method will process the links.
  20. *
  21. * For example:
  22. *
  23. * var autolinker = new Autolinker( {
  24. * newWindow : false,
  25. * truncate : 30
  26. * } );
  27. *
  28. * var html = autolinker.link( "Joe went to www.yahoo.com" );
  29. * // produces: 'Joe went to <a href="http://www.yahoo.com">yahoo.com</a>'
  30. *
  31. *
  32. * The {@link #static-link static link()} method may also be used to inline
  33. * options into a single call, which may be more convenient for one-off uses.
  34. * For example:
  35. *
  36. * var html = Autolinker.link( "Joe went to www.yahoo.com", {
  37. * newWindow : false,
  38. * truncate : 30
  39. * } );
  40. * // produces: 'Joe went to <a href="http://www.yahoo.com">yahoo.com</a>'
  41. *
  42. *
  43. * ## Custom Replacements of Links
  44. *
  45. * If the configuration options do not provide enough flexibility, a {@link #replaceFn}
  46. * may be provided to fully customize the output of Autolinker. This function is
  47. * called once for each URL/Email/Phone#/Hashtag/Mention (Twitter, Instagram, Soundcloud)
  48. * match that is encountered.
  49. *
  50. * For example:
  51. *
  52. * var input = "..."; // string with URLs, Email Addresses, Phone #s, Hashtags, and Mentions (Twitter, Instagram, Soundcloud)
  53. *
  54. * var linkedText = Autolinker.link( input, {
  55. * replaceFn : function( match ) {
  56. * console.log( "href = ", match.getAnchorHref() );
  57. * console.log( "text = ", match.getAnchorText() );
  58. *
  59. * switch( match.getType() ) {
  60. * case 'url' :
  61. * console.log( "url: ", match.getUrl() );
  62. *
  63. * if( match.getUrl().indexOf( 'mysite.com' ) === -1 ) {
  64. * var tag = match.buildTag(); // returns an `Autolinker.HtmlTag` instance, which provides mutator methods for easy changes
  65. * tag.setAttr( 'rel', 'nofollow' );
  66. * tag.addClass( 'external-link' );
  67. *
  68. * return tag;
  69. *
  70. * } else {
  71. * return true; // let Autolinker perform its normal anchor tag replacement
  72. * }
  73. *
  74. * case 'email' :
  75. * var email = match.getEmail();
  76. * console.log( "email: ", email );
  77. *
  78. * if( email === "my@own.address" ) {
  79. * return false; // don't auto-link this particular email address; leave as-is
  80. * } else {
  81. * return; // no return value will have Autolinker perform its normal anchor tag replacement (same as returning `true`)
  82. * }
  83. *
  84. * case 'phone' :
  85. * var phoneNumber = match.getPhoneNumber();
  86. * console.log( phoneNumber );
  87. *
  88. * return '<a href="http://newplace.to.link.phone.numbers.to/">' + phoneNumber + '</a>';
  89. *
  90. * case 'hashtag' :
  91. * var hashtag = match.getHashtag();
  92. * console.log( hashtag );
  93. *
  94. * return '<a href="http://newplace.to.link.hashtag.handles.to/">' + hashtag + '</a>';
  95. *
  96. * case 'mention' :
  97. * var mention = match.getMention();
  98. * console.log( mention );
  99. *
  100. * return '<a href="http://newplace.to.link.mention.to/">' + mention + '</a>';
  101. * }
  102. * }
  103. * } );
  104. *
  105. *
  106. * The function may return the following values:
  107. *
  108. * - `true` (Boolean): Allow Autolinker to replace the match as it normally
  109. * would.
  110. * - `false` (Boolean): Do not replace the current match at all - leave as-is.
  111. * - Any String: If a string is returned from the function, the string will be
  112. * used directly as the replacement HTML for the match.
  113. * - An {@link Autolinker.HtmlTag} instance, which can be used to build/modify
  114. * an HTML tag before writing out its HTML text.
  115. */
  116. var Autolinker = /** @class */ (function () {
  117. /**
  118. * @method constructor
  119. * @param {Object} [cfg] The configuration options for the Autolinker instance,
  120. * specified in an Object (map).
  121. */
  122. function Autolinker(cfg) {
  123. if (cfg === void 0) { cfg = {}; }
  124. /**
  125. * The Autolinker version number exposed on the instance itself.
  126. *
  127. * Ex: 0.25.1
  128. *
  129. * @property {String} version
  130. */
  131. this.version = Autolinker.version;
  132. /**
  133. * @cfg {Boolean/Object} [urls]
  134. *
  135. * `true` if URLs should be automatically linked, `false` if they should not
  136. * be. Defaults to `true`.
  137. *
  138. * Examples:
  139. *
  140. * urls: true
  141. *
  142. * // or
  143. *
  144. * urls: {
  145. * schemeMatches : true,
  146. * tldMatches : true,
  147. * ipV4Matches : true
  148. * }
  149. *
  150. * As shown above, this option also accepts an Object form with 3 properties
  151. * to allow for more customization of what exactly gets linked. All default
  152. * to `true`:
  153. *
  154. * @cfg {Boolean} [urls.schemeMatches] `true` to match URLs found prefixed
  155. * with a scheme, i.e. `http://google.com`, or `other+scheme://google.com`,
  156. * `false` to prevent these types of matches.
  157. * @cfg {Boolean} [urls.tldMatches] `true` to match URLs with known top
  158. * level domains (.com, .net, etc.) that are not prefixed with a scheme
  159. * (such as 'http://'). This option attempts to match anything that looks
  160. * like a URL in the given text. Ex: `google.com`, `asdf.org/?page=1`, etc.
  161. * `false` to prevent these types of matches.
  162. * @cfg {Boolean} [urls.ipV4Matches] `true` to match IPv4 addresses in text
  163. * that are not prefixed with a scheme (such as 'http://'). This option
  164. * attempts to match anything that looks like an IPv4 address in text. Ex:
  165. * `192.168.0.1`, `10.0.0.1/?page=1`, etc. `false` to prevent these types
  166. * of matches.
  167. */
  168. this.urls = {}; // default value just to get the above doc comment in the ES5 output and documentation generator
  169. /**
  170. * @cfg {Boolean} [email=true]
  171. *
  172. * `true` if email addresses should be automatically linked, `false` if they
  173. * should not be.
  174. */
  175. this.email = true; // default value just to get the above doc comment in the ES5 output and documentation generator
  176. /**
  177. * @cfg {Boolean} [phone=true]
  178. *
  179. * `true` if Phone numbers ("(555)555-5555") should be automatically linked,
  180. * `false` if they should not be.
  181. */
  182. this.phone = true; // default value just to get the above doc comment in the ES5 output and documentation generator
  183. /**
  184. * @cfg {Boolean/String} [hashtag=false]
  185. *
  186. * A string for the service name to have hashtags (ex: "#myHashtag")
  187. * auto-linked to. The currently-supported values are:
  188. *
  189. * - 'twitter'
  190. * - 'facebook'
  191. * - 'instagram'
  192. * - 'tiktok'
  193. * - 'youtube'
  194. *
  195. * Pass `false` to skip auto-linking of hashtags.
  196. */
  197. this.hashtag = false; // default value just to get the above doc comment in the ES5 output and documentation generator
  198. /**
  199. * @cfg {String/Boolean} [mention=false]
  200. *
  201. * A string for the service name to have mentions (ex: "@myuser")
  202. * auto-linked to. The currently supported values are:
  203. *
  204. * - 'twitter'
  205. * - 'instagram'
  206. * - 'soundcloud'
  207. * - 'tiktok'
  208. * - 'youtube'
  209. *
  210. * Defaults to `false` to skip auto-linking of mentions.
  211. */
  212. this.mention = false; // default value just to get the above doc comment in the ES5 output and documentation generator
  213. /**
  214. * @cfg {Boolean} [newWindow=true]
  215. *
  216. * `true` if the links should open in a new window, `false` otherwise.
  217. */
  218. this.newWindow = true; // default value just to get the above doc comment in the ES5 output and documentation generator
  219. /**
  220. * @cfg {Boolean/Object} [stripPrefix=true]
  221. *
  222. * `true` if 'http://' (or 'https://') and/or the 'www.' should be stripped
  223. * from the beginning of URL links' text, `false` otherwise. Defaults to
  224. * `true`.
  225. *
  226. * Examples:
  227. *
  228. * stripPrefix: true
  229. *
  230. * // or
  231. *
  232. * stripPrefix: {
  233. * scheme : true,
  234. * www : true
  235. * }
  236. *
  237. * As shown above, this option also accepts an Object form with 2 properties
  238. * to allow for more customization of what exactly is prevented from being
  239. * displayed. Both default to `true`:
  240. *
  241. * @cfg {Boolean} [stripPrefix.scheme] `true` to prevent the scheme part of
  242. * a URL match from being displayed to the user. Example:
  243. * `'http://google.com'` will be displayed as `'google.com'`. `false` to
  244. * not strip the scheme. NOTE: Only an `'http://'` or `'https://'` scheme
  245. * will be removed, so as not to remove a potentially dangerous scheme
  246. * (such as `'file://'` or `'javascript:'`)
  247. * @cfg {Boolean} [stripPrefix.www] www (Boolean): `true` to prevent the
  248. * `'www.'` part of a URL match from being displayed to the user. Ex:
  249. * `'www.google.com'` will be displayed as `'google.com'`. `false` to not
  250. * strip the `'www'`.
  251. */
  252. this.stripPrefix = {
  253. scheme: true,
  254. www: true,
  255. }; // default value just to get the above doc comment in the ES5 output and documentation generator
  256. /**
  257. * @cfg {Boolean} [stripTrailingSlash=true]
  258. *
  259. * `true` to remove the trailing slash from URL matches, `false` to keep
  260. * the trailing slash.
  261. *
  262. * Example when `true`: `http://google.com/` will be displayed as
  263. * `http://google.com`.
  264. */
  265. this.stripTrailingSlash = true; // default value just to get the above doc comment in the ES5 output and documentation generator
  266. /**
  267. * @cfg {Boolean} [decodePercentEncoding=true]
  268. *
  269. * `true` to decode percent-encoded characters in URL matches, `false` to keep
  270. * the percent-encoded characters.
  271. *
  272. * Example when `true`: `https://en.wikipedia.org/wiki/San_Jos%C3%A9` will
  273. * be displayed as `https://en.wikipedia.org/wiki/San_José`.
  274. */
  275. this.decodePercentEncoding = true; // default value just to get the above doc comment in the ES5 output and documentation generator
  276. /**
  277. * @cfg {Number/Object} [truncate=0]
  278. *
  279. * ## Number Form
  280. *
  281. * A number for how many characters matched text should be truncated to
  282. * inside the text of a link. If the matched text is over this number of
  283. * characters, it will be truncated to this length by adding a two period
  284. * ellipsis ('..') to the end of the string.
  285. *
  286. * For example: A url like 'http://www.yahoo.com/some/long/path/to/a/file'
  287. * truncated to 25 characters might look something like this:
  288. * 'yahoo.com/some/long/pat..'
  289. *
  290. * Example Usage:
  291. *
  292. * truncate: 25
  293. *
  294. *
  295. * Defaults to `0` for "no truncation."
  296. *
  297. *
  298. * ## Object Form
  299. *
  300. * An Object may also be provided with two properties: `length` (Number) and
  301. * `location` (String). `location` may be one of the following: 'end'
  302. * (default), 'middle', or 'smart'.
  303. *
  304. * Example Usage:
  305. *
  306. * truncate: { length: 25, location: 'middle' }
  307. *
  308. * @cfg {Number} [truncate.length=0] How many characters to allow before
  309. * truncation will occur. Defaults to `0` for "no truncation."
  310. * @cfg {"end"/"middle"/"smart"} [truncate.location="end"]
  311. *
  312. * - 'end' (default): will truncate up to the number of characters, and then
  313. * add an ellipsis at the end. Ex: 'yahoo.com/some/long/pat..'
  314. * - 'middle': will truncate and add the ellipsis in the middle. Ex:
  315. * 'yahoo.com/s..th/to/a/file'
  316. * - 'smart': for URLs where the algorithm attempts to strip out unnecessary
  317. * parts first (such as the 'www.', then URL scheme, hash, etc.),
  318. * attempting to make the URL human-readable before looking for a good
  319. * point to insert the ellipsis if it is still too long. Ex:
  320. * 'yahoo.com/some..to/a/file'. For more details, see
  321. * {@link Autolinker.truncate.TruncateSmart}.
  322. */
  323. this.truncate = {
  324. length: 0,
  325. location: 'end',
  326. }; // default value just to get the above doc comment in the ES5 output and documentation generator
  327. /**
  328. * @cfg {String} className
  329. *
  330. * A CSS class name to add to the generated links. This class will be added
  331. * to all links, as well as this class plus match suffixes for styling
  332. * url/email/phone/hashtag/mention links differently.
  333. *
  334. * For example, if this config is provided as "myLink", then:
  335. *
  336. * - URL links will have the CSS classes: "myLink myLink-url"
  337. * - Email links will have the CSS classes: "myLink myLink-email", and
  338. * - Phone links will have the CSS classes: "myLink myLink-phone"
  339. * - Hashtag links will have the CSS classes: "myLink myLink-hashtag"
  340. * - Mention links will have the CSS classes: "myLink myLink-mention myLink-[type]"
  341. * where [type] is either "instagram", "twitter" or "soundcloud"
  342. */
  343. this.className = ''; // default value just to get the above doc comment in the ES5 output and documentation generator
  344. /**
  345. * @cfg {Function} replaceFn
  346. *
  347. * A function to individually process each match found in the input string.
  348. *
  349. * See the class's description for usage.
  350. *
  351. * The `replaceFn` can be called with a different context object (`this`
  352. * reference) using the {@link #context} cfg.
  353. *
  354. * This function is called with the following parameter:
  355. *
  356. * @cfg {Autolinker.match.Match} replaceFn.match The Match instance which
  357. * can be used to retrieve information about the match that the `replaceFn`
  358. * is currently processing. See {@link Autolinker.match.Match} subclasses
  359. * for details.
  360. */
  361. this.replaceFn = null; // default value just to get the above doc comment in the ES5 output and documentation generator
  362. /**
  363. * @cfg {Object} context
  364. *
  365. * The context object (`this` reference) to call the `replaceFn` with.
  366. *
  367. * Defaults to this Autolinker instance.
  368. */
  369. this.context = undefined; // default value just to get the above doc comment in the ES5 output and documentation generator
  370. /**
  371. * @cfg {Boolean} [sanitizeHtml=false]
  372. *
  373. * `true` to HTML-encode the start and end brackets of existing HTML tags found
  374. * in the input string. This will escape `<` and `>` characters to `&lt;` and
  375. * `&gt;`, respectively.
  376. *
  377. * Setting this to `true` will prevent XSS (Cross-site Scripting) attacks,
  378. * but will remove the significance of existing HTML tags in the input string. If
  379. * you would like to maintain the significance of existing HTML tags while also
  380. * making the output HTML string safe, leave this option as `false` and use a
  381. * tool like https://github.com/cure53/DOMPurify (or others) on the input string
  382. * before running Autolinker.
  383. */
  384. this.sanitizeHtml = false; // default value just to get the above doc comment in the ES5 output and documentation generator
  385. /**
  386. * @private
  387. * @property {Autolinker.AnchorTagBuilder} tagBuilder
  388. *
  389. * The AnchorTagBuilder instance used to build match replacement anchor tags.
  390. * Note: this is lazily instantiated in the {@link #getTagBuilder} method.
  391. */
  392. this.tagBuilder = null;
  393. // Note: when `this.something` is used in the rhs of these assignments,
  394. // it refers to the default values set above the constructor
  395. this.urls = normalizeUrlsCfg(cfg.urls);
  396. this.email = isBoolean(cfg.email) ? cfg.email : this.email;
  397. this.phone = isBoolean(cfg.phone) ? cfg.phone : this.phone;
  398. this.hashtag = cfg.hashtag || this.hashtag;
  399. this.mention = cfg.mention || this.mention;
  400. this.newWindow = isBoolean(cfg.newWindow) ? cfg.newWindow : this.newWindow;
  401. this.stripPrefix = normalizeStripPrefixCfg(cfg.stripPrefix);
  402. this.stripTrailingSlash = isBoolean(cfg.stripTrailingSlash)
  403. ? cfg.stripTrailingSlash
  404. : this.stripTrailingSlash;
  405. this.decodePercentEncoding = isBoolean(cfg.decodePercentEncoding)
  406. ? cfg.decodePercentEncoding
  407. : this.decodePercentEncoding;
  408. this.sanitizeHtml = cfg.sanitizeHtml || false;
  409. // Validate the value of the `mention` cfg
  410. var mention = this.mention;
  411. if (mention !== false && mentionServices.indexOf(mention) === -1) {
  412. throw new Error("invalid `mention` cfg '".concat(mention, "' - see docs"));
  413. }
  414. // Validate the value of the `hashtag` cfg
  415. var hashtag = this.hashtag;
  416. if (hashtag !== false && hashtagServices.indexOf(hashtag) === -1) {
  417. throw new Error("invalid `hashtag` cfg '".concat(hashtag, "' - see docs"));
  418. }
  419. this.truncate = normalizeTruncateCfg(cfg.truncate);
  420. this.className = cfg.className || this.className;
  421. this.replaceFn = cfg.replaceFn || this.replaceFn;
  422. this.context = cfg.context || this;
  423. }
  424. /**
  425. * Automatically links URLs, Email addresses, Phone Numbers, Twitter handles,
  426. * Hashtags, and Mentions found in the given chunk of HTML. Does not link URLs
  427. * found within HTML tags.
  428. *
  429. * For instance, if given the text: `You should go to http://www.yahoo.com`,
  430. * then the result will be `You should go to &lt;a href="http://www.yahoo.com"&gt;http://www.yahoo.com&lt;/a&gt;`
  431. *
  432. * Example:
  433. *
  434. * var linkedText = Autolinker.link( "Go to google.com", { newWindow: false } );
  435. * // Produces: "Go to <a href="http://google.com">google.com</a>"
  436. *
  437. * @static
  438. * @param {String} textOrHtml The HTML or text to find matches within (depending
  439. * on if the {@link #urls}, {@link #email}, {@link #phone}, {@link #mention},
  440. * {@link #hashtag}, and {@link #mention} options are enabled).
  441. * @param {Object} [options] Any of the configuration options for the Autolinker
  442. * class, specified in an Object (map). See the class description for an
  443. * example call.
  444. * @return {String} The HTML text, with matches automatically linked.
  445. */
  446. Autolinker.link = function (textOrHtml, options) {
  447. var autolinker = new Autolinker(options);
  448. return autolinker.link(textOrHtml);
  449. };
  450. /**
  451. * Parses the input `textOrHtml` looking for URLs, email addresses, phone
  452. * numbers, username handles, and hashtags (depending on the configuration
  453. * of the Autolinker instance), and returns an array of {@link Autolinker.match.Match}
  454. * objects describing those matches (without making any replacements).
  455. *
  456. * Note that if parsing multiple pieces of text, it is slightly more efficient
  457. * to create an Autolinker instance, and use the instance-level {@link #parse}
  458. * method.
  459. *
  460. * Example:
  461. *
  462. * var matches = Autolinker.parse("Hello google.com, I am asdf@asdf.com", {
  463. * urls: true,
  464. * email: true
  465. * });
  466. *
  467. * console.log(matches.length); // 2
  468. * console.log(matches[0].getType()); // 'url'
  469. * console.log(matches[0].getUrl()); // 'google.com'
  470. * console.log(matches[1].getType()); // 'email'
  471. * console.log(matches[1].getEmail()); // 'asdf@asdf.com'
  472. *
  473. * @static
  474. * @param {String} textOrHtml The HTML or text to find matches within
  475. * (depending on if the {@link #urls}, {@link #email}, {@link #phone},
  476. * {@link #hashtag}, and {@link #mention} options are enabled).
  477. * @param {Object} [options] Any of the configuration options for the Autolinker
  478. * class, specified in an Object (map). See the class description for an
  479. * example call.
  480. * @return {Autolinker.match.Match[]} The array of Matches found in the
  481. * given input `textOrHtml`.
  482. */
  483. Autolinker.parse = function (textOrHtml, options) {
  484. var autolinker = new Autolinker(options);
  485. return autolinker.parse(textOrHtml);
  486. };
  487. /**
  488. * Parses the input `textOrHtml` looking for URLs, email addresses, phone
  489. * numbers, username handles, and hashtags (depending on the configuration
  490. * of the Autolinker instance), and returns an array of {@link Autolinker.match.Match}
  491. * objects describing those matches (without making any replacements).
  492. *
  493. * This method is used by the {@link #link} method, but can also be used to
  494. * simply do parsing of the input in order to discover what kinds of links
  495. * there are and how many.
  496. *
  497. * Example usage:
  498. *
  499. * var autolinker = new Autolinker( {
  500. * urls: true,
  501. * email: true
  502. * } );
  503. *
  504. * var matches = autolinker.parse( "Hello google.com, I am asdf@asdf.com" );
  505. *
  506. * console.log( matches.length ); // 2
  507. * console.log( matches[ 0 ].getType() ); // 'url'
  508. * console.log( matches[ 0 ].getUrl() ); // 'google.com'
  509. * console.log( matches[ 1 ].getType() ); // 'email'
  510. * console.log( matches[ 1 ].getEmail() ); // 'asdf@asdf.com'
  511. *
  512. * @param {String} textOrHtml The HTML or text to find matches within
  513. * (depending on if the {@link #urls}, {@link #email}, {@link #phone},
  514. * {@link #hashtag}, and {@link #mention} options are enabled).
  515. * @return {Autolinker.match.Match[]} The array of Matches found in the
  516. * given input `textOrHtml`.
  517. */
  518. Autolinker.prototype.parse = function (textOrHtml) {
  519. var _this = this;
  520. var skipTagNames = ['a', 'style', 'script'];
  521. var skipTagsStackCount = 0; // used to only Autolink text outside of anchor/script/style tags. We don't want to autolink something that is already linked inside of an <a> tag, for instance
  522. var matches = [];
  523. // Find all matches within the `textOrHtml` (but not matches that are
  524. // already nested within <a>, <style> and <script> tags)
  525. parseHtml(textOrHtml, {
  526. onOpenTag: function (tagName) {
  527. if (skipTagNames.indexOf(tagName) >= 0) {
  528. skipTagsStackCount++;
  529. }
  530. },
  531. onText: function (text, offset) {
  532. // Only process text nodes that are not within an <a>, <style> or <script> tag
  533. if (skipTagsStackCount === 0) {
  534. // "Walk around" common HTML entities. An '&nbsp;' (for example)
  535. // could be at the end of a URL, but we don't want to
  536. // include the trailing '&' in the URL. See issue #76
  537. // TODO: Handle HTML entities separately in parseHtml() and
  538. // don't emit them as "text" except for &amp; entities
  539. var htmlCharacterEntitiesRegex = /(&nbsp;|&#160;|&lt;|&#60;|&gt;|&#62;|&quot;|&#34;|&#39;)/gi; // NOTE: capturing group is significant to include the split characters in the .split() call below
  540. var textSplit = text.split(htmlCharacterEntitiesRegex);
  541. var currentOffset_1 = offset;
  542. textSplit.forEach(function (splitText, i) {
  543. // even number matches are text, odd numbers are html entities
  544. if (i % 2 === 0) {
  545. var textNodeMatches = _this.parseText(splitText, currentOffset_1);
  546. matches.push.apply(matches, __spreadArray([], __read(textNodeMatches), false));
  547. }
  548. currentOffset_1 += splitText.length;
  549. });
  550. }
  551. },
  552. onCloseTag: function (tagName) {
  553. if (skipTagNames.indexOf(tagName) >= 0) {
  554. skipTagsStackCount = Math.max(skipTagsStackCount - 1, 0); // attempt to handle extraneous </a> tags by making sure the stack count never goes below 0
  555. }
  556. },
  557. onComment: function ( /*_offset: number*/) { }, // no need to process comment nodes
  558. onDoctype: function ( /*_offset: number*/) { }, // no need to process doctype nodes
  559. });
  560. // After we have found all matches, remove subsequent matches that
  561. // overlap with a previous match. This can happen for instance with an
  562. // email address where the local-part of the email is also a top-level
  563. // domain, such as in "google.com@aaa.com". In this case, the entire
  564. // email address should be linked rather than just the 'google.com'
  565. // part.
  566. matches = this.compactMatches(matches);
  567. // And finally, remove matches for match types that have been turned
  568. // off. We needed to have all match types turned on initially so that
  569. // things like hashtags could be filtered out if they were really just
  570. // part of a URL match (for instance, as a named anchor).
  571. matches = this.removeUnwantedMatches(matches);
  572. return matches;
  573. };
  574. /**
  575. * After we have found all matches, we need to remove matches that overlap
  576. * with a previous match. This can happen for instance with an
  577. * email address where the local-part of the email is also a top-level
  578. * domain, such as in "google.com@aaa.com". In this case, the entire email
  579. * address should be linked rather than just the 'google.com' part.
  580. *
  581. * @private
  582. * @param {Autolinker.match.Match[]} matches
  583. * @return {Autolinker.match.Match[]}
  584. */
  585. Autolinker.prototype.compactMatches = function (matches) {
  586. // First, the matches need to be sorted in order of offset in the input
  587. // string
  588. matches.sort(byMatchOffset);
  589. var i = 0;
  590. while (i < matches.length - 1) {
  591. var match = matches[i];
  592. var offset = match.getOffset();
  593. var matchedTextLength = match.getMatchedText().length;
  594. if (i + 1 < matches.length) {
  595. // Remove subsequent matches that equal offset with current match
  596. // This can happen when matching the text "google.com@aaa.com"
  597. // where we have both a URL ('google.com') and an email. We
  598. // should only keep the email match in this case.
  599. if (matches[i + 1].getOffset() === offset) {
  600. // Remove the shorter match
  601. var removeIdx = matches[i + 1].getMatchedText().length > matchedTextLength ? i : i + 1;
  602. matches.splice(removeIdx, 1);
  603. continue;
  604. }
  605. // Remove subsequent matches that overlap with the current match
  606. //
  607. // NOTE: This was a fundamental snippet of the Autolinker.js v3
  608. // algorithm where we had multiple regular expressions searching
  609. // the input string for matches. The regexes would sometimes
  610. // overlap such as in the case of "google.com/#link", where we
  611. // would have both a URL match and a hashtag match.
  612. //
  613. // However, the Autolinker.js v4 algorithm uses a state machine
  614. // parser and knows that the '#link' part of 'google.com/#link'
  615. // is part of the URL that precedes it, so we don't need this
  616. // piece of code any more. Keeping it here commented for now in
  617. // case we need to put it back at some point, but none of the
  618. // test cases are currently able to trigger the need for it.
  619. // const endIdx = offset + matchedTextLength;
  620. // if (matches[i + 1].getOffset() < endIdx) {
  621. // matches.splice(i + 1, 1);
  622. // continue;
  623. // }
  624. }
  625. i++;
  626. }
  627. return matches;
  628. };
  629. /**
  630. * Removes matches for matchers that were turned off in the options. For
  631. * example, if {@link #hashtag hashtags} were not to be matched, we'll
  632. * remove them from the `matches` array here.
  633. *
  634. * Note: we *must* use all Matchers on the input string, and then filter
  635. * them out later. For example, if the options were `{ url: false, hashtag: true }`,
  636. * we wouldn't want to match the text '#link' as a HashTag inside of the text
  637. * 'google.com/#link'. The way the algorithm works is that we match the full
  638. * URL first (which prevents the accidental HashTag match), and then we'll
  639. * simply throw away the URL match.
  640. *
  641. * @private
  642. * @param {Autolinker.match.Match[]} matches The array of matches to remove
  643. * the unwanted matches from. Note: this array is mutated for the
  644. * removals.
  645. * @return {Autolinker.match.Match[]} The mutated input `matches` array.
  646. */
  647. Autolinker.prototype.removeUnwantedMatches = function (matches) {
  648. if (!this.hashtag)
  649. removeWithPredicate(matches, function (match) {
  650. return match.getType() === 'hashtag';
  651. });
  652. if (!this.email)
  653. removeWithPredicate(matches, function (match) {
  654. return match.getType() === 'email';
  655. });
  656. if (!this.phone)
  657. removeWithPredicate(matches, function (match) {
  658. return match.getType() === 'phone';
  659. });
  660. if (!this.mention)
  661. removeWithPredicate(matches, function (match) {
  662. return match.getType() === 'mention';
  663. });
  664. if (!this.urls.schemeMatches) {
  665. removeWithPredicate(matches, function (m) {
  666. return m.getType() === 'url' && m.getUrlMatchType() === 'scheme';
  667. });
  668. }
  669. if (!this.urls.tldMatches) {
  670. removeWithPredicate(matches, function (m) { return m.getType() === 'url' && m.getUrlMatchType() === 'tld'; });
  671. }
  672. if (!this.urls.ipV4Matches) {
  673. removeWithPredicate(matches, function (m) { return m.getType() === 'url' && m.getUrlMatchType() === 'ipV4'; });
  674. }
  675. return matches;
  676. };
  677. /**
  678. * Parses the input `text` looking for URLs, email addresses, phone
  679. * numbers, username handles, and hashtags (depending on the configuration
  680. * of the Autolinker instance), and returns an array of {@link Autolinker.match.Match}
  681. * objects describing those matches.
  682. *
  683. * This method processes a **non-HTML string**, and is used to parse and
  684. * match within the text nodes of an HTML string. This method is used
  685. * internally by {@link #parse}.
  686. *
  687. * @private
  688. * @param {String} text The text to find matches within (depending on if the
  689. * {@link #urls}, {@link #email}, {@link #phone},
  690. * {@link #hashtag}, and {@link #mention} options are enabled). This must be a non-HTML string.
  691. * @param {Number} [offset=0] The offset of the text node within the
  692. * original string. This is used when parsing with the {@link #parse}
  693. * method to generate correct offsets within the {@link Autolinker.match.Match}
  694. * instances, but may be omitted if calling this method publicly.
  695. * @return {Autolinker.match.Match[]} The array of Matches found in the
  696. * given input `text`.
  697. */
  698. Autolinker.prototype.parseText = function (text, offset) {
  699. offset = offset || 0;
  700. var matches = parseMatches(text, {
  701. tagBuilder: this.getTagBuilder(),
  702. stripPrefix: this.stripPrefix,
  703. stripTrailingSlash: this.stripTrailingSlash,
  704. decodePercentEncoding: this.decodePercentEncoding,
  705. hashtagServiceName: this.hashtag,
  706. mentionServiceName: this.mention || 'twitter',
  707. });
  708. // Correct the offset of each of the matches. They are originally
  709. // the offset of the match within the provided text node, but we
  710. // need to correct them to be relative to the original HTML input
  711. // string (i.e. the one provided to #parse).
  712. for (var i = 0, numTextMatches = matches.length; i < numTextMatches; i++) {
  713. matches[i].setOffset(offset + matches[i].getOffset());
  714. }
  715. return matches;
  716. };
  717. /**
  718. * Automatically links URLs, Email addresses, Phone numbers, Hashtags,
  719. * and Mentions (Twitter, Instagram, Soundcloud) found in the given chunk of HTML. Does not link
  720. * URLs found within HTML tags.
  721. *
  722. * For instance, if given the text: `You should go to http://www.yahoo.com`,
  723. * then the result will be `You should go to
  724. * &lt;a href="http://www.yahoo.com"&gt;http://www.yahoo.com&lt;/a&gt;`
  725. *
  726. * This method finds the text around any HTML elements in the input
  727. * `textOrHtml`, which will be the text that is processed. Any original HTML
  728. * elements will be left as-is, as well as the text that is already wrapped
  729. * in anchor (&lt;a&gt;) tags.
  730. *
  731. * @param {String} textOrHtml The HTML or text to autolink matches within
  732. * (depending on if the {@link #urls}, {@link #email}, {@link #phone}, {@link #hashtag}, and {@link #mention} options are enabled).
  733. * @return {String} The HTML, with matches automatically linked.
  734. */
  735. Autolinker.prototype.link = function (textOrHtml) {
  736. if (!textOrHtml) {
  737. return '';
  738. } // handle `null` and `undefined` (for JavaScript users that don't have TypeScript support), and nothing to do with an empty string too
  739. /* We would want to sanitize the start and end characters of a tag
  740. * before processing the string in order to avoid an XSS scenario.
  741. * This behaviour can be changed by toggling the sanitizeHtml option.
  742. */
  743. if (this.sanitizeHtml) {
  744. textOrHtml = textOrHtml.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  745. }
  746. var matches = this.parse(textOrHtml);
  747. var newHtml = new Array(matches.length * 2 + 1);
  748. var lastIndex = 0;
  749. for (var i = 0, len = matches.length; i < len; i++) {
  750. var match = matches[i];
  751. newHtml.push(textOrHtml.substring(lastIndex, match.getOffset()));
  752. newHtml.push(this.createMatchReturnVal(match));
  753. lastIndex = match.getOffset() + match.getMatchedText().length;
  754. }
  755. newHtml.push(textOrHtml.substring(lastIndex)); // handle the text after the last match
  756. return newHtml.join('');
  757. };
  758. /**
  759. * Creates the return string value for a given match in the input string.
  760. *
  761. * This method handles the {@link #replaceFn}, if one was provided.
  762. *
  763. * @private
  764. * @param {Autolinker.match.Match} match The Match object that represents
  765. * the match.
  766. * @return {String} The string that the `match` should be replaced with.
  767. * This is usually the anchor tag string, but may be the `matchStr` itself
  768. * if the match is not to be replaced.
  769. */
  770. Autolinker.prototype.createMatchReturnVal = function (match) {
  771. // Handle a custom `replaceFn` being provided
  772. var replaceFnResult;
  773. if (this.replaceFn) {
  774. replaceFnResult = this.replaceFn.call(this.context, match); // Autolinker instance is the context
  775. }
  776. if (typeof replaceFnResult === 'string') {
  777. return replaceFnResult; // `replaceFn` returned a string, use that
  778. }
  779. else if (replaceFnResult === false) {
  780. return match.getMatchedText(); // no replacement for the match
  781. }
  782. else if (replaceFnResult instanceof HtmlTag) {
  783. return replaceFnResult.toAnchorString();
  784. }
  785. else {
  786. // replaceFnResult === true, or no/unknown return value from function
  787. // Perform Autolinker's default anchor tag generation
  788. var anchorTag = match.buildTag(); // returns an Autolinker.HtmlTag instance
  789. return anchorTag.toAnchorString();
  790. }
  791. };
  792. /**
  793. * Returns the {@link #tagBuilder} instance for this Autolinker instance,
  794. * lazily instantiating it if it does not yet exist.
  795. *
  796. * @private
  797. * @return {Autolinker.AnchorTagBuilder}
  798. */
  799. Autolinker.prototype.getTagBuilder = function () {
  800. var tagBuilder = this.tagBuilder;
  801. if (!tagBuilder) {
  802. tagBuilder = this.tagBuilder = new AnchorTagBuilder({
  803. newWindow: this.newWindow,
  804. truncate: this.truncate,
  805. className: this.className,
  806. });
  807. }
  808. return tagBuilder;
  809. };
  810. // NOTE: must be 'export default' here for UMD module
  811. /**
  812. * @static
  813. * @property {String} version
  814. *
  815. * The Autolinker version number in the form major.minor.patch
  816. *
  817. * Ex: 3.15.0
  818. */
  819. Autolinker.version = version;
  820. return Autolinker;
  821. }());
  822. export default Autolinker;
  823. /**
  824. * Normalizes the {@link #urls} config into an Object with its 2 properties:
  825. * `schemeMatches` and `tldMatches`, both booleans.
  826. *
  827. * See {@link #urls} config for details.
  828. *
  829. * @private
  830. * @param {Boolean/Object} urls
  831. * @return {Object}
  832. */
  833. function normalizeUrlsCfg(urls) {
  834. if (urls == null)
  835. urls = true; // default to `true`
  836. if (isBoolean(urls)) {
  837. return { schemeMatches: urls, tldMatches: urls, ipV4Matches: urls };
  838. }
  839. else {
  840. // object form
  841. return {
  842. schemeMatches: isBoolean(urls.schemeMatches) ? urls.schemeMatches : true,
  843. tldMatches: isBoolean(urls.tldMatches) ? urls.tldMatches : true,
  844. ipV4Matches: isBoolean(urls.ipV4Matches) ? urls.ipV4Matches : true,
  845. };
  846. }
  847. }
  848. /**
  849. * Normalizes the {@link #stripPrefix} config into an Object with 2
  850. * properties: `scheme`, and `www` - both Booleans.
  851. *
  852. * See {@link #stripPrefix} config for details.
  853. *
  854. * @private
  855. * @param {Boolean/Object} stripPrefix
  856. * @return {Object}
  857. */
  858. function normalizeStripPrefixCfg(stripPrefix) {
  859. if (stripPrefix == null)
  860. stripPrefix = true; // default to `true`
  861. if (isBoolean(stripPrefix)) {
  862. return { scheme: stripPrefix, www: stripPrefix };
  863. }
  864. else {
  865. // object form
  866. return {
  867. scheme: isBoolean(stripPrefix.scheme) ? stripPrefix.scheme : true,
  868. www: isBoolean(stripPrefix.www) ? stripPrefix.www : true,
  869. };
  870. }
  871. }
  872. /**
  873. * Normalizes the {@link #truncate} config into an Object with 2 properties:
  874. * `length` (Number), and `location` (String).
  875. *
  876. * See {@link #truncate} config for details.
  877. *
  878. * @private
  879. * @param {Number/Object} truncate
  880. * @return {Object}
  881. */
  882. function normalizeTruncateCfg(truncate) {
  883. if (typeof truncate === 'number') {
  884. return { length: truncate, location: 'end' };
  885. }
  886. else {
  887. // object, or undefined/null
  888. return __assign({ length: Number.POSITIVE_INFINITY, location: 'end' }, truncate);
  889. }
  890. }
  891. /**
  892. * Helper function for Array.prototype.sort() to sort the Matches by
  893. * their offset in the input string.
  894. */
  895. function byMatchOffset(a, b) {
  896. return a.getOffset() - b.getOffset();
  897. }
  898. //# sourceMappingURL=autolinker.js.map