vendor/sulu/sulu/src/Sulu/Component/Content/Types/TextEditor.php line 81

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\Content\Types;
  11. use PHPCR\NodeInterface;
  12. use Sulu\Bundle\MarkupBundle\Markup\MarkupParserInterface;
  13. use Sulu\Component\Content\Compat\PropertyInterface;
  14. use Sulu\Component\Content\Compat\PropertyParameter;
  15. use Sulu\Component\Content\SimpleContentType;
  16. /**
  17.  * ContentType for TextEditor.
  18.  */
  19. class TextEditor extends SimpleContentType
  20. {
  21.     public const INVALID_REGEX '/(<%s-[a-z]+\b[^\/>]*)(\/>|>[^<]*<\/%s-[^\/>]*>)/';
  22.     /**
  23.      * @var MarkupParserInterface
  24.      */
  25.     private $markupParser;
  26.     /**
  27.      * @var string
  28.      */
  29.     private $markupNamespace;
  30.     public function __construct(MarkupParserInterface $markupParser$markupNamespace 'sulu')
  31.     {
  32.         parent::__construct('TextEditor''');
  33.         $this->markupParser $markupParser;
  34.         $this->markupNamespace $markupNamespace;
  35.     }
  36.     public function read(NodeInterface $nodePropertyInterface $property$webspaceKey$languageCode$segmentKey)
  37.     {
  38.         $value $node->getPropertyValueWithDefault($property->getName(), $this->defaultValue);
  39.         $property->setValue($this->validate($value$languageCode));
  40.         return $value;
  41.     }
  42.     public function write(
  43.         NodeInterface $node,
  44.         PropertyInterface $property,
  45.         $userId,
  46.         $webspaceKey,
  47.         $languageCode,
  48.         $segmentKey
  49.     ) {
  50.         $value $property->getValue();
  51.         if (null !== $value) {
  52.             $node->setProperty($property->getName(),
  53.                 $this->removeValidation(
  54.                     $this->removeIllegalCharacters($value)
  55.                 )
  56.             );
  57.         } else {
  58.             $this->remove($node$property$webspaceKey$languageCode$segmentKey);
  59.         }
  60.     }
  61.     /**
  62.      * Returns validated content.
  63.      *
  64.      * @param string $content
  65.      * @param string $locale
  66.      *
  67.      * @return string
  68.      */
  69.     private function validate($content$locale)
  70.     {
  71.         $validation $this->markupParser->validate($content$locale);
  72.         $regex = \sprintf(self::INVALID_REGEX$this->markupNamespace$this->markupNamespace);
  73.         foreach ($validation as $tag => $state) {
  74.             if (false === \strpos($tag'sulu-validation-state="' $state '"')) {
  75.                 $newTag = \preg_replace($regex'$1 sulu-validation-state="' $state '"$2'$tag);
  76.                 $content = \str_replace($tag$newTag$content);
  77.             }
  78.         }
  79.         return $content;
  80.     }
  81.     /**
  82.      * Removes validation attributes.
  83.      *
  84.      * @param string $content
  85.      *
  86.      * @return string
  87.      */
  88.     private function removeValidation($content)
  89.     {
  90.         return \preg_replace('/ sulu-validation-state="[a-zA-Z ]*"/'''$content);
  91.     }
  92.     public function getDefaultParams(PropertyInterface $property null)
  93.     {
  94.         return [
  95.             'table' => new PropertyParameter('table'true),
  96.             'link' => new PropertyParameter('link'true),
  97.             'max_height' => new PropertyParameter('max_height'300),
  98.             'paste_from_word' => new PropertyParameter('paste_from_word'true),
  99.         ];
  100.     }
  101. }