Dominic.sauer has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/219174

Change subject: Allow raw content for html builder, escape content by default
......................................................................

Allow raw content for html builder, escape content by default

Change-Id: I30a40b02b5fbd673468dbc0218f0263915fe20ed
---
M includes/Html/HtmlTableCellBuilder.php
M includes/Html/HtmlTableHeaderBuilder.php
2 files changed, 49 insertions(+), 11 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikidataQuality 
refs/changes/74/219174/1

diff --git a/includes/Html/HtmlTableCellBuilder.php 
b/includes/Html/HtmlTableCellBuilder.php
index 348451d..c840949 100755
--- a/includes/Html/HtmlTableCellBuilder.php
+++ b/includes/Html/HtmlTableCellBuilder.php
@@ -25,17 +25,31 @@
        private $attributes;
 
        /**
-        * @param $content Html
-        * @param array $attributes
+        * Determines, whether the content is raw html or should be escaped.
+        *
+        * @var bool
         */
-       public function __construct( $content, array $attributes = array() ) {
+       private $isRawContent;
+
+       /**
+        * @param string HTML $content
+        * @param array $attributes
+        * @param bool $isRawContent
+        *
+        * @throws InvalidArgumentException
+        */
+       public function __construct( $content, array $attributes = array(), 
$isRawContent = false ) {
                // Check parameters
                if ( !is_string( $content ) ) {
                        throw new InvalidArgumentException( '$content must be 
string.' );
                }
+               if ( !is_bool( $isRawContent ) ) {
+                       throw new InvalidArgumentException( '$isRawContent must 
be boolean.' );
+               }
 
                $this->content = $content;
                $this->attributes = $attributes;
+               $this->isRawContent = $isRawContent;
        }
 
        /**
@@ -58,12 +72,17 @@
         * @return string HTML
         */
        public function toHtml() {
+               $content = $this->content;
+               if ( !$this->isRawContent ) {
+                       $content = htmlspecialchars( $this->content );
+               }
+
                return
                        Html::openElement(
                                'td',
                                $this->getAttributes()
                        )
-                       . $this->content
+                       . $content
                        . Html::closeElement( 'td' );
        }
-}
\ No newline at end of file
+}
diff --git a/includes/Html/HtmlTableHeaderBuilder.php 
b/includes/Html/HtmlTableHeaderBuilder.php
index 10a004b..6814278 100755
--- a/includes/Html/HtmlTableHeaderBuilder.php
+++ b/includes/Html/HtmlTableHeaderBuilder.php
@@ -27,19 +27,33 @@
        private $isSortable;
 
        /**
-        * @param string $content HTML
-        * @param bool $isSortable
+        * Determines, whether the content is raw html or should be escaped.
+        *
+        * @var bool
         */
-       public function __construct( $content, $isSortable = false ) {
+       private $isRawContent;
+
+       /**
+        * @param string HTML $content
+        * @param bool $isSortable
+        * @param bool $isRawContent
+        *
+        * @throws InvalidArgumentException
+        */
+       public function __construct( $content, $isSortable = false, 
$isRawContent = false ) {
                if ( !is_string( $content ) ) {
                        throw new InvalidArgumentException( '$content must be 
string.' );
                }
                if ( !is_bool( $isSortable ) ) {
                        throw new InvalidArgumentException( '$isSortable must 
be boolean.' );
                }
+               if ( !is_bool( $isRawContent ) ) {
+                       throw new InvalidArgumentException( '$isRawContent must 
be boolean.' );
+               }
 
                $this->content = $content;
                $this->isSortable = $isSortable;
+               $this->isRawContent = $isRawContent;
        }
 
        /**
@@ -69,12 +83,17 @@
                        $attributes['class'] = 'unsortable';
                }
 
+               $content = $this->content;
+               if ( !$this->isRawContent ) {
+                       $content = htmlspecialchars( $this->content );
+               }
+
                return
                        Html::openElement(
                                'th',
                                $attributes
                        )
-                       . $this->content
-                       . Html::closeElement( 'th' );
+                       . $content
+                       . Html::closeElement('th');
        }
-}
\ No newline at end of file
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/219174
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I30a40b02b5fbd673468dbc0218f0263915fe20ed
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikidataQuality
Gerrit-Branch: master
Gerrit-Owner: Dominic.sauer <dominic.sa...@yahoo.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to