\true, 'specialChars' => [ '*' => '[\\pL\\pN]*', '?' => '.', ' ' => '\\s*' ] ]; protected $tagName = 'CENSOR'; protected function setUp() { $this->collection = new NormalizedCollection; $this->collection->onDuplicate('replace'); if (isset($this->configurator->tags[$this->tagName])) return; $tag = $this->configurator->tags->add($this->tagName); $tag->attributes->add($this->attrName)->required = \false; $tag->rules->ignoreTags(); $tag->template = ' ' . \htmlspecialchars($this->defaultReplacement) . ' '; } public function allow($word) { $this->allowed[$word] = \true; } public function getHelper() { $config = $this->asConfig(); if (isset($config)) $config = ConfigHelper::filterConfig($config, 'PHP'); else $config = [ 'attrName' => $this->attrName, 'regexp' => '/(?!)/', 'tagName' => $this->tagName ]; return new Helper($config); } public function asConfig() { $words = $this->getWords(); if (empty($words)) return; $config = [ 'attrName' => $this->attrName, 'regexp' => $this->getWordsRegexp(\array_keys($words)), 'regexpHtml' => $this->getWordsRegexp(\array_map('htmlspecialchars', \array_keys($words))), 'tagName' => $this->tagName ]; $replacementWords = []; foreach ($words as $word => $replacement) if (isset($replacement) && $replacement !== $this->defaultReplacement) $replacementWords[$replacement][] = $word; foreach ($replacementWords as $replacement => $words) { $wordsRegexp = '/^' . RegexpBuilder::fromList($words, $this->regexpOptions) . '$/Diu'; $regexp = new Regexp($wordsRegexp); $regexp->setJS(RegexpConvertor::toJS(\str_replace('[\\pL\\pN]', '[^\\s!-\\/:-?]', $wordsRegexp))); $config['replacements'][] = [$regexp, $replacement]; } if (!empty($this->allowed)) $config['allowed'] = $this->getWordsRegexp(\array_keys($this->allowed)); return $config; } public function getJSHints() { $hints = [ 'CENSOR_HAS_ALLOWED' => !empty($this->allowed), 'CENSOR_HAS_REPLACEMENTS' => \false ]; foreach ($this->getWords() as $replacement) if (isset($replacement) && $replacement !== $this->defaultReplacement) { $hints['CENSOR_HAS_REPLACEMENTS'] = \true; break; } return $hints; } protected function getWords() { return \array_diff_key(\iterator_to_array($this->collection), $this->allowed); } protected function getWordsRegexp(array $words) { $expr = RegexpBuilder::fromList($words, $this->regexpOptions); $expr = \preg_replace('/(?\\\\\\\\)*)\\(\\?:/', '$1(?>', $expr); $regexp = new Regexp('/(?setJS('/(?:^|\\W)' . \str_replace('[\\pL\\pN]', '[^\\s!-\\/:-?]', $expr) . '(?!\\w)/gi'); return $regexp; } }