Skip to content

Commit

Permalink
Merge pull request #42 from WebFiori/dev
Browse files Browse the repository at this point in the history
refactor: Code Quality Improvements
  • Loading branch information
usernane committed Jul 4, 2024
2 parents 3d01b28 + 194756b commit 052ffa7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 29 deletions.
91 changes: 76 additions & 15 deletions tests/webfiori/test/ui/HTMLNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -344,6 +345,14 @@ public function testAddChild17() {
. '</html>'.HTMLDoc::NL, $node->asCode([
'with-colors' => false
]));
$this->assertEquals('<span style="color:rgb(204,225,70)">&lt;</span><span style="color:rgb(204,225,70)">!DOCTYPE html</span><span style="color:rgb(204,225,70)">&gt;</span>'.HTMLDoc::NL
. '<span style="color:rgb(204,225,70)">&lt;</span><span style="color:rgb(204,225,70)">html</span><span style="color:rgb(204,225,70)">&gt;</span>'.HTMLDoc::NL
. ' <span style="color:rgb(204,225,70)">&lt;</span><span style="color:rgb(204,225,70)">body</span><span style="color:rgb(204,225,70)">&gt;</span>'.HTMLDoc::NL
. ' <span style="color:rgb(204,225,70)">&lt;</span><span style="color:rgb(204,225,70)">img</span> <span style="color:rgb(0,124,0)">src</span> <span style="color:gray">=</span> <span style="color:rgb(170,85,137)">"image.png"</span><span style="color:rgb(204,225,70)">&gt;</span>'.HTMLDoc::NL
. ' <span style="color:rgb(204,225,70)">&lt;/</span><span style="color:rgb(204,225,70)">body</span><span style="color:rgb(204,225,70)">&gt;</span>'.HTMLDoc::NL
. '<span style="color:rgb(204,225,70)">&lt;/</span><span style="color:rgb(204,225,70)">html</span><span style="color:rgb(204,225,70)">&gt;</span>'.HTMLDoc::NL, $node->asCode([
'with-colors' => true
]));
$node->setIsQuotedAttribute(false);
}
/**
Expand Down Expand Up @@ -392,29 +401,60 @@ public function testApplyClass00() {
* @test
*/
public function testAsCode00() {
$node = new HTMLNode();
$node = new HTMLNode('div', [
'class' => 'box',
"hidden"
]);
$this->assertEquals("<pre style=\"margin:0;background-color:rgb(21, 18, 33);"
. " color:gray\">\r\n<span style=\"color:rgb(204,225,70)\">"
. "&lt;</span><span style=\"color:rgb(204,225,70)\">"
. "div</span><span style=\"color:rgb(204,225,70)\">"
. "&gt;</span>\r\n<span style=\"color:rgb(204,225,70)\">"
. "div</span>"
. " <span style=\"color:rgb(0,124,0)\">class</span>"
. " <span style=\"color:gray\">=</span>"
. " <span style=\"color:rgb(170,85,137)\">\"box\"</span>"
. " <span style=\"color:rgb(0,124,0)\">hidden</span>"
. "<span style=\"color:rgb(204,225,70)\">&gt;</span>\r\n"
. ""
. ""

. "<span style=\"color:rgb(204,225,70)\">"
. "&lt;/</span><span style=\"color:rgb(204,225,70)\">"
. "div</span><span style=\"color:rgb(204,225,70)\">"
. "&gt;</span>\r\n</pre>",$node->asCode());
. "&gt;</span>\r\n"
. "</pre>",$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("<pre style=\"margin:0;background-color:rgb(21, 18, 33); color:gray\">\r\n<span style=\"color:rgb(204,225,70)\">&lt;</span><span style=\"color:rgb(204,225,70)\">div</span><span style=\"color:rgb(204,225,70)\">&gt;</span>\r\n<span style=\"color:rgb(204,225,70)\">&lt;/</span><span style=\"color:rgb(204,225,70)\">div</span><span style=\"color:rgb(204,225,70)\">&gt;</span>\r\n</pre>",$node->asCode());
$node = new HTMLNode('code');
$this->assertEquals(""
. "<pre style=\"margin:0;background-color:rgb(21, 18, 33); color:gray\">\r\n"
. "<span style=\"color:rgb(204,225,70)\">&lt;</span><span style=\"color:rgb(204,225,70)\">code</span>"
. "<span style=\"color:rgb(204,225,70)\">&gt;</span><span style=\"color:rgb(204,225,70)\">&lt;/</span><span style=\"color:rgb(204,225,70)\">code</span><span style=\"color:rgb(204,225,70)\">&gt;</span>\r\n"
. "</pre>",$node->asCode());
}
/**
* @test
*/
public function testAsCode02() {
$node = new HTMLNode('div');
$node->comment('Hello');
$this->assertEquals(""
. "&lt;div&gt;\r\n"
. " &lt!--Hello--&gt;\r\n"
. "&lt;/div&gt;\r\n"
. "",$node->asCode([
'with-colors' => false
]));
$this->assertEquals("<pre style=\"margin:0\">\r\n"
. "&lt;div&gt;\r\n"
. " &lt!--Hello--&gt;\r\n"
. "&lt;/div&gt;\r\n"
. "</pre>",$node->asCode([
'with-colors' => false,
'use-pre' => true
]));
}
/**
* @test
Expand Down Expand Up @@ -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('<ul>'
. '<li class="first-menu-item">Hello</li>'
. '<li>World</li>'
. '<li><a href=World target=_self>https://example.com</a></li>'
. '<li>From PHP</li>'
. '</ul>', $node->toHTML());
}
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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('<div><div><br>Cool</div></div>',$node->toHTML());
$this->assertEquals('', $node->getChild(0)->getChild(0)->close());
$this->assertEquals('', $node->getChild(0)->getChild(1)->close());
}
/**
* @test
*/
Expand Down
14 changes: 3 additions & 11 deletions webfiori/ui/HTMLDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions webfiori/ui/HTMLNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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 '<span style="color:'.$FO['colors']['lt-gt-color'].'">&lt;/</span>'
.'<span style="color:'.$FO['colors']['node-name-color'].'">'.$this->getNodeName().'</span>'
.'<span style="color:'.$FO['colors']['lt-gt-color'].'">&gt;</span>';
} else if (!$this->isTextNode() && !$this->isComment()) {
} else {
return '&lt;/'.$this->getNodeName().'&gt;';
}

return '';
}
/**
*
Expand Down

0 comments on commit 052ffa7

Please sign in to comment.