Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

escape.js 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. Copyright (c) 2010, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.com/yui/license.html
  5. version: 3.4.0
  6. build: nightly
  7. */
  8. YUI.add('escape', function(Y) {
  9. /**
  10. Provides utility methods for escaping strings.
  11. @module escape
  12. @class Escape
  13. @static
  14. @since 3.3.0
  15. **/
  16. var HTML_CHARS = {
  17. '&': '&',
  18. '<': '&lt;',
  19. '>': '&gt;',
  20. '"': '&quot;',
  21. "'": '&#x27;',
  22. '/': '&#x2F;',
  23. '`': '&#x60;'
  24. },
  25. Escape = {
  26. // -- Public Static Methods ------------------------------------------------
  27. /**
  28. Returns a copy of the specified string with special HTML characters
  29. escaped. The following characters will be converted to their
  30. corresponding character entities:
  31. & < > " ' / `
  32. This implementation is based on the [OWASP HTML escaping
  33. recommendations][1]. In addition to the characters in the OWASP
  34. recommendations, we also escape the <code>&#x60;</code> character, since IE
  35. interprets it as an attribute delimiter.
  36. If _string_ is not already a string, it will be coerced to a string.
  37. [1]: http://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
  38. @method html
  39. @param {String} string String to escape.
  40. @return {String} Escaped string.
  41. @static
  42. **/
  43. html: function (string) {
  44. return (string + '').replace(/[&<>"'\/`]/g, Escape._htmlReplacer);
  45. },
  46. /**
  47. Returns a copy of the specified string with special regular expression
  48. characters escaped, allowing the string to be used safely inside a regex.
  49. The following characters, and all whitespace characters, are escaped:
  50. - # $ ^ * ( ) + [ ] { } | \ , . ?
  51. If _string_ is not already a string, it will be coerced to a string.
  52. @method regex
  53. @param {String} string String to escape.
  54. @return {String} Escaped string.
  55. @static
  56. **/
  57. regex: function (string) {
  58. return (string + '').replace(/[\-#$\^*()+\[\]{}|\\,.?\s]/g, '\\$&');
  59. },
  60. // -- Protected Static Methods ---------------------------------------------
  61. /**
  62. * Regex replacer for HTML escaping.
  63. *
  64. * @method _htmlReplacer
  65. * @param {String} match Matched character (must exist in HTML_CHARS).
  66. * @returns {String} HTML entity.
  67. * @static
  68. * @protected
  69. */
  70. _htmlReplacer: function (match) {
  71. return HTML_CHARS[match];
  72. }
  73. };
  74. Escape.regexp = Escape.regex;
  75. Y.Escape = Escape;
  76. }, '3.4.0' ,{requires:['yui-base']});