Normalizer.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Wysiwyg;
  7. /**
  8. * Normalize widget content in Wysiwyg editor
  9. */
  10. class Normalizer
  11. {
  12. const WYSIWYG_RESERVED_CHARACTERS_REPLACEMENT_MAP = [
  13. '{' => '^[',
  14. '}' => '^]',
  15. '"' => '`',
  16. '\\' => '|',
  17. '<' => '^(',
  18. '>' => '^)'
  19. ];
  20. /**
  21. * Replace the reserved characters in the content
  22. *
  23. * @param string $content
  24. * @return string
  25. */
  26. public function replaceReservedCharacters($content)
  27. {
  28. return str_replace(
  29. array_keys(Normalizer::WYSIWYG_RESERVED_CHARACTERS_REPLACEMENT_MAP),
  30. array_values(Normalizer::WYSIWYG_RESERVED_CHARACTERS_REPLACEMENT_MAP),
  31. $content
  32. );
  33. }
  34. /**
  35. * Restore the reserved characters in the content
  36. *
  37. * @param string $content
  38. * @return string
  39. */
  40. public function restoreReservedCharacters($content)
  41. {
  42. return str_replace(
  43. array_values(Normalizer::WYSIWYG_RESERVED_CHARACTERS_REPLACEMENT_MAP),
  44. array_keys(Normalizer::WYSIWYG_RESERVED_CHARACTERS_REPLACEMENT_MAP),
  45. $content
  46. );
  47. }
  48. }