|
1 |
| -window.url = (function() { |
| 1 | +(function() { |
| 2 | + var url = (function() { |
2 | 3 |
|
3 |
| - function _t() { |
4 |
| - return new RegExp(/(.*?)\.?([^\.]*?)\.?(com|net|org|biz|ws|in|me|co\.uk|co|org\.uk|ltd\.uk|plc\.uk|me\.uk|edu|mil|br\.com|cn\.com|eu\.com|hu\.com|no\.com|qc\.com|sa\.com|se\.com|se\.net|us\.com|uy\.com|ac|co\.ac|gv\.ac|or\.ac|ac\.ac|af|am|as|at|ac\.at|co\.at|gv\.at|or\.at|asn\.au|com\.au|edu\.au|org\.au|net\.au|id\.au|be|ac\.be|adm\.br|adv\.br|am\.br|arq\.br|art\.br|bio\.br|cng\.br|cnt\.br|com\.br|ecn\.br|eng\.br|esp\.br|etc\.br|eti\.br|fm\.br|fot\.br|fst\.br|g12\.br|gov\.br|ind\.br|inf\.br|jor\.br|lel\.br|med\.br|mil\.br|net\.br|nom\.br|ntr\.br|odo\.br|org\.br|ppg\.br|pro\.br|psc\.br|psi\.br|rec\.br|slg\.br|tmp\.br|tur\.br|tv\.br|vet\.br|zlg\.br|br|ab\.ca|bc\.ca|mb\.ca|nb\.ca|nf\.ca|ns\.ca|nt\.ca|on\.ca|pe\.ca|qc\.ca|sk\.ca|yk\.ca|ca|cc|ac\.cn|com\.cn|edu\.cn|gov\.cn|org\.cn|bj\.cn|sh\.cn|tj\.cn|cq\.cn|he\.cn|nm\.cn|ln\.cn|jl\.cn|hl\.cn|js\.cn|zj\.cn|ah\.cn|gd\.cn|gx\.cn|hi\.cn|sc\.cn|gz\.cn|yn\.cn|xz\.cn|sn\.cn|gs\.cn|qh\.cn|nx\.cn|xj\.cn|tw\.cn|hk\.cn|mo\.cn|cn|cx|cz|de|dk|fo|com\.ec|tm\.fr|com\.fr|asso\.fr|presse\.fr|fr|gf|gs|co\.il|net\.il|ac\.il|k12\.il|gov\.il|muni\.il|ac\.in|co\.in|org\.in|ernet\.in|gov\.in|net\.in|res\.in|is|it|ac\.jp|co\.jp|go\.jp|or\.jp|ne\.jp|ac\.kr|co\.kr|go\.kr|ne\.kr|nm\.kr|or\.kr|li|lt|lu|asso\.mc|tm\.mc|com\.mm|org\.mm|net\.mm|edu\.mm|gov\.mm|ms|nl|no|nu|pl|ro|org\.ro|store\.ro|tm\.ro|firm\.ro|www\.ro|arts\.ro|rec\.ro|info\.ro|nom\.ro|nt\.ro|se|si|com\.sg|org\.sg|net\.sg|gov\.sg|sk|st|tf|ac\.th|co\.th|go\.th|mi\.th|net\.th|or\.th|tm|to|com\.tr|edu\.tr|gov\.tr|k12\.tr|net\.tr|org\.tr|com\.tw|org\.tw|net\.tw|ac\.uk|uk\.com|uk\.net|gb\.com|gb\.net|vg|sh|kz|ch|info|ua|gov|name|pro|ie|hk|com\.hk|org\.hk|net\.hk|edu\.hk|us|tk|cd|by|ad|lv|eu\.lv|bz|es|jp|cl|ag|mobi|eu|co\.nz|org\.nz|net\.nz|maori\.nz|iwi\.nz|io|la|md|sc|sg|vc|tw|travel|my|se|tv|pt|com\.pt|edu\.pt|asia|fi|com\.ve|net\.ve|fi|org\.ve|web\.ve|info\.ve|co\.ve|tel|im|gr|ru|net\.ru|org\.ru|hr|com\.hr|ly|xyz)$/); |
5 |
| - } |
| 4 | + function _t() { |
| 5 | + return new RegExp(/(.*?)\.?([^\.]*?)\.?(com|net|org|biz|ws|in|me|co\.uk|co|org\.uk|ltd\.uk|plc\.uk|me\.uk|edu|mil|br\.com|cn\.com|eu\.com|hu\.com|no\.com|qc\.com|sa\.com|se\.com|se\.net|us\.com|uy\.com|ac|co\.ac|gv\.ac|or\.ac|ac\.ac|af|am|as|at|ac\.at|co\.at|gv\.at|or\.at|asn\.au|com\.au|edu\.au|org\.au|net\.au|id\.au|be|ac\.be|adm\.br|adv\.br|am\.br|arq\.br|art\.br|bio\.br|cng\.br|cnt\.br|com\.br|ecn\.br|eng\.br|esp\.br|etc\.br|eti\.br|fm\.br|fot\.br|fst\.br|g12\.br|gov\.br|ind\.br|inf\.br|jor\.br|lel\.br|med\.br|mil\.br|net\.br|nom\.br|ntr\.br|odo\.br|org\.br|ppg\.br|pro\.br|psc\.br|psi\.br|rec\.br|slg\.br|tmp\.br|tur\.br|tv\.br|vet\.br|zlg\.br|br|ab\.ca|bc\.ca|mb\.ca|nb\.ca|nf\.ca|ns\.ca|nt\.ca|on\.ca|pe\.ca|qc\.ca|sk\.ca|yk\.ca|ca|cc|ac\.cn|com\.cn|edu\.cn|gov\.cn|org\.cn|bj\.cn|sh\.cn|tj\.cn|cq\.cn|he\.cn|nm\.cn|ln\.cn|jl\.cn|hl\.cn|js\.cn|zj\.cn|ah\.cn|gd\.cn|gx\.cn|hi\.cn|sc\.cn|gz\.cn|yn\.cn|xz\.cn|sn\.cn|gs\.cn|qh\.cn|nx\.cn|xj\.cn|tw\.cn|hk\.cn|mo\.cn|cn|cx|cz|de|dk|fo|com\.ec|tm\.fr|com\.fr|asso\.fr|presse\.fr|fr|gf|gs|co\.il|net\.il|ac\.il|k12\.il|gov\.il|muni\.il|ac\.in|co\.in|org\.in|ernet\.in|gov\.in|net\.in|res\.in|is|it|ac\.jp|co\.jp|go\.jp|or\.jp|ne\.jp|ac\.kr|co\.kr|go\.kr|ne\.kr|nm\.kr|or\.kr|li|lt|lu|asso\.mc|tm\.mc|com\.mm|org\.mm|net\.mm|edu\.mm|gov\.mm|ms|nl|no|nu|pl|ro|org\.ro|store\.ro|tm\.ro|firm\.ro|www\.ro|arts\.ro|rec\.ro|info\.ro|nom\.ro|nt\.ro|se|si|com\.sg|org\.sg|net\.sg|gov\.sg|sk|st|tf|ac\.th|co\.th|go\.th|mi\.th|net\.th|or\.th|tm|to|com\.tr|edu\.tr|gov\.tr|k12\.tr|net\.tr|org\.tr|com\.tw|org\.tw|net\.tw|ac\.uk|uk\.com|uk\.net|gb\.com|gb\.net|vg|sh|kz|ch|info|ua|gov|name|pro|ie|hk|com\.hk|org\.hk|net\.hk|edu\.hk|us|tk|cd|by|ad|lv|eu\.lv|bz|es|jp|cl|ag|mobi|eu|co\.nz|org\.nz|net\.nz|maori\.nz|iwi\.nz|io|la|md|sc|sg|vc|tw|travel|my|se|tv|pt|com\.pt|edu\.pt|asia|fi|com\.ve|net\.ve|fi|org\.ve|web\.ve|info\.ve|co\.ve|tel|im|gr|ru|net\.ru|org\.ru|hr|com\.hr|ly|xyz)$/); |
| 6 | + } |
6 | 7 |
|
7 |
| - function _d(s) { |
8 |
| - return decodeURIComponent(s.replace(/\+/g, ' ')); |
9 |
| - } |
| 8 | + function _d(s) { |
| 9 | + return decodeURIComponent(s.replace(/\+/g, ' ')); |
| 10 | + } |
10 | 11 |
|
11 |
| - function _i(arg, str) { |
12 |
| - var sptr = arg.charAt(0), |
13 |
| - split = str.split(sptr); |
| 12 | + function _i(arg, str) { |
| 13 | + var sptr = arg.charAt(0), |
| 14 | + split = str.split(sptr); |
14 | 15 |
|
15 |
| - if (sptr === arg) { return split; } |
| 16 | + if (sptr === arg) { return split; } |
16 | 17 |
|
17 |
| - arg = parseInt(arg.substring(1), 10); |
| 18 | + arg = parseInt(arg.substring(1), 10); |
18 | 19 |
|
19 |
| - return split[arg < 0 ? split.length + arg : arg - 1]; |
20 |
| - } |
| 20 | + return split[arg < 0 ? split.length + arg : arg - 1]; |
| 21 | + } |
21 | 22 |
|
22 |
| - function _f(arg, str) { |
23 |
| - var sptr = arg.charAt(0), |
24 |
| - split = str.split('&'), |
25 |
| - field = [], |
26 |
| - params = {}, |
27 |
| - tmp = [], |
28 |
| - arg2 = arg.substring(1); |
| 23 | + function _f(arg, str) { |
| 24 | + var sptr = arg.charAt(0), |
| 25 | + split = str.split('&'), |
| 26 | + field = [], |
| 27 | + params = {}, |
| 28 | + tmp = [], |
| 29 | + arg2 = arg.substring(1); |
29 | 30 |
|
30 |
| - for (var i = 0, ii = split.length; i < ii; i++) { |
31 |
| - field = split[i].match(/(.*?)=(.*)/); |
| 31 | + for (var i = 0, ii = split.length; i < ii; i++) { |
| 32 | + field = split[i].match(/(.*?)=(.*)/); |
32 | 33 |
|
33 |
| - // TODO: regex should be able to handle this. |
34 |
| - if ( ! field) { |
35 |
| - field = [split[i], split[i], '']; |
36 |
| - } |
| 34 | + // TODO: regex should be able to handle this. |
| 35 | + if ( ! field) { |
| 36 | + field = [split[i], split[i], '']; |
| 37 | + } |
37 | 38 |
|
38 |
| - if (field[1].replace(/\s/g, '') !== '') { |
39 |
| - field[2] = _d(field[2] || ''); |
| 39 | + if (field[1].replace(/\s/g, '') !== '') { |
| 40 | + field[2] = _d(field[2] || ''); |
40 | 41 |
|
41 |
| - // If we have a match just return it right away. |
42 |
| - if (arg2 === field[1]) { return field[2]; } |
| 42 | + // If we have a match just return it right away. |
| 43 | + if (arg2 === field[1]) { return field[2]; } |
43 | 44 |
|
44 |
| - // Check for array pattern. |
45 |
| - tmp = field[1].match(/(.*)\[([0-9]+)\]/); |
| 45 | + // Check for array pattern. |
| 46 | + tmp = field[1].match(/(.*)\[([0-9]+)\]/); |
46 | 47 |
|
47 |
| - if (tmp) { |
48 |
| - params[tmp[1]] = params[tmp[1]] || []; |
| 48 | + if (tmp) { |
| 49 | + params[tmp[1]] = params[tmp[1]] || []; |
49 | 50 |
|
50 |
| - params[tmp[1]][tmp[2]] = field[2]; |
51 |
| - } |
52 |
| - else { |
53 |
| - params[field[1]] = field[2]; |
| 51 | + params[tmp[1]][tmp[2]] = field[2]; |
| 52 | + } |
| 53 | + else { |
| 54 | + params[field[1]] = field[2]; |
| 55 | + } |
54 | 56 | }
|
55 | 57 | }
|
56 |
| - } |
57 | 58 |
|
58 |
| - if (sptr === arg) { return params; } |
| 59 | + if (sptr === arg) { return params; } |
59 | 60 |
|
60 |
| - return params[arg2]; |
61 |
| - } |
| 61 | + return params[arg2]; |
| 62 | + } |
62 | 63 |
|
63 |
| - return function(arg, url) { |
64 |
| - var _l = {}, tmp, tmp2; |
| 64 | + return function(arg, url) { |
| 65 | + var _l = {}, tmp, tmp2; |
65 | 66 |
|
66 |
| - if (arg === 'tld?') { return _t(); } |
| 67 | + if (arg === 'tld?') { return _t(); } |
67 | 68 |
|
68 |
| - url = url || window.location.toString(); |
| 69 | + url = url || window.location.toString(); |
69 | 70 |
|
70 |
| - if ( ! arg) { return url; } |
| 71 | + if ( ! arg) { return url; } |
71 | 72 |
|
72 |
| - arg = arg.toString(); |
| 73 | + arg = arg.toString(); |
73 | 74 |
|
74 |
| - if (tmp = url.match(/^mailto:([^\/].+)/)) { |
75 |
| - _l.protocol = 'mailto'; |
76 |
| - _l.email = tmp[1]; |
77 |
| - } |
78 |
| - else { |
79 |
| - |
80 |
| - // Ignore Hashbangs. |
81 |
| - if (tmp = url.match(/(.*?)\/#\!(.*)/)) { |
82 |
| - url = tmp[1] + tmp[2]; |
| 75 | + if (tmp = url.match(/^mailto:([^\/].+)/)) { |
| 76 | + _l.protocol = 'mailto'; |
| 77 | + _l.email = tmp[1]; |
83 | 78 | }
|
| 79 | + else { |
84 | 80 |
|
85 |
| - // Hash. |
86 |
| - if (tmp = url.match(/(.*?)#(.*)/)) { |
87 |
| - _l.hash = tmp[2]; |
88 |
| - url = tmp[1]; |
89 |
| - } |
| 81 | + // Ignore Hashbangs. |
| 82 | + if (tmp = url.match(/(.*?)\/#\!(.*)/)) { |
| 83 | + url = tmp[1] + tmp[2]; |
| 84 | + } |
90 | 85 |
|
91 |
| - // Return hash parts. |
92 |
| - if (_l.hash && arg.match(/^#/)) { return _f(arg, _l.hash); } |
| 86 | + // Hash. |
| 87 | + if (tmp = url.match(/(.*?)#(.*)/)) { |
| 88 | + _l.hash = tmp[2]; |
| 89 | + url = tmp[1]; |
| 90 | + } |
93 | 91 |
|
94 |
| - // Query |
95 |
| - if (tmp = url.match(/(.*?)\?(.*)/)) { |
96 |
| - _l.query = tmp[2]; |
97 |
| - url = tmp[1]; |
98 |
| - } |
| 92 | + // Return hash parts. |
| 93 | + if (_l.hash && arg.match(/^#/)) { return _f(arg, _l.hash); } |
99 | 94 |
|
100 |
| - // Return query parts. |
101 |
| - if (_l.query && arg.match(/^\?/)) { return _f(arg, _l.query); } |
| 95 | + // Query |
| 96 | + if (tmp = url.match(/(.*?)\?(.*)/)) { |
| 97 | + _l.query = tmp[2]; |
| 98 | + url = tmp[1]; |
| 99 | + } |
102 | 100 |
|
103 |
| - // Protocol. |
104 |
| - if (tmp = url.match(/(.*?)\:?\/\/(.*)/)) { |
105 |
| - _l.protocol = tmp[1].toLowerCase(); |
106 |
| - url = tmp[2]; |
107 |
| - } |
| 101 | + // Return query parts. |
| 102 | + if (_l.query && arg.match(/^\?/)) { return _f(arg, _l.query); } |
108 | 103 |
|
109 |
| - // Path. |
110 |
| - if (tmp = url.match(/(.*?)(\/.*)/)) { |
111 |
| - _l.path = tmp[2]; |
112 |
| - url = tmp[1]; |
113 |
| - } |
| 104 | + // Protocol. |
| 105 | + if (tmp = url.match(/(.*?)\:?\/\/(.*)/)) { |
| 106 | + _l.protocol = tmp[1].toLowerCase(); |
| 107 | + url = tmp[2]; |
| 108 | + } |
114 | 109 |
|
115 |
| - // Clean up path. |
116 |
| - _l.path = (_l.path || '').replace(/^([^\/])/, '/$1').replace(/\/$/, ''); |
| 110 | + // Path. |
| 111 | + if (tmp = url.match(/(.*?)(\/.*)/)) { |
| 112 | + _l.path = tmp[2]; |
| 113 | + url = tmp[1]; |
| 114 | + } |
117 | 115 |
|
118 |
| - // Return path parts. |
119 |
| - if (arg.match(/^[\-0-9]+$/)) { arg = arg.replace(/^([^\/])/, '/$1'); } |
120 |
| - if (arg.match(/^\//)) { return _i(arg, _l.path.substring(1)); } |
| 116 | + // Clean up path. |
| 117 | + _l.path = (_l.path || '').replace(/^([^\/])/, '/$1').replace(/\/$/, ''); |
121 | 118 |
|
122 |
| - // File. |
123 |
| - tmp = _i('/-1', _l.path.substring(1)); |
| 119 | + // Return path parts. |
| 120 | + if (arg.match(/^[\-0-9]+$/)) { arg = arg.replace(/^([^\/])/, '/$1'); } |
| 121 | + if (arg.match(/^\//)) { return _i(arg, _l.path.substring(1)); } |
124 | 122 |
|
125 |
| - if (tmp && (tmp = tmp.match(/(.*?)\.(.*)/))) { |
126 |
| - _l.file = tmp[0]; |
127 |
| - _l.filename = tmp[1]; |
128 |
| - _l.fileext = tmp[2]; |
129 |
| - } |
| 123 | + // File. |
| 124 | + tmp = _i('/-1', _l.path.substring(1)); |
130 | 125 |
|
131 |
| - // Port. |
132 |
| - if (tmp = url.match(/(.*)\:([0-9]+)$/)) { |
133 |
| - _l.port = tmp[2]; |
134 |
| - url = tmp[1]; |
135 |
| - } |
| 126 | + if (tmp && (tmp = tmp.match(/(.*?)\.(.*)/))) { |
| 127 | + _l.file = tmp[0]; |
| 128 | + _l.filename = tmp[1]; |
| 129 | + _l.fileext = tmp[2]; |
| 130 | + } |
136 | 131 |
|
137 |
| - // Auth. |
138 |
| - if (tmp = url.match(/(.*?)@(.*)/)) { |
139 |
| - _l.auth = tmp[1]; |
140 |
| - url = tmp[2]; |
141 |
| - } |
| 132 | + // Port. |
| 133 | + if (tmp = url.match(/(.*)\:([0-9]+)$/)) { |
| 134 | + _l.port = tmp[2]; |
| 135 | + url = tmp[1]; |
| 136 | + } |
142 | 137 |
|
143 |
| - // User and pass. |
144 |
| - if (_l.auth) { |
145 |
| - tmp = _l.auth.match(/(.*)\:(.*)/); |
| 138 | + // Auth. |
| 139 | + if (tmp = url.match(/(.*?)@(.*)/)) { |
| 140 | + _l.auth = tmp[1]; |
| 141 | + url = tmp[2]; |
| 142 | + } |
146 | 143 |
|
147 |
| - _l.user = tmp ? tmp[1] : _l.auth; |
148 |
| - _l.pass = tmp ? tmp[2] : undefined; |
149 |
| - } |
| 144 | + // User and pass. |
| 145 | + if (_l.auth) { |
| 146 | + tmp = _l.auth.match(/(.*)\:(.*)/); |
150 | 147 |
|
151 |
| - // Hostname. |
152 |
| - _l.hostname = url.toLowerCase(); |
| 148 | + _l.user = tmp ? tmp[1] : _l.auth; |
| 149 | + _l.pass = tmp ? tmp[2] : undefined; |
| 150 | + } |
153 | 151 |
|
154 |
| - // Return hostname parts. |
155 |
| - if (arg.charAt(0) === '.') { return _i(arg, _l.hostname); } |
| 152 | + // Hostname. |
| 153 | + _l.hostname = url.toLowerCase(); |
156 | 154 |
|
157 |
| - // Domain, tld and sub domain. |
158 |
| - if (_t()) { |
159 |
| - tmp = _l.hostname.match(_t()); |
| 155 | + // Return hostname parts. |
| 156 | + if (arg.charAt(0) === '.') { return _i(arg, _l.hostname); } |
160 | 157 |
|
161 |
| - if (tmp) { |
162 |
| - _l.tld = tmp[3]; |
163 |
| - _l.domain = tmp[2] ? tmp[2] + '.' + tmp[3] : undefined; |
164 |
| - _l.sub = tmp[1] || undefined; |
| 158 | + // Domain, tld and sub domain. |
| 159 | + if (_t()) { |
| 160 | + tmp = _l.hostname.match(_t()); |
| 161 | + |
| 162 | + if (tmp) { |
| 163 | + _l.tld = tmp[3]; |
| 164 | + _l.domain = tmp[2] ? tmp[2] + '.' + tmp[3] : undefined; |
| 165 | + _l.sub = tmp[1] || undefined; |
| 166 | + } |
165 | 167 | }
|
| 168 | + |
| 169 | + // Set port and protocol defaults if not set. |
| 170 | + _l.port = _l.port || (_l.protocol === 'https' ? '443' : '80'); |
| 171 | + _l.protocol = _l.protocol || (_l.port === '443' ? 'https' : 'http'); |
166 | 172 | }
|
167 | 173 |
|
168 |
| - // Set port and protocol defaults if not set. |
169 |
| - _l.port = _l.port || (_l.protocol === 'https' ? '443' : '80'); |
170 |
| - _l.protocol = _l.protocol || (_l.port === '443' ? 'https' : 'http'); |
171 |
| - } |
| 174 | + // Return arg. |
| 175 | + if (arg in _l) { return _l[arg]; } |
172 | 176 |
|
173 |
| - // Return arg. |
174 |
| - if (arg in _l) { return _l[arg]; } |
| 177 | + // Return everything. |
| 178 | + if (arg === '{}') { return _l; } |
175 | 179 |
|
176 |
| - // Return everything. |
177 |
| - if (arg === '{}') { return _l; } |
| 180 | + // Default to undefined for no match. |
| 181 | + return undefined; |
| 182 | + }; |
| 183 | + })(); |
178 | 184 |
|
179 |
| - // Default to undefined for no match. |
180 |
| - return undefined; |
181 |
| - }; |
182 |
| -})(); |
| 185 | + if(typeof window.jQuery !== 'undefined') { |
| 186 | + window.jQuery.extend({ |
| 187 | + url: function(arg, url) { return window.url(arg, url); } |
| 188 | + }); |
| 189 | + } |
183 | 190 |
|
184 |
| -if(typeof jQuery !== 'undefined') { |
185 |
| - jQuery.extend({ |
186 |
| - url: function(arg, url) { return window.url(arg, url); } |
187 |
| - }); |
188 |
| -} |
| 191 | + window.url = url; |
| 192 | +})(); |
0 commit comments