re.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. module.exports = function (opts) {
  3. var re = {};
  4. // Use direct extract instead of `regenerate` to reduse browserified size
  5. re.src_Any = require('uc.micro/properties/Any/regex').source;
  6. re.src_Cc = require('uc.micro/categories/Cc/regex').source;
  7. re.src_Z = require('uc.micro/categories/Z/regex').source;
  8. re.src_P = require('uc.micro/categories/P/regex').source;
  9. // \p{\Z\P\Cc\CF} (white spaces + control + format + punctuation)
  10. re.src_ZPCc = [ re.src_Z, re.src_P, re.src_Cc ].join('|');
  11. // \p{\Z\Cc} (white spaces + control)
  12. re.src_ZCc = [ re.src_Z, re.src_Cc ].join('|');
  13. // Experimental. List of chars, completely prohibited in links
  14. // because can separate it from other part of text
  15. var text_separators = '[><\uff5c]';
  16. // All possible word characters (everything without punctuation, spaces & controls)
  17. // Defined via punctuation & spaces to save space
  18. // Should be something like \p{\L\N\S\M} (\w but without `_`)
  19. re.src_pseudo_letter = '(?:(?!' + text_separators + '|' + re.src_ZPCc + ')' + re.src_Any + ')';
  20. // The same as abothe but without [0-9]
  21. // var src_pseudo_letter_non_d = '(?:(?![0-9]|' + src_ZPCc + ')' + src_Any + ')';
  22. ////////////////////////////////////////////////////////////////////////////////
  23. re.src_ip4 =
  24. '(?:(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)';
  25. // Prohibit any of "@/[]()" in user/pass to avoid wrong domain fetch.
  26. re.src_auth = '(?:(?:(?!' + re.src_ZCc + '|[@/\\[\\]()]).)+@)?';
  27. re.src_port =
  28. '(?::(?:6(?:[0-4]\\d{3}|5(?:[0-4]\\d{2}|5(?:[0-2]\\d|3[0-5])))|[1-5]?\\d{1,4}))?';
  29. re.src_host_terminator =
  30. '(?=$|' + text_separators + '|' + re.src_ZPCc + ')(?!-|_|:\\d|\\.-|\\.(?!$|' + re.src_ZPCc + '))';
  31. re.src_path =
  32. '(?:' +
  33. '[/?#]' +
  34. '(?:' +
  35. '(?!' + re.src_ZCc + '|' + text_separators + '|[()[\\]{}.,"\'?!\\-;]).|' +
  36. '\\[(?:(?!' + re.src_ZCc + '|\\]).)*\\]|' +
  37. '\\((?:(?!' + re.src_ZCc + '|[)]).)*\\)|' +
  38. '\\{(?:(?!' + re.src_ZCc + '|[}]).)*\\}|' +
  39. '\\"(?:(?!' + re.src_ZCc + '|["]).)+\\"|' +
  40. "\\'(?:(?!" + re.src_ZCc + "|[']).)+\\'|" +
  41. "\\'(?=" + re.src_pseudo_letter + '|[-]).|' + // allow `I'm_king` if no pair found
  42. '\\.{2,}[a-zA-Z0-9%/&]|' + // google has many dots in "google search" links (#66, #81).
  43. // github has ... in commit range links,
  44. // Restrict to
  45. // - english
  46. // - percent-encoded
  47. // - parts of file path
  48. // - params separator
  49. // until more examples found.
  50. '\\.(?!' + re.src_ZCc + '|[.]).|' +
  51. (opts && opts['---'] ?
  52. '\\-(?!--(?:[^-]|$))(?:-*)|' // `---` => long dash, terminate
  53. :
  54. '\\-+|'
  55. ) +
  56. ',(?!' + re.src_ZCc + ').|' + // allow `,,,` in paths
  57. ';(?!' + re.src_ZCc + ').|' + // allow `;` if not followed by space-like char
  58. '\\!+(?!' + re.src_ZCc + '|[!]).|' + // allow `!!!` in paths, but not at the end
  59. '\\?(?!' + re.src_ZCc + '|[?]).' +
  60. ')+' +
  61. '|\\/' +
  62. ')?';
  63. // Allow anything in markdown spec, forbid quote (") at the first position
  64. // because emails enclosed in quotes are far more common
  65. re.src_email_name =
  66. '[\\-;:&=\\+\\$,\\.a-zA-Z0-9_][\\-;:&=\\+\\$,\\"\\.a-zA-Z0-9_]*';
  67. re.src_xn =
  68. 'xn--[a-z0-9\\-]{1,59}';
  69. // More to read about domain names
  70. // http://serverfault.com/questions/638260/
  71. re.src_domain_root =
  72. // Allow letters & digits (http://test1)
  73. '(?:' +
  74. re.src_xn +
  75. '|' +
  76. re.src_pseudo_letter + '{1,63}' +
  77. ')';
  78. re.src_domain =
  79. '(?:' +
  80. re.src_xn +
  81. '|' +
  82. '(?:' + re.src_pseudo_letter + ')' +
  83. '|' +
  84. '(?:' + re.src_pseudo_letter + '(?:-|' + re.src_pseudo_letter + '){0,61}' + re.src_pseudo_letter + ')' +
  85. ')';
  86. re.src_host =
  87. '(?:' +
  88. // Don't need IP check, because digits are already allowed in normal domain names
  89. // src_ip4 +
  90. // '|' +
  91. '(?:(?:(?:' + re.src_domain + ')\\.)*' + re.src_domain/*_root*/ + ')' +
  92. ')';
  93. re.tpl_host_fuzzy =
  94. '(?:' +
  95. re.src_ip4 +
  96. '|' +
  97. '(?:(?:(?:' + re.src_domain + ')\\.)+(?:%TLDS%))' +
  98. ')';
  99. re.tpl_host_no_ip_fuzzy =
  100. '(?:(?:(?:' + re.src_domain + ')\\.)+(?:%TLDS%))';
  101. re.src_host_strict =
  102. re.src_host + re.src_host_terminator;
  103. re.tpl_host_fuzzy_strict =
  104. re.tpl_host_fuzzy + re.src_host_terminator;
  105. re.src_host_port_strict =
  106. re.src_host + re.src_port + re.src_host_terminator;
  107. re.tpl_host_port_fuzzy_strict =
  108. re.tpl_host_fuzzy + re.src_port + re.src_host_terminator;
  109. re.tpl_host_port_no_ip_fuzzy_strict =
  110. re.tpl_host_no_ip_fuzzy + re.src_port + re.src_host_terminator;
  111. ////////////////////////////////////////////////////////////////////////////////
  112. // Main rules
  113. // Rude test fuzzy links by host, for quick deny
  114. re.tpl_host_fuzzy_test =
  115. 'localhost|www\\.|\\.\\d{1,3}\\.|(?:\\.(?:%TLDS%)(?:' + re.src_ZPCc + '|>|$))';
  116. re.tpl_email_fuzzy =
  117. '(^|' + text_separators + '|"|\\(|' + re.src_ZCc + ')' +
  118. '(' + re.src_email_name + '@' + re.tpl_host_fuzzy_strict + ')';
  119. re.tpl_link_fuzzy =
  120. // Fuzzy link can't be prepended with .:/\- and non punctuation.
  121. // but can start with > (markdown blockquote)
  122. '(^|(?![.:/\\-_@])(?:[$+<=>^`|\uff5c]|' + re.src_ZPCc + '))' +
  123. '((?![$+<=>^`|\uff5c])' + re.tpl_host_port_fuzzy_strict + re.src_path + ')';
  124. re.tpl_link_no_ip_fuzzy =
  125. // Fuzzy link can't be prepended with .:/\- and non punctuation.
  126. // but can start with > (markdown blockquote)
  127. '(^|(?![.:/\\-_@])(?:[$+<=>^`|\uff5c]|' + re.src_ZPCc + '))' +
  128. '((?![$+<=>^`|\uff5c])' + re.tpl_host_port_no_ip_fuzzy_strict + re.src_path + ')';
  129. return re;
  130. };