2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.dtl.filter.strings"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.dtl.filter.strings"] = true;
|
|
|
3 |
dojo.provide("dojox.dtl.filter.strings");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.dtl.filter.htmlstrings");
|
|
|
6 |
dojo.require("dojox.string.sprintf");
|
|
|
7 |
dojo.require("dojox.string.tokenize");
|
|
|
8 |
|
|
|
9 |
dojo.mixin(dojox.dtl.filter.strings, {
|
|
|
10 |
addslashes: function(value){
|
|
|
11 |
// summary: Adds slashes - useful for passing strings to JavaScript, for example.
|
|
|
12 |
return value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/'/g, "\\'");
|
|
|
13 |
},
|
|
|
14 |
capfirst: function(value){
|
|
|
15 |
// summary: Capitalizes the first character of the value
|
|
|
16 |
value = "" + value;
|
|
|
17 |
return value.charAt(0).toUpperCase() + value.substring(1);
|
|
|
18 |
},
|
|
|
19 |
center: function(value, arg){
|
|
|
20 |
// summary: Centers the value in a field of a given width
|
|
|
21 |
arg = arg || value.length;
|
|
|
22 |
value = value + "";
|
|
|
23 |
var diff = arg - value.length;
|
|
|
24 |
if(diff % 2){
|
|
|
25 |
value = value + " ";
|
|
|
26 |
diff -= 1;
|
|
|
27 |
}
|
|
|
28 |
for(var i = 0; i < diff; i += 2){
|
|
|
29 |
value = " " + value + " ";
|
|
|
30 |
}
|
|
|
31 |
return value;
|
|
|
32 |
},
|
|
|
33 |
cut: function(value, arg){
|
|
|
34 |
// summary: Removes all values of arg from the given string
|
|
|
35 |
arg = arg + "" || "";
|
|
|
36 |
value = value + "";
|
|
|
37 |
return value.replace(new RegExp(arg, "g"), "");
|
|
|
38 |
},
|
|
|
39 |
_fix_ampersands: /&(?!(\w+|#\d+);)/g,
|
|
|
40 |
fix_ampersands: function(value){
|
|
|
41 |
// summary: Replaces ampersands with ``&`` entities
|
|
|
42 |
return value.replace(dojox.dtl.filter.strings._fix_ampersands, "&");
|
|
|
43 |
},
|
|
|
44 |
floatformat: function(value, arg){
|
|
|
45 |
// summary: Format a number according to arg
|
|
|
46 |
// description:
|
|
|
47 |
// If called without an argument, displays a floating point
|
|
|
48 |
// number as 34.2 -- but only if there's a point to be displayed.
|
|
|
49 |
// With a positive numeric argument, it displays that many decimal places
|
|
|
50 |
// always.
|
|
|
51 |
// With a negative numeric argument, it will display that many decimal
|
|
|
52 |
// places -- but only if there's places to be displayed.
|
|
|
53 |
arg = parseInt(arg || -1);
|
|
|
54 |
value = parseFloat(value);
|
|
|
55 |
var m = value - value.toFixed(0);
|
|
|
56 |
if(!m && arg < 0){
|
|
|
57 |
return value.toFixed();
|
|
|
58 |
}
|
|
|
59 |
value = value.toFixed(Math.abs(arg));
|
|
|
60 |
return (arg < 0) ? parseFloat(value) + "" : value;
|
|
|
61 |
},
|
|
|
62 |
iriencode: function(value){
|
|
|
63 |
return dojox.dtl.text.urlquote(value, "/#%[]=:;$&()+,!");
|
|
|
64 |
},
|
|
|
65 |
linenumbers: function(value){
|
|
|
66 |
// summary: Displays text with line numbers
|
|
|
67 |
var df = dojox.dtl.filter;
|
|
|
68 |
var lines = value.split("\n");
|
|
|
69 |
var output = [];
|
|
|
70 |
var width = (lines.length + "").length;
|
|
|
71 |
for(var i = 0, line; i < lines.length; i++){
|
|
|
72 |
line = lines[i];
|
|
|
73 |
output.push(df.strings.ljust(i + 1, width) + ". " + df.htmlstrings.escape(line));
|
|
|
74 |
}
|
|
|
75 |
return output.join("\n");
|
|
|
76 |
},
|
|
|
77 |
ljust: function(value, arg){
|
|
|
78 |
value = value + "";
|
|
|
79 |
arg = parseInt(arg);
|
|
|
80 |
while(value.length < arg){
|
|
|
81 |
value = value + " ";
|
|
|
82 |
}
|
|
|
83 |
return value;
|
|
|
84 |
},
|
|
|
85 |
lower: function(value){
|
|
|
86 |
// summary: Converts a string into all lowercase
|
|
|
87 |
return (value + "").toLowerCase();
|
|
|
88 |
},
|
|
|
89 |
make_list: function(value){
|
|
|
90 |
// summary:
|
|
|
91 |
// Returns the value turned into a list. For an integer, it's a list of
|
|
|
92 |
// digits. For a string, it's a list of characters.
|
|
|
93 |
var output = [];
|
|
|
94 |
if(typeof value == "number"){
|
|
|
95 |
value = value + "";
|
|
|
96 |
}
|
|
|
97 |
if(value.charAt){
|
|
|
98 |
for(var i = 0; i < value.length; i++){
|
|
|
99 |
output.push(value.charAt(i));
|
|
|
100 |
}
|
|
|
101 |
return output;
|
|
|
102 |
}
|
|
|
103 |
if(typeof value == "object"){
|
|
|
104 |
for(var key in value){
|
|
|
105 |
output.push(value[key]);
|
|
|
106 |
}
|
|
|
107 |
return output;
|
|
|
108 |
}
|
|
|
109 |
return [];
|
|
|
110 |
},
|
|
|
111 |
rjust: function(value, arg){
|
|
|
112 |
value = value + "";
|
|
|
113 |
arg = parseInt(arg);
|
|
|
114 |
while(value.length < arg){
|
|
|
115 |
value = " " + value;
|
|
|
116 |
}
|
|
|
117 |
return value;
|
|
|
118 |
},
|
|
|
119 |
slugify: function(value){
|
|
|
120 |
// summary: Converts to lowercase, removes
|
|
|
121 |
// non-alpha chars and converts spaces to hyphens
|
|
|
122 |
value = value.replace(/[^\w\s-]/g, "").toLowerCase();
|
|
|
123 |
return value.replace(/[\-\s]+/g, "-");
|
|
|
124 |
},
|
|
|
125 |
_strings: {},
|
|
|
126 |
stringformat: function(value, arg){
|
|
|
127 |
// summary:
|
|
|
128 |
// Formats the variable according to the argument, a string formatting specifier.
|
|
|
129 |
// This specifier uses Python string formating syntax, with the exception that
|
|
|
130 |
// the leading "%" is dropped.
|
|
|
131 |
arg = "" + arg;
|
|
|
132 |
var strings = dojox.dtl.filter.strings._strings;
|
|
|
133 |
if(!strings[arg]){
|
|
|
134 |
strings[arg] = new dojox.string.sprintf.Formatter("%" + arg);
|
|
|
135 |
}
|
|
|
136 |
return strings[arg].format(value);
|
|
|
137 |
},
|
|
|
138 |
title: function(value){
|
|
|
139 |
// summary: Converts a string into titlecase
|
|
|
140 |
var last, title = "";
|
|
|
141 |
for(var i = 0, current; i < value.length; i++){
|
|
|
142 |
current = value.charAt(i);
|
|
|
143 |
if(last == " " || last == "\n" || last == "\t" || !last){
|
|
|
144 |
title += current.toUpperCase();
|
|
|
145 |
}else{
|
|
|
146 |
title += current.toLowerCase();
|
|
|
147 |
}
|
|
|
148 |
last = current;
|
|
|
149 |
}
|
|
|
150 |
return title;
|
|
|
151 |
},
|
|
|
152 |
_truncatewords: /[ \n\r\t]/,
|
|
|
153 |
truncatewords: function(value, arg){
|
|
|
154 |
// summary: Truncates a string after a certain number of words
|
|
|
155 |
// arg: Integer
|
|
|
156 |
// Number of words to truncate after
|
|
|
157 |
arg = parseInt(arg);
|
|
|
158 |
if(!arg){
|
|
|
159 |
return value;
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
for(var i = 0, j = value.length, count = 0, current, last; i < value.length; i++){
|
|
|
163 |
current = value.charAt(i);
|
|
|
164 |
if(dojox.dtl.filter.strings._truncatewords.test(last)){
|
|
|
165 |
if(!dojox.dtl.filter.strings._truncatewords.test(current)){
|
|
|
166 |
++count;
|
|
|
167 |
if(count == arg){
|
|
|
168 |
return value.substring(0, j + 1);
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
}else if(!dojox.dtl.filter.strings._truncatewords.test(current)){
|
|
|
172 |
j = i;
|
|
|
173 |
}
|
|
|
174 |
last = current;
|
|
|
175 |
}
|
|
|
176 |
return value;
|
|
|
177 |
},
|
|
|
178 |
_truncate_words: /(&.*?;|<.*?>|(\w[\w-]*))/g,
|
|
|
179 |
_truncate_tag: /<(\/)?([^ ]+?)(?: (\/)| .*?)?>/,
|
|
|
180 |
_truncate_singlets: { br: true, col: true, link: true, base: true, img: true, param: true, area: true, hr: true, input: true },
|
|
|
181 |
truncatewords_html: function(value, arg){
|
|
|
182 |
arg = parseInt(arg);
|
|
|
183 |
|
|
|
184 |
if(arg <= 0){
|
|
|
185 |
return "";
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
var strings = dojox.dtl.filter.strings;
|
|
|
189 |
var words = 0;
|
|
|
190 |
var open = [];
|
|
|
191 |
|
|
|
192 |
var output = dojox.string.tokenize(value, strings._truncate_words, function(all, word){
|
|
|
193 |
if(word){
|
|
|
194 |
// It's an actual non-HTML word
|
|
|
195 |
++words;
|
|
|
196 |
if(words < arg){
|
|
|
197 |
return word;
|
|
|
198 |
}else if(words == arg){
|
|
|
199 |
return word + " ...";
|
|
|
200 |
}
|
|
|
201 |
}
|
|
|
202 |
// Check for tag
|
|
|
203 |
var tag = all.match(strings._truncate_tag);
|
|
|
204 |
if(!tag || words >= arg){
|
|
|
205 |
// Don't worry about non tags or tags after our truncate point
|
|
|
206 |
return;
|
|
|
207 |
}
|
|
|
208 |
var closing = tag[1];
|
|
|
209 |
var tagname = tag[2].toLowerCase();
|
|
|
210 |
var selfclosing = tag[3];
|
|
|
211 |
if(closing || strings._truncate_singlets[tagname]){
|
|
|
212 |
}else if(closing){
|
|
|
213 |
var i = dojo.indexOf(open, tagname);
|
|
|
214 |
if(i != -1){
|
|
|
215 |
open = open.slice(i + 1);
|
|
|
216 |
}
|
|
|
217 |
}else{
|
|
|
218 |
open.unshift(tagname);
|
|
|
219 |
}
|
|
|
220 |
return all;
|
|
|
221 |
}).join("");
|
|
|
222 |
|
|
|
223 |
output = output.replace(/\s+$/g, "");
|
|
|
224 |
|
|
|
225 |
for(var i = 0, tag; tag = open[i]; i++){
|
|
|
226 |
output += "</" + tag + ">";
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
return output;
|
|
|
230 |
},
|
|
|
231 |
upper: function(value){
|
|
|
232 |
return value.toUpperCase();
|
|
|
233 |
},
|
|
|
234 |
urlencode: function(value){
|
|
|
235 |
return dojox.dtl.text.urlquote(value);
|
|
|
236 |
},
|
|
|
237 |
_urlize: /^((?:[(>]|<)*)(.*?)((?:[.,)>\n]|>)*)$/,
|
|
|
238 |
_urlize2: /^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$/,
|
|
|
239 |
urlize: function(value){
|
|
|
240 |
return dojox.dtl.filter.strings.urlizetrunc(value);
|
|
|
241 |
},
|
|
|
242 |
urlizetrunc: function(value, arg){
|
|
|
243 |
arg = parseInt(arg);
|
|
|
244 |
return dojox.string.tokenize(value, /(\S+)/g, function(word){
|
|
|
245 |
var matches = dojox.dtl.filter.strings._urlize.exec(word);
|
|
|
246 |
if(!matches){
|
|
|
247 |
return word;
|
|
|
248 |
}
|
|
|
249 |
var lead = matches[1];
|
|
|
250 |
var middle = matches[2];
|
|
|
251 |
var trail = matches[3];
|
|
|
252 |
|
|
|
253 |
var startsWww = middle.indexOf("www.") == 0;
|
|
|
254 |
var hasAt = middle.indexOf("@") != -1;
|
|
|
255 |
var hasColon = middle.indexOf(":") != -1;
|
|
|
256 |
var startsHttp = middle.indexOf("http://") == 0;
|
|
|
257 |
var startsHttps = middle.indexOf("https://") == 0;
|
|
|
258 |
var firstAlpha = /[a-zA-Z0-9]/.test(middle.charAt(0));
|
|
|
259 |
var last4 = middle.substring(middle.length - 4);
|
|
|
260 |
|
|
|
261 |
var trimmed = middle;
|
|
|
262 |
if(arg > 3){
|
|
|
263 |
trimmed = trimmed.substring(0, arg - 3) + "...";
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
if(startsWww || (!hasAt && !startsHttp && middle.length && firstAlpha && (last4 == ".org" || last4 == ".net" || last4 == ".com"))){
|
|
|
267 |
return '<a href="http://' + middle + '" rel="nofollow">' + trimmed + '</a>';
|
|
|
268 |
}else if(startsHttp || startsHttps){
|
|
|
269 |
return '<a href="' + middle + '" rel="nofollow">' + trimmed + '</a>';
|
|
|
270 |
}else if(hasAt && !startsWww && !hasColon && dojox.dtl.filter.strings._urlize2.test(middle)){
|
|
|
271 |
return '<a href="mailto:' + middle + '">' + middle + '</a>';
|
|
|
272 |
}
|
|
|
273 |
return word;
|
|
|
274 |
}).join("");
|
|
|
275 |
},
|
|
|
276 |
wordcount: function(value){
|
|
|
277 |
return dojox.dtl.text.pySplit(value).length;
|
|
|
278 |
},
|
|
|
279 |
wordwrap: function(value, arg){
|
|
|
280 |
arg = parseInt(arg);
|
|
|
281 |
// summary: Wraps words at specified line length
|
|
|
282 |
var output = [];
|
|
|
283 |
var parts = value.split(/ /g);
|
|
|
284 |
if(parts.length){
|
|
|
285 |
var word = parts.shift();
|
|
|
286 |
output.push(word);
|
|
|
287 |
var pos = word.length - word.lastIndexOf("\n") - 1;
|
|
|
288 |
for(var i = 0; i < parts.length; i++){
|
|
|
289 |
word = parts[i];
|
|
|
290 |
if(word.indexOf("\n") != -1){
|
|
|
291 |
var lines = word.split(/\n/g);
|
|
|
292 |
}else{
|
|
|
293 |
var lines = [word];
|
|
|
294 |
}
|
|
|
295 |
pos += lines[0].length + 1;
|
|
|
296 |
if(arg && pos > arg){
|
|
|
297 |
output.push("\n");
|
|
|
298 |
pos = lines[lines.length - 1].length;
|
|
|
299 |
}else{
|
|
|
300 |
output.push(" ");
|
|
|
301 |
if(lines.length > 1){
|
|
|
302 |
pos = lines[lines.length - 1].length;
|
|
|
303 |
}
|
|
|
304 |
}
|
|
|
305 |
output.push(word);
|
|
|
306 |
}
|
|
|
307 |
}
|
|
|
308 |
return output.join("");
|
|
|
309 |
}
|
|
|
310 |
});
|
|
|
311 |
|
|
|
312 |
}
|