diff --git a/tests/webfiori/test/ui/HTMLNodeTest.php b/tests/webfiori/test/ui/HTMLNodeTest.php index a1e0a1a..f69704a 100644 --- a/tests/webfiori/test/ui/HTMLNodeTest.php +++ b/tests/webfiori/test/ui/HTMLNodeTest.php @@ -141,6 +141,7 @@ public function testAddChild00() { $this->assertEquals('p', $node->getChild(2)->getNodeName()); $this->assertEquals('img', $node->getChild(3)->getNodeName()); $this->assertEquals('ok', $node->getChild(3)->getAttribute('src')); + } /** * @test @@ -344,6 +345,14 @@ public function testAddChild17() { . '</html>'.HTMLDoc::NL, $node->asCode([ 'with-colors' => false ])); + $this->assertEquals('<!DOCTYPE html>'.HTMLDoc::NL + . '<html>'.HTMLDoc::NL + . ' <body>'.HTMLDoc::NL + . ' <img src = "image.png">'.HTMLDoc::NL + . ' </body>'.HTMLDoc::NL + . '</html>'.HTMLDoc::NL, $node->asCode([ + 'with-colors' => true + ])); $node->setIsQuotedAttribute(false); } /** @@ -392,29 +401,60 @@ public function testApplyClass00() { * @test */ public function testAsCode00() { - $node = new HTMLNode(); + $node = new HTMLNode('div', [ + 'class' => 'box', + "hidden" + ]); $this->assertEquals("
\r\n"
                 . "<"
-                . "div"
-                . ">\r\n"
+                . "div"
+                . " class"
+                . " ="
+                . " \"box\""
+                . " hidden"
+                . ">\r\n"
+                . ""
+                . ""
+                
+                . ""
                 . "</"
                 . "div"
-                . ">\r\n
",$node->asCode()); + . ">\r\n" + . "",$node->asCode()); } /** * @test */ public function testAsCode01() { - $node = new HTMLNode(); - $node->addCommentNode('This is a comment.'); - $node->addTextNode('This is a simple text node.'); - $child00 = new HTMLNode('input'); - $child00->setID('child-00'); - $child00->setWritingDir('ltr'); - $node->addChild($child00); - $this->assertTrue(true); - //$this->assertEquals("
\r\n<div>\r\n</div>\r\n
",$node->asCode()); + $node = new HTMLNode('code'); + $this->assertEquals("" + . "
\r\n"
+                . "<code"
+                . "></code>\r\n"
+                . "
",$node->asCode()); + } + /** + * @test + */ + public function testAsCode02() { + $node = new HTMLNode('div'); + $node->comment('Hello'); + $this->assertEquals("" + . "<div>\r\n" + . " <!--Hello-->\r\n" + . "</div>\r\n" + . "",$node->asCode([ + 'with-colors' => false + ])); + $this->assertEquals("
\r\n"
+                . "<div>\r\n"
+                . "    <!--Hello-->\r\n"
+                . "</div>\r\n"
+                . "
",$node->asCode([ + 'with-colors' => false, + 'use-pre' => true + ])); } /** * @test @@ -510,12 +550,12 @@ public function testChaining03() { public function testChaining04() { $node = new HTMLNode('ul'); $node->li('Hello', ['class' => 'first-menu-item']) - ->li('World') + ->li(new Anchor('World', 'https://example.com')) ->li('From PHP'); $this->assertEquals(3, $node->childrenCount()); $this->assertEquals('', $node->toHTML()); } @@ -2325,6 +2365,16 @@ public function testSetText00() { $this->assertEquals('Hello & Welcome. Do you know that 1 is < 3 and 7 > 6?' .'Also, 0>-100 && 0<8.',$node->getTextUnescaped()); } + /** + * + * @test + */ + public function testSetText01() { + $node = new HTMLNode(); + $node->setText('Hello'); + $this->assertEquals('', $node->getText()); + $this->assertEquals('', $node->getTextUnescaped()); + } /** * @test */ @@ -2487,6 +2537,17 @@ public function testToHTML11() { $array = TemplateCompiler::htmlAsArray($htmlTxt); $this->assertEquals(count($array),0); } + /** + * @test + */ + public function testToHTML12() { + $node = new HTMLNode('div'); + $node->addChild('div')->br()->text('Cool'); + + $this->assertEquals('

Cool
',$node->toHTML()); + $this->assertEquals('', $node->getChild(0)->getChild(0)->close()); + $this->assertEquals('', $node->getChild(0)->getChild(1)->close()); + } /** * @test */ diff --git a/webfiori/ui/HTMLNode.php b/webfiori/ui/HTMLNode.php index de8448d..c7ccece 100644 --- a/webfiori/ui/HTMLNode.php +++ b/webfiori/ui/HTMLNode.php @@ -473,6 +473,7 @@ public function applyClass(string $cName, bool $override = true) { public function asCode(array $formattingOptions = HTMLNode::DEFAULT_CODE_FORMAT) { $formattingOptionsV = $this->validateFormattingOptions($formattingOptions); $this->nl = HTMLDoc::NL; + $this->codeString = ''; //number of spaces in a tab $spacesCount = $formattingOptionsV['tab-spaces']; $this->tabCount = $formattingOptionsV['initial-tab']; @@ -2373,16 +2374,15 @@ private function addTab() { * @return string * */ - private function closeAsCode(array $FO) { + private function closeAsCode(array $FO) : string { if ($FO['with-colors'] === true && !$this->isTextNode() && !$this->isComment()) { return '</' .''.$this->getNodeName().'' .'>'; - } else if (!$this->isTextNode() && !$this->isComment()) { + } else { return '</'.$this->getNodeName().'>'; } - return ''; } /** * diff --git a/webfiori/ui/TemplateCompiler.php b/webfiori/ui/TemplateCompiler.php index 2ffcb40..5d36344 100644 --- a/webfiori/ui/TemplateCompiler.php +++ b/webfiori/ui/TemplateCompiler.php @@ -135,49 +135,15 @@ public function compile(array $varsToPass = []) { */ public static function fromHTMLText(string $text, bool $asHTMLDocObj = true) { $nodesArr = self::htmlAsArray($text); - $TN = 'tag-name'; - $retVal = []; - + if (count($nodesArr) >= 1) { + $TN = 'tag-name'; + $retVal = []; + if ($asHTMLDocObj && ($nodesArr[0][$TN] == 'html' || $nodesArr[0][$TN] == '!DOCTYPE')) { - $retVal = new HTMLDoc(); - $retVal->getHeadNode()->removeAllChildNodes(); - $retVal->getBody()->removeAttributes(); - - for ($x = 0 ; $x < count($nodesArr) ; $x++) { - if ($nodesArr[$x][$TN] == 'html') { - $htmlNode = self::fromHTMLTextHelper00($nodesArr[$x]); - - for ($y = 0 ; $y < $htmlNode->childrenCount() ; $y++) { - $child = $htmlNode->children()->get($y); - - if ($child->getNodeName() == 'head') { - $retVal->setHeadNode($child); - } else { - if ($child->getNodeName() == 'body') { - for ($z = 0 ; $z < $child->childrenCount() ; $z++) { - $node = $child->children()->get($z); - $retVal->addChild($node); - } - } - } - } - } else { - if ($nodesArr[$x][$TN] == 'head') { - $headNode = self::fromHTMLTextHelper00($nodesArr[$x]); - $retVal->setHeadNode($headNode); - } - } - } + $retVal = self::parseHTMLDoc($nodesArr); } else { - if (count($nodesArr) != 1) { - foreach ($nodesArr as $node) { - $asHtmlNode = self::fromHTMLTextHelper00($node); - $retVal[] = $asHtmlNode; - } - } else if (count($nodesArr) == 1) { - return self::fromHTMLTextHelper00($nodesArr[0]); - } + $retVal = self::parseHTMLNode($nodesArr); } return $retVal; @@ -185,6 +151,45 @@ public static function fromHTMLText(string $text, bool $asHTMLDocObj = true) { return null; } + private static function parseHTMLNode($nodesArr) { + if (count($nodesArr) != 1) { + $retVal = []; + foreach ($nodesArr as $node) { + $asHtmlNode = self::fromHTMLTextHelper00($node); + $retVal[] = $asHtmlNode; + } + return $retVal; + } else { + return self::fromHTMLTextHelper00($nodesArr[0]); + } + } + private static function parseHTMLDoc($children) : HTMLDoc { + $retVal = new HTMLDoc(); + $retVal->getHeadNode()->removeAllChildNodes(); + $retVal->getBody()->removeAttributes(); + $TN = 'tag-name'; + + for ($x = 0 ; $x < count($children) ; $x++) { + if ($children[$x][$TN] == 'html') { + $htmlNode = self::fromHTMLTextHelper00($children[$x]); + + for ($y = 0 ; $y < $htmlNode->childrenCount() ; $y++) { + $child = $htmlNode->children()->get($y); + + if ($child->getNodeName() == 'head') { + $retVal->setHeadNode($child); + } else if ($child->getNodeName() == 'body') { + for ($z = 0 ; $z < $child->childrenCount() ; $z++) { + $node = $child->children()->get($z); + $retVal->addChild($node); + } + + } + } + } + } + return $retVal; + } /** * Returns an array that contains directories names of the calling files. *