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/HTMLDoc.php b/webfiori/ui/HTMLDoc.php index 8fa7ec0..21c276c 100644 --- a/webfiori/ui/HTMLDoc.php +++ b/webfiori/ui/HTMLDoc.php @@ -344,19 +344,11 @@ public function saveToHTMLFile(string $path, string $fileName, bool $wellFormatt * * @param HeadNode $node The node to set. * - * @return bool If head node is set, the method will return true. - * if it is not set, the method will return false. - * - * @since 1.0 */ - public function setHeadNode(HeadNode $node) : bool { - if ($this->getDocumentRoot()->replaceChild($this->headNode, $node)) { - $this->headNode = $node; - - return true; - } + public function setHeadNode(HeadNode $node) { + $this->getDocumentRoot()->replaceChild($this->headNode, $node); + $this->headNode = $node; - return false; } /** * Sets the language of the document. 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 ''; } /** *