Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/WebFiori/ui into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jul 8, 2024
2 parents 568ea7a + 194756b commit eb03de1
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 58 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
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
85 changes: 45 additions & 40 deletions webfiori/ui/TemplateCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,56 +135,61 @@ 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;
}

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.
*
Expand Down

0 comments on commit eb03de1

Please sign in to comment.