PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/1.4/api/source/escape.html

https://github.com/yiminghe/kissyteam.github.com
HTML | 286 lines | 271 code | 15 blank | 0 comment | 0 complexity | f67caeb28ed3b7045aab6a969db57e8a MD5 | raw file
  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='global-property-'>/**
  19. </span> * @ignore
  20. * escape of lang
  21. * @author yiminghe@gmail.com
  22. *
  23. */
  24. (function (S, undefined) {
  25. // IE doesn't include non-breaking-space (0xa0) in their \s character
  26. // class (as required by section 7.2 of the ECMAScript spec), we explicitly
  27. // include it in the regexp to enforce consistent cross-browser behavior.
  28. var SEP = '&amp;',
  29. EMPTY = '',
  30. EQ = '=',
  31. logger= S.getLogger('s/lang'),
  32. TRUE = true,
  33. // FALSE = false,
  34. HEX_BASE = 16,
  35. // http://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
  36. // http://wonko.com/post/html-escaping
  37. htmlEntities = {
  38. '&amp;amp;': '&amp;',
  39. '&amp;gt;': '&gt;',
  40. '&amp;lt;': '&lt;',
  41. '&amp;#x60;': '`',
  42. '&amp;#x2F;': '/',
  43. '&amp;quot;': '&quot;',
  44. '&amp;#x27;': &quot;'&quot;
  45. },
  46. reverseEntities = {},
  47. escapeReg,
  48. unEscapeReg,
  49. // - # $ ^ * ( ) + [ ] { } | \ , . ?
  50. escapeRegExp = /[\-#$\^*()+\[\]{}|\\,.?\s]/g;
  51. (function () {
  52. for (var k in htmlEntities) {
  53. reverseEntities[htmlEntities[k]] = k;
  54. }
  55. })();
  56. function isValidParamValue(val) {
  57. var t = typeof val;
  58. // If the type of val is null, undefined, number, string, boolean, return TRUE.
  59. return val == null || (t !== 'object' &amp;&amp; t !== 'function');
  60. }
  61. function getEscapeReg() {
  62. if (escapeReg) {
  63. return escapeReg
  64. }
  65. var str = EMPTY;
  66. S.each(htmlEntities, function (entity) {
  67. str += entity + '|';
  68. });
  69. str = str.slice(0, -1);
  70. return escapeReg = new RegExp(str, 'g');
  71. }
  72. function getUnEscapeReg() {
  73. if (unEscapeReg) {
  74. return unEscapeReg
  75. }
  76. var str = EMPTY;
  77. S.each(reverseEntities, function (entity) {
  78. str += entity + '|';
  79. });
  80. str += '&amp;#(\\d{1,5});';
  81. return unEscapeReg = new RegExp(str, 'g');
  82. }
  83. S.mix(S, {
  84. <span id='KISSY-method-urlEncode'> /**
  85. </span> * Call encodeURIComponent to encode a url component
  86. * @param {String} s part of url to be encoded.
  87. * @return {String} encoded url part string.
  88. * @member KISSY
  89. */
  90. urlEncode: function (s) {
  91. return encodeURIComponent(String(s));
  92. },
  93. <span id='KISSY-method-urlDecode'> /**
  94. </span> * Call decodeURIComponent to decode a url component
  95. * and replace '+' with space.
  96. * @param {String} s part of url to be decoded.
  97. * @return {String} decoded url part string.
  98. * @member KISSY
  99. */
  100. urlDecode: function (s) {
  101. return decodeURIComponent(s.replace(/\+/g, ' '));
  102. },
  103. <span id='KISSY-method-fromUnicode'> /**
  104. </span> * frequently used in taobao cookie about nick
  105. * @member KISSY
  106. * @return {String} un-unicode string.
  107. */
  108. fromUnicode: function (str) {
  109. return str.replace(/\\u([a-f\d]{4})/ig, function (m, u) {
  110. return String.fromCharCode(parseInt(u, HEX_BASE));
  111. });
  112. },
  113. <span id='KISSY-method-escapeHtml'> /**
  114. </span> * get escaped string from html.
  115. * only escape
  116. * &amp; &gt; &lt; ` / &quot; '
  117. * refer:
  118. *
  119. * [http://yiminghe.javaeye.com/blog/788929](http://yiminghe.javaeye.com/blog/788929)
  120. *
  121. * [http://wonko.com/post/html-escaping](http://wonko.com/post/html-escaping)
  122. * @param str {string} text2html show
  123. * @member KISSY
  124. * @return {String} escaped html
  125. */
  126. escapeHtml: function (str) {
  127. return (str + '').replace(getEscapeReg(), function (m) {
  128. return reverseEntities[m];
  129. });
  130. },
  131. <span id='KISSY-method-escapeRegExp'> /**
  132. </span> * get escaped regexp string for construct regexp.
  133. * @param str
  134. * @member KISSY
  135. * @return {String} escaped regexp
  136. */
  137. escapeRegExp: function (str) {
  138. return str.replace(escapeRegExp, '\\$&amp;');
  139. },
  140. <span id='KISSY-method-unEscapeHtml'> /**
  141. </span> * un-escape html to string.
  142. * only unescape
  143. * &amp;amp; &amp;lt; &amp;gt; &amp;#x60; &amp;#x2F; &amp;quot; &amp;#x27; &amp;#\d{1,5}
  144. * @param str {string} html2text
  145. * @member KISSY
  146. * @return {String} un-escaped html
  147. */
  148. unEscapeHtml: function (str) {
  149. return str.replace(getUnEscapeReg(), function (m, n) {
  150. return htmlEntities[m] || String.fromCharCode(+n);
  151. });
  152. },
  153. <span id='KISSY-method-param'> /**
  154. </span> * Creates a serialized string of an array or object.
  155. *
  156. * for example:
  157. * @example
  158. * {foo: 1, bar: 2} // -&gt; 'foo=1&amp;bar=2'
  159. * {foo: 1, bar: [2, 3]} // -&gt; 'foo=1&amp;bar=2&amp;bar=3'
  160. * {foo: '', bar: 2} // -&gt; 'foo=&amp;bar=2'
  161. * {foo: undefined, bar: 2} // -&gt; 'foo=undefined&amp;bar=2'
  162. * {foo: TRUE, bar: 2} // -&gt; 'foo=TRUE&amp;bar=2'
  163. *
  164. * @param {Object} o json data
  165. * @param {String} [sep='&amp;'] separator between each pair of data
  166. * @param {String} [eq='='] separator between key and value of data
  167. * @param {Boolean} [serializeArray=true] whether add '[]' to array key of data
  168. * @return {String}
  169. * @member KISSY
  170. */
  171. param: function (o, sep, eq, serializeArray) {
  172. sep = sep || SEP;
  173. eq = eq || EQ;
  174. if (serializeArray === undefined) {
  175. serializeArray = TRUE;
  176. }
  177. var buf = [], key, i, v, len, val,
  178. encode = S.urlEncode;
  179. for (key in o) {
  180. val = o[key];
  181. key = encode(key);
  182. // val is valid non-array value
  183. if (isValidParamValue(val)) {
  184. buf.push(key);
  185. if (val !== undefined) {
  186. buf.push(eq, encode(val + EMPTY));
  187. }
  188. buf.push(sep);
  189. }
  190. // val is not empty array
  191. else if (S.isArray(val) &amp;&amp; val.length) {
  192. for (i = 0, len = val.length; i &lt; len; ++i) {
  193. v = val[i];
  194. if (isValidParamValue(v)) {
  195. buf.push(key, (serializeArray ? encode('[]') : EMPTY));
  196. if (v !== undefined) {
  197. buf.push(eq, encode(v + EMPTY));
  198. }
  199. buf.push(sep);
  200. }
  201. }
  202. }
  203. // ignore other cases, including empty array, Function, RegExp, Date etc.
  204. }
  205. buf.pop();
  206. return buf.join(EMPTY);
  207. },
  208. <span id='KISSY-method-unparam'> /**
  209. </span> * Parses a URI-like query string and returns an object composed of parameter/value pairs.
  210. *
  211. * for example:
  212. * @example
  213. * 'section=blog&amp;id=45' // -&gt; {section: 'blog', id: '45'}
  214. * 'section=blog&amp;tag=js&amp;tag=doc' // -&gt; {section: 'blog', tag: ['js', 'doc']}
  215. * 'tag=ruby%20on%20rails' // -&gt; {tag: 'ruby on rails'}
  216. * 'id=45&amp;raw' // -&gt; {id: '45', raw: ''}
  217. * @param {String} str param string
  218. * @param {String} [sep='&amp;'] separator between each pair of data
  219. * @param {String} [eq='='] separator between key and value of data
  220. * @return {Object} json data
  221. * @member KISSY
  222. */
  223. unparam: function (str, sep, eq) {
  224. if (typeof str != 'string' || !(str = S.trim(str))) {
  225. return {};
  226. }
  227. sep = sep || SEP;
  228. eq = eq || EQ;
  229. var ret = {},
  230. eqIndex,
  231. decode = S.urlDecode,
  232. pairs = str.split(sep),
  233. key, val,
  234. i = 0, len = pairs.length;
  235. for (; i &lt; len; ++i) {
  236. eqIndex = pairs[i].indexOf(eq);
  237. if (eqIndex == -1) {
  238. key = decode(pairs[i]);
  239. val = undefined;
  240. } else {
  241. // remember to decode key!
  242. key = decode(pairs[i].substring(0, eqIndex));
  243. val = pairs[i].substring(eqIndex + 1);
  244. try {
  245. val = decode(val);
  246. } catch (e) {
  247. logger.error('decodeURIComponent error : ' + val);
  248. logger.error(e);
  249. }
  250. if (S.endsWith(key, '[]')) {
  251. key = key.substring(0, key.length - 2);
  252. }
  253. }
  254. if (key in ret) {
  255. if (S.isArray(ret[key])) {
  256. ret[key].push(val);
  257. } else {
  258. ret[key] = [ret[key], val];
  259. }
  260. } else {
  261. ret[key] = val;
  262. }
  263. }
  264. return ret;
  265. }
  266. });
  267. S.escapeHTML = S.escapeHtml;
  268. S.unEscapeHTML = S.unEscapeHtml;
  269. })(KISSY);</pre>
  270. </body>
  271. </html>