string.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * $Id: TinyMCE_DOMUtils.class.js 91 2006-10-02 14:53:22Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
  6. */
  7. /**#@+
  8. * @class This class contains common event handling logic.
  9. * @member mox.String
  10. * @static
  11. */
  12. mox.create('static mox.String', {
  13. _entities : {'&' : '&amp;', '"' : '', '\'' : '&#39;', '<' : '&lt;', '>' : '&gt;', '"' : '&quot;'},
  14. _xmlRe : /[<>&]/g,
  15. _xmlReQuot : /[<>&\"\']/g,
  16. /**#@+
  17. * @method
  18. */
  19. /**
  20. * Encodes the string to raw XML entities. This will only convert the most common ones.
  21. * For real entity encoding use the xmlEncode method of the Cleanup class.
  22. *
  23. * @param {string} s String to encode.
  24. * @param {bool} sq Optional skip encode quotes.
  25. * @return {string} XML Encoded string.
  26. */
  27. xmlEncode : function(s, sq) {
  28. var l = this._entities;
  29. return s ? ('' + s).replace(!sq ? this._xmlReQuot : this._xmlRe, function(c, b) {
  30. return l[c];
  31. }) : s;
  32. },
  33. /**
  34. * Decodes the string from XML entities into plain text.
  35. *
  36. * @param {string} s String to decode.
  37. * @return {string} XML Decoded string.
  38. */
  39. xmlDecode : function(s) {
  40. var e = document.createElement("div");
  41. if (!s)
  42. return s;
  43. e.innerHTML = s;
  44. return e.firstChild.nodeValue;
  45. },
  46. stripTags : function(s) {
  47. return !s ? s : s.replace(/<[^>]+>/g, '');
  48. }
  49. /**#@-*/
  50. });