2007-03-09 03:23:24 +00:00
|
|
|
Object.extend(String, {
|
2007-03-09 04:23:28 +00:00
|
|
|
interpret: function(value) {
|
2007-03-09 03:23:24 +00:00
|
|
|
return value == null ? '' : String(value);
|
|
|
|
},
|
|
|
|
specialChar: {
|
|
|
|
'\b': '\\b',
|
|
|
|
'\t': '\\t',
|
|
|
|
'\n': '\\n',
|
|
|
|
'\f': '\\f',
|
|
|
|
'\r': '\\r',
|
|
|
|
'\\': '\\\\'
|
|
|
|
}
|
|
|
|
});
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
Object.extend(String.prototype, {
|
|
|
|
gsub: function(pattern, replacement) {
|
|
|
|
var result = '', source = this, match;
|
|
|
|
replacement = arguments.callee.prepareReplacement(replacement);
|
|
|
|
|
|
|
|
while (source.length > 0) {
|
|
|
|
if (match = source.match(pattern)) {
|
|
|
|
result += source.slice(0, match.index);
|
|
|
|
result += String.interpret(replacement(match));
|
|
|
|
source = source.slice(match.index + match[0].length);
|
|
|
|
} else {
|
|
|
|
result += source, source = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
|
|
|
sub: function(pattern, replacement, count) {
|
|
|
|
replacement = this.gsub.prepareReplacement(replacement);
|
|
|
|
count = count === undefined ? 1 : count;
|
|
|
|
|
|
|
|
return this.gsub(pattern, function(match) {
|
|
|
|
if (--count < 0) return match[0];
|
|
|
|
return replacement(match);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
scan: function(pattern, iterator) {
|
|
|
|
this.gsub(pattern, iterator);
|
2007-07-24 16:30:19 +00:00
|
|
|
return String(this);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
truncate: function(length, truncation) {
|
|
|
|
length = length || 30;
|
|
|
|
truncation = truncation === undefined ? '...' : truncation;
|
|
|
|
return this.length > length ?
|
2007-05-31 22:32:25 +00:00
|
|
|
this.slice(0, length - truncation.length) + truncation : String(this);
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
strip: function() {
|
|
|
|
return this.replace(/^\s+/, '').replace(/\s+$/, '');
|
|
|
|
},
|
|
|
|
|
|
|
|
stripTags: function() {
|
|
|
|
return this.replace(/<\/?[^>]+>/gi, '');
|
|
|
|
},
|
|
|
|
|
|
|
|
stripScripts: function() {
|
|
|
|
return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
|
|
|
|
},
|
|
|
|
|
|
|
|
extractScripts: function() {
|
|
|
|
var matchAll = new RegExp(Prototype.ScriptFragment, 'img');
|
|
|
|
var matchOne = new RegExp(Prototype.ScriptFragment, 'im');
|
|
|
|
return (this.match(matchAll) || []).map(function(scriptTag) {
|
|
|
|
return (scriptTag.match(matchOne) || ['', ''])[1];
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
evalScripts: function() {
|
|
|
|
return this.extractScripts().map(function(script) { return eval(script) });
|
|
|
|
},
|
|
|
|
|
|
|
|
escapeHTML: function() {
|
2007-01-27 18:33:03 +00:00
|
|
|
var self = arguments.callee;
|
|
|
|
self.text.data = this;
|
|
|
|
return self.div.innerHTML;
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
unescapeHTML: function() {
|
Merge -r6634:HEAD from ../branches/dom.
* Make Element#update and Element#insert work for SELECT tags in IE and Opera. [Tobie Langel]
* Make Element#insert and Element#update better handle TABLE related elements in IE and Opera. Closes #7776, #8040, #7550, #7776, #7938. [Tobie Langel]
* Make Element#readAttribute('title') work in Opera. [Tobie Langel]
* Make Element#replace work with form elements in Firefox and Safari. Closes #8010, #7989. [dsl239, Tobie Langel]
* Add Element#wrap which wraps the element inside a new one. Closes #5732. [P. Vande, Tobie Langel]
* Make Element into a constructor: new Element(tagName, attributes). Add Element#writeAttribute which accepts a hash of attributes or a name/value pair. Closes #7476. [Mislav Marohnić, haraldmartin, Tobie Langel]
* Insertion overhaul: Add Element.insert(content[, position = 'Bottom']). Deprecate Insertion (kept for backwards compatibility). Make Ajax.Updater option.insertion accept both Insertion.Top or the now preferred 'Top'. Closes #7907. [Tobie Langel]
2007-05-12 05:01:56 +00:00
|
|
|
var div = new Element('div');
|
2007-01-18 22:24:27 +00:00
|
|
|
div.innerHTML = this.stripTags();
|
|
|
|
return div.childNodes[0] ? (div.childNodes.length > 1 ?
|
2007-03-09 04:23:28 +00:00
|
|
|
$A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) :
|
2007-01-18 22:24:27 +00:00
|
|
|
div.childNodes[0].nodeValue) : '';
|
|
|
|
},
|
|
|
|
|
|
|
|
toQueryParams: function(separator) {
|
|
|
|
var match = this.strip().match(/([^?#]*)(#.*)?$/);
|
2007-08-08 23:19:45 +00:00
|
|
|
if (!match) return { };
|
2007-01-18 22:24:27 +00:00
|
|
|
|
2007-08-08 23:19:45 +00:00
|
|
|
return match[1].split(separator || '&').inject({ }, function(hash, pair) {
|
2007-01-18 22:24:27 +00:00
|
|
|
if ((pair = pair.split('='))[0]) {
|
2007-03-27 20:55:56 +00:00
|
|
|
var key = decodeURIComponent(pair.shift());
|
|
|
|
var value = pair.length > 1 ? pair.join('=') : pair[0];
|
|
|
|
if (value != undefined) value = decodeURIComponent(value);
|
|
|
|
|
|
|
|
if (key in hash) {
|
2007-07-24 18:52:17 +00:00
|
|
|
if (!Object.isArray(hash[key])) hash[key] = [hash[key]];
|
2007-03-27 20:55:56 +00:00
|
|
|
hash[key].push(value);
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
2007-03-27 20:55:56 +00:00
|
|
|
else hash[key] = value;
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
toArray: function() {
|
|
|
|
return this.split('');
|
|
|
|
},
|
|
|
|
|
|
|
|
succ: function() {
|
|
|
|
return this.slice(0, this.length - 1) +
|
|
|
|
String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
|
|
|
|
},
|
2007-03-09 03:23:24 +00:00
|
|
|
|
|
|
|
times: function(count) {
|
|
|
|
var result = '';
|
|
|
|
for (var i = 0; i < count; i++) result += this;
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
camelize: function() {
|
|
|
|
var parts = this.split('-'), len = parts.length;
|
|
|
|
if (len == 1) return parts[0];
|
|
|
|
|
|
|
|
var camelized = this.charAt(0) == '-'
|
|
|
|
? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
|
|
|
|
: parts[0];
|
|
|
|
|
|
|
|
for (var i = 1; i < len; i++)
|
|
|
|
camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);
|
|
|
|
|
|
|
|
return camelized;
|
|
|
|
},
|
|
|
|
|
2007-02-19 21:32:37 +00:00
|
|
|
capitalize: function() {
|
2007-01-18 22:24:27 +00:00
|
|
|
return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
|
|
|
|
},
|
|
|
|
|
|
|
|
underscore: function() {
|
|
|
|
return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase();
|
|
|
|
},
|
|
|
|
|
|
|
|
dasherize: function() {
|
|
|
|
return this.gsub(/_/,'-');
|
|
|
|
},
|
|
|
|
|
|
|
|
inspect: function(useDoubleQuotes) {
|
2007-03-09 03:23:24 +00:00
|
|
|
var escapedString = this.gsub(/[\x00-\x1f\\]/, function(match) {
|
|
|
|
var character = String.specialChar[match[0]];
|
|
|
|
return character ? character : '\\u00' + match[0].charCodeAt().toPaddedString(2, 16);
|
|
|
|
});
|
|
|
|
if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"';
|
|
|
|
return "'" + escapedString.replace(/'/g, '\\\'') + "'";
|
|
|
|
},
|
|
|
|
|
2007-03-09 04:23:28 +00:00
|
|
|
toJSON: function() {
|
2007-03-09 03:23:24 +00:00
|
|
|
return this.inspect(true);
|
2007-01-27 18:45:54 +00:00
|
|
|
},
|
|
|
|
|
2007-04-24 03:31:14 +00:00
|
|
|
unfilterJSON: function(filter) {
|
|
|
|
return this.sub(filter || Prototype.JSONFilter, '#{1}');
|
|
|
|
},
|
|
|
|
|
2007-06-17 16:03:38 +00:00
|
|
|
isJSON: function() {
|
|
|
|
var str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
|
|
|
|
return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
|
|
|
|
},
|
|
|
|
|
2007-03-09 04:23:28 +00:00
|
|
|
evalJSON: function(sanitize) {
|
2007-04-24 03:31:14 +00:00
|
|
|
var json = this.unfilterJSON();
|
2007-03-09 03:23:24 +00:00
|
|
|
try {
|
2007-06-17 16:03:38 +00:00
|
|
|
if (!sanitize || json.isJSON()) return eval('(' + json + ')');
|
2007-04-24 03:31:14 +00:00
|
|
|
} catch (e) { }
|
|
|
|
throw new SyntaxError('Badly formed JSON string: ' + this.inspect());
|
2007-03-09 03:23:24 +00:00
|
|
|
},
|
|
|
|
|
2007-02-19 21:32:37 +00:00
|
|
|
include: function(pattern) {
|
2007-01-27 18:45:54 +00:00
|
|
|
return this.indexOf(pattern) > -1;
|
|
|
|
},
|
|
|
|
|
2007-02-19 21:32:37 +00:00
|
|
|
startsWith: function(pattern) {
|
2007-03-27 17:43:30 +00:00
|
|
|
return this.indexOf(pattern) === 0;
|
2007-01-27 18:45:54 +00:00
|
|
|
},
|
|
|
|
|
2007-02-19 21:32:37 +00:00
|
|
|
endsWith: function(pattern) {
|
2007-03-27 17:43:30 +00:00
|
|
|
var d = this.length - pattern.length;
|
|
|
|
return d >= 0 && this.lastIndexOf(pattern) === d;
|
2007-02-19 21:32:37 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
empty: function() {
|
|
|
|
return this == '';
|
|
|
|
},
|
|
|
|
|
|
|
|
blank: function() {
|
|
|
|
return /^\s*$/.test(this);
|
2007-07-24 17:24:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
interpolate: function(object, pattern) {
|
|
|
|
return new Template(this, pattern).evaluate(object);
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2007-03-29 17:39:48 +00:00
|
|
|
if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, {
|
2007-03-28 11:35:05 +00:00
|
|
|
escapeHTML: function() {
|
2007-03-29 17:39:48 +00:00
|
|
|
return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
2007-03-28 11:35:05 +00:00
|
|
|
},
|
|
|
|
unescapeHTML: function() {
|
2007-03-29 17:39:48 +00:00
|
|
|
return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
|
2007-03-28 11:35:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
String.prototype.gsub.prepareReplacement = function(replacement) {
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isFunction(replacement)) return replacement;
|
2007-01-18 22:24:27 +00:00
|
|
|
var template = new Template(replacement);
|
|
|
|
return function(match) { return template.evaluate(match) };
|
2007-08-07 19:41:13 +00:00
|
|
|
};
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
|
|
String.prototype.parseQuery = String.prototype.toQueryParams;
|
|
|
|
|
2007-01-27 18:33:03 +00:00
|
|
|
Object.extend(String.prototype.escapeHTML, {
|
|
|
|
div: document.createElement('div'),
|
|
|
|
text: document.createTextNode('')
|
|
|
|
});
|
|
|
|
|
|
|
|
with (String.prototype.escapeHTML) div.appendChild(text);
|
|
|
|
|
2007-08-18 23:21:29 +00:00
|
|
|
var Template = Class.create({
|
2007-01-18 22:24:27 +00:00
|
|
|
initialize: function(template, pattern) {
|
|
|
|
this.template = template.toString();
|
2007-07-24 17:24:25 +00:00
|
|
|
this.pattern = pattern || Template.Pattern;
|
2007-01-18 22:24:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
evaluate: function(object) {
|
2007-08-08 23:19:45 +00:00
|
|
|
if (Object.isFunction(object.toTemplateReplacements))
|
2007-07-24 17:24:25 +00:00
|
|
|
object = object.toTemplateReplacements();
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
return this.template.gsub(this.pattern, function(match) {
|
2007-07-24 17:24:25 +00:00
|
|
|
if (object == null) return '';
|
|
|
|
|
|
|
|
var before = match[1] || '';
|
2007-01-18 22:24:27 +00:00
|
|
|
if (before == '\\') return match[2];
|
2007-07-24 17:24:25 +00:00
|
|
|
|
|
|
|
var ctx = object, expr = match[3];
|
|
|
|
var pattern = /^([^.[]+|\[((?:.*?[^\\])?)\])(\.|\[|$)/, match = pattern.exec(expr);
|
|
|
|
if (match == null) return '';
|
|
|
|
|
|
|
|
while (match != null) {
|
|
|
|
var comp = match[1].startsWith('[') ? match[2].gsub('\\\\]', ']') : match[1];
|
|
|
|
ctx = ctx[comp];
|
|
|
|
if (null == ctx || '' == match[3]) break;
|
|
|
|
expr = expr.substring('[' == match[3] ? match[1].length : match[0].length);
|
|
|
|
match = pattern.exec(expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return before + String.interpret(ctx);
|
|
|
|
}.bind(this));
|
2007-01-18 22:24:27 +00:00
|
|
|
}
|
2007-08-18 23:21:29 +00:00
|
|
|
});
|
|
|
|
Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;
|