web.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. /**
  3. * Escape the given string of `html`.
  4. *
  5. * @param {String} html
  6. * @return {String}
  7. * @public
  8. */
  9. exports.escape = require('escape-html');
  10. /**
  11. * Unescape the given string from html
  12. * @param {String} html
  13. * @param {String} type
  14. * @return {String}
  15. * @public
  16. */
  17. exports.unescape = require('unescape');
  18. /**
  19. * Safe encodeURIComponent, won't throw any error.
  20. * If `encodeURIComponent` error happen, just return the original value.
  21. *
  22. * @param {String} text
  23. * @return {String} URL encode string.
  24. */
  25. exports.encodeURIComponent = function encodeURIComponent_(text) {
  26. try {
  27. return encodeURIComponent(text);
  28. } catch (e) {
  29. return text;
  30. }
  31. };
  32. /**
  33. * Safe decodeURIComponent, won't throw any error.
  34. * If `decodeURIComponent` error happen, just return the original value.
  35. *
  36. * @param {String} encodeText
  37. * @return {String} URL decode original string.
  38. */
  39. exports.decodeURIComponent = function decodeURIComponent_(encodeText) {
  40. try {
  41. return decodeURIComponent(encodeText);
  42. } catch (e) {
  43. return encodeText;
  44. }
  45. };