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