Added context support for html processors
authorAlexander Ebert <ebert@woltlab.com>
Sun, 26 Jun 2016 11:22:35 +0000 (13:22 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 26 Jun 2016 11:22:42 +0000 (13:22 +0200)
wcfsetup/install/files/lib/acp/form/ArticleAddForm.class.php
wcfsetup/install/files/lib/data/article/content/ArticleContent.class.php
wcfsetup/install/files/lib/system/html/IHtmlProcessor.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/system/html/input/HtmlInputProcessor.class.php
wcfsetup/install/files/lib/system/html/node/AbstractHtmlNodeProcessor.class.php
wcfsetup/install/files/lib/system/html/node/IHtmlNodeProcessor.class.php
wcfsetup/install/files/lib/system/html/output/HtmlOutputProcessor.class.php

index 41f8d5158ba72b6084f94a8173b01325bd9e9430..3040ea7dbcece6603e087a5281ea53c09e71c78d 100644 (file)
@@ -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);
                }
                
        }
index 259a13acbdb73cd4c1e14db8efb472871dd2ac55..b89798d69a9bbe361042d7beb8f182a5963862fe 100644 (file)
@@ -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 (file)
index 0000000..16bc22d
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+namespace wcf\system\html;
+
+/**
+ * Default interface for html processors.
+ * 
+ * @author      Alexander Ebert
+ * @copyright   2001-2016 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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();
+}
index 60781f360e35679f5e7524f7f9cbe684d8f228c1..4ddda621945ff3add456a0db1831c0c7c3232ba6 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 namespace wcf\system\html\input;
 use wcf\system\bbcode\HtmlBBCodeParser;
+use wcf\system\html\IHtmlProcessor;
 use wcf\system\html\input\filter\IHtmlInputFilter;
 use wcf\system\html\input\filter\MessageHtmlInputFilter;
 use wcf\system\html\input\node\HtmlInputNodeProcessor;
@@ -15,7 +16,7 @@ use wcf\util\StringUtil;
  * @package     WoltLabSuite\Core\System\Html\Input
  * @since       3.0
  */
-class HtmlInputProcessor {
+class HtmlInputProcessor implements IHtmlProcessor {
        /**
         * list of embedded content grouped by type
         * @var array
@@ -33,12 +34,9 @@ class HtmlInputProcessor {
        protected $htmlInputNodeProcessor;
        
        /**
-        * Processes input HTML by applying filters and parsing all nodes
-        * including bbcodes.
-        * 
-        * @param       string          $html           html string
+        * @inheritDoc
         */
-       public function process($html) {
+       public function process($html, $objectType, $objectID) {
                // enforce consistent newlines
                $html = StringUtil::unifyNewlines($html);
                
@@ -48,6 +46,8 @@ class HtmlInputProcessor {
                // filter HTML
                $html = $this->getHtmlInputFilter()->apply($html);
                
+               $this->getHtmlInputNodeProcessor()->setContext($objectType, $objectID);
+               
                // pre-parse HTML
                $this->getHtmlInputNodeProcessor()->load($html);
                $this->getHtmlInputNodeProcessor()->process();
index fc4610fd39fb673b2bd708bbd476c7f420a8bbf7..005759c33437e02f4fd8f449655d3ae4cd8ca80a 100644 (file)
@@ -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.
         * 
index 133b4b2fad9fef5f09b3bdc9f09c4472949fe8e6..6d68b357fae233eade8a480ed0007831028d46b3 100644 (file)
@@ -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);
 }
index a6fac5a0f4cf29d6af35c4ce5ec241406910d779..b00cbcf821e1f05b059081e725f6ff0cc18340a0 100644 (file)
@@ -1,24 +1,45 @@
 <?php
 namespace wcf\system\html\output;
+use wcf\system\html\IHtmlProcessor;
 use wcf\system\html\output\node\HtmlOutputNodeProcessor;
 
 /**
- * TOOD documentation
- * @since      3.0
+ * Processes stored HTML for final display.
+ * 
+ * @author      Alexander Ebert
+ * @copyright   2001-2016 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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();