Skip to content

Commit

Permalink
Merge pull request #33 from WebFiori/dev
Browse files Browse the repository at this point in the history
Code Quality Improvments
  • Loading branch information
usernane authored Mar 25, 2023
2 parents 74267a3 + b567aba commit 9b081eb
Show file tree
Hide file tree
Showing 18 changed files with 347 additions and 392 deletions.
12 changes: 6 additions & 6 deletions tests/webfiori/test/ui/HTMLNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ public function testChaining03() {
$this->assertEquals('<div><div itemscope @onclick>'
. '<ul><li>Hello</li><li>World</li></ul>'
. '<ol><li>Good</li><li>Girl</li><li><label>Test With Node</label></li><li></li></ol>'
. '<a href="" target=_self class="imag-link"><img src=Test alt=Test></a>'
. '<a target=_self class="imag-link"><img src=Test alt=Test></a>'
. '</div></div>', $node->toHTML());
}
/**
Expand Down Expand Up @@ -777,7 +777,7 @@ public function testFromHTML_07() {
$htmlTxt = '<html><head><title>This is a test document. ';
$val = TemplateCompiler::fromHTMLText($htmlTxt);
$this->assertTrue($val instanceof HTMLDoc);
$this->assertEquals('This is a test document.',$val->getHeadNode()->getTitle());
$this->assertEquals('This is a test document.',$val->getHeadNode()->getPageTitle());
}
/**
* @test
Expand All @@ -786,7 +786,7 @@ public function testFromHTML_08() {
$htmlTxt = '<html><HEAD><meta CHARSET="utf-8"><title>This is a test document.</title>';
$val = TemplateCompiler::fromHTMLText($htmlTxt);
$this->assertTrue($val instanceof HTMLDoc);
$this->assertEquals('This is a test document.',$val->getHeadNode()->getTitle());
$this->assertEquals('This is a test document.',$val->getHeadNode()->getPageTitle());
$this->assertEquals('utf-8',$val->getHeadNode()->getCharSet());
}
/**
Expand All @@ -797,7 +797,7 @@ public function testFromHTML_09() {
.'<input type = text ID="input-el-1">';
$val = TemplateCompiler::fromHTMLText($htmlTxt);
$this->assertTrue($val instanceof HTMLDoc);
$this->assertEquals('This is a test document.',$val->getHeadNode()->getTitle());
$this->assertEquals('This is a test document.',$val->getHeadNode()->getPageTitle());
$this->assertEquals('utf-8',$val->getHeadNode()->getMeta('charset')->getAttributeValue('charset'));
$el = $val->getChildByID('input-el-1');
$this->assertTrue($el instanceof HTMLNode);
Expand Down Expand Up @@ -863,7 +863,7 @@ public function testFromHTML_10() {
.'<input type = text ID="input-el-1">';
$val = TemplateCompiler::fromHTMLText($htmlTxt);
$this->assertTrue($val instanceof HTMLDoc);
$this->assertEquals('This is a test document.',$val->getHeadNode()->getTitle());
$this->assertEquals('This is a test document.',$val->getHeadNode()->getPageTitle());
$this->assertEquals('https://example.com/',$val->getHeadNode()->getBaseURL());
$this->assertEquals('utf-8',$val->getHeadNode()->getMeta('charset')->getAttributeValue('charset'));
$el = $val->getChildByID('input-el-1');
Expand Down Expand Up @@ -924,7 +924,7 @@ public function testFromHTML_11() {
';
$val = TemplateCompiler::fromHTMLText($html);
$this->assertTrue($val instanceof HTMLDoc);
$this->assertEquals('TODO supply a title',$val->getHeadNode()->getTitle());
$this->assertEquals('TODO supply a title',$val->getHeadNode()->getPageTitle());
$this->assertEquals(12,$val->getHeadNode()->childrenCount());
$this->assertEquals(2,$val->getHeadNode()->getJSNodes()->size());
$this->assertEquals(3,$val->getHeadNode()->getCSSNodes()->size());
Expand Down
2 changes: 1 addition & 1 deletion tests/webfiori/test/ui/HeadNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ public function testSetTitle01() {
$this->assertEquals(2,$node->childrenCount());
$this->assertNotNull($node->getTitleNode());
$this->assertEquals('Hello',$node->getPageTitle());
$node->setTitle('');
$node->setPageTitle();
$this->assertEquals(1,$node->childrenCount());
$this->assertEquals('',$node->getPageTitle());
$this->assertNotNull($node->getTitleNode());
Expand Down
4 changes: 2 additions & 2 deletions tests/webfiori/test/ui/LoadTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function test01() {
$node = $compiler->getCompiled();
$this->assertTrue($node instanceof HTMLDoc);
$this->assertEquals(3, $node->getHeadNode()->childrenCount());
$this->assertEquals('TODO supply a title', $node->getHeadNode()->getTitle());
$this->assertEquals('TODO supply a title', $node->getHeadNode()->getPageTitle());
$this->assertEquals('UTF-8', $node->getHeadNode()->getCharSet());
$this->assertEquals(1, $node->getBody()->childrenCount());
$this->assertEquals('TODO write content', $node->getBody()->getChild(0)->getChild(0)->getText());
Expand Down Expand Up @@ -61,7 +61,7 @@ public function test02() {
$this->assertTrue($node instanceof HTMLDoc);
$this->assertEquals(5, $node->getHeadNode()->childrenCount());
$this->assertEquals('https://example.com/ajaxlib.js', $node->getChildByID('my-script')->getAttribute('src'));
$this->assertEquals('Users Status', $node->getHeadNode()->getTitle());
$this->assertEquals('Users Status', $node->getHeadNode()->getPageTitle());
$this->assertEquals('A page that shows the status of users accounts.', $node->getHeadNode()->getMeta('description')->getAttribute('content'));
$this->assertEquals('Users Status', $node->getChildByID('h-title')->getChild(0)->getText());
$headerRow = $node->getChildByID('header-row');
Expand Down
8 changes: 4 additions & 4 deletions webfiori/ui/Anchor.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class Anchor extends HTMLNode {
*/
public function __construct(string $href, $body, string $target = '_self') {
parent::__construct('a');
$this->setAttribute(self::$Attrs[1],$href);
$this->setHref($href);

if (strlen($target) != 0) {
$this->setAttribute(self::$Attrs[0],$target);
$this->setTarget($target);
} else {
$this->setAttribute(self::$Attrs[0], '_blank');
$this->setTarget('_blank');
}

if ($body instanceof HTMLNode) {
Expand Down Expand Up @@ -78,7 +78,7 @@ public function setHref(string $link) {
*
* @since 1.0
*/
public function setTarget($name) {
public function setTarget(string $name) {
$this->setAttribute(self::$Attrs[0], $name);
}
/**
Expand Down
2 changes: 1 addition & 1 deletion webfiori/ui/Br.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public function __construct() {
*
* @since 1.0
*/
public function addChild($param, $attrs = [], bool $chainOnParent = true) {
public function addChild($node, $attrsOrChainOrChain = [], bool $chainOnParent = false) {
}
}
48 changes: 15 additions & 33 deletions webfiori/ui/CodeSnippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,13 @@
* @version 1.0.3
*/
class CodeSnippet extends HTMLNode {
/**
*
* @var HTMLCode
* @since 1.0
*/

private $code;
/**
*
* @var HTMLCode
* @since 1.0
*/

private $codeDisplay;
/**
*
* @var HTMLNode
* @since 1.0.1
*/

private $codeStrNode;
private $currentLineNum;
/**
*
* @var HTMLCode
* @since 1.0
*/
private $lineNumsNode;
/**
* The original code text.
Expand Down Expand Up @@ -174,7 +157,7 @@ public function __construct(string $title = 'Code Snippet',string $code = null)
*
* @since 1.0.1
*/
public function addCodeLine($codeAsTxt) {
public function addCodeLine(string $codeAsTxt) {
$this->originalCode .= $codeAsTxt;
$this->addLineHelper();
$oldCode = $this->codeStrNode->getTextUnescaped();
Expand All @@ -188,7 +171,7 @@ public function addCodeLine($codeAsTxt) {
*
* @since 1.0.3
*/
public function getCodeElement() {
public function getCodeElement() : HTMLNode {
return $this->code;
}
/**
Expand All @@ -198,7 +181,7 @@ public function getCodeElement() {
*
* @since 1.0.2
*/
public function getOriginalCode() {
public function getOriginalCode() : string {
return $this->originalCode;
}
/**
Expand All @@ -209,7 +192,7 @@ public function getOriginalCode() {
*
* @since 1.0.2
*/
public function getOriginalTitle() {
public function getOriginalTitle() : string {
return $this->titleNode->getOriginalText();
}
/**
Expand All @@ -220,7 +203,7 @@ public function getOriginalTitle() {
*
* @since 1.0.2
*/
public function getTitle() {
public function getTitle() : string {
return $this->titleNode->getText();
}
/**
Expand All @@ -231,7 +214,7 @@ public function getTitle() {
*
* @since 1.0
*/
public function setCode($code) {
public function setCode(string $code) {
$this->originalCode = $code;
$xCode = trim($code);
$len = strlen($xCode);
Expand All @@ -253,27 +236,26 @@ public function setCode($code) {
* 'Java Code' or 'HTML Code'). The title will appear at the top of the snippet
* block.
*
* @param string $title The title of the snippet.
* @param string $val The title of the snippet.
*
* @since 1.0
*/
public function setTitle(string $title) : HTMLNode {
public function setTitle(string $val) : HTMLNode {
$this->titleNode->clear();
$this->titleNode->addText($title);
$this->titleNode->addText($val);

return $this;
}
private function addLineHelper() {
$span = new HTMLNode('span');
$span->setClassName('line-number');
$span->setAttribute('style', ''
.'font-weight: bold;'
$span->setAttribute('style',
'font-weight: bold;'
.'display: block;'
.'font-family: monospace;'
.'border-right: 1px dotted white;'
.'padding-right: 4px;'
.'color: #378e80;'
.'');
.'color: #378e80;');
$span->addTextNode($this->currentLineNum);
$this->currentLineNum++;
$this->lineNumsNode->addChild($span);
Expand Down
38 changes: 16 additions & 22 deletions webfiori/ui/HTMLDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ class HTMLDoc {
*/
private $body;

/**
* The whole document as HTML string.
*
* @var string
*
* @since 1.0
*/
private $document;
/**
* The head tag of the document.
* @var HTMLNode
Expand Down Expand Up @@ -130,7 +122,7 @@ public function __toString() {
*
* @since 1.0
*/
public function addChild($node, array $attributes = [], bool $chainOnParent = false) {
public function addChild($node, array $attributes = [], bool $chainOnParent = false) : HTMLNode {
$name = $node instanceof HTMLNode ? $node->getNodeName() : trim($node);

if ($name != 'body' && $name != 'head' && $name != 'html') {
Expand Down Expand Up @@ -166,7 +158,7 @@ public function addChild($node, array $attributes = [], bool $chainOnParent = fa
*
* @since 1.4
*/
public function asCode(array $formattingOptions = HTMLNode::DEFAULT_CODE_FORMAT) {
public function asCode(array $formattingOptions = HTMLNode::DEFAULT_CODE_FORMAT) : string {
return $this->getDocumentRoot()->asCode($formattingOptions);
}
/**
Expand All @@ -189,7 +181,7 @@ public function getBody() : HTMLNode {
*
* @since 1.2
*/
public function getChildByID($id) {
public function getChildByID(string $id) {
return $this->getDocumentRoot()->getChildByID($id);
}
/**
Expand Down Expand Up @@ -306,7 +298,7 @@ public function removeChild($node) {
public function removeChildByID(string $id) {
$toRemove = $this->getDocumentRoot()->getChildByID($id);

if ($toRemove !== $this->body && $toRemove !== $this->headNode) {
if ($toRemove !== null && $toRemove !== $this->body && $toRemove !== $this->headNode) {
return $this->removeChildHelper($this->getDocumentRoot(), $toRemove);
}

Expand All @@ -330,7 +322,7 @@ public function removeChildByID(string $id) {
*
* @since 1.0
*/
public function saveToHTMLFile(string $path, string $fileName, bool $wellFormatted = true) {
public function saveToHTMLFile(string $path, string $fileName, bool $wellFormatted = true) : bool {
$trimmedPath = trim($path);
$trimmedName = trim($fileName);

Expand Down Expand Up @@ -358,7 +350,7 @@ public function saveToHTMLFile(string $path, string $fileName, bool $wellFormatt
* @since 1.0
*/
public function setHeadNode(HeadNode $node) : bool {
if ($node instanceof HeadNode && $this->getDocumentRoot()->replaceChild($this->headNode, $node)) {
if ($this->getDocumentRoot()->replaceChild($this->headNode, $node)) {
$this->headNode = $node;

return true;
Expand All @@ -379,7 +371,7 @@ public function setHeadNode(HeadNode $node) : bool {
*
* @since 1.0
*/
public function setLanguage(string $lang = null) {
public function setLanguage(string $lang = null) : bool {
if ($lang === null) {
$this->getDocumentRoot()->removeAttribute('lang');

Expand Down Expand Up @@ -413,10 +405,10 @@ public function toHTML(bool $formatted = true) : string {
} else {
$this->nl = self::NL;
}
$this->document = '<!DOCTYPE html>'.$this->nl;
$this->document .= $this->getDocumentRoot()->toHTML($formatted);
$document = '<!DOCTYPE html>'.$this->nl;
$document .= $this->getDocumentRoot()->toHTML($formatted);

return $this->document;
return $document;
}
/**
*
Expand All @@ -438,11 +430,11 @@ private function getChildrenByAttributeValueHelper(string $attr, string $val, Li
}
/**
*
* @param sring $val
* @param string $val
* @param LinkedList $list
* @param HTMLNode $child
*/
private function getChildrenByTagHelper($val,$list,$child) {
private function getChildrenByTagHelper(string $val, LinkedList $list, HTMLNode $child) {
if ($child->getNodeName() == $val) {
$list->add($child);
}
Expand All @@ -457,12 +449,14 @@ private function getChildrenByTagHelper($val,$list,$child) {
}
}
}

/**
*
*
* @param HTMLNode $ch
* @param HTMLNode $nodeToRemove Description
* @return HTMLNode|null
*/
private function removeChildHelper($ch,$nodeToRemove) {
private function removeChildHelper(HTMLNode $ch, HTMLNode $nodeToRemove) {
for ($x = 0 ; $x < $ch->childrenCount() ; $x++) {
$removed = $this->removeChildHelper($ch->children()->get($x),$nodeToRemove);

Expand Down
8 changes: 4 additions & 4 deletions webfiori/ui/HTMLList.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __construct(string $listType = 'ul', array $arrOfItems = [], boo
* class 'HTMLNode' or a string that represents the
* content of the list item that will be added.
*
* @param array|bool $attrs An optional array of attributes which will be set in
* @param array|bool $attrsOrChain An optional array of attributes which will be set in
* the newly added list item.
*
* @param bool $chainOnParent If this parameter is set to true, the method
Expand All @@ -69,9 +69,9 @@ public function __construct(string $listType = 'ul', array $arrOfItems = [], boo
*
* @since 1.0
*/
public function addChild($node, $attrs = [], bool $chainOnParent = false) {
public function addChild($node, $attrsOrChain = [], bool $chainOnParent = false) {
if ($node instanceof ListItem) {
return parent::addChild($node, $attrs, $chainOnParent);
return parent::addChild($node, $attrsOrChain, $chainOnParent);
} else {
$li = new ListItem();

Expand All @@ -81,7 +81,7 @@ public function addChild($node, $attrs = [], bool $chainOnParent = false) {
$li->text($node);
}

return parent::addChild($li, $attrs, $chainOnParent);
return parent::addChild($li, $attrsOrChain, $chainOnParent);
}
}
/**
Expand Down
Loading

0 comments on commit 9b081eb

Please sign in to comment.