home *** CD-ROM | disk | FTP | other *** search
- //
- // Copyright 1999 Macromedia, Inc. All rights reserved.
- //
- //string.js
- //
- //Generic set of functions for manipulating and parsing text strings
- //
- //------------------------------------------------------------------
- //
- //
- //extractArgs(behFnCallStr){
- //escQuotes(theStr){
- //unescQuotes(theStr){
- //errMsg() {
- //badChars(theStr){
- //getParam(tagStr,param){
- //quote(textStr,quoteType){
- //stripSpaces(theStr) {
-
- //Given a function call, extracts the args and returns them in array
-
- //Respects ', skips over stuff between quotes, and returns them dequoted.
- //IMPORTANT: argArray[0] is the function call!! Actual args start at argArray[1].
-
- function extractArgs(behFnCallStr){
- var i, theStr, lastPos, argArray;
- argArray = getTokens(behFnCallStr,"(),");
- for (i=0; i<argArray.length; i++) {
- theStr = stripSpaces(unescQuotes(argArray[i]));
- lastPos = theStr.length-1;
- if (theStr.charAt(0) == "'" && lastPos > 0 && theStr.charAt(lastPos) == "'")
- argArray[i] = theStr.substring(1,lastPos);
- }
- return argArray
- }
-
-
-
- //Passed a string, finds special chars '"\ and escapes them with \
-
- function escQuotes(theStr){
- var i, theChar, escStr = "";
- for(var i=0; i<theStr.length; i++) {
- theChar = theStr.charAt(i);
- escStr += (theChar=='"' || theChar=="'" || theChar=="\\")?("\\"+theChar):theChar;
- }
- return escStr;
- }
-
-
- //Passed a string, finds any escape chars \ and removes them
-
- function unescQuotes(theStr){
- var strLen, i, theChar, unescStr = "";
- strLen = theStr.length;
- for(i=0; i<strLen; i++) {
- theChar = theStr.charAt(i);
- if (theChar == "\\" && i < strLen - 1) //if escape char and not end
- theChar = theStr.charAt(++i); //append next char and skip over
- unescStr += theChar;
- }
- return unescStr;
- }
-
-
- //Emulates printf("blah blah %s blah %s",str1,str2)
- //Used for concatenating error message for easier localization.
- //Returns assembled string.
-
- function errMsg() {
- var i,numArgs,errStr="",argNum=0,startPos;
-
- numArgs = errMsg.arguments.length;
- if (numArgs) {
- theStr = errMsg.arguments[argNum++];
- startPos = 0; endPos = theStr.indexOf("%s",startPos);
- if (endPos == -1) endPos = theStr.length;
- while (startPos < theStr.length) {
- errStr += theStr.substring(startPos,endPos);
- if (argNum < numArgs) errStr += errMsg.arguments[argNum++];
- startPos = endPos+2; endPos = theStr.indexOf("%s",startPos);
- if (endPos == -1) endPos = theStr.length;
- }
- if (!errStr) errStr = errMsg.arguments[0];
- }
- return errStr;
- }
-
-
- //Passed a string, finds removes special chars '"! and space
-
- function badChars(theStr){
- var i,theChar,isBad=false;
- var someBadChars = " ~!@#$%^&*()_+|`-=\\{}[]:\";'<>,./?";
- for (i=0; i<theStr.length; i++) {
- theChar = theStr.charAt(i);
- if (someBadChars.indexOf(theChar) != -1) isBad = true;
- }
- return isBad;
- }
-
-
- //Custom non-Javascript code to extract tags and get object names.
- //Passed HTML tag (ie IMG), gets the current doc source
- //HTML and returns an array of names (empty if unnamed).
- //This argument is not case sensitive (can be LAYER, Layer, or layer).
- //For Example, given <IMG NAME="myPhoto"> <IMG><IMG name="bobsPhoto">
- //returns array: myPhoto,,bobsPhoto
-
- function getParam(tagStr,param){
- var j,tokenString;
- var theName = "";
- var tokenArray = new Array;
- tokenArray = getTokens(tagStr," =<>");
- for (j=0; j<(tokenArray.length - 1); j++) {
- tokenString = tokenArray[j].toUpperCase(); //force UPPER CASE
- if (tokenString.indexOf(param.toUpperCase()) == 0) { //found name
- theName = tokenArray[j+1]; //should return single quoted element in array
- firstChar = theName.charAt(0);
- lastChar = theName.charAt(theName.length - 1);
- if ((firstChar == lastChar) && (firstChar == "'" || firstChar == "\""))
- theName = theName.substring(1,theName.length - 1);
- break;
- } }
- return theName;
- }
-
- //function: quote
- //description: wraps text string in single or double quotes
- //argument - textStr
- // quote type - use 1 for single quotes and 2 for double quotes
-
- function quote(textStr,quoteType){
- var quote = (quoteType == 1)?"'":'"';
- return quote + textStr + quote;
- }
-
-
- //Removes any spaces at the beginning or end of the string
-
- function stripSpaces(theStr) {
- if (!theStr) theStr = ""; //ensure its not null
- theStr = theStr.replace(/^\s*/,""); //strip leading
- theStr = theStr.replace(/\s*$/,""); //strip trailing
- return theStr;
- }
-
-
- //Given an object reference string, returns the object name. For ex:
- // objName = getNameFromRef("document.image1"); //returns "image1"
- // objName = getNameFromRef("document.layers['image1']"); //returns "image1"
- //
- //If given an object in a frame, returns the objName?frameNameOrNum:
- // objName = getNameFromRef("parent.frames['main'].document.image1"); //returns "image1?main"
- //This is an expected value for MM_findObj().
-
- function getNameFromRef(objRefStr) {
- var c, startPos, objName=objRefStr, frameSearch;
- var lastDot = objRefStr.lastIndexOf(".");
- var lastBracket = objRefStr.lastIndexOf("]");
-
- if (lastDot != -1 || lastBracket != -1) {
- if (lastDot > lastBracket) { //name after a dot
- objName = objRefStr.substring(lastDot+1);
- } else { //name in brackets
- while (lastBracket > 0 && ((c=objRefStr.charAt(lastBracket-1))=="'" || c=='"' || c=="\\"))
- lastBracket--; //skip ',",\
- startPos = lastBracket-1; //start at end of name
- while (startPos > 0 && ((c=objRefStr.charAt(startPos))!="'" && c!='"' && c!="\\" && c!="["))
- startPos--; //seek ',",\,[
- objName = objRefStr.substring(startPos+1,lastBracket);
- }
- frameSearch = objRefStr.match(/\.frames\[\\?['"]?([^'"\\]+)\\?['"]?\]/); //find .frames['foo'] or .frames[3]
- if (frameSearch && frameSearch[1]) { //if framename, add after a question mark
- objName += "?"+frameSearch[1];
- }
- }
- return objName;
- }
-