2008-12-14 04:36:59 +00:00
|
|
|
|
/** section: lang
|
|
|
|
|
* String
|
|
|
|
|
**/
|
2007-03-09 03:23:24 +00:00
|
|
|
|
Object.extend(String, {
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String.interpret(value) -> String
|
|
|
|
|
*
|
|
|
|
|
* TODO: String.interpret
|
|
|
|
|
**/
|
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
|
|
|
|
|
2008-12-11 10:50:51 +00:00
|
|
|
|
Object.extend(String.prototype, (function() {
|
|
|
|
|
|
|
|
|
|
function prepareReplacement(replacement) {
|
|
|
|
|
if (Object.isFunction(replacement)) return replacement;
|
|
|
|
|
var template = new Template(replacement);
|
|
|
|
|
return function(match) { return template.evaluate(match) };
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#gsub(pattern, replacement) -> String
|
|
|
|
|
*
|
|
|
|
|
* Returns the string with every occurence of a given pattern replaced by either
|
|
|
|
|
* a regular string, the returned value of a function or a [[Template]] string.
|
|
|
|
|
* The pattern can be a string or a regular expression.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function gsub(pattern, replacement) {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
var result = '', source = this, match;
|
2008-12-11 10:50:51 +00:00
|
|
|
|
replacement = prepareReplacement(replacement);
|
|
|
|
|
|
2009-02-05 01:59:00 +00:00
|
|
|
|
if (!(pattern.length || pattern.source)) {
|
|
|
|
|
replacement = replacement('');
|
|
|
|
|
return replacement + source.split('').join(replacement) + replacement;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
|
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;
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#sub(pattern, replacement[, count = 1]) -> String
|
|
|
|
|
*
|
|
|
|
|
* Returns a string with the first count occurrences of pattern replaced by either
|
|
|
|
|
* a regular string, the returned value of a function or a [[Template]] string.
|
|
|
|
|
* The pattern can be a string or a regular expression.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function sub(pattern, replacement, count) {
|
|
|
|
|
replacement = prepareReplacement(replacement);
|
2007-11-15 21:53:15 +00:00
|
|
|
|
count = Object.isUndefined(count) ? 1 : count;
|
2008-12-11 10:50:51 +00:00
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.gsub(pattern, function(match) {
|
|
|
|
|
if (--count < 0) return match[0];
|
|
|
|
|
return replacement(match);
|
|
|
|
|
});
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/** related to: String#gsub
|
|
|
|
|
* String#scan(pattern, iterator) -> String
|
|
|
|
|
*
|
|
|
|
|
* Allows iterating over every occurrence of the given pattern (which can be a
|
|
|
|
|
* string or a regular expression).
|
|
|
|
|
* Returns the original string.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function scan(pattern, iterator) {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
this.gsub(pattern, iterator);
|
2007-07-24 16:30:19 +00:00
|
|
|
|
return String(this);
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#truncate([length = 30[, suffix = '...']]) -> String
|
|
|
|
|
*
|
|
|
|
|
* Truncates a string to given `length` and appends `suffix` to it (indicating
|
|
|
|
|
* that it is only an excerpt).
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function truncate(length, truncation) {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
length = length || 30;
|
2007-11-15 21:53:15 +00:00
|
|
|
|
truncation = Object.isUndefined(truncation) ? '...' : truncation;
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.length > length ?
|
2007-05-31 22:32:25 +00:00
|
|
|
|
this.slice(0, length - truncation.length) + truncation : String(this);
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#strip() -> String
|
|
|
|
|
*
|
|
|
|
|
* Strips all leading and trailing whitespace from a string.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function strip() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.replace(/^\s+/, '').replace(/\s+$/, '');
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#stripTags() -> String
|
|
|
|
|
*
|
|
|
|
|
* Strips a string of any HTML tag.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function stripTags() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.replace(/<\/?[^>]+>/gi, '');
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#stripScripts() -> String
|
|
|
|
|
*
|
|
|
|
|
* Strips a string of anything that looks like an HTML script block.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function stripScripts() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.replace(new RegExp(Prototype.ScriptFragment, 'img'), '');
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#extractScripts() -> Array
|
|
|
|
|
*
|
|
|
|
|
* Exctracts the content of any script block present in the string and returns
|
|
|
|
|
* them as an array of strings.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function extractScripts() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
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];
|
|
|
|
|
});
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#evalScripts() -> Array
|
|
|
|
|
*
|
|
|
|
|
* Evaluates the content of any script block present in the string.
|
|
|
|
|
* Returns an array containing the value returned by each script.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function evalScripts() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.extractScripts().map(function(script) { return eval(script) });
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#escapeHTML() -> String
|
|
|
|
|
*
|
|
|
|
|
* Converts HTML special characters to their entity equivalents.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function escapeHTML() {
|
2008-12-11 03:28:35 +00:00
|
|
|
|
escapeHTML.text.data = this;
|
|
|
|
|
return escapeHTML.div.innerHTML;
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/** related to: String#escapeHTML
|
|
|
|
|
* String#unescapeHTML() -> String
|
|
|
|
|
*
|
|
|
|
|
* Strips tags and converts the entity forms of special HTML characters to their normal form.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function unescapeHTML() {
|
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) : '';
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/** alias of: String#parseQuery, related to: Hash#toQueryString
|
|
|
|
|
* String#toQueryParams([separator = '&']) -> Object
|
|
|
|
|
*
|
|
|
|
|
* Parses a URI-like query string and returns an object composed of parameter/value pairs.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function toQueryParams(separator) {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
var match = this.strip().match(/([^?#]*)(#.*)?$/);
|
2007-08-08 23:19:45 +00:00
|
|
|
|
if (!match) return { };
|
2008-12-11 10:50:51 +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);
|
2008-12-11 10:50:51 +00:00
|
|
|
|
|
2007-03-27 20:55:56 +00:00
|
|
|
|
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;
|
|
|
|
|
});
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#toArray() -> Array
|
|
|
|
|
*
|
|
|
|
|
* Splits the string character-by-character and returns an array with the result.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function toArray() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.split('');
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#succ() -> String
|
|
|
|
|
*
|
|
|
|
|
* Used internally by ObjectRange.
|
|
|
|
|
* Converts the last character of the string to the following character in the
|
|
|
|
|
* Unicode alphabet.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function succ() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.slice(0, this.length - 1) +
|
|
|
|
|
String.fromCharCode(this.charCodeAt(this.length - 1) + 1);
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#times(count) -> String
|
|
|
|
|
*
|
|
|
|
|
* Concatenates the string `count` times.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function times(count) {
|
2007-10-13 19:05:19 +00:00
|
|
|
|
return count < 1 ? '' : new Array(count + 1).join(this);
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#camelize() -> String
|
|
|
|
|
*
|
|
|
|
|
* Converts a string separated by dashes into a camelCase equivalent.
|
|
|
|
|
* For instance, 'foo-bar' would be converted to 'fooBar'.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function camelize() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
var parts = this.split('-'), len = parts.length;
|
|
|
|
|
if (len == 1) return parts[0];
|
2008-12-11 10:50:51 +00:00
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
|
var camelized = this.charAt(0) == '-'
|
|
|
|
|
? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
|
|
|
|
|
: parts[0];
|
2008-12-11 10:50:51 +00:00
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
|
for (var i = 1; i < len; i++)
|
|
|
|
|
camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);
|
2008-12-11 10:50:51 +00:00
|
|
|
|
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return camelized;
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#capitalize() -> String
|
|
|
|
|
*
|
|
|
|
|
* Capitalizes the first letter of a string and downcases all the others.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function capitalize() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#underscore() -> String
|
|
|
|
|
*
|
|
|
|
|
* Converts a camelized string into a series of words separated by an underscore ("_").
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function underscore() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase();
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#dasherize() -> String
|
|
|
|
|
*
|
|
|
|
|
* Replaces every instance of the underscore character ("_") by a dash ("-").
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function dasherize() {
|
2007-01-18 22:24:27 +00:00
|
|
|
|
return this.gsub(/_/,'-');
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-18 22:24:27 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/** related to: Object.inspect
|
|
|
|
|
* String#inspect([useDoubleQuotes = false]) -> String
|
|
|
|
|
*
|
|
|
|
|
* Returns a debug-oriented version of the string (i.e. wrapped in single or
|
|
|
|
|
* double quotes, with backslashes and quotes escaped).
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function inspect(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, '\\\'') + "'";
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/** related to: Object.toJSON
|
|
|
|
|
* String#toJSON() -> String
|
|
|
|
|
*
|
|
|
|
|
* Returns a JSON string.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function toJSON() {
|
2007-03-09 03:23:24 +00:00
|
|
|
|
return this.inspect(true);
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-27 18:45:54 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#unfilterJSON([filter = Prototype.JSONFilter]) -> String
|
|
|
|
|
*
|
|
|
|
|
* Strips comment delimiters around Ajax JSON or JavaScript responses.
|
|
|
|
|
* This security method is called internally.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function unfilterJSON(filter) {
|
2007-04-24 03:31:14 +00:00
|
|
|
|
return this.sub(filter || Prototype.JSONFilter, '#{1}');
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-04-24 03:31:14 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#isJSON() -> Boolean
|
|
|
|
|
*
|
|
|
|
|
* Check if the string is valid JSON by the use of regular expressions.
|
|
|
|
|
* This security method is called internally.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function isJSON() {
|
2007-11-15 19:37:26 +00:00
|
|
|
|
var str = this;
|
|
|
|
|
if (str.blank()) return false;
|
|
|
|
|
str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
|
2007-06-17 16:03:38 +00:00
|
|
|
|
return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#evalJSON([sanitize = false]) -> object
|
|
|
|
|
*
|
|
|
|
|
* Evaluates the JSON in the string and returns the resulting object.
|
|
|
|
|
* If the optional sanitize parameter is set to true, the string is checked for
|
|
|
|
|
* possible malicious attempts and eval is not called if one is detected.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function evalJSON(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());
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#include(substring) -> Boolean
|
|
|
|
|
*
|
|
|
|
|
* Check if the string contains a substring.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function include(pattern) {
|
2007-01-27 18:45:54 +00:00
|
|
|
|
return this.indexOf(pattern) > -1;
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-27 18:45:54 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#startsWith(substring) -> Boolean
|
|
|
|
|
*
|
|
|
|
|
* Checks if the string starts with substring.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function startsWith(pattern) {
|
2007-03-27 17:43:30 +00:00
|
|
|
|
return this.indexOf(pattern) === 0;
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-01-27 18:45:54 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#endsWith(substring) -> Boolean
|
|
|
|
|
*
|
|
|
|
|
* Checks if the string ends with substring.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function endsWith(pattern) {
|
2007-03-27 17:43:30 +00:00
|
|
|
|
var d = this.length - pattern.length;
|
|
|
|
|
return d >= 0 && this.lastIndexOf(pattern) === d;
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#empty() -> Boolean
|
|
|
|
|
*
|
|
|
|
|
* Checks if the string is empty.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function empty() {
|
2007-02-19 21:32:37 +00:00
|
|
|
|
return this == '';
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#blank() -> Boolean
|
|
|
|
|
*
|
|
|
|
|
* Check if the string is 'blank', meaning either empty or containing only whitespace.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function blank() {
|
2007-02-19 21:32:37 +00:00
|
|
|
|
return /^\s*$/.test(this);
|
2008-12-11 10:50:51 +00:00
|
|
|
|
}
|
2007-07-24 17:24:25 +00:00
|
|
|
|
|
2008-12-14 04:36:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* String#interpolate(object[, pattern]) -> String
|
|
|
|
|
*
|
|
|
|
|
* Treats the string as a Template and fills it with object’s properties.
|
|
|
|
|
**/
|
2008-12-11 10:50:51 +00:00
|
|
|
|
function interpolate(object, pattern) {
|
2007-07-24 17:24:25 +00:00
|
|
|
|
return new Template(this, pattern).evaluate(object);
|
2007-01-18 22:24:27 +00:00
|
|
|
|
}
|
2008-12-11 10:50:51 +00:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
gsub: gsub,
|
|
|
|
|
sub: sub,
|
|
|
|
|
scan: scan,
|
|
|
|
|
truncate: truncate,
|
|
|
|
|
strip: strip,
|
|
|
|
|
stripTags: stripTags,
|
|
|
|
|
stripScripts: stripScripts,
|
|
|
|
|
extractScripts: extractScripts,
|
|
|
|
|
evalScripts: evalScripts,
|
|
|
|
|
escapeHTML: escapeHTML,
|
|
|
|
|
unescapeHTML: unescapeHTML,
|
|
|
|
|
toQueryParams: toQueryParams,
|
|
|
|
|
parseQuery: toQueryParams,
|
|
|
|
|
toArray: toArray,
|
|
|
|
|
succ: succ,
|
|
|
|
|
times: times,
|
|
|
|
|
camelize: camelize,
|
|
|
|
|
capitalize: capitalize,
|
|
|
|
|
underscore: underscore,
|
|
|
|
|
dasherize: dasherize,
|
|
|
|
|
inspect: inspect,
|
|
|
|
|
toJSON: toJSON,
|
|
|
|
|
unfilterJSON: unfilterJSON,
|
|
|
|
|
isJSON: isJSON,
|
|
|
|
|
evalJSON: evalJSON,
|
|
|
|
|
include: include,
|
|
|
|
|
startsWith: startsWith,
|
|
|
|
|
endsWith: endsWith,
|
|
|
|
|
empty: empty,
|
|
|
|
|
blank: blank,
|
|
|
|
|
interpolate: interpolate
|
|
|
|
|
};
|
|
|
|
|
})());
|
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() {
|
2009-02-24 02:07:43 +00:00
|
|
|
|
return this.stripTags().replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
|
2007-03-28 11:35:05 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2007-01-27 18:33:03 +00:00
|
|
|
|
Object.extend(String.prototype.escapeHTML, {
|
|
|
|
|
div: document.createElement('div'),
|
|
|
|
|
text: document.createTextNode('')
|
|
|
|
|
});
|
|
|
|
|
|
2008-03-16 19:08:34 +00:00
|
|
|
|
String.prototype.escapeHTML.div.appendChild(String.prototype.escapeHTML.text);
|
2007-01-27 18:33:03 +00:00
|
|
|
|
|