Skip to content

Commit

Permalink
Add Li::class and Ul::class. (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Aug 30, 2023
1 parent 08c5d6d commit cec598d
Show file tree
Hide file tree
Showing 13 changed files with 567 additions and 22 deletions.
2 changes: 2 additions & 0 deletions src/Base/AbstractA.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
abstract class AbstractA extends AbstractElement
{
use Attribute\Aria\HasAriaDisabled;
use Attribute\Aria\HasRole;
use Attribute\CanBeAutofocus;
use Attribute\CanBeHidden;
use Attribute\HasData;
Expand Down
4 changes: 0 additions & 4 deletions src/Base/AbstractButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ protected function beforeRun(): bool
$this->attributes['type'] = 'button';
}

if (array_key_exists('type', $this->attributes) && $this->attributes['type'] === 'link') {
unset($this->attributes['type']);
}

return parent::beforeRun();
}

Expand Down
13 changes: 4 additions & 9 deletions src/Base/AbstractButtonLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace PHPForge\Html\Base;

use PHPForge\Html\A;
use PHPForge\Html\Attribute;
use PHPForge\Html\Button;
use PHPForge\Widget\Element;

/**
Expand Down Expand Up @@ -34,19 +34,14 @@ abstract class AbstractButtonLink extends Element
protected function run(): string
{
$attributes = $this->attributes;
$button = Button::widget();
$a = A::widget();

if (isset($attributes['disabled']) && is_bool($attributes['disabled']) && $attributes['disabled']) {
$button = $button->ariaDisabled('true')->class('disabled');
$a = $a->ariaDisabled('true')->class('disabled');

unset($attributes['disabled']);
}

return $button->attributes($attributes)
->content($this->content)
->role('button')
->tagName('a')
->type('link')
->render();
return $a->attributes($attributes)->content($this->content)->role('button')->render();
}
}
1 change: 0 additions & 1 deletion src/Base/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ abstract class AbstractElement extends Element
{
use Attribute\Custom\HasAttributes;
use Attribute\Custom\HasContent;
use Attribute\Custom\HasTagName;
use Attribute\HasClass;
use Attribute\HasId;
use Attribute\HasLang;
Expand Down
62 changes: 62 additions & 0 deletions src/Base/AbstractLi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Base;

use PHPForge\Html\Attribute;
use PHPForge\Html\Helper\Encode;
use PHPForge\Html\HtmlBuilder;
use PHPForge\Html\Ul;
use PHPForge\Widget\Element;

/**
* Provides a foundation for creating HTML `<li>` elements with various attributes and content.
*/
abstract class AbstractLi extends Element
{
use Attribute\Custom\HasAttributes;
use Attribute\HasClass;
use Attribute\HasId;
use Attribute\HasLang;
use Attribute\HasStyle;
use Attribute\HasTabindex;
use Attribute\HasTitle;
use Attribute\Input\HasName;
use Attribute\Input\HasValue;

protected array $attributes = [];
protected string $content = '';


/**
* Set the `HTML` content value.
*
* @param string|Ul|self ...$values The `HTML` content value.
*
* @return static A new instance of the current class with the specified content value.
*/
public function content(string|Ul|self ...$values): static
{
$content = '';

foreach ($values as $value) {
$content .= "$value\n";
}

$new = clone $this;
$new->content = trim(Encode::create()->santizeXSS($content));

return $new;
}

/**
* Generate the HTML representation of the element.
*
* @return string The HTML representation of the element.
*/
protected function run(): string
{
return HtmlBuilder::create('li', $this->content, $this->attributes);
}
}
63 changes: 63 additions & 0 deletions src/Base/AbstractUl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Base;

use PHPForge\Html\Attribute;
use PHPForge\Html\Helper\Encode;
use PHPForge\Html\HtmlBuilder;
use PHPForge\Html\Li;
use PHPForge\Widget\Element;

use function trim;

/**
* Provides a foundation for creating HTML `<ul>` elements with various attributes and content.
*/
abstract class AbstractUl extends Element
{
use Attribute\Custom\HasAttributes;
use Attribute\HasClass;
use Attribute\HasId;
use Attribute\HasLang;
use Attribute\HasStyle;
use Attribute\HasTabindex;
use Attribute\HasTitle;
use Attribute\Input\HasName;
use Attribute\Input\HasType;

protected array $attributes = [];
protected string $content = '';

/**
* Set the `HTML` content value.
*
* @param string|Li|self ...$values The `HTML` content value.
*
* @return static A new instance of the current class with the specified content value.
*/
public function content(string|Li|self ...$values): static
{
$content = '';

foreach ($values as $value) {
$content .= "$value\n";
}

$new = clone $this;
$new->content = trim(Encode::create()->santizeXSS($content));

return $new;
}

/**
* Generate the HTML representation of the element.
*
* @return string The HTML representation of the element.
*/
protected function run(): string
{
return HtmlBuilder::create('ul', $this->content, $this->attributes);
}
}
17 changes: 17 additions & 0 deletions src/Li.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html;

/**
* The `<li>` HTML element is used to represent an item in a list.
* It must be contained in a parent element: an ordered list `<ol>`, an unordered list `<ul>`, or a menu `<menu>`.
* In menus and unordered lists, list items are usually displayed using bullet points. In ordered lists, they are
* usually displayed with an ascending counter on the left, such as a number or letter.
*
* @link https://html.spec.whatwg.org/multipage/grouping-content.html#the-li-element
*/
final class Li extends Base\AbstractLi
{
}
14 changes: 14 additions & 0 deletions src/Ul.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html;

/**
* The <ul> HTML element represents an unordered list of items, typically rendered as a bulleted list.
*
* @link https://html.spec.whatwg.org/multipage/grouping-content.html#the-ul-element
*/
final class Ul extends Base\AbstractUl
{
}
8 changes: 0 additions & 8 deletions tests/Button/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,6 @@ public function testTabIndex(): void
);
}

public function testTagName(): void
{
$this->assertSame(
'<a></a>',
Button::widget()->tagName('a')->type('link')->render(),
);
}

public function testTitle(): void
{
$this->assertSame(
Expand Down
21 changes: 21 additions & 0 deletions tests/Li/ImmutableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Tests\Li;

use PHPForge\Html\Li;
use PHPUnit\Framework\TestCase;

/**
* @psalm-suppress PropertyNotSetInConstructor
*/
final class ImmutableTest extends TestCase
{
public function testImmutable(): void
{
$li = Li::widget();

$this->assertNotSame($li, $li->content(''));
}
}

0 comments on commit cec598d

Please sign in to comment.