From 3ba08b8e8bdbd2e653d0d06d0ea1c66a2d562b43 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Sun, 26 Jun 2016 13:22:35 +0200 Subject: [PATCH] Added context support for html processors --- .../lib/acp/form/ArticleAddForm.class.php | 4 +-- .../article/content/ArticleContent.class.php | 6 ++-- .../lib/system/html/IHtmlProcessor.class.php | 29 +++++++++++++++++ .../html/input/HtmlInputProcessor.class.php | 12 +++---- .../node/AbstractHtmlNodeProcessor.class.php | 30 ++++++++++++++++++ .../html/node/IHtmlNodeProcessor.class.php | 16 ++++++++++ .../html/output/HtmlOutputProcessor.class.php | 31 ++++++++++++++++--- 7 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/html/IHtmlProcessor.class.php diff --git a/wcfsetup/install/files/lib/acp/form/ArticleAddForm.class.php b/wcfsetup/install/files/lib/acp/form/ArticleAddForm.class.php index 41f8d5158b..3040ea7dbc 100644 --- a/wcfsetup/install/files/lib/acp/form/ArticleAddForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/ArticleAddForm.class.php @@ -286,7 +286,7 @@ class ArticleAddForm extends AbstractForm { } $this->htmlInputProcessors[$language->languageID] = new HtmlInputProcessor(); - $this->htmlInputProcessors[$language->languageID]->process($this->content[$language->languageID]); + $this->htmlInputProcessors[$language->languageID]->process($this->content[$language->languageID], 'com.woltlab.wcf.article.content', 0); } } else { @@ -300,7 +300,7 @@ class ArticleAddForm extends AbstractForm { } $this->htmlInputProcessors[0] = new HtmlInputProcessor(); - $this->htmlInputProcessors[0]->process($this->content[0]); + $this->htmlInputProcessors[0]->process($this->content[0], 'com.woltlab.wcf.article.content', 0); } } diff --git a/wcfsetup/install/files/lib/data/article/content/ArticleContent.class.php b/wcfsetup/install/files/lib/data/article/content/ArticleContent.class.php index 259a13acbd..b89798d69a 100644 --- a/wcfsetup/install/files/lib/data/article/content/ArticleContent.class.php +++ b/wcfsetup/install/files/lib/data/article/content/ArticleContent.class.php @@ -96,8 +96,10 @@ class ArticleContent extends DatabaseObject implements ILinkableObject, IRouteCo // assign embedded objects MessageEmbeddedObjectManager::getInstance()->setActiveMessage('com.woltlab.wcf.article.content', $this->articleContentID); - // TODO - return (new HtmlOutputProcessor())->process($this->content); + $processor = new HtmlOutputProcessor(); + $processor->process($this->message, 'com.woltlab.wcf.article.content', $this->articleContentID); + + return $processor->getHtml(); } /** diff --git a/wcfsetup/install/files/lib/system/html/IHtmlProcessor.class.php b/wcfsetup/install/files/lib/system/html/IHtmlProcessor.class.php new file mode 100644 index 0000000000..16bc22da9b --- /dev/null +++ b/wcfsetup/install/files/lib/system/html/IHtmlProcessor.class.php @@ -0,0 +1,29 @@ + + * @package WoltLabSuite\Core\System\Html + * @since 3.0 + */ +interface IHtmlProcessor { + /** + * Processes the input html string. + * + * @param string $html html string + * @param string $objectType object type identifier + * @param integer $objectID object id + */ + public function process($html, $objectType, $objectID); + + /** + * Returns the parsed html. + * + * @return string parsed html + */ + public function getHtml(); +} diff --git a/wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php b/wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php index 60781f360e..4ddda62194 100644 --- a/wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php +++ b/wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php @@ -1,6 +1,7 @@ getHtmlInputFilter()->apply($html); + $this->getHtmlInputNodeProcessor()->setContext($objectType, $objectID); + // pre-parse HTML $this->getHtmlInputNodeProcessor()->load($html); $this->getHtmlInputNodeProcessor()->process(); diff --git a/wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php b/wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php index fc4610fd39..005759c334 100644 --- a/wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php @@ -13,6 +13,15 @@ use wcf\util\JSON; * @since 3.0 */ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor { + /** + * context data + * @var array + */ + protected $context = [ + 'objectType' => '', + 'objectID' => 0 + ]; + /** * active DOM document * @var \DOMDocument @@ -35,6 +44,10 @@ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor { * @inheritDOc */ public function load($html) { + if (empty($this->context['objectType'])) { + throw new SystemException('Missing object type, please set the context before attempting to call `load()`.'); + } + $this->document = new \DOMDocument('1.0', 'UTF-8'); $this->xpath = null; @@ -169,6 +182,23 @@ abstract class AbstractHtmlNodeProcessor implements IHtmlNodeProcessor { return $parsedAttributes; } + /** + * @inheritDoc + */ + public function setContext($objectType, $objectID) { + $this->context = [ + 'objectType' => $objectType, + 'objectID' => $objectID + ]; + } + + /** + * @inheritDoc + */ + public function getContext() { + return $this->context; + } + /** * Invokes a html node processor. * diff --git a/wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php b/wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php index 133b4b2fad..6d68b357fa 100644 --- a/wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php +++ b/wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php @@ -11,6 +11,13 @@ namespace wcf\system\html\node; * @since 3.0 */ interface IHtmlNodeProcessor { + /** + * Returns an array containing `objectType` and `objectID`. + * + * @return array + */ + public function getContext(); + /** * Returns the currently loaded DOM document. * @@ -36,4 +43,13 @@ interface IHtmlNodeProcessor { * Processes the HTML and transforms it depending on the output type. */ public function process(); + + /** + * Sets the context of provided HTML, `$objectID` should be + * `0` for objects in creation. + * + * @param string $objectType object type identifier + * @param integer $objectID object id + */ + public function setContext($objectType, $objectID); } diff --git a/wcfsetup/install/files/lib/system/html/output/HtmlOutputProcessor.class.php b/wcfsetup/install/files/lib/system/html/output/HtmlOutputProcessor.class.php index a6fac5a0f4..b00cbcf821 100644 --- a/wcfsetup/install/files/lib/system/html/output/HtmlOutputProcessor.class.php +++ b/wcfsetup/install/files/lib/system/html/output/HtmlOutputProcessor.class.php @@ -1,24 +1,45 @@ + * @package WoltLabSuite\Core\System\Html\Output + * @since 3.0 */ -class HtmlOutputProcessor { +class HtmlOutputProcessor implements IHtmlProcessor { /** + * output node processor instance * @var HtmlOutputNodeProcessor */ protected $htmlOutputNodeProcessor; - public function process($html) { + /** + * @inheritDoc + */ + public function process($html, $objectType, $objectID) { + $this->getHtmlOutputNodeProcessor()->setContext($objectType, $objectID); $this->getHtmlOutputNodeProcessor()->load($html); $this->getHtmlOutputNodeProcessor()->process(); - + } + + /** + * @inheritDoc + */ + public function getHtml() { return $this->getHtmlOutputNodeProcessor()->getHtml(); } + /** + * Returns the output node processor instance. + * + * @return HtmlOutputNodeProcessor output node processor instance + */ protected function getHtmlOutputNodeProcessor() { if ($this->htmlOutputNodeProcessor === null) { $this->htmlOutputNodeProcessor = new HtmlOutputNodeProcessor(); -- 2.20.1