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