📄 util.js.svn-base
字号:
/* * Copyright 2005 Joe Walker * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//** * Declare an object to which we can add real functions. */if (dwr == null) var dwr = {};if (dwr.util == null) dwr.util = {};if (DWRUtil == null) var DWRUtil = dwr.util;/** @private The flag we use to decide if we should escape html */dwr.util._escapeHtml = true;/** * Set the global escapeHtml flag */dwr.util.setEscapeHtml = function(escapeHtml) { dwr.util._escapeHtml = escapeHtml;}/** @private Work out from an options list and global settings if we should be esccaping */dwr.util._shouldEscapeHtml = function(options) { if (options && options.escapeHtml != null) { return options.escapeHtml; } return dwr.util._escapeHtml;}/** * Return a string with &, <, >, ' and " replaced with their entities * @see TODO */dwr.util.escapeHtml = function(original) { var div = document.createElement('div'); var text = document.createTextNode(original); div.appendChild(text); return div.innerHTML;}/** * Replace common XML entities with characters (see dwr.util.escapeHtml()) * @see TODO */dwr.util.unescapeHtml = function(original) { var div = document.createElement('div'); div.innerHTML = original.replace(/<\/?[^>]+>/gi, ''); return div.childNodes[0] ? div.childNodes[0].nodeValue : '';}/** * Replace characters dangerous for XSS reasons with visually similar characters * @see TODO */dwr.util.replaceXmlCharacters = function(original) { original = original.replace("&", "+"); original = original.replace("<", "\u2039"); original = original.replace(">", "\u203A"); original = original.replace("\'", "\u2018"); original = original.replace("\"", "\u201C"); return original;}/** * Return true iff the input string contains any XSS dangerous characters * @see TODO */dwr.util.containsXssRiskyCharacters = function(original) { return (original.indexOf('&') != -1 || original.indexOf('<') != -1 || original.indexOf('>') != -1 || original.indexOf('\'') != -1 || original.indexOf('\"') != -1);}/** * Enables you to react to return being pressed in an input * @see http://getahead.org/dwr/browser/util/selectrange */dwr.util.onReturn = function(event, action) { if (!event) event = window.event; if (event && event.keyCode && event.keyCode == 13) action();};/** * Select a specific range in a text box. Useful for 'google suggest' type functions. * @see http://getahead.org/dwr/browser/util/selectrange */dwr.util.selectRange = function(ele, start, end) { ele = dwr.util._getElementById(ele, "selectRange()"); if (ele == null) return; if (ele.setSelectionRange) { ele.setSelectionRange(start, end); } else if (ele.createTextRange) { var range = ele.createTextRange(); range.moveStart("character", start); range.moveEnd("character", end - ele.value.length); range.select(); } ele.focus();};/** * Find the element in the current HTML document with the given id or ids * @see http://getahead.org/dwr/browser/util/$ */if (document.getElementById) { dwr.util.byId = function() { var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') { element = document.getElementById(element); } if (arguments.length == 1) { return element; } elements.push(element); } return elements; };}else if (document.all) { dwr.util.byId = function() { var elements = new Array(); for (var i = 0; i < arguments.length; i++) { var element = arguments[i]; if (typeof element == 'string') { element = document.all[element]; } if (arguments.length == 1) { return element; } elements.push(element); } return elements; };}/** * Alias $ to dwr.util.byId * @see http://getahead.org/dwr/browser/util/$ */var $;if (!$) { $ = dwr.util.byId;}/** * This function pretty-prints simple data or whole object graphs, f ex as an aid in debugging. * @see http://getahead.org/dwr/browser/util/todescriptivestring */dwr.util.toDescriptiveString = function(data, showLevels, options) { if (showLevels === undefined) showLevels = 1; var opt = {}; if (dwr.util._isObject(options)) opt = options; var defaultoptions = { escapeHtml:false, baseIndent: "", childIndent: "\u00A0\u00A0", lineTerminator: "\n", oneLineMaxItems: 5, shortStringMaxLength: 13, propertyNameMaxLength: 30 }; for (var p in defaultoptions) if (!(p in opt)) opt[p] = defaultoptions[p]; if (typeof options == "number") { var baseDepth = options; opt.baseIndent = dwr.util._indent2(baseDepth, opt); } var skipDomProperties = { document:true, ownerDocument:true, all:true, parentElement:true, parentNode:true, offsetParent:true, children:true, firstChild:true, lastChild:true, previousSibling:true, nextSibling:true, innerHTML:true, outerHTML:true, innerText:true, outerText:true, textContent:true, attributes:true, style:true, currentStyle:true, runtimeStyle:true, parentTextEdit:true }; function recursive(data, showLevels, indentDepth, options) { var reply = ""; try { // string if (typeof data == "string") { var str = data; if (showLevels == 0 && str.length > options.shortStringMaxLength) str = str.substring(0, options.shortStringMaxLength-3) + "..."; if (options.escapeHtml) { // Do the escape separately for every line as escapeHtml() on some // browsers (IE) will strip line breaks and we want to preserve them var lines = str.split("\n"); for (var i = 0; i < lines.length; i++) lines[i] = dwr.util.escapeHtml(lines[i]); str = lines.join("\n"); } if (showLevels == 0) { // Short format str = str.replace(/\n|\r|\t/g, function(ch) { switch (ch) { case "\n": return "\\n"; case "\r": return ""; case "\t": return "\\t"; } }); } else { // Long format str = str.replace(/\n|\r|\t/g, function(ch) { switch (ch) { case "\n": return options.lineTerminator + indent(indentDepth+1, options); case "\r": return ""; case "\t": return "\\t"; } }); } reply = '"' + str + '"'; } // function else if (typeof data == "function") { reply = "function"; } // Array else if (dwr.util._isArray(data)) { if (showLevels == 0) { // Short format (don't show items) if (data.length > 0) reply = "[...]"; else reply = "[]"; } else { // Long format (show items) var strarr = []; strarr.push("["); var count = 0; for (var i = 0; i < data.length; i++) { if (! (i in data)) continue; var itemvalue = data[i]; if (count > 0) strarr.push(", "); if (showLevels == 1) { // One-line format if (count == options.oneLineMaxItems) { strarr.push("..."); break; } } else { // Multi-line format strarr.push(options.lineTerminator + indent(indentDepth+1, options)); } if (i != count) { strarr.push(i); strarr.push(":"); } strarr.push(recursive(itemvalue, showLevels-1, indentDepth+1, options)); count++; } if (showLevels > 1) strarr.push(options.lineTerminator + indent(indentDepth, options)); strarr.push("]"); reply = strarr.join(""); } } // Objects except Date else if (dwr.util._isObject(data) && !dwr.util._isDate(data)) { if (showLevels == 0) { // Short format (don't show properties) reply = dwr.util._detailedTypeOf(data); } else { // Long format (show properties) var strarr = []; if (dwr.util._detailedTypeOf(data) != "Object") { strarr.push(dwr.util._detailedTypeOf(data)); if (typeof data.valueOf() != "object") { strarr.push(":"); strarr.push(recursive(data.valueOf(), 1, indentDepth, options)); } strarr.push(" "); } strarr.push("{"); var isDomObject = dwr.util._isHTMLElement(data); var count = 0; for (var prop in data) { var propvalue = data[prop]; if (isDomObject) { if (!propvalue) continue; if (typeof propvalue == "function") continue; if (skipDomProperties[prop]) continue; if (prop.toUpperCase() == prop) continue; } if (count > 0) strarr.push(", "); if (showLevels == 1) { // One-line format if (count == options.oneLineMaxItems) { strarr.push("..."); break; } } else { // Multi-line format strarr.push(options.lineTerminator + indent(indentDepth+1, options)); } strarr.push(prop.length > options.propertyNameMaxLength ? prop.substring(0, options.propertyNameMaxLength-3) + "..." : prop); strarr.push(":"); strarr.push(recursive(propvalue, showLevels-1, indentDepth+1, options)); count++; } if (showLevels > 1 && count > 0) strarr.push(options.lineTerminator + indent(indentDepth, options)); strarr.push("}"); reply = strarr.join(""); } } // undefined, null, number, boolean, Date else { reply = "" + data; } return reply; } catch(err) { return (err.message ? err.message : ""+err); } } function indent(count, options) { var strarr = []; strarr.push(options.baseIndent); for (var i=0; i<count; i++) { strarr.push(options.childIndent); } return strarr.join(""); }; return recursive(data, showLevels, 0, opt);}/** * Setup a GMail style loading message. * @see http://getahead.org/dwr/browser/util/useloadingmessage */dwr.util.useLoadingMessage = function(message) { var loadingMessage; if (message) loadingMessage = message; else loadingMessage = "Loading"; dwr.engine.setPreHook(function() { var disabledZone = dwr.util.byId('disabledZone'); if (!disabledZone) { disabledZone = document.createElement('div'); disabledZone.setAttribute('id', 'disabledZone'); disabledZone.style.position = "absolute"; disabledZone.style.zIndex = "1000"; disabledZone.style.left = "0px"; disabledZone.style.top = "0px"; disabledZone.style.width = "100%"; disabledZone.style.height = "100%"; document.body.appendChild(disabledZone); var messageZone = document.createElement('div'); messageZone.setAttribute('id', 'messageZone'); messageZone.style.position = "absolute"; messageZone.style.top = "0px";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -