web.js 893 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. * Safe encodeURIComponent, won't throw any error.
  12. * If `encodeURIComponent` error happen, just return the original value.
  13. *
  14. * @param {String} text
  15. * @return {String} URL encode string.
  16. */
  17. exports.encodeURIComponent = function encodeURIComponent_(text) {
  18. try {
  19. return encodeURIComponent(text);
  20. } catch (e) {
  21. return text;
  22. }
  23. };
  24. /**
  25. * Safe decodeURIComponent, won't throw any error.
  26. * If `decodeURIComponent` error happen, just return the original value.
  27. *
  28. * @param {String} encodeText
  29. * @return {String} URL decode original string.
  30. */
  31. exports.decodeURIComponent = function decodeURIComponent_(encodeText) {
  32. try {
  33. return decodeURIComponent(encodeText);
  34. } catch (e) {
  35. return encodeText;
  36. }
  37. };