diff --git a/tests/webfiori/test/ui/HTMLNodeTest.php b/tests/webfiori/test/ui/HTMLNodeTest.php index 059f17c..a1e0a1a 100644 --- a/tests/webfiori/test/ui/HTMLNodeTest.php +++ b/tests/webfiori/test/ui/HTMLNodeTest.php @@ -1056,7 +1056,6 @@ public function testGetComment01() { $node->setText('Hello World!'); $this->assertEquals('Hello World!',$node->getText()); $this->assertEquals('',$node->getComment()); - $this->assertEquals('Hello World!',$node->getOriginalText()); } /** * @test @@ -1066,10 +1065,8 @@ public function testGetComment02() { $this->assertEquals('',$node->getComment()); $node->setText('A Comment
with
html.'); $this->assertEquals('A Comment
with
html.',$node->getText()); - $this->assertEquals('A Comment
with
html.',$node->getOriginalText()); $node->setText('',$node->getComment()); $node->setText(''); $this->assertEquals(' --A Comment X -- ',$node->getText()); @@ -1170,13 +1167,10 @@ public function testGetText01() { $this->assertEquals('',$node->getText()); $node->setText('Hello World!'); $this->assertEquals('Hello World!',$node->getText()); - $this->assertEquals('Hello World!',$node->getOriginalText()); $node->setText('X < 6 and Y > 100'); $this->assertEquals('X < 6 and Y > 100',$node->getText()); - $this->assertEquals('X < 6 and Y > 100',$node->getOriginalText()); $node->setText('X < 6 and Y > 100',false); $this->assertEquals('X < 6 and Y > 100',$node->getText()); - $this->assertEquals('X < 6 and Y > 100',$node->getOriginalText()); } /** * @test @@ -2480,9 +2474,9 @@ public function testToHTML10() { $html = new HTMLNode(); $html->addChild($txtNode); $this->assertEquals('
<a>Link</a>
',$html.''); - $txtNode->setUseOriginal(true); + $txtNode->setEscapeEntities(false); $this->assertEquals('
Link
',$html.''); - $txtNode->setUseOriginal(false); + $txtNode->setEscapeEntities(true); $this->assertEquals('
<a>Link</a>
',$html.''); } /** diff --git a/webfiori/ui/HTMLNode.php b/webfiori/ui/HTMLNode.php index 667bc2a..fd90761 100644 --- a/webfiori/ui/HTMLNode.php +++ b/webfiori/ui/HTMLNode.php @@ -153,6 +153,7 @@ class HTMLNode implements Countable, Iterator { * */ private $isVoid; + private $isEsc; /** * The name of the tag (such as 'div') * @var string @@ -173,12 +174,6 @@ class HTMLNode implements Countable, Iterator { private $nodesStack; private $null; - /** - * The original text of a text node. - * @var string - * - */ - private $originalText; /** * The parent node of the instance. * @var HTMLNode @@ -245,11 +240,15 @@ public function __construct(string $name = 'div', array $attrs = []) { $this->text = ''; $this->useOriginalTxt = false; $this->attributes = []; + $this->isEsc = true; $nameUpper = strtoupper(trim($name)); if ($nameUpper == self::TEXT_NODE || $nameUpper == self::COMMENT_NODE) { $this->name = $nameUpper; + if ($nameUpper == self::COMMENT_NODE) { + $this->isEsc = false; + } $this->setIsVoidNode(true); } else { $this->name = trim($name); @@ -330,7 +329,7 @@ public function addChild($node, $attrsOrChain = [], bool $chainOnParent = false) $lastChild = $this->getLastChild(); if ($lastChild !== null && $lastChild->getNodeName() == '#TEXT') { - $lastChild->setText($lastChild->getText().$toAdd->getText(), $toAdd->getOriginalText() != $toAdd->getText()); + $lastChild->setText($lastChild->getText().$toAdd->getText()); } else { $toAdd->setParentHelper($this); $this->childrenList->add($toAdd); @@ -1063,14 +1062,17 @@ public function getNodeName() : string { return $this->name; } /** - * Returns the original text which was set in the body of the node. + * Sets the value of the property which is used to check if the text + * on the body of the node will be escaped or not if it has HTML entities. * - * This only applies to text nodes and comment nodes. + * This only applies to text node. * - * @return string The original text without any modifications. + * @param bool $esc */ - public function getOriginalText() { - return $this->originalText; + public function setEscapeEntities(bool $esc) { + if ($this->isTextNode()) { + $this->isEsc = $esc; + } } /** * Returns the parent node. @@ -1131,7 +1133,11 @@ public function getTabIndex() { */ public function getText() : string { if ($this->isComment() || $this->isTextNode()) { - return $this->text; + if ($this->isEntityEscaped()) { + return htmlentities($this->text, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401); + } else { + return $this->text; + } } return ''; @@ -1340,6 +1346,17 @@ public function insert(HTMLNode $el, int $position) : HTMLNode { public function isComment() : bool { return $this->getNodeName() == self::COMMENT_NODE; } + /** + * Checks if HTML entities will be escaped or not. + * + * This method is applicable only if node type is text. + * + * @return bool The method will return true if they will be escaped. False + * otherwise. + */ + public function isEntityEscaped() : bool { + return $this->isEsc; + } /** * Returns the value of the property $isFormatted. * @@ -1404,6 +1421,8 @@ public function isUseForwardSlash() : bool { * * @return bool True if original text will be used in the body of the * text node. False if not. Default is false. + * + * @deprecated since version 2.5.4 * */ public function isUseOriginalText() : bool { @@ -1956,7 +1975,9 @@ public function setNodeName(string $name) : bool { if (($this->isTextNode() && $uName == self::COMMENT_NODE) || ($this->isComment() && $uName == self::TEXT_NODE)) { $this->name = $uName; - + if ($uName == self::COMMENT_NODE) { + $this->isEsc = false; + } return true; } else { return false; @@ -2074,12 +2095,13 @@ public function setTabIndex(int $val) : HTMLNode { */ public function setText(string $text, bool $escHtmlEntities = true) : HTMLNode { if ($this->isTextNode() || $this->isComment()) { - $this->originalText = $text; - + + + if ($this->isComment()) { $text = str_replace('', '-- ', $text)); - } else if ($escHtmlEntities === true) { - $text = htmlentities($text, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401); + } else { + $this->setEscapeEntities($escHtmlEntities); } $this->text = $text; } @@ -2115,25 +2137,6 @@ public function setTitle(string $val) : HTMLNode { public function setUseForwardSlash(bool $hasForward) { self::$UseForwardSlash = $hasForward; } - /** - * Sets the value of the property $useOriginalTxt. - * - * The property is used when parsing text nodes. If it is set to true, - * the text that will be in the body of the node will be the exact text - * which was set using the method HTMLNode::setText() (The value which will be - * returned by the method HTMLNode::getOriginalText()). If it is set to - * false, then the text which is in the body of the node will be the - * value which is returned by the method HTMLNode::getText(). - * - * @param bool $boolean True or false. - * - * - */ - public function setUseOriginal(bool $boolean) { - if ($this->isTextNode()) { - $this->useOriginalTxt = $boolean === true; - } - } /** * Sets the value of the attribute 'dir' of the node. * @@ -2532,11 +2535,7 @@ private function popNodeAsCode(array $FO) { private function pushNode(HTMLNode $node) { if ($node->isTextNode()) { if (!self::isFormatted()) { - if ($node->isUseOriginalText()) { - $this->htmlString .= $node->getOriginalText(); - } else { - $this->htmlString .= $node->getText(); - } + $this->htmlString .= $node->getText(); } else { $parent = $node->getParent(); @@ -2595,11 +2594,7 @@ private function pushNode(HTMLNode $node) { */ private function pushNodeAsCode(HTMLNode $node, array $FO) { if ($node->isTextNode()) { - if ($node->isUseOriginalText()) { - $this->codeString .= $this->getTab().$node->getOriginalText().$this->nl; - } else { - $this->codeString .= $this->getTab().$node->getText().$this->nl; - } + $this->codeString .= $this->getTab().$node->getText().$this->nl; } else if ($node->isComment()) { if ($FO['with-colors'] === true) { $this->codeString .= $this->getTab().'<!--'.$node->getText().'-->'.$this->nl; diff --git a/webfiori/ui/HTMLTable.php b/webfiori/ui/HTMLTable.php index f4d0b79..9b3921c 100644 --- a/webfiori/ui/HTMLTable.php +++ b/webfiori/ui/HTMLTable.php @@ -237,11 +237,16 @@ public function rows() : int { * * @param array $attributes An array that contains the attributes and * their values. + * + * @return HTMLTable the method will return the same instance at which the + * method is called on. */ - public function setColAttributes(int $colNum, array $attributes) { + public function setColAttributes(int $colNum, array $attributes) : HTMLTable { for ($x = 0 ; $x < $this->rows() ; $x++) { $this->getCell($x, $colNum)->setAttributes($attributes); } + + return $this; } /** * Sets the attributes of cells in one specific row. @@ -253,13 +258,18 @@ public function setColAttributes(int $colNum, array $attributes) { * * @param array $attributes An array that contains the attributes and * their values. + * + * @return HTMLTable the method will return the same instance at which the + * method is called on. */ - public function setRowAttributes(int $rowNum, array $attributes) { + public function setRowAttributes(int $rowNum, array $attributes) : HTMLTable { $row = $this->getRow($rowNum); foreach ($row->children() as $cell) { $cell->setAttributes($attributes); } + + return $this; } /** * Sets the value of a specific cell in the table. @@ -273,21 +283,26 @@ public function setRowAttributes(int $rowNum, array $attributes) { * * @throws InvalidArgumentException If row index or column index is invalid. * + * @return HTMLTable the method will return the same instance at which the + * method is called on. + * * @since 1.0 */ - public function setValue(int $rowIndex, int $colIndex, $value) { + public function setValue(int $rowIndex, int $colIndex, $value) : HTMLTable { if ($rowIndex < $this->rows() && $rowIndex >= 0) { if ($colIndex < $this->cols() && $colIndex >= 0) { $cell = $this->getChild($rowIndex)->getChild($colIndex); $cell->removeAllChildNodes(); - if ($value instanceof HTMLNode) { - $cell->addChild($value); - } else { - $cell->text($value); + if ($value !== null) { + if ($value instanceof HTMLNode) { + $cell->addChild($value); + } else { + $cell->text($value); + } } - return; + return $this; } throw new InvalidArgumentException("Column index must be less than ".$this->cols().' and greater than -1.'); }