Skip to content

Commit 052ffa7

Browse files
authored
Merge pull request #42 from WebFiori/dev
refactor: Code Quality Improvements
2 parents 3d01b28 + 194756b commit 052ffa7

File tree

3 files changed

+82
-29
lines changed

3 files changed

+82
-29
lines changed

tests/webfiori/test/ui/HTMLNodeTest.php

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public function testAddChild00() {
141141
$this->assertEquals('p', $node->getChild(2)->getNodeName());
142142
$this->assertEquals('img', $node->getChild(3)->getNodeName());
143143
$this->assertEquals('ok', $node->getChild(3)->getAttribute('src'));
144+
144145
}
145146
/**
146147
* @test
@@ -344,6 +345,14 @@ public function testAddChild17() {
344345
. '</html>'.HTMLDoc::NL, $node->asCode([
345346
'with-colors' => false
346347
]));
348+
$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
349+
. '<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
350+
. ' <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
351+
. ' <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
352+
. ' <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
353+
. '<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([
354+
'with-colors' => true
355+
]));
347356
$node->setIsQuotedAttribute(false);
348357
}
349358
/**
@@ -392,29 +401,60 @@ public function testApplyClass00() {
392401
* @test
393402
*/
394403
public function testAsCode00() {
395-
$node = new HTMLNode();
404+
$node = new HTMLNode('div', [
405+
'class' => 'box',
406+
"hidden"
407+
]);
396408
$this->assertEquals("<pre style=\"margin:0;background-color:rgb(21, 18, 33);"
397409
. " color:gray\">\r\n<span style=\"color:rgb(204,225,70)\">"
398410
. "&lt;</span><span style=\"color:rgb(204,225,70)\">"
399-
. "div</span><span style=\"color:rgb(204,225,70)\">"
400-
. "&gt;</span>\r\n<span style=\"color:rgb(204,225,70)\">"
411+
. "div</span>"
412+
. " <span style=\"color:rgb(0,124,0)\">class</span>"
413+
. " <span style=\"color:gray\">=</span>"
414+
. " <span style=\"color:rgb(170,85,137)\">\"box\"</span>"
415+
. " <span style=\"color:rgb(0,124,0)\">hidden</span>"
416+
. "<span style=\"color:rgb(204,225,70)\">&gt;</span>\r\n"
417+
. ""
418+
. ""
419+
420+
. "<span style=\"color:rgb(204,225,70)\">"
401421
. "&lt;/</span><span style=\"color:rgb(204,225,70)\">"
402422
. "div</span><span style=\"color:rgb(204,225,70)\">"
403-
. "&gt;</span>\r\n</pre>",$node->asCode());
423+
. "&gt;</span>\r\n"
424+
. "</pre>",$node->asCode());
404425
}
405426
/**
406427
* @test
407428
*/
408429
public function testAsCode01() {
409-
$node = new HTMLNode();
410-
$node->addCommentNode('This is a comment.');
411-
$node->addTextNode('This is a simple text node.');
412-
$child00 = new HTMLNode('input');
413-
$child00->setID('child-00');
414-
$child00->setWritingDir('ltr');
415-
$node->addChild($child00);
416-
$this->assertTrue(true);
417-
//$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());
430+
$node = new HTMLNode('code');
431+
$this->assertEquals(""
432+
. "<pre style=\"margin:0;background-color:rgb(21, 18, 33); color:gray\">\r\n"
433+
. "<span style=\"color:rgb(204,225,70)\">&lt;</span><span style=\"color:rgb(204,225,70)\">code</span>"
434+
. "<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"
435+
. "</pre>",$node->asCode());
436+
}
437+
/**
438+
* @test
439+
*/
440+
public function testAsCode02() {
441+
$node = new HTMLNode('div');
442+
$node->comment('Hello');
443+
$this->assertEquals(""
444+
. "&lt;div&gt;\r\n"
445+
. " &lt!--Hello--&gt;\r\n"
446+
. "&lt;/div&gt;\r\n"
447+
. "",$node->asCode([
448+
'with-colors' => false
449+
]));
450+
$this->assertEquals("<pre style=\"margin:0\">\r\n"
451+
. "&lt;div&gt;\r\n"
452+
. " &lt!--Hello--&gt;\r\n"
453+
. "&lt;/div&gt;\r\n"
454+
. "</pre>",$node->asCode([
455+
'with-colors' => false,
456+
'use-pre' => true
457+
]));
418458
}
419459
/**
420460
* @test
@@ -510,12 +550,12 @@ public function testChaining03() {
510550
public function testChaining04() {
511551
$node = new HTMLNode('ul');
512552
$node->li('Hello', ['class' => 'first-menu-item'])
513-
->li('World')
553+
->li(new Anchor('World', 'https://example.com'))
514554
->li('From PHP');
515555
$this->assertEquals(3, $node->childrenCount());
516556
$this->assertEquals('<ul>'
517557
. '<li class="first-menu-item">Hello</li>'
518-
. '<li>World</li>'
558+
. '<li><a href=World target=_self>https://example.com</a></li>'
519559
. '<li>From PHP</li>'
520560
. '</ul>', $node->toHTML());
521561
}
@@ -2325,6 +2365,16 @@ public function testSetText00() {
23252365
$this->assertEquals('Hello & Welcome. Do you know that 1 is < 3 and 7 > 6?'
23262366
.'Also, 0>-100 && 0<8.',$node->getTextUnescaped());
23272367
}
2368+
/**
2369+
*
2370+
* @test
2371+
*/
2372+
public function testSetText01() {
2373+
$node = new HTMLNode();
2374+
$node->setText('Hello');
2375+
$this->assertEquals('', $node->getText());
2376+
$this->assertEquals('', $node->getTextUnescaped());
2377+
}
23282378
/**
23292379
* @test
23302380
*/
@@ -2487,6 +2537,17 @@ public function testToHTML11() {
24872537
$array = TemplateCompiler::htmlAsArray($htmlTxt);
24882538
$this->assertEquals(count($array),0);
24892539
}
2540+
/**
2541+
* @test
2542+
*/
2543+
public function testToHTML12() {
2544+
$node = new HTMLNode('div');
2545+
$node->addChild('div')->br()->text('Cool');
2546+
2547+
$this->assertEquals('<div><div><br>Cool</div></div>',$node->toHTML());
2548+
$this->assertEquals('', $node->getChild(0)->getChild(0)->close());
2549+
$this->assertEquals('', $node->getChild(0)->getChild(1)->close());
2550+
}
24902551
/**
24912552
* @test
24922553
*/

webfiori/ui/HTMLDoc.php

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -344,19 +344,11 @@ public function saveToHTMLFile(string $path, string $fileName, bool $wellFormatt
344344
*
345345
* @param HeadNode $node The node to set.
346346
*
347-
* @return bool If head node is set, the method will return true.
348-
* if it is not set, the method will return false.
349-
*
350-
* @since 1.0
351347
*/
352-
public function setHeadNode(HeadNode $node) : bool {
353-
if ($this->getDocumentRoot()->replaceChild($this->headNode, $node)) {
354-
$this->headNode = $node;
355-
356-
return true;
357-
}
348+
public function setHeadNode(HeadNode $node) {
349+
$this->getDocumentRoot()->replaceChild($this->headNode, $node);
350+
$this->headNode = $node;
358351

359-
return false;
360352
}
361353
/**
362354
* Sets the language of the document.

webfiori/ui/HTMLNode.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,7 @@ public function applyClass(string $cName, bool $override = true) {
473473
public function asCode(array $formattingOptions = HTMLNode::DEFAULT_CODE_FORMAT) {
474474
$formattingOptionsV = $this->validateFormattingOptions($formattingOptions);
475475
$this->nl = HTMLDoc::NL;
476+
$this->codeString = '';
476477
//number of spaces in a tab
477478
$spacesCount = $formattingOptionsV['tab-spaces'];
478479
$this->tabCount = $formattingOptionsV['initial-tab'];
@@ -2373,16 +2374,15 @@ private function addTab() {
23732374
* @return string
23742375
*
23752376
*/
2376-
private function closeAsCode(array $FO) {
2377+
private function closeAsCode(array $FO) : string {
23772378
if ($FO['with-colors'] === true && !$this->isTextNode() && !$this->isComment()) {
23782379
return '<span style="color:'.$FO['colors']['lt-gt-color'].'">&lt;/</span>'
23792380
.'<span style="color:'.$FO['colors']['node-name-color'].'">'.$this->getNodeName().'</span>'
23802381
.'<span style="color:'.$FO['colors']['lt-gt-color'].'">&gt;</span>';
2381-
} else if (!$this->isTextNode() && !$this->isComment()) {
2382+
} else {
23822383
return '&lt;/'.$this->getNodeName().'&gt;';
23832384
}
23842385

2385-
return '';
23862386
}
23872387
/**
23882388
*

0 commit comments

Comments
 (0)