Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.regexp");
14
dojo.evalObjPath("dojo.regexp.us", true);
15
dojo.regexp.tld = function (flags) {
16
	flags = (typeof flags == "object") ? flags : {};
17
	if (typeof flags.allowCC != "boolean") {
18
		flags.allowCC = true;
19
	}
20
	if (typeof flags.allowInfra != "boolean") {
21
		flags.allowInfra = true;
22
	}
23
	if (typeof flags.allowGeneric != "boolean") {
24
		flags.allowGeneric = true;
25
	}
26
	var infraRE = "arpa";
27
	var genericRE = "aero|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|xxx|jobs|mobi|post";
28
	var ccRE = "ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|" + "bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|" + "ec|ee|eg|er|eu|es|et|fi|fj|fk|fm|fo|fr|ga|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|" + "gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kr|kw|ky|kz|" + "la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|" + "my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|" + "re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sk|sl|sm|sn|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|" + "tn|to|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw";
29
	var a = [];
30
	if (flags.allowInfra) {
31
		a.push(infraRE);
32
	}
33
	if (flags.allowGeneric) {
34
		a.push(genericRE);
35
	}
36
	if (flags.allowCC) {
37
		a.push(ccRE);
38
	}
39
	var tldRE = "";
40
	if (a.length > 0) {
41
		tldRE = "(" + a.join("|") + ")";
42
	}
43
	return tldRE;
44
};
45
dojo.regexp.ipAddress = function (flags) {
46
	flags = (typeof flags == "object") ? flags : {};
47
	if (typeof flags.allowDottedDecimal != "boolean") {
48
		flags.allowDottedDecimal = true;
49
	}
50
	if (typeof flags.allowDottedHex != "boolean") {
51
		flags.allowDottedHex = true;
52
	}
53
	if (typeof flags.allowDottedOctal != "boolean") {
54
		flags.allowDottedOctal = true;
55
	}
56
	if (typeof flags.allowDecimal != "boolean") {
57
		flags.allowDecimal = true;
58
	}
59
	if (typeof flags.allowHex != "boolean") {
60
		flags.allowHex = true;
61
	}
62
	if (typeof flags.allowIPv6 != "boolean") {
63
		flags.allowIPv6 = true;
64
	}
65
	if (typeof flags.allowHybrid != "boolean") {
66
		flags.allowHybrid = true;
67
	}
68
	var dottedDecimalRE = "((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
69
	var dottedHexRE = "(0[xX]0*[\\da-fA-F]?[\\da-fA-F]\\.){3}0[xX]0*[\\da-fA-F]?[\\da-fA-F]";
70
	var dottedOctalRE = "(0+[0-3][0-7][0-7]\\.){3}0+[0-3][0-7][0-7]";
71
	var decimalRE = "(0|[1-9]\\d{0,8}|[1-3]\\d{9}|4[01]\\d{8}|42[0-8]\\d{7}|429[0-3]\\d{6}|" + "4294[0-8]\\d{5}|42949[0-5]\\d{4}|429496[0-6]\\d{3}|4294967[01]\\d{2}|42949672[0-8]\\d|429496729[0-5])";
72
	var hexRE = "0[xX]0*[\\da-fA-F]{1,8}";
73
	var ipv6RE = "([\\da-fA-F]{1,4}\\:){7}[\\da-fA-F]{1,4}";
74
	var hybridRE = "([\\da-fA-F]{1,4}\\:){6}" + "((\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])";
75
	var a = [];
76
	if (flags.allowDottedDecimal) {
77
		a.push(dottedDecimalRE);
78
	}
79
	if (flags.allowDottedHex) {
80
		a.push(dottedHexRE);
81
	}
82
	if (flags.allowDottedOctal) {
83
		a.push(dottedOctalRE);
84
	}
85
	if (flags.allowDecimal) {
86
		a.push(decimalRE);
87
	}
88
	if (flags.allowHex) {
89
		a.push(hexRE);
90
	}
91
	if (flags.allowIPv6) {
92
		a.push(ipv6RE);
93
	}
94
	if (flags.allowHybrid) {
95
		a.push(hybridRE);
96
	}
97
	var ipAddressRE = "";
98
	if (a.length > 0) {
99
		ipAddressRE = "(" + a.join("|") + ")";
100
	}
101
	return ipAddressRE;
102
};
103
dojo.regexp.host = function (flags) {
104
	flags = (typeof flags == "object") ? flags : {};
105
	if (typeof flags.allowIP != "boolean") {
106
		flags.allowIP = true;
107
	}
108
	if (typeof flags.allowLocal != "boolean") {
109
		flags.allowLocal = false;
110
	}
111
	if (typeof flags.allowPort != "boolean") {
112
		flags.allowPort = true;
113
	}
114
	var domainNameRE = "([0-9a-zA-Z]([-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?\\.)+" + dojo.regexp.tld(flags);
115
	var portRE = (flags.allowPort) ? "(\\:" + dojo.regexp.integer({signed:false}) + ")?" : "";
116
	var hostNameRE = domainNameRE;
117
	if (flags.allowIP) {
118
		hostNameRE += "|" + dojo.regexp.ipAddress(flags);
119
	}
120
	if (flags.allowLocal) {
121
		hostNameRE += "|localhost";
122
	}
123
	return "(" + hostNameRE + ")" + portRE;
124
};
125
dojo.regexp.url = function (flags) {
126
	flags = (typeof flags == "object") ? flags : {};
127
	if (typeof flags.scheme == "undefined") {
128
		flags.scheme = [true, false];
129
	}
130
	var protocolRE = dojo.regexp.buildGroupRE(flags.scheme, function (q) {
131
		if (q) {
132
			return "(https?|ftps?)\\://";
133
		}
134
		return "";
135
	});
136
	var pathRE = "(/([^?#\\s/]+/)*)?([^?#\\s/]+(\\?[^?#\\s/]*)?(#[A-Za-z][\\w.:-]*)?)?";
137
	return protocolRE + dojo.regexp.host(flags) + pathRE;
138
};
139
dojo.regexp.emailAddress = function (flags) {
140
	flags = (typeof flags == "object") ? flags : {};
141
	if (typeof flags.allowCruft != "boolean") {
142
		flags.allowCruft = false;
143
	}
144
	flags.allowPort = false;
145
	var usernameRE = "([\\da-z]+[-._+&'])*[\\da-z]+";
146
	var emailAddressRE = usernameRE + "@" + dojo.regexp.host(flags);
147
	if (flags.allowCruft) {
148
		emailAddressRE = "<?(mailto\\:)?" + emailAddressRE + ">?";
149
	}
150
	return emailAddressRE;
151
};
152
dojo.regexp.emailAddressList = function (flags) {
153
	flags = (typeof flags == "object") ? flags : {};
154
	if (typeof flags.listSeparator != "string") {
155
		flags.listSeparator = "\\s;,";
156
	}
157
	var emailAddressRE = dojo.regexp.emailAddress(flags);
158
	var emailAddressListRE = "(" + emailAddressRE + "\\s*[" + flags.listSeparator + "]\\s*)*" + emailAddressRE + "\\s*[" + flags.listSeparator + "]?\\s*";
159
	return emailAddressListRE;
160
};
161
dojo.regexp.integer = function (flags) {
162
	flags = (typeof flags == "object") ? flags : {};
163
	if (typeof flags.signed == "undefined") {
164
		flags.signed = [true, false];
165
	}
166
	if (typeof flags.separator == "undefined") {
167
		flags.separator = "";
168
	} else {
169
		if (typeof flags.groupSize == "undefined") {
170
			flags.groupSize = 3;
171
		}
172
	}
173
	var signRE = dojo.regexp.buildGroupRE(flags.signed, function (q) {
174
		return q ? "[-+]" : "";
175
	});
176
	var numberRE = dojo.regexp.buildGroupRE(flags.separator, function (sep) {
177
		if (sep == "") {
178
			return "(0|[1-9]\\d*)";
179
		}
180
		var grp = flags.groupSize, grp2 = flags.groupSize2;
181
		if (typeof grp2 != "undefined") {
182
			var grp2RE = "(0|[1-9]\\d{0," + (grp2 - 1) + "}([" + sep + "]\\d{" + grp2 + "})*[" + sep + "]\\d{" + grp + "})";
183
			return ((grp - grp2) > 0) ? "(" + grp2RE + "|(0|[1-9]\\d{0," + (grp - 1) + "}))" : grp2RE;
184
		}
185
		return "(0|[1-9]\\d{0," + (grp - 1) + "}([" + sep + "]\\d{" + grp + "})*)";
186
	});
187
	return signRE + numberRE;
188
};
189
dojo.regexp.realNumber = function (flags) {
190
	flags = (typeof flags == "object") ? flags : {};
191
	if (typeof flags.places != "number") {
192
		flags.places = Infinity;
193
	}
194
	if (typeof flags.decimal != "string") {
195
		flags.decimal = ".";
196
	}
197
	if (typeof flags.fractional == "undefined") {
198
		flags.fractional = [true, false];
199
	}
200
	if (typeof flags.exponent == "undefined") {
201
		flags.exponent = [true, false];
202
	}
203
	if (typeof flags.eSigned == "undefined") {
204
		flags.eSigned = [true, false];
205
	}
206
	var integerRE = dojo.regexp.integer(flags);
207
	var decimalRE = dojo.regexp.buildGroupRE(flags.fractional, function (q) {
208
		var re = "";
209
		if (q && (flags.places > 0)) {
210
			re = "\\" + flags.decimal;
211
			if (flags.places == Infinity) {
212
				re = "(" + re + "\\d+)?";
213
			} else {
214
				re = re + "\\d{" + flags.places + "}";
215
			}
216
		}
217
		return re;
218
	});
219
	var exponentRE = dojo.regexp.buildGroupRE(flags.exponent, function (q) {
220
		if (q) {
221
			return "([eE]" + dojo.regexp.integer({signed:flags.eSigned}) + ")";
222
		}
223
		return "";
224
	});
225
	return integerRE + decimalRE + exponentRE;
226
};
227
dojo.regexp.currency = function (flags) {
228
	flags = (typeof flags == "object") ? flags : {};
229
	if (typeof flags.signed == "undefined") {
230
		flags.signed = [true, false];
231
	}
232
	if (typeof flags.symbol == "undefined") {
233
		flags.symbol = "$";
234
	}
235
	if (typeof flags.placement != "string") {
236
		flags.placement = "before";
237
	}
238
	if (typeof flags.signPlacement != "string") {
239
		flags.signPlacement = "before";
240
	}
241
	if (typeof flags.separator == "undefined") {
242
		flags.separator = ",";
243
	}
244
	if (typeof flags.fractional == "undefined" && typeof flags.cents != "undefined") {
245
		dojo.deprecated("dojo.regexp.currency: flags.cents", "use flags.fractional instead", "0.5");
246
		flags.fractional = flags.cents;
247
	}
248
	if (typeof flags.decimal != "string") {
249
		flags.decimal = ".";
250
	}
251
	var signRE = dojo.regexp.buildGroupRE(flags.signed, function (q) {
252
		if (q) {
253
			return "[-+]";
254
		}
255
		return "";
256
	});
257
	var symbolRE = dojo.regexp.buildGroupRE(flags.symbol, function (symbol) {
258
		return "\\s?" + symbol.replace(/([.$?*!=:|\\\/^])/g, "\\$1") + "\\s?";
259
	});
260
	switch (flags.signPlacement) {
261
	  case "before":
262
		symbolRE = signRE + symbolRE;
263
		break;
264
	  case "after":
265
		symbolRE = symbolRE + signRE;
266
		break;
267
	}
268
	var flagsCopy = flags;
269
	flagsCopy.signed = false;
270
	flagsCopy.exponent = false;
271
	var numberRE = dojo.regexp.realNumber(flagsCopy);
272
	var currencyRE;
273
	switch (flags.placement) {
274
	  case "before":
275
		currencyRE = symbolRE + numberRE;
276
		break;
277
	  case "after":
278
		currencyRE = numberRE + symbolRE;
279
		break;
280
	}
281
	switch (flags.signPlacement) {
282
	  case "around":
283
		currencyRE = "(" + currencyRE + "|" + "\\(" + currencyRE + "\\)" + ")";
284
		break;
285
	  case "begin":
286
		currencyRE = signRE + currencyRE;
287
		break;
288
	  case "end":
289
		currencyRE = currencyRE + signRE;
290
		break;
291
	}
292
	return currencyRE;
293
};
294
dojo.regexp.us.state = function (flags) {
295
	flags = (typeof flags == "object") ? flags : {};
296
	if (typeof flags.allowTerritories != "boolean") {
297
		flags.allowTerritories = true;
298
	}
299
	if (typeof flags.allowMilitary != "boolean") {
300
		flags.allowMilitary = true;
301
	}
302
	var statesRE = "AL|AK|AZ|AR|CA|CO|CT|DE|DC|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|" + "NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|WV|WI|WY";
303
	var territoriesRE = "AS|FM|GU|MH|MP|PW|PR|VI";
304
	var militaryRE = "AA|AE|AP";
305
	if (flags.allowTerritories) {
306
		statesRE += "|" + territoriesRE;
307
	}
308
	if (flags.allowMilitary) {
309
		statesRE += "|" + militaryRE;
310
	}
311
	return "(" + statesRE + ")";
312
};
313
dojo.regexp.time = function (flags) {
314
	dojo.deprecated("dojo.regexp.time", "Use dojo.date.parse instead", "0.5");
315
	flags = (typeof flags == "object") ? flags : {};
316
	if (typeof flags.format == "undefined") {
317
		flags.format = "h:mm:ss t";
318
	}
319
	if (typeof flags.amSymbol != "string") {
320
		flags.amSymbol = "AM";
321
	}
322
	if (typeof flags.pmSymbol != "string") {
323
		flags.pmSymbol = "PM";
324
	}
325
	var timeRE = function (format) {
326
		format = format.replace(/([.$?*!=:|{}\(\)\[\]\\\/^])/g, "\\$1");
327
		var amRE = flags.amSymbol.replace(/([.$?*!=:|{}\(\)\[\]\\\/^])/g, "\\$1");
328
		var pmRE = flags.pmSymbol.replace(/([.$?*!=:|{}\(\)\[\]\\\/^])/g, "\\$1");
329
		format = format.replace("hh", "(0[1-9]|1[0-2])");
330
		format = format.replace("h", "([1-9]|1[0-2])");
331
		format = format.replace("HH", "([01][0-9]|2[0-3])");
332
		format = format.replace("H", "([0-9]|1[0-9]|2[0-3])");
333
		format = format.replace("mm", "([0-5][0-9])");
334
		format = format.replace("m", "([1-5][0-9]|[0-9])");
335
		format = format.replace("ss", "([0-5][0-9])");
336
		format = format.replace("s", "([1-5][0-9]|[0-9])");
337
		format = format.replace("t", "\\s?(" + amRE + "|" + pmRE + ")\\s?");
338
		return format;
339
	};
340
	return dojo.regexp.buildGroupRE(flags.format, timeRE);
341
};
342
dojo.regexp.numberFormat = function (flags) {
343
	flags = (typeof flags == "object") ? flags : {};
344
	if (typeof flags.format == "undefined") {
345
		flags.format = "###-###-####";
346
	}
347
	var digitRE = function (format) {
348
		format = format.replace(/([.$*!=:|{}\(\)\[\]\\\/^])/g, "\\$1");
349
		format = format.replace(/\?/g, "\\d?");
350
		format = format.replace(/#/g, "\\d");
351
		return format;
352
	};
353
	return dojo.regexp.buildGroupRE(flags.format, digitRE);
354
};
355
dojo.regexp.buildGroupRE = function (a, re) {
356
	if (!(a instanceof Array)) {
357
		return re(a);
358
	}
359
	var b = [];
360
	for (var i = 0; i < a.length; i++) {
361
		b.push(re(a[i]));
362
	}
363
	return "(" + b.join("|") + ")";
364
};
365