1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
var foswiki; if (foswiki == undefined) foswiki = {};
foswiki.String = {
/**
Checks if a string is a WikiWord.
@param inValue : string to test
@return True if a WikiWord, false if not.
*/
isWikiWord:function(inValue) {
if (!inValue) return false;
var re = new RegExp(foswiki.StringConstants.getInstance().WIKIWORD_REGEX);
return (inValue.match(re)) ? true : false;
},
/**
Capitalizes words in the string. For example: "A handy dictionary" becomes "A Handy Dictionary".
@param inValue : (String) text to convert
@return The capitalized text.
*/
capitalize:function(inValue) {
if (!inValue) return null;
var re = new RegExp("[" + foswiki.StringConstants.getInstance().MIXED_ALPHANUM_CHARS + "]+", "g");
return inValue.replace(re, function(a) {
return a.charAt(0).toLocaleUpperCase() + a.substr(1);
});
},
/**
Checks if a string is a 'boolean string'.
@param inValue : (String) text to check
Returns True if the string is either "on", "true" or "1"; otherwise: false.
*/
isBoolean:function(inValue) {
return (inValue == "on") || (inValue == "true") || (inValue == "1");
},
/**
Removes spaces from a string. For example: "A Handy Dictionary" becomes "AHandyDictionary".
@param inValue : the string to remove spaces from
@return A new string free from spaces.
*/
removeSpaces:function(inValue) {
if (!inValue) return null;
var sIn = inValue;
var sOut = '';
for ( var i = 0; i < sIn.length; i++ ) {
ch = sIn.charAt( i );
if( ch==' ' ) {
chgUpper = true;
continue;
}
sOut += ch;
}
return sOut;
},
/**
Removes punctuation characters from a string by stripping all characters except for MIXED_ALPHANUM_CHARS. For example: "A / Z" becomes "AZ".
@param inValue : the string to remove chars from
@return A new string free from punctuation characters.
*/
removePunctuation:function(inValue) {
if (!inValue) return null;
var allowedRegex = "[^" + foswiki.StringConstants.getInstance().MIXED_ALPHANUM_CHARS + "]";
var re = new RegExp(allowedRegex, "g");
return inValue.replace(re, "");
},
/**
Creates a WikiWord from a string. For example: "A handy dictionary" becomes "AHandyDictionary".
@param inValue : (String) the text to convert to a WikiWord
@return A new WikiWord string.
*/
makeWikiWord:function(inValue) {
if (!inValue) return null;
return foswiki.String.removePunctuation(foswiki.String.capitalize(inValue));
},
/**
Makes a text safe to insert in a Foswiki table. Any table-breaking characters are replaced.
@param inText: (String) the text to make safe
@return table-safe text.
*/
makeSafeForTableEntry:function(inText) {
if (inText.length == 0) return "";
var safeString = inText;
var re;
// replace \n by \r
re = new RegExp(/\r/g);
safeString = safeString.replace(re, "\n");
// replace pipes by forward slashes
re = new RegExp(/\|/g);
safeString = safeString.replace(re, "/");
// replace double newlines
re = new RegExp(/\n\s*\n/g);
safeString = safeString.replace(re, "%<nop>BR%%<nop>BR%");
// replace single newlines
re = new RegExp(/\n/g);
safeString = safeString.replace(re, "%<nop>BR%");
// make left-aligned by appending a space
safeString += " ";
return safeString;
}
}
/*
Unicode conversion tools:
Convert text to hexadecimal Unicode escape sequence (\uXXXX)
http://www.hot-tips.co.uk/useful/unicode_converter.HTML
Convert hexadecimal Unicode escape sequence (\uXXXX) to text
http://www.hot-tips.co.uk/useful/unicode_convert_back.HTML
More international characters in foswikiStringUnicodeChars.js
Import file when international support is needed:
<script type="text/javascript" src="%PUBURLPATH%/%SYSTEMWEB%/JavascriptFiles/foswikiStringUnicodeChars.js"></script>
foswikiStringUnicodeChars.js will overwrite the regexes below:
Info on unicode: http://www.fileformat.info/info/unicode/
*/
foswiki.StringConstants = function () {
this.init();
}
foswiki.StringConstants.__instance__ = null; // define the static property
foswiki.StringConstants.getInstance = function () {
if (this.__instance__ == null) {
this.__instance__ = new foswiki.StringConstants();
}
return this.__instance__;
}
foswiki.StringConstants.prototype.UPPER_ALPHA_CHARS = "A-Z";
foswiki.StringConstants.prototype.LOWER_ALPHA_CHARS = "a-z";
foswiki.StringConstants.prototype.NUMERIC_CHARS = "\\d";
foswiki.StringConstants.prototype.MIXED_ALPHA_CHARS;
foswiki.StringConstants.prototype.MIXED_ALPHANUM_CHARS;
foswiki.StringConstants.prototype.LOWER_ALPHANUM_CHARS;
foswiki.StringConstants.prototype.WIKIWORD_REGEX;
foswiki.StringConstants.prototype.ALLOWED_URL_CHARS;
foswiki.StringConstants.prototype.init = function () {
foswiki.StringConstants.prototype.MIXED_ALPHA_CHARS = foswiki.StringConstants.prototype.LOWER_ALPHA_CHARS + foswiki.StringConstants.prototype.UPPER_ALPHA_CHARS;
foswiki.StringConstants.prototype.MIXED_ALPHANUM_CHARS = foswiki.StringConstants.prototype.MIXED_ALPHA_CHARS + foswiki.StringConstants.prototype.NUMERIC_CHARS;
foswiki.StringConstants.prototype.LOWER_ALPHANUM_CHARS = foswiki.StringConstants.prototype.LOWER_ALPHA_CHARS + foswiki.StringConstants.prototype.NUMERIC_CHARS;
foswiki.StringConstants.prototype.WIKIWORD_REGEX = "^" + "[" + foswiki.StringConstants.prototype.UPPER_ALPHA_CHARS + "]" + "+" + "[" + foswiki.StringConstants.prototype.LOWER_ALPHANUM_CHARS + "]" + "+" + "[" + foswiki.StringConstants.prototype.UPPER_ALPHA_CHARS + "]" + "+" + "[" + foswiki.StringConstants.prototype.MIXED_ALPHANUM_CHARS + "]" + "*";
foswiki.StringConstants.prototype.ALLOWED_URL_CHARS = foswiki.StringConstants.prototype.MIXED_ALPHANUM_CHARS + "-_^";
}
|