AjxStringUtil.js
Summary
No overview generated for 'AjxStringUtil.js'
Class Summary
|
AjxStringUtil |
This class provides static methods to perform miscellaneous string-related utility functions. |
function AjxStringUtil() {
}
AjxStringUtil.TRIM_RE = /^\s+|\s+$/g;
AjxStringUtil.COMPRESS_RE = /\s+/g;
AjxStringUtil.ELLIPSIS = " ... ";
AjxStringUtil.makeString =
function(val) {
return val ? String(val) : "";
};
AjxStringUtil.trim =
function(str, compress, space) {
if (!str) return "";
var trim_re = AjxStringUtil.TRIM_RE;
var compress_re = AjxStringUtil.COMPRESS_RE;
if (space) {
trim_re = new RegExp("^" + space + "+|" + space + "+$", "g");
compress_re = new RegExp(space + "+", "g");
} else {
space = " ";
}
str = str.replace(trim_re, '');
if (compress)
str = str.replace(compress_re, space);
return str;
}
AjxStringUtil.repeat =
function(str, num) {
var text = "";
for (var i = 0; i < num; i++)
text += str;
return text;
}
AjxStringUtil.getUnitsFromSizeString =
function(sizeString) {
var units="px";
if(typeof(sizeString) == "string") {
var digitString=Number(parseInt(sizeString)).toString();
if(sizeString.length > digitString.length) {
units = sizeString.substr(digitString.length, (sizeString.length-digitString.length));
if(!(units=="em" || units=="ex" || units=="px" || units=="in" || units=="cm" == units=="mm" || units=="pt" || units=="pc" || units=="%")) {
units="px";
}
}
}
return units;
}
AjxStringUtil.split =
function(str, dels) {
if (!str) return new Array();
dels = dels ? dels : ',';
var isDel = new Object;
if (typeof dels == 'string') {
isDel[dels] = 1;
} else {
for (var i = 0; i < dels.length; i++)
isDel[dels[i]] = 1;
}
var q = false;
var p = 0;
var start = 0;
var chunk;
var chunks = new Array();
var j = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charAt(i);
if (c == '"') {
q = !q;
} else if (c == '(') {
p++;
} else if (c == ')') {
p--;
} else if (isDel[c]) {
if (!q && !p) {
chunk = str.substring(start, i);
chunks[j++] = chunk;
start = i + 1;
}
}
}
chunk = str.substring(start, str.length);
chunks[j++] = chunk;
return chunks;
}
AjxStringUtil.wordWrap =
function(text, len, pre, eol, breakOkay, compress) {
if (!text) return "";
len = len ? len : 80;
eol = eol ? eol : '\n';
pre = pre ? pre : '';
len -= pre.length;
var chunks = new Array();
var c = 0;
text = AjxStringUtil.trim(text, false);
text = text.replace(/[ \t]+\n/g, '\n');
if (compress)
text = text.replace(/\b\n\b/g, ' ');
var textLen = text.length;
for (var i = 0, bk = 0, sp = -1; i < textLen; i++) {
var ch = text.charAt(i);
if (ch.match(/[ \t]/)) {
sp = i;
}
if (ch == '\n') {
chunks[c++] = pre + text.substring(bk, i);
bk = i + 1;
sp = -1;
}
if (i - bk >= len) {
if (sp == -1) {
if (breakOkay) {
chunks[c++] = pre + text.substring(bk, i);
bk = i;
sp = -1;
}
} else {
chunks[c++] = pre + text.substring(bk, sp);
bk = sp + 1;
sp = -1;
}
}
}
if (i > bk) {
chunks[c++] = pre + text.substring(bk, i);
}
return chunks.join(eol) + eol;
}
AjxStringUtil.IS_PRINT_CODE = new Object();
var print_codes = [32,48,49,50,51,52,53,54,55,56,57,59,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,
81,82,83,84,85,86,87,88,89,90,96,97,98,99,100,101,102,103,104,105,106,107,109,110,111,186,
187,188,189,190,191,192,219,220,221,222];
var l = print_codes.length;
for (var i = 0; i < l; i++) {
AjxStringUtil.IS_PRINT_CODE[print_codes[i]] = true;
}
AjxStringUtil.isPrintKey =
function(keycode) {
return AjxStringUtil.IS_PRINT_CODE[keycode];
}
AjxStringUtil.SHIFT_CHAR = { 48:')', 49:'!', 50:'@', 51:'#', 52:'$', 53:'%', 54:'^', 55:'&', 56:'*', 57:'(',
59:':', 186:':', 187:'+', 188:'<', 189:'_', 190:'>', 191:'?', 192:'~',
219:'{', 220:'|', 221:'}', 222:'"' }
AjxStringUtil.shiftChar =
function(keycode, shifted) {
return shifted ? AjxStringUtil.SHIFT_CHAR[keycode] || String.fromCharCode(keycode) : String.fromCharCode(keycode);
}
AjxStringUtil.diffPoint =
function(str1, str2) {
if (!(str1 && str2))
return 0;
var len = Math.min(str1.length, str2.length);
var i = 0;
while (i < len && (str1.charAt(i) == str2.charAt(i)))
i++;
return i;
}
AjxStringUtil.resolve =
function(str, values) {
DBG.println(AjxDebug.DBG1, "Call to deprecated function AjxStringUtil.resolve");
return AjxMessageFormat.format(str, values);
}
AjxStringUtil.urlEncode =
function(str) {
if (!str) return "";
return escape(str.replace(/ /g, '+')).replace(/[+]/g, '%2B');
}
AjxStringUtil.ENCODE_MAP = { '>' : '>', '<' : '<', '&' : '&' };
AjxStringUtil.htmlEncode =
function(str, includeSpaces) {
if (!str) return "";
if (!AjxEnv.isSafari) {
if (includeSpaces)
return str.replace(/[<>&]/g, function(htmlChar) { return AjxStringUtil.ENCODE_MAP[htmlChar]; }).replace(/ /g, ' ');
else
return str.replace(/[<>&]/g, function(htmlChar) { return AjxStringUtil.ENCODE_MAP[htmlChar]; });
} else {
if (includeSpaces)
return str.replace(/[&]/g, '&').replace(/ /g, ' ').replace(/[<]/g, '<').replace(/[>]/g, '>');
else
return str.replace(/[&]/g, '&').replace(/[<]/g, '<').replace(/[>]/g, '>');
}
}
AjxStringUtil.convertToHtml = function(str) {
if (!str) return "";
str = str
.replace(/&/mg, "&")
.replace(/ /mg, " ")
.replace(/^ /mg, " ")
.replace(/\t/mg, " ")
.replace(/</mg, "<")
.replace(/>/mg, ">")
.replace(/\r?\n/mg, "<br />");
return str;
};
AjxStringUtil.SPACE_ENCODE_MAP = { ' ' : ' ', '>' : '>', '<' : '<', '&' : '&' , '\n': '<br>'};
AjxStringUtil.htmlEncodeSpace =
function(str) {
if (!str) return "";
if (!AjxEnv.isSafari) {
return str.replace(/[ <>&\n]/g, function(htmlChar) { return AjxStringUtil.SPACE_ENCODE_MAP[htmlChar]; });
} else {
return str.replace(/[&]/g, '&').replace(/ /g, ' ').replace(/[<]/g, '<').replace(/[>]/g, '>');
}
}
AjxStringUtil.nl2br =
function(str) {
if (!str) return "";
return str.replace(/^ /mg, " ").replace(/\t/g, "<pre style='display:inline;'>\t</pre>").replace(/\n/g, "<br>");
}
AjxStringUtil.xmlEncode =
function(str) {
return str ? str.replace(/&/g,"&").replace(/</g,"<") : "";
}
AjxStringUtil.xmlDecode =
function(str) {
return str ? str.replace(/&/g,"&").replace(/</g,"<") : "";
}
AjxStringUtil.xmlAttrEncode =
function(str) {
return str ? str.replace(/&/g,"&").replace(/</g,"<").replace(/\x22/g, '"').replace(/\x27/g,"'") : "";
}
AjxStringUtil.xmlAttrDecode =
function(str) {
return str ? str.replace(/&/g,"&").replace(/</g,"<").replace(/"/g, '"').replace(/'/g,"'") : "";
}
AjxStringUtil.regExEscape =
function(str) {
return str.replace(/(\W)/g, "\\$1");
};
var AjxStringUtil_calcDIV = null;
AjxStringUtil.calcDIV =
function() {
if (AjxStringUtil_calcDIV == null) {
AjxStringUtil_calcDIV = document.createElement("div");
AjxStringUtil_calcDIV.style.zIndex = 0;
AjxStringUtil_calcDIV.style.position = DwtControl.ABSOLUTE_STYLE;
AjxStringUtil_calcDIV.style.visibility = "hidden";
document.body.appendChild(AjxStringUtil_calcDIV);
}
return AjxStringUtil_calcDIV;
}
AjxStringUtil.clip =
function(origString, pixelWidth, className) {
var calcDIV = AjxStringUtil.calcDIV();
if (arguments.length == 3) calcDIV.className = className;
calcDIV.innerHTML = origString;
if (calcDIV.offsetWidth <= pixelWidth) return origString;
for (var i=origString.length-1; i>0; i--) {
var newString = origString.substr(0,i);
calcDIV.innerHTML = newString + AjxStringUtil.ELLIPSIS;
if (calcDIV.offsetWidth <= pixelWidth) return newString + AjxStringUtil.ELLIPSIS;
}
return origString;
}
AjxStringUtil.wrap =
function(origString, pixelWidth, className) {
var calcDIV = AjxStringUtil.calcDIV();
if (arguments.length == 3) calcDIV.className = className;
var newString = "";
var newLine = "";
textRows = origString.split("/n");
for (var trCount=0; trCount<textRows.length; trCount++) {
if (trCount != 0) {
newString += newLine + "<br>";
newLine = "";
}
htmlRows = textRows[trCount].split("<br>");
for (var hrCount=0; hrCount<htmlRows.length; hrCount++) {
if (hrCount != 0) {
newString += newLine + "<br>";
newLine = "";
}
words = htmlRows[hrCount].split(" ");
var wCount=0;
while (wCount<words.length) {
calcDIV.innerHTML = newLine + " " + words[wCount];
var newLinePixels = calcDIV.offsetWidth;
if (newLinePixels > pixelWidth) {
calcDIV.innerHTML = words[wCount];
newLinePixels = newLinePixels - calcDIV.offsetWidth;
if ( (newLinePixels >= pixelWidth) || (calcDIV.offsetWidth <= pixelWidth) ) {
newString += newLine + "<br>";
newLine = "";
}
else {
var keepLooping = true;
var atPos = 0;
while (keepLooping) {
atPos++;
calcDIV.innerHTML = newLine + " " + words[wCount].substring(0,atPos);
keepLooping = (calcDIV.offsetWidth <= pixelWidth);
}
atPos--;
newString += newLine + words[wCount].substring(0,atPos) + "<br>";
words[wCount] = words[wCount].substr(atPos);
newLine = "";
}
} else {
newLine += " " + words[wCount];
wCount++;
}
}
}
}
newString += newLine;
return newString;
}
AjxStringUtil.MSG_SEP_RE = new RegExp("^\\s*--+\\s*(" + "Original Message" + "|" + "Forwarded Message" + ")\\s*--+", "i");
AjxStringUtil.SIG_RE = /^(- ?-+)|(__+)\r?$/;
AjxStringUtil.COLON_RE = /\S+:$/;
AjxStringUtil.PREFIX_RE = /^\s*(>|\|)/;
AjxStringUtil.BRACKET_RE = /^\s*\[.+\]\s*$/;
AjxStringUtil.LINE_RE = /^\s*_{30,}\s*$/;
AjxStringUtil.BLANK_RE = /^\s*$/;
AjxStringUtil.HDR_RE = /^\s*\w+:/;
AjxStringUtil.getTopLevel =
function(text, eol) {
eol = eol ? eol : '\n';
text = AjxStringUtil._trimBlankLines(text, eol);
var lines = text.split(eol);
var len = lines.length;
var i = 0, start = 0;
var chunks = new Array();
var skipping = false;
while (i < len) {
var wasSkipping = skipping;
var skip = AjxStringUtil._linesToSkip(lines, i);
skipping = (skip > 0);
if (wasSkipping && !skipping)
start = i;
else if (!wasSkipping && skipping && i > start)
chunks.push(AjxStringUtil._trimBlankLines(lines.slice(start, i).join(eol), eol) + eol);
i += skipping ? skip : 1;
}
if (!skipping && i > start)
chunks.push(AjxStringUtil._trimBlankLines(lines.slice(start, i).join(eol), eol) + eol);
return chunks;
}
AjxStringUtil._linesToSkip =
function(lines, i) {
var len = lines.length;
var skip = 0;
var start = i;
if (AjxStringUtil.MSG_SEP_RE.test(lines[i])) {
skip = len - i;
} else if (AjxStringUtil.SIG_RE.test(lines[i])) {
skip = len - i;
} else if (AjxStringUtil.PREFIX_RE.test(lines[i])) {
while (i < lines.length && (AjxStringUtil.PREFIX_RE.test(lines[i]) || AjxStringUtil.BLANK_RE.test(lines[i])))
i++;
skip = i - start;
} else if (AjxStringUtil.COLON_RE.test(lines[i])) {
var idx = AjxStringUtil._nextNonBlankLineIndex(lines, i + 1);
var line1 = (idx != -1) ? lines[idx] : null;
if (line1 && AjxStringUtil.PREFIX_RE.test(line1)) {
skip = idx - i;
} else {
if (idx != -1)
idx = AjxStringUtil._nextNonBlankLineIndex(lines, idx + 1);
var line2 = (idx != -1) ? lines[idx] : null;
if (line2 && AjxStringUtil.BRACKET_RE.test(line1) && AjxStringUtil.PREFIX_RE.test(line2))
skip = idx - i;
}
} else if (AjxStringUtil.LINE_RE.test(lines[i])) {
var idx = AjxStringUtil._nextNonBlankLineIndex(lines, i + 1);
var line1 = (idx != -1) ? lines[idx] : null;
if (line1 && AjxStringUtil.HDR_RE.test(line1))
skip = len - i;
}
return skip;
}
AjxStringUtil._nextNonBlankLineIndex =
function(lines, i) {
while (i < lines.length && AjxStringUtil.BLANK_RE.test(lines[i]))
i++;
return ((i < lines.length) ? i : -1);
}
AjxStringUtil._trimBlankLines =
function(text, eol) {
eol = eol ? eol : '\n';
var lines = text.split(eol);
var len = lines.length;
var i = 0;
while (i < len && AjxStringUtil.BLANK_RE.test(lines[i]))
i++;
var j = len;
while (j > 0 && AjxStringUtil.BLANK_RE.test(lines[j - 1]))
j--;
if (i != 0 || j != len)
text = lines.slice(i, j).join(eol) + eol;
return text;
}
AjxStringUtil._NO_LIST = 0;
AjxStringUtil._ORDERED_LIST = 1;
AjxStringUtil._UNORDERED_LIST = 2;
AjxStringUtil._INDENT = " ";
AjxStringUtil._NON_WHITESPACE = /\S+/;
AjxStringUtil._LF = /\n/;
AjxStringUtil.convertHtml2Text =
function(domRoot) {
if (!domRoot) return null;
var text = new Array();
var idx = 0;
var ctxt = new Object();
this._traverse(domRoot, text, idx, AjxStringUtil._NO_LIST, 0, 0, ctxt);
var textStr = text.join("");
return textStr;
}
AjxStringUtil._traverse =
function(el, text, idx, listType, listLevel, bulletNum, ctxt) {
var nodeName = el.nodeName.toLowerCase();
if (nodeName == "#text") {
if (el.nodeValue.search(AjxStringUtil._NON_WHITESPACE) != -1) {
if (ctxt.lastNode == "ol" || ctxt.lastNode == "ul")
text[idx++] = "\n";
text[idx++] = AjxStringUtil.trim(el.nodeValue.replace(AjxStringUtil._LF, " "), true) + " ";
}
} else if (nodeName == "p") {
text[idx++] = "\n\n";
} else if (listType == AjxStringUtil._NO_LIST && (nodeName == "br" || nodeName == "hr")) {
text[idx++] = "\n";
} else if (nodeName == "ol" || nodeName == "ul") {
text[idx++] = "\n";
if (el.parentNode.nodeName.toLowerCase() != "li" && ctxt.lastNode != "br"
&& ctxt.lastNode != "hr")
text[idx++] = "\n";
listType = (nodeName == "ol") ? AjxStringUtil._ORDERED_LIST : AjxStringUtil._UNORDERED_LIST;
listLevel++;
bulletNum = 0;
} else if (nodeName == "li") {
for (var i = 0; i < listLevel; i++)
text[idx++] = AjxStringUtil._INDENT;
if (listType == AjxStringUtil._ORDERED_LIST)
text[idx++] = bulletNum + ". ";
else
text[idx++] = "\u2022 ";
} else if (nodeName == "img") {
if (el.alt && el.alt != "")
text[idx++] = el.alt;
} else if (nodeName == "tr" && el.parentNode.firstChild != el) {
text[idx++] = "\n";
} else if (nodeName == "td" && el.parentNode.firstChild != el) {
text[idx++] = "\t";
} else if (nodeName == "div") {
text[idx++] = "\n";
} else if (nodeName == "#comment" ||
nodeName == "script" ||
nodeName == "select" ||
nodeName == "style") {
return idx;
}
var childNodes = el.childNodes;
var len = childNodes.length;
for (var i = 0; i < len; i++) {
if (nodeName == "ol")
bulletNum++;
idx = this._traverse(childNodes[i], text, idx, listType, listLevel, bulletNum, ctxt);
}
if (nodeName == "h1" || nodeName == "h2" || nodeName == "h3" || nodeName == "h4"
|| nodeName == "h5" || nodeName == "h6") {
text[idx++] = "\n";
ctxt.list = false;
} else if (nodeName == "li") {
if (!ctxt.list)
text[idx++] = "\n";
ctxt.list = false;
} else if (nodeName == "ol" || nodeName == "ul") {
ctxt.list = true;
} else if (nodeName != "#text") {
ctxt.list = false;
}
ctxt.lastNode = nodeName;
return idx;
}
Documentation generated by
JSDoc on Thu Apr 27 13:58:41 2006