re.js 6.0 KB

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