Subversion Repositories Applications.papyrus

Rev

Rev 1318 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1318 Rev 1422
1
/*
1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
3
	All Rights Reserved.
4
 
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
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:
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
9
*/
-
 
10
 
-
 
11
 
10
 
12
 
11
dojo.provide("dojo.string.extras");
13
dojo.provide("dojo.string.extras");
12
dojo.require("dojo.string.common");
14
dojo.require("dojo.string.common");
13
dojo.require("dojo.lang.common");
15
dojo.require("dojo.lang.common");
14
dojo.require("dojo.lang.array");
16
dojo.require("dojo.lang.array");
15
dojo.string.substituteParams = function (template, hash) {
17
dojo.string.substituteParams = function (template, hash) {
16
	var map = (typeof hash == "object") ? hash : dojo.lang.toArray(arguments, 1);
18
	var map = (typeof hash == "object") ? hash : dojo.lang.toArray(arguments, 1);
17
	return template.replace(/\%\{(\w+)\}/g, function (match, key) {
19
	return template.replace(/\%\{(\w+)\}/g, function (match, key) {
18
		if (typeof (map[key]) != "undefined" && map[key] != null) {
20
		if (typeof (map[key]) != "undefined" && map[key] != null) {
19
			return map[key];
21
			return map[key];
20
		}
22
		}
21
		dojo.raise("Substitution not found: " + key);
23
		dojo.raise("Substitution not found: " + key);
22
	});
24
	});
23
};
25
};
24
dojo.string.capitalize = function (str) {
26
dojo.string.capitalize = function (str) {
25
	if (!dojo.lang.isString(str)) {
27
	if (!dojo.lang.isString(str)) {
26
		return "";
28
		return "";
27
	}
29
	}
28
	if (arguments.length == 0) {
30
	if (arguments.length == 0) {
29
		str = this;
31
		str = this;
30
	}
32
	}
31
	var words = str.split(" ");
33
	var words = str.split(" ");
32
	for (var i = 0; i < words.length; i++) {
34
	for (var i = 0; i < words.length; i++) {
33
		words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
35
		words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
34
	}
36
	}
35
	return words.join(" ");
37
	return words.join(" ");
36
};
38
};
37
dojo.string.isBlank = function (str) {
39
dojo.string.isBlank = function (str) {
38
	if (!dojo.lang.isString(str)) {
40
	if (!dojo.lang.isString(str)) {
39
		return true;
41
		return true;
40
	}
42
	}
41
	return (dojo.string.trim(str).length == 0);
43
	return (dojo.string.trim(str).length == 0);
42
};
44
};
43
dojo.string.encodeAscii = function (str) {
45
dojo.string.encodeAscii = function (str) {
44
	if (!dojo.lang.isString(str)) {
46
	if (!dojo.lang.isString(str)) {
45
		return str;
47
		return str;
46
	}
48
	}
47
	var ret = "";
49
	var ret = "";
48
	var value = escape(str);
50
	var value = escape(str);
49
	var match, re = /%u([0-9A-F]{4})/i;
51
	var match, re = /%u([0-9A-F]{4})/i;
50
	while ((match = value.match(re))) {
52
	while ((match = value.match(re))) {
51
		var num = Number("0x" + match[1]);
53
		var num = Number("0x" + match[1]);
52
		var newVal = escape("&#" + num + ";");
54
		var newVal = escape("&#" + num + ";");
53
		ret += value.substring(0, match.index) + newVal;
55
		ret += value.substring(0, match.index) + newVal;
54
		value = value.substring(match.index + match[0].length);
56
		value = value.substring(match.index + match[0].length);
55
	}
57
	}
56
	ret += value.replace(/\+/g, "%2B");
58
	ret += value.replace(/\+/g, "%2B");
57
	return ret;
59
	return ret;
58
};
60
};
59
dojo.string.escape = function (type, str) {
61
dojo.string.escape = function (type, str) {
60
	var args = dojo.lang.toArray(arguments, 1);
62
	var args = dojo.lang.toArray(arguments, 1);
61
	switch (type.toLowerCase()) {
63
	switch (type.toLowerCase()) {
62
	  case "xml":
64
	  case "xml":
63
	  case "html":
65
	  case "html":
64
	  case "xhtml":
66
	  case "xhtml":
65
		return dojo.string.escapeXml.apply(this, args);
67
		return dojo.string.escapeXml.apply(this, args);
66
	  case "sql":
68
	  case "sql":
67
		return dojo.string.escapeSql.apply(this, args);
69
		return dojo.string.escapeSql.apply(this, args);
68
	  case "regexp":
70
	  case "regexp":
69
	  case "regex":
71
	  case "regex":
70
		return dojo.string.escapeRegExp.apply(this, args);
72
		return dojo.string.escapeRegExp.apply(this, args);
71
	  case "javascript":
73
	  case "javascript":
72
	  case "jscript":
74
	  case "jscript":
73
	  case "js":
75
	  case "js":
74
		return dojo.string.escapeJavaScript.apply(this, args);
76
		return dojo.string.escapeJavaScript.apply(this, args);
75
	  case "ascii":
77
	  case "ascii":
76
		return dojo.string.encodeAscii.apply(this, args);
78
		return dojo.string.encodeAscii.apply(this, args);
77
	  default:
79
	  default:
78
		return str;
80
		return str;
79
	}
81
	}
80
};
82
};
81
dojo.string.escapeXml = function (str, noSingleQuotes) {
83
dojo.string.escapeXml = function (str, noSingleQuotes) {
82
	str = str.replace(/&/gm, "&amp;").replace(/</gm, "&lt;").replace(/>/gm, "&gt;").replace(/"/gm, "&quot;");
84
	str = str.replace(/&/gm, "&amp;").replace(/</gm, "&lt;").replace(/>/gm, "&gt;").replace(/"/gm, "&quot;");
83
	if (!noSingleQuotes) {
85
	if (!noSingleQuotes) {
84
		str = str.replace(/'/gm, "&#39;");
86
		str = str.replace(/'/gm, "&#39;");
85
	}
87
	}
86
	return str;
88
	return str;
87
};
89
};
88
dojo.string.escapeSql = function (str) {
90
dojo.string.escapeSql = function (str) {
89
	return str.replace(/'/gm, "''");
91
	return str.replace(/'/gm, "''");
90
};
92
};
91
dojo.string.escapeRegExp = function (str) {
93
dojo.string.escapeRegExp = function (str) {
92
	return str.replace(/\\/gm, "\\\\").replace(/([\f\b\n\t\r[\^$|?*+(){}])/gm, "\\$1");
94
	return str.replace(/\\/gm, "\\\\").replace(/([\f\b\n\t\r[\^$|?*+(){}])/gm, "\\$1");
93
};
95
};
94
dojo.string.escapeJavaScript = function (str) {
96
dojo.string.escapeJavaScript = function (str) {
95
	return str.replace(/(["'\f\b\n\t\r])/gm, "\\$1");
97
	return str.replace(/(["'\f\b\n\t\r])/gm, "\\$1");
96
};
98
};
97
dojo.string.escapeString = function (str) {
99
dojo.string.escapeString = function (str) {
98
	return ("\"" + str.replace(/(["\\])/g, "\\$1") + "\"").replace(/[\f]/g, "\\f").replace(/[\b]/g, "\\b").replace(/[\n]/g, "\\n").replace(/[\t]/g, "\\t").replace(/[\r]/g, "\\r");
100
	return ("\"" + str.replace(/(["\\])/g, "\\$1") + "\"").replace(/[\f]/g, "\\f").replace(/[\b]/g, "\\b").replace(/[\n]/g, "\\n").replace(/[\t]/g, "\\t").replace(/[\r]/g, "\\r");
99
};
101
};
100
dojo.string.summary = function (str, len) {
102
dojo.string.summary = function (str, len) {
101
	if (!len || str.length <= len) {
103
	if (!len || str.length <= len) {
102
		return str;
104
		return str;
103
	}
105
	}
104
	return str.substring(0, len).replace(/\.+$/, "") + "...";
106
	return str.substring(0, len).replace(/\.+$/, "") + "...";
105
};
107
};
106
dojo.string.endsWith = function (str, end, ignoreCase) {
108
dojo.string.endsWith = function (str, end, ignoreCase) {
107
	if (ignoreCase) {
109
	if (ignoreCase) {
108
		str = str.toLowerCase();
110
		str = str.toLowerCase();
109
		end = end.toLowerCase();
111
		end = end.toLowerCase();
110
	}
112
	}
111
	if ((str.length - end.length) < 0) {
113
	if ((str.length - end.length) < 0) {
112
		return false;
114
		return false;
113
	}
115
	}
114
	return str.lastIndexOf(end) == str.length - end.length;
116
	return str.lastIndexOf(end) == str.length - end.length;
115
};
117
};
116
dojo.string.endsWithAny = function (str) {
118
dojo.string.endsWithAny = function (str) {
117
	for (var i = 1; i < arguments.length; i++) {
119
	for (var i = 1; i < arguments.length; i++) {
118
		if (dojo.string.endsWith(str, arguments[i])) {
120
		if (dojo.string.endsWith(str, arguments[i])) {
119
			return true;
121
			return true;
120
		}
122
		}
121
	}
123
	}
122
	return false;
124
	return false;
123
};
125
};
124
dojo.string.startsWith = function (str, start, ignoreCase) {
126
dojo.string.startsWith = function (str, start, ignoreCase) {
125
	if (ignoreCase) {
127
	if (ignoreCase) {
126
		str = str.toLowerCase();
128
		str = str.toLowerCase();
127
		start = start.toLowerCase();
129
		start = start.toLowerCase();
128
	}
130
	}
129
	return str.indexOf(start) == 0;
131
	return str.indexOf(start) == 0;
130
};
132
};
131
dojo.string.startsWithAny = function (str) {
133
dojo.string.startsWithAny = function (str) {
132
	for (var i = 1; i < arguments.length; i++) {
134
	for (var i = 1; i < arguments.length; i++) {
133
		if (dojo.string.startsWith(str, arguments[i])) {
135
		if (dojo.string.startsWith(str, arguments[i])) {
134
			return true;
136
			return true;
135
		}
137
		}
136
	}
138
	}
137
	return false;
139
	return false;
138
};
140
};
139
dojo.string.has = function (str) {
141
dojo.string.has = function (str) {
140
	for (var i = 1; i < arguments.length; i++) {
142
	for (var i = 1; i < arguments.length; i++) {
141
		if (str.indexOf(arguments[i]) > -1) {
143
		if (str.indexOf(arguments[i]) > -1) {
142
			return true;
144
			return true;
143
		}
145
		}
144
	}
146
	}
145
	return false;
147
	return false;
146
};
148
};
147
dojo.string.normalizeNewlines = function (text, newlineChar) {
149
dojo.string.normalizeNewlines = function (text, newlineChar) {
148
	if (newlineChar == "\n") {
150
	if (newlineChar == "\n") {
149
		text = text.replace(/\r\n/g, "\n");
151
		text = text.replace(/\r\n/g, "\n");
150
		text = text.replace(/\r/g, "\n");
152
		text = text.replace(/\r/g, "\n");
151
	} else {
153
	} else {
152
		if (newlineChar == "\r") {
154
		if (newlineChar == "\r") {
153
			text = text.replace(/\r\n/g, "\r");
155
			text = text.replace(/\r\n/g, "\r");
154
			text = text.replace(/\n/g, "\r");
156
			text = text.replace(/\n/g, "\r");
155
		} else {
157
		} else {
156
			text = text.replace(/([^\r])\n/g, "$1\r\n").replace(/\r([^\n])/g, "\r\n$1");
158
			text = text.replace(/([^\r])\n/g, "$1\r\n").replace(/\r([^\n])/g, "\r\n$1");
157
		}
159
		}
158
	}
160
	}
159
	return text;
161
	return text;
160
};
162
};
161
dojo.string.splitEscaped = function (str, charac) {
163
dojo.string.splitEscaped = function (str, charac) {
162
	var components = [];
164
	var components = [];
163
	for (var i = 0, prevcomma = 0; i < str.length; i++) {
165
	for (var i = 0, prevcomma = 0; i < str.length; i++) {
164
		if (str.charAt(i) == "\\") {
166
		if (str.charAt(i) == "\\") {
165
			i++;
167
			i++;
166
			continue;
168
			continue;
167
		}
169
		}
168
		if (str.charAt(i) == charac) {
170
		if (str.charAt(i) == charac) {
169
			components.push(str.substring(prevcomma, i));
171
			components.push(str.substring(prevcomma, i));
170
			prevcomma = i + 1;
172
			prevcomma = i + 1;
171
		}
173
		}
172
	}
174
	}
173
	components.push(str.substr(prevcomma));
175
	components.push(str.substr(prevcomma));
174
	return components;
176
	return components;
175
};
177
};
176
 
178