/*
* 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) {
return original.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
};
/**
* Replace common XML entities with characters (see dwr.util.escapeHtml())
* @see TODO
*/
dwr.util.unescapeHtml = function(original) {
return original.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
};
/**
* 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];
}
}
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) s