From: Matthias Schmidt Date: Sat, 24 Sep 2016 19:52:56 +0000 (+0200) Subject: Add AbstractHtmlInputNodeProcessorListener X-Git-Tag: 3.0.0_Beta_2~160 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=f1e6e7ece05bef7d1252144aa90a48e09bf5a5c6;p=GitHub%2FWoltLab%2FWCF.git Add AbstractHtmlInputNodeProcessorListener --- diff --git a/wcfsetup/install/files/lib/system/event/listener/AbstractHtmlInputNodeProcessorListener.class.php b/wcfsetup/install/files/lib/system/event/listener/AbstractHtmlInputNodeProcessorListener.class.php new file mode 100644 index 0000000000..3a302b4813 --- /dev/null +++ b/wcfsetup/install/files/lib/system/event/listener/AbstractHtmlInputNodeProcessorListener.class.php @@ -0,0 +1,108 @@ + + * @package WoltLabSuite\Core\System\Event\Listener + * @since 3.0 + */ +abstract class AbstractHtmlInputNodeProcessorListener implements IParameterizedEventListener { + /** + * Returns the ids of the objects linked in the text proccessed by the given + * processor matching the given regular expression. + * + * @param HtmlInputNodeProcessor $processor + * @param Regex $regex + * @return integer[] + */ + protected function getObjectIDs(HtmlInputNodeProcessor $processor, Regex $regex) { + $objectIDs = []; + foreach ($processor->getDocument()->getElementsByTagName('a') as $element) { + /** @var \DOMElement $element */ + if ($element->getAttribute('href') === $element->textContent) { + if ($regex->match($element->getAttribute('href'), true)) { + $objectIDs[] = $regex->getMatches()[2][0]; + } + } + } + + return array_unique($objectIDs); + } + + /** + * Returns a `Regex` object for matching links based on the given string + * followed by an object id. + * + * @param string $link + * @return Regex + */ + protected function getRegexFromLink($link) { + return new Regex('^(' . preg_replace('~^https?~', 'https?', preg_quote($link)) . '(\d+)-.*?)$'); + } + + /** + * Replaces relevant object links with bbcodes. + * + * @param HtmlInputNodeProcessor $processor + * @param Regex $regex + * @param ITitledObject[] $objects + * @param string $bbcodeName + */ + protected function replaceLinksWithBBCode(HtmlInputNodeProcessor $processor, Regex $regex, array $objects, $bbcodeName) { + foreach ($processor->getDocument()->getElementsByTagName('a') as $element) { + /** @var \DOMElement $element */ + if ($element->getAttribute('href') === $element->textContent) { + if ($regex->match($element->getAttribute('href'), true)) { + $objectID = $regex->getMatches()[2][0]; + + if (isset($objects[$objectID])) { + $metacodeElement = $processor->getDocument()->createElement('woltlab-metacode'); + $metacodeElement->setAttribute('data-name', $bbcodeName); + $metacodeElement->setAttribute('data-attributes', base64_encode(JSON::encode([$objectID]))); + + DOMUtil::replaceElement($element, $metacodeElement, false); + } + } + } + } + } + + /** + * Replaces the text content of relevant object links with the titles of + * the objects. + * + * @param HtmlInputNodeProcessor $processor + * @param Regex $regex + * @param ITitledObject[] $objects + */ + protected function setObjectTitles(HtmlInputNodeProcessor $processor, Regex $regex, array $objects) { + foreach ($processor->getDocument()->getElementsByTagName('a') as $element) { + /** @var \DOMElement $element */ + if ($element->getAttribute('href') === $element->textContent) { + if ($regex->match($element->getAttribute('href'), true)) { + $objectID = $regex->getMatches()[2][0]; + + if (isset($objects[$objectID])) { + $object = $objects[$objectID]; + if (!($object instanceof ITitledObject)) { + throw new ImplementationException(get_class($object), ITitledObject::class); + } + + $element->textContent = $object->getTitle(); + } + } + } + } + } +}