Skip to content

Commit

Permalink
Add hrefAttributes() and hrefClass() methods. (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Aug 28, 2023
1 parent ff06206 commit 0aa43e3
Show file tree
Hide file tree
Showing 13 changed files with 201 additions and 117 deletions.
16 changes: 16 additions & 0 deletions src/Attribute/Custom/HasLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@
*/
trait HasLink
{
protected string $link = '';
protected array $linkAttributes = [];

/**
* Set the link of the menu item.
*
* @param string $value The link of the menu item.
*
* @return static A new instance of the current class with the specified link.
*/
public function link(string $value): static
{
$new = clone $this;
$new->link = $value;

return $new;
}

/**
* Set the `HTML` attributes for the link tag.
*
Expand Down
34 changes: 34 additions & 0 deletions src/Attribute/Tag/HasHref.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

namespace PHPForge\Html\Attribute\Tag;

use PHPForge\Html\Helper\CssClass;

/**
* Is used by widgets that implement the href method.
*/
trait HasHref
{
protected array $hrefAttributes = [];

/**
* Set the URL that the hyperlink points to.
*
Expand All @@ -27,4 +31,34 @@ public function href(string $value): static

return $new;
}

/**
* Set the `HTML` attributes.
*
* @param array $values Attribute values indexed by attribute names.
*
* @return static A new instance of the current class with the specified href attributes.
*/
public function hrefAttributes(array $values): static
{
$new = clone $this;
$new->hrefAttributes = array_merge($this->hrefAttributes, $values);

return $new;
}

/**
* Set the `CSS` class.
*
* @param string $value The `CSS` class.
*
* @return static A new instance of the current class with the specified href class.
*/
public function hrefClass(string $value): static
{
$new = clone $this;
CssClass::add($new->hrefAttributes, $value);

return $new;
}
}
4 changes: 2 additions & 2 deletions tests/Attribute/Custom/HasContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class HasContainerTest extends TestCase
{
public function testContainerClass(): void
public function testClass(): void
{
$instance = new class() {
use HasContainer;
Expand All @@ -35,7 +35,7 @@ public function getContainerClass(): string
$this->assertSame('foo bar', $instance->getContainerClass());
}

public function testContainerTagException(): void
public function testException(): void
{
$instance = new class() {
use HasContainer;
Expand Down
34 changes: 17 additions & 17 deletions tests/Attribute/Custom/HasContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,7 @@

final class HasContentTest extends TestCase
{
public function testContentWithXSS(): void
{
$instance = new class() {
use HasContent;

public function getContent(): string
{
return $this->content;
}
};

$instance = $instance->content("<script>alert('Hack');</script>");

$this->assertEmpty($instance->getContent());
}

public function testGetContent(): void
public function testGet(): void
{
$instance = new class() {
use HasContent;
Expand All @@ -48,4 +32,20 @@ public function testImmutablity(): void

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

public function testXSS(): void
{
$instance = new class() {
use HasContent;

public function getContent(): string
{
return $this->content;
}
};

$instance = $instance->content("<script>alert('Hack');</script>");

$this->assertEmpty($instance->getContent());
}
}
4 changes: 2 additions & 2 deletions tests/Attribute/Custom/HasIconTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class HasIconTest extends TestCase
{
public function testIconClass(): void
public function testClass(): void
{
$instance = new class() {
use HasIcon;
Expand All @@ -31,7 +31,7 @@ public function getIconAttributes(): array
$this->assertSame(['class' => 'test test1'], $instance->getIconAttributes());
}

public function testIconContainerClass(): void
public function testContainerClass(): void
{
$instance = new class() {
use HasIcon;
Expand Down
76 changes: 38 additions & 38 deletions tests/Attribute/Custom/HasLabelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,7 @@

final class HasLabelTest extends TestCase
{
public function testImmutablity(): void
{
$instance = new class() {
use HasLabel;
};

$this->assertNotSame($instance, $instance->labelAttributes([]));
$this->assertNotSame($instance, $instance->labelClass(''));
$this->assertNotSame($instance, $instance->labelClosure(static fn (): string => ''));
$this->assertNotSame($instance, $instance->labelContent(''));
$this->assertNotSame($instance, $instance->labelFor(''));
$this->assertNotSame($instance, $instance->notLabel());
}

public function testIsNotLabel(): void
{
$instance = new class() {
use HasLabel;

public function getIsNotLabel(): bool
{
return $this->isNotLabel();
}
};

$this->assertTrue($instance->notLabel()->getIsNotLabel());
$this->assertFalse($instance->getIsNotLabel());
}

public function testLabelClass(): void
public function testClass(): void
{
$instance = new class() {
use HasLabel;
Expand All @@ -62,7 +33,7 @@ public function getLabelClass(): string
$this->assertSame('foo bar', $instance->getLabelClass());
}

public function testLabelClosure(): void
public function testClosure(): void
{
$instance = new class() {
use HasLabel;
Expand All @@ -80,7 +51,7 @@ public function getLabelClosure(): Closure|null
$this->assertInstanceOf(Closure::class, $instance->getLabelClosure());
}

public function testLabelContent(): void
public function testContent(): void
{
$instance = new class() {
use HasLabel;
Expand All @@ -96,20 +67,33 @@ public function getLabelContent(): string
$this->assertSame('foo && bar<span>foo && bar</span>', $instance->getLabelContent());
}

public function testLabelContentWithXSS(): void
public function testImmutablity(): void
{
$instance = new class() {
use HasLabel;
};

public function getLabelContent(): string
$this->assertNotSame($instance, $instance->labelAttributes([]));
$this->assertNotSame($instance, $instance->labelClass(''));
$this->assertNotSame($instance, $instance->labelClosure(static fn (): string => ''));
$this->assertNotSame($instance, $instance->labelContent(''));
$this->assertNotSame($instance, $instance->labelFor(''));
$this->assertNotSame($instance, $instance->notLabel());
}

public function testIsNotLabel(): void
{
$instance = new class() {
use HasLabel;

public function getIsNotLabel(): bool
{
return $this->labelContent;
return $this->isNotLabel();
}
};

$instance = $instance->labelContent("<script>alert('Hack');</script>", Span::widget()->content('foo && bar'));

$this->assertSame("<span>foo && bar</span>", $instance->getLabelContent());
$this->assertTrue($instance->notLabel()->getIsNotLabel());
$this->assertFalse($instance->getIsNotLabel());
}

public function testNotLabel(): void
Expand All @@ -121,4 +105,20 @@ public function testNotLabel(): void
$this->assertFalse($instance->isNotLabel());
$this->assertTrue($instance->notLabel()->isNotLabel());
}

public function testXSS(): void
{
$instance = new class() {
use HasLabel;

public function getLabelContent(): string
{
return $this->labelContent;
}
};

$instance = $instance->labelContent("<script>alert('Hack');</script>", Span::widget()->content('foo && bar'));

$this->assertSame("<span>foo && bar</span>", $instance->getLabelContent());
}
}
5 changes: 3 additions & 2 deletions tests/Attribute/Custom/HasLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

final class HasLinkTest extends TestCase
{
public function testLinkAttributes(): void
public function testAttributes(): void
{
$instance = new class() {
use HasLink;
Expand All @@ -26,7 +26,7 @@ public function getLinkAttributes(): array
$this->assertSame(['class' => 'foo', 'disabled' => true], $instance->getLinkAttributes());
}

public function testLinkClass(): void
public function testClass(): void
{
$instance = new class() {
use HasLink;
Expand All @@ -46,6 +46,7 @@ public function testImmutablity(): void
use HasLink;
};

$this->assertNotSame($instance, $instance->link(''));
$this->assertNotSame($instance, $instance->linkAttributes([]));
$this->assertNotSame($instance, $instance->linkClass(''));
}
Expand Down

0 comments on commit 0aa43e3

Please sign in to comment.