String2.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-String'>/**
  19. </span> * @class Ext.String
  20. *
  21. * A collection of useful static methods to deal with strings
  22. * @singleton
  23. */
  24. Ext.String = (function() {
  25. var trimRegex = /^[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]+|[\x09\x0a\x0b\x0c\x0d\x20\xa0\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000]+$/g,
  26. escapeRe = /('|\\)/g,
  27. formatRe = /\{(\d+)\}/g,
  28. escapeRegexRe = /([-.*+?\^${}()|\[\]\/\\])/g,
  29. basicTrimRe = /^\s+|\s+$/g,
  30. whitespaceRe = /\s+/,
  31. varReplace = /(^[^a-z]*|[^\w])/gi,
  32. charToEntity,
  33. entityToChar,
  34. charToEntityRegex,
  35. entityToCharRegex,
  36. htmlEncodeReplaceFn = function(match, capture) {
  37. return charToEntity[capture];
  38. },
  39. htmlDecodeReplaceFn = function(match, capture) {
  40. return (capture in entityToChar) ? entityToChar[capture] : String.fromCharCode(parseInt(capture.substr(2), 10));
  41. };
  42. return {
  43. <span id='Ext-String-method-createVarName'> /**
  44. </span> * Converts a string of characters into a legal, parseable Javascript `var` name as long as the passed
  45. * string contains at least one alphabetic character. Non alphanumeric characters, and *leading* non alphabetic
  46. * characters will be removed.
  47. * @param {String} s A string to be converted into a `var` name.
  48. * @return {String} A legal Javascript `var` name.
  49. */
  50. createVarName: function(s) {
  51. return s.replace(varReplace, '');
  52. },
  53. <span id='Ext-String-method-htmlEncode'> /**
  54. </span> * Convert certain characters (&amp;, &lt;, &gt;, ', and &quot;) to their HTML character equivalents for literal display in web pages.
  55. * @param {String} value The string to encode
  56. * @return {String} The encoded text
  57. * @method
  58. */
  59. htmlEncode: function(value) {
  60. return (!value) ? value : String(value).replace(charToEntityRegex, htmlEncodeReplaceFn);
  61. },
  62. <span id='Ext-String-method-htmlDecode'> /**
  63. </span> * Convert certain characters (&amp;, &lt;, &gt;, ', and &quot;) from their HTML character equivalents.
  64. * @param {String} value The string to decode
  65. * @return {String} The decoded text
  66. * @method
  67. */
  68. htmlDecode: function(value) {
  69. return (!value) ? value : String(value).replace(entityToCharRegex, htmlDecodeReplaceFn);
  70. },
  71. <span id='Ext-String-method-addCharacterEntities'> /**
  72. </span> * Adds a set of character entity definitions to the set used by
  73. * {@link Ext.String#htmlEncode} and {@link Ext.String#htmlDecode}.
  74. *
  75. * This object should be keyed by the entity name sequence,
  76. * with the value being the textual representation of the entity.
  77. *
  78. * Ext.String.addCharacterEntities({
  79. * '&amp;amp;Uuml;':'Ü',
  80. * '&amp;amp;ccedil;':'ç',
  81. * '&amp;amp;ntilde;':'ñ',
  82. * '&amp;amp;egrave;':'è'
  83. * });
  84. * var s = Ext.String.htmlEncode(&quot;A string with entities: èÜçñ&quot;);
  85. *
  86. * Note: the values of the character entites defined on this object are expected
  87. * to be single character values. As such, the actual values represented by the
  88. * characters are sensitive to the character encoding of the javascript source
  89. * file when defined in string literal form. Script tasgs referencing server
  90. * resources with character entities must ensure that the 'charset' attribute
  91. * of the script node is consistent with the actual character encoding of the
  92. * server resource.
  93. *
  94. * The set of character entities may be reset back to the default state by using
  95. * the {@link Ext.String#resetCharacterEntities} method
  96. *
  97. * @param {Object} entities The set of character entities to add to the current
  98. * definitions.
  99. */
  100. addCharacterEntities: function(newEntities) {
  101. var charKeys = [],
  102. entityKeys = [],
  103. key, echar;
  104. for (key in newEntities) {
  105. echar = newEntities[key];
  106. entityToChar[key] = echar;
  107. charToEntity[echar] = key;
  108. charKeys.push(echar);
  109. entityKeys.push(key);
  110. }
  111. charToEntityRegex = new RegExp('(' + charKeys.join('|') + ')', 'g');
  112. entityToCharRegex = new RegExp('(' + entityKeys.join('|') + '|&amp;#[0-9]{1,5};' + ')', 'g');
  113. },
  114. <span id='Ext-String-method-resetCharacterEntities'> /**
  115. </span> * Resets the set of character entity definitions used by
  116. * {@link Ext.String#htmlEncode} and {@link Ext.String#htmlDecode} back to the
  117. * default state.
  118. */
  119. resetCharacterEntities: function() {
  120. charToEntity = {};
  121. entityToChar = {};
  122. // add the default set
  123. this.addCharacterEntities({
  124. '&amp;amp;' : '&amp;',
  125. '&amp;gt;' : '&gt;',
  126. '&amp;lt;' : '&lt;',
  127. '&amp;quot;' : '&quot;',
  128. '&amp;#39;' : &quot;'&quot;
  129. });
  130. },
  131. <span id='Ext-String-method-urlAppend'> /**
  132. </span> * Appends content to the query string of a URL, handling logic for whether to place
  133. * a question mark or ampersand.
  134. * @param {String} url The URL to append to.
  135. * @param {String} string The content to append to the URL.
  136. * @return {String} The resulting URL
  137. */
  138. urlAppend : function(url, string) {
  139. if (!Ext.isEmpty(string)) {
  140. return url + (url.indexOf('?') === -1 ? '?' : '&amp;') + string;
  141. }
  142. return url;
  143. },
  144. <span id='Ext-String-method-trim'> /**
  145. </span> * Trims whitespace from either end of a string, leaving spaces within the string intact. Example:
  146. * @example
  147. var s = ' foo bar ';
  148. alert('-' + s + '-'); //alerts &quot;- foo bar -&quot;
  149. alert('-' + Ext.String.trim(s) + '-'); //alerts &quot;-foo bar-&quot;
  150. * @param {String} string The string to escape
  151. * @return {String} The trimmed string
  152. */
  153. trim: function(string) {
  154. return string.replace(trimRegex, &quot;&quot;);
  155. },
  156. <span id='Ext-String-method-capitalize'> /**
  157. </span> * Capitalize the given string
  158. * @param {String} string
  159. * @return {String}
  160. */
  161. capitalize: function(string) {
  162. return string.charAt(0).toUpperCase() + string.substr(1);
  163. },
  164. <span id='Ext-String-method-uncapitalize'> /**
  165. </span> * Uncapitalize the given string
  166. * @param {String} string
  167. * @return {String}
  168. */
  169. uncapitalize: function(string) {
  170. return string.charAt(0).toLowerCase() + string.substr(1);
  171. },
  172. <span id='Ext-String-method-ellipsis'> /**
  173. </span> * Truncate a string and add an ellipsis ('...') to the end if it exceeds the specified length
  174. * @param {String} value The string to truncate
  175. * @param {Number} length The maximum length to allow before truncating
  176. * @param {Boolean} word True to try to find a common word break
  177. * @return {String} The converted text
  178. */
  179. ellipsis: function(value, len, word) {
  180. if (value &amp;&amp; value.length &gt; len) {
  181. if (word) {
  182. var vs = value.substr(0, len - 2),
  183. index = Math.max(vs.lastIndexOf(' '), vs.lastIndexOf('.'), vs.lastIndexOf('!'), vs.lastIndexOf('?'));
  184. if (index !== -1 &amp;&amp; index &gt;= (len - 15)) {
  185. return vs.substr(0, index) + &quot;...&quot;;
  186. }
  187. }
  188. return value.substr(0, len - 3) + &quot;...&quot;;
  189. }
  190. return value;
  191. },
  192. <span id='Ext-String-method-escapeRegex'> /**
  193. </span> * Escapes the passed string for use in a regular expression
  194. * @param {String} string
  195. * @return {String}
  196. */
  197. escapeRegex: function(string) {
  198. return string.replace(escapeRegexRe, &quot;\\$1&quot;);
  199. },
  200. <span id='Ext-String-method-escape'> /**
  201. </span> * Escapes the passed string for ' and \
  202. * @param {String} string The string to escape
  203. * @return {String} The escaped string
  204. */
  205. escape: function(string) {
  206. return string.replace(escapeRe, &quot;\\$1&quot;);
  207. },
  208. <span id='Ext-String-method-toggle'> /**
  209. </span> * Utility function that allows you to easily switch a string between two alternating values. The passed value
  210. * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
  211. * they are already different, the first value passed in is returned. Note that this method returns the new value
  212. * but does not change the current string.
  213. * &lt;pre&gt;&lt;code&gt;
  214. // alternate sort directions
  215. sort = Ext.String.toggle(sort, 'ASC', 'DESC');
  216. // instead of conditional logic:
  217. sort = (sort == 'ASC' ? 'DESC' : 'ASC');
  218. &lt;/code&gt;&lt;/pre&gt;
  219. * @param {String} string The current string
  220. * @param {String} value The value to compare to the current string
  221. * @param {String} other The new value to use if the string already equals the first value passed in
  222. * @return {String} The new value
  223. */
  224. toggle: function(string, value, other) {
  225. return string === value ? other : value;
  226. },
  227. <span id='Ext-String-method-leftPad'> /**
  228. </span> * Pads the left side of a string with a specified character. This is especially useful
  229. * for normalizing number and date strings. Example usage:
  230. *
  231. * &lt;pre&gt;&lt;code&gt;
  232. var s = Ext.String.leftPad('123', 5, '0');
  233. // s now contains the string: '00123'
  234. &lt;/code&gt;&lt;/pre&gt;
  235. * @param {String} string The original string
  236. * @param {Number} size The total length of the output string
  237. * @param {String} character (optional) The character with which to pad the original string (defaults to empty string &quot; &quot;)
  238. * @return {String} The padded string
  239. */
  240. leftPad: function(string, size, character) {
  241. var result = String(string);
  242. character = character || &quot; &quot;;
  243. while (result.length &lt; size) {
  244. result = character + result;
  245. }
  246. return result;
  247. },
  248. <span id='Ext-String-method-format'> /**
  249. </span> * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
  250. * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
  251. * &lt;pre&gt;&lt;code&gt;
  252. var cls = 'my-class', text = 'Some text';
  253. var s = Ext.String.format('&amp;lt;div class=&quot;{0}&quot;&gt;{1}&amp;lt;/div&gt;', cls, text);
  254. // s now contains the string: '&amp;lt;div class=&quot;my-class&quot;&gt;Some text&amp;lt;/div&gt;'
  255. &lt;/code&gt;&lt;/pre&gt;
  256. * @param {String} string The tokenized string to be formatted
  257. * @param {String} value1 The value to replace token {0}
  258. * @param {String} value2 Etc...
  259. * @return {String} The formatted string
  260. */
  261. format: function(format) {
  262. var args = Ext.Array.toArray(arguments, 1);
  263. return format.replace(formatRe, function(m, i) {
  264. return args[i];
  265. });
  266. },
  267. <span id='Ext-String-method-repeat'> /**
  268. </span> * Returns a string with a specified number of repititions a given string pattern.
  269. * The pattern be separated by a different string.
  270. *
  271. * var s = Ext.String.repeat('---', 4); // = '------------'
  272. * var t = Ext.String.repeat('--', 3, '/'); // = '--/--/--'
  273. *
  274. * @param {String} pattern The pattern to repeat.
  275. * @param {Number} count The number of times to repeat the pattern (may be 0).
  276. * @param {String} sep An option string to separate each pattern.
  277. */
  278. repeat: function(pattern, count, sep) {
  279. for (var buf = [], i = count; i--; ) {
  280. buf.push(pattern);
  281. }
  282. return buf.join(sep || '');
  283. },
  284. <span id='Ext-String-method-splitWords'> /**
  285. </span> * Splits a string of space separated words into an array, trimming as needed. If the
  286. * words are already an array, it is returned.
  287. *
  288. * @param {String/Array} words
  289. */
  290. splitWords: function (words) {
  291. if (words &amp;&amp; typeof words == 'string') {
  292. return words.replace(basicTrimRe, '').split(whitespaceRe);
  293. }
  294. return words || [];
  295. }
  296. };
  297. }());
  298. // initialize the default encode / decode entities
  299. Ext.String.resetCharacterEntities();
  300. <span id='Ext-method-htmlEncode'>/**
  301. </span> * Old alias to {@link Ext.String#htmlEncode}
  302. * @deprecated Use {@link Ext.String#htmlEncode} instead
  303. * @method
  304. * @member Ext
  305. * @inheritdoc Ext.String#htmlEncode
  306. */
  307. Ext.htmlEncode = Ext.String.htmlEncode;
  308. <span id='Ext-method-htmlDecode'>/**
  309. </span> * Old alias to {@link Ext.String#htmlDecode}
  310. * @deprecated Use {@link Ext.String#htmlDecode} instead
  311. * @method
  312. * @member Ext
  313. * @inheritdoc Ext.String#htmlDecode
  314. */
  315. Ext.htmlDecode = Ext.String.htmlDecode;
  316. <span id='Ext-method-urlAppend'>/**
  317. </span> * Old alias to {@link Ext.String#urlAppend}
  318. * @deprecated Use {@link Ext.String#urlAppend} instead
  319. * @method
  320. * @member Ext
  321. * @inheritdoc Ext.String#urlAppend
  322. */
  323. Ext.urlAppend = Ext.String.urlAppend;</pre>
  324. </body>
  325. </html>