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