?? prototype.js
字號:
/** * Escapes any HTML characters in a string. * @alias String.escapeHTML() * @return {String} Returns the string with the HTML characters escaped. * @extends {String} */ escapeHTML: function() { var self = arguments.callee; self.text.data = this; return self.div.innerHTML; }, /** * Converts any escaped HTML characters in a string to real HTML characters. * @alias String.unescapeHTML() * @return {String} Returns the string with HTML characters unescaped. * @extends {String} * unescapeHTML: function() { var div = document.createElement('div'); div.innerHTML = this.stripTags(); return div.childNodes[0] ? (div.childNodes.length > 1 ? $A(div.childNodes).inject('', function(memo, node) { return memo+node.nodeValue }) : div.childNodes[0].nodeValue) : ''; }, /** * Creates an associative array (similar to a hash) from a string using parameter names as an index. * @alias String.toQueryParams() * @return {Array} Returns an associative array from the string. * @extends {String} */ toQueryParams: function(separator) { var match = this.strip().match(/([^?#]*)(#.*)?$/); if (!match) return {}; return match[1].split(separator || '&').inject({}, function(hash, pair) { if ((pair = pair.split('='))[0]) { var key = decodeURIComponent(pair.shift()); var value = pair.length > 1 ? pair.join('=') : pair[0]; if (value != undefined) value = decodeURIComponent(value); if (key in hash) { if (hash[key].constructor != Array) hash[key] = [hash[key]]; hash[key].push(value); } else hash[key] = value; } return hash; }); }, /** * Creates an array from the characters of a string. * @alias String.toArray() * @return {Array} Returns an array of all of the characters in a string. * @extends {String} */ toArray: function() { return this.split(''); }, /** * Used internally by ObjectRange. Converts the last character of the string to the following character in the Unicode alphabet. * @alias String.succ * @return {String} Returns the converted string. * @extends {String} */ succ: function() { return this.slice(0, this.length - 1) + String.fromCharCode(this.charCodeAt(this.length - 1) + 1); }, /** * Concatenates the string count times. * @alias String.times * @param {Number} count Number of times to concatenate the string. * @return {String} Returns the concatenated string. * @extends {String} */ times: function(count) { var result = ''; for (var i = 0; i < count; i++) result += this; return result; }, /** * Converts a hyphen-delimited string to camel case. (e.g. thisIsCamelCase) * @alias String.camelize() * @return {String} Returns the string as camel case. * @extends {String} */ 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; }, /** * Capitalizes the first letter of a string and downcases all the others. * @alias String.capitalize * @return {String} Returns the capitalized string. * @extends {String} */ capitalize: function() { return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase(); }, /** * Converts a camelized string into a series of words separated by an underscore ("_"). * @alias String.underscore * @return {String} Converts a camelized string into a series of words separated by an underscore ("_"). * @extends {String} */ underscore: function() { return this.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'#{1}_#{2}').gsub(/([a-z\d])([A-Z])/,'#{1}_#{2}').gsub(/-/,'_').toLowerCase(); }, /** * Replaces every instance of the underscore character ("_") by a dash ("-"). * @alias String.dasherize * @return {String} Replaces every instance of the underscore character ("_") by a dash ("-"). * @extends {String} */ dasherize: function() { return this.gsub(/_/,'-'); }, /** * Converts the string to human-readable characters. * @alias String.inspect() * @return {String} Returns a version of the string that can easily be read by humans. * @extends {String} */ inspect: function(useDoubleQuotes) { 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, '\\\'') + "'"; }, /** * Returns a JSON string. * @alias String.toJSON * @return {String} Returns a JSON string. * @extends {String} */ toJSON: function() { return this.inspect(true); }, /** * Strips comment delimiters around Ajax JSON or JavaScript responses. This security method is called internally. * @alias String.unfilterJSON * @param {String} filter Filter to use. * @return {String} Returns the unfiltered string. * @extends {String} */ unfilterJSON: function(filter) { return this.sub(filter || Prototype.JSONFilter, '#{1}'); }, /** * Check if the string is valid JSON by the use of regular expressions. This security method is called internally. * @alias String.isJSON * @return {Boolean} Returns true if the string is a JSON string. * @extends {String} */ isJSON: function() { var str = this.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''); return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str); }, /** * 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. * @alias String.evalJSON * @param {Object} sanitize If true, checks for malicious syntax. * @return {Object} Returns the result of the evaluation. * @extends {String} */ evalJSON: function(sanitize) { var json = this.unfilterJSON(); try { if (!sanitize || json.isJSON()) return eval('(' + json + ')'); } catch (e) { } throw new SyntaxError('Badly formed JSON string: ' + this.inspect()); }, /** * Check if the string contains a pattern. * @alias String.include * @param {String} pattern Pattern to search for. * @return {Boolean} Returns true if the string contains a pattern. * @extends {String} */ include: function(pattern) { return this.indexOf(pattern) > -1; }, /** * Checks if the string starts with pattern. * @alias String.startsWith * @param {String} pattern Pattern to search for. * @return {Boolean} Returns true if the string starts with a pattern. * @extends {String} */ startsWith: function(pattern) { return this.indexOf(pattern) === 0; }, /** * Checks if the string ends with pattern. * @alias String.startsWith * @param {String} pattern Pattern to search for. * @return {Boolean} Returns true if the string starts with a pattern. * @extends {String} */ endsWith: function(pattern) { var d = this.length - pattern.length; return d >= 0 && this.lastIndexOf(pattern) === d; }, /** * Checks if the string is empty. * @alias String.empty * @return {Boolean} Returns true if the string is empty. * @extends {String} */ empty: function() { return this == ''; }, /** * Check if the string is 'blank', meaning either empty or containing only whitespace. * @alias String.blank * @return {Boolean} Returns true if the string is blank. * @extends {String} */ blank: function() { return /^\s*$/.test(this); }});if (Prototype.Browser.WebKit || Prototype.Browser.IE) Object.extend(String.prototype, { escapeHTML: function() { return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); }, unescapeHTML: function() { return this.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>'); }});String.prototype.gsub.prepareReplacement = function(replacement) { if (typeof replacement == 'function') return replacement; var template = new Template(replacement); return function(match) { return template.evaluate(match) };}String.prototype.parseQuery = String.prototype.toQueryParams;Object.extend(String.prototype.escapeHTML, { div: document.createElement('div'), text: document.createTextNode('')});with (String.prototype.escapeHTML) div.appendChild(text);/** * @classDescription Any time you have a group of similar objects and you need to produce formatted output for these objects, maybe inside a loop, you typically resort to concatenating string literals with the object's fields. There's nothing wrong with the above approach, except that it is hard to visualize the output immediately just by glancing at the concatenation expression. The Template class provides a much nicer and clearer way of achieving this formatting. */var Template = Class.create();Template.Pattern = /(^|.|\r|\n)(#\{(.*?)\})/;Template.prototype = { initialize: function(template, pattern) { this.template = template.toString(); this.pattern = pattern || Template.Pattern; }, /** * Applies the template to the given object's data, producing a formatted string with symbols replaced by corresponding object's properties. * @alias Template.evaluate * @param {Object} object Object containing data. * @return {Object} Returns the formatted data. * @extends {Template} */ evaluate: function(object) { return this.template.gsub(this.pattern, function(match) { var before = match[1]; if (before == '\\') return match[2]; return before + String.interpret(object[match[3]]); }); }}var $break = {}, $continue = new Error('"throw $continue" is deprecated, use "return" instead');/** * @classDescription Allows you to easily iterate items in a list. */var Enumerable = { /** * Calls the specified iterator function. * @alias Enumerable.each * @param {Function} iterator Iterator function to call. Takes the arguments elementValue, and elementIndex, respectively. */ each: function(iterator) { var index = 0; try { this._each(function(value) { iterator(value, index++); }); } catch (e) { if (e != $break) throw e; } return this; }, /** * Groups items in chunks based on a given size, with last chunk being possibly smaller. * @alias Enumerable.eachSlice * @param {Number} number Number in each slice * @param {Object} iterator Iterator to use * @return {Object} Returns formatted items. */ eachSlice: function(number, iterator) { var index = -number, slices = [], array = this.toArray(); while ((index += number) < array.length) slices.push(array.slice(index, index+number)); return slices.map(iterator); }, /** * Calls an iterator function to test the values in a list to see if they are all true. * @alias Enumerable.all * @param {Function} iterator Iterator function to call. Takes the arguments elementValue, and elementIndex, respectively. * @return {Boolean} Returns true if the iterator returns true for all elements. */ all: function(iterator) { var result = true; this.each(function(value, index) { result = result && !!(iterator || Prototype.K)(value, index); if (!result) throw $break; }); return result; }, /** * Calls an iterator function to test the values in a list to see if any are true. * @alias Enumerable.any * @param {Function} iterator Iterator function to call. Takes the arguments elementValue, and elementIndex, respectively. * @return {Boolean} Returns true if any of the iterator returns true for any of the elements. */ any: function(iterator) { var result = false; this.each(function(value, index) { if (result = !!(iterator || Prototype.K)(value, index)) throw $break; }); return result; }, /** * Calls an iterator function and returns the results in an Array. * @alias Enumerable.collect * @param {Function} iterator Iterator function to call. Takes the arguments elementValue, and elementIndex, respectively. * @return {Array} Array of the results of calling the iterator on each element. */ collect: function(iterator) { var results = []; this.each(function(value, index) { results.push((iterator || Prototype.K)(value, index)); }); return results; }, /** * Calls an iterator function on the elements in a list and returns the first element that causes the iterator to return true. * @alias Enumerable.detect * @param {Function} iterator Iterator function to call. Takes the arguments elementValue, and elementIndex, respectively. * @return {Object} Returns the first element that causes the iterator function to return true. */ detect: function(iterator) { var result; this.each(function(value, index) { if (iterator(value, index)) { result = value; throw $break; } }); return result; }, /** * Calls an iterator function on the elements in a list and returns all of the elements that cause the iterator to return true. * @alias Enumerable.findAll * @param {Function} iterator Iterator function to call. Takes the arguments elementValue, and elementIndex, respectively. * @return {Array} Returns the elements that the cause the iterator to return true. */ findAll: function(iterator) { var results = []; this.each(function(value, index) { if (iterator(value, index)) results.push(value); }); return results; }, /** * Tests each element in a list to see if it contains the specified regular expression. * @alias Enumerable.grep * @param {RegExp} pattern RegExp to match.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -