Skip to content

Commit ff70cca

Browse files
committed
chore: refactor global attributes
1 parent 96d5a0d commit ff70cca

9 files changed

Lines changed: 362 additions & 90 deletions

File tree

src/Resources/specifications/html5.yaml

Lines changed: 182 additions & 14 deletions
Large diffs are not rendered by default.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Html\Trait\GlobalAttribute;
6+
7+
trait AutofocusTrait
8+
{
9+
/**
10+
* Indicates whether the element is hidden
11+
*/
12+
public ?bool $autofocus = null;
13+
14+
15+
/**
16+
* @description Hides the element from rendering.
17+
*/
18+
public function setAutofocus(bool|string $autofocus = true): static
19+
{
20+
// cast string to bool
21+
if (is_string($autofocus)) {
22+
$autofocus = match (strtolower($autofocus)) {
23+
'true' => true,
24+
'false' => false,
25+
default => throw new \InvalidArgumentException('Autofocus attribute can only be "true" or "false".'),
26+
};
27+
}
28+
if ($autofocus) {
29+
$this->autofocus = $autofocus;
30+
$this->delegated->setAttribute('autofocus', $autofocus ? 'true' : 'false');
31+
}
32+
return $this;
33+
}
34+
35+
public function getAutofocus(): bool
36+
{
37+
return $this->autofocus;
38+
}
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Html\Trait\GlobalAttribute;
6+
7+
/**
8+
* ClassName property not declared here, as it's part of PHPs HTMLElement class
9+
*/
10+
trait ClassTrait
11+
{
12+
/**
13+
* @description Assigns CSS class names to an element.
14+
*/
15+
public function setClass(string $className): static
16+
{
17+
$this->className = $className;
18+
return $this;
19+
}
20+
21+
public function getClass(): ?string
22+
{
23+
return $this->className;
24+
}
25+
26+
/**
27+
* alias
28+
*/
29+
public function getClassName(): ?string
30+
{
31+
return $this->className;
32+
}
33+
34+
/**
35+
* alias
36+
*/
37+
public function setClassName(string $className): static
38+
{
39+
return $this->setClass($className);
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
namespace Html\Trait\GlobalAttribute;
4+
5+
use Html\Enum\ContentEditableEnum;
6+
use InvalidArgumentException;
7+
8+
trait ContenteditableTrait
9+
{
10+
/**
11+
* Indicates whether the element can be edited in place
12+
*/
13+
protected ?ContentEditableEnum $contenteditable = null;
14+
15+
/**
16+
* @description Defines whether the content is editable by the user.
17+
*/
18+
public function setContentEditable(
19+
bool|string|ContentEditableEnum $contentEditable = ContentEditableEnum::TRUE
20+
): static {
21+
if (is_string($contentEditable) && ! in_array(
22+
$contentEditable,
23+
array_map(fn ($e) => $e->value, ContentEditableEnum::cases())
24+
)) {
25+
throw new InvalidArgumentException('Invalid value for contenteditable');
26+
}
27+
$contentEditable = is_bool(
28+
$contentEditable
29+
) ? ($contentEditable === true ? 'true' : 'false') : $contentEditable;
30+
$contentEditable = is_string($contentEditable) ? ContentEditableEnum::from($contentEditable) : $contentEditable;
31+
$this->contenteditable = $contentEditable;
32+
$this->delegated->setAttribute(ContentEditableEnum::getQualifiedName(), $contentEditable->value);
33+
return $this;
34+
}
35+
36+
public function getContentEditable(): ?ContentEditableEnum
37+
{
38+
return $this->contenteditable;
39+
}
40+
}

src/Trait/GlobalAttribute/DraggableTrait.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ public function setDraggable(bool|string $draggable = true): static
1919
if (is_string($draggable) && in_array($draggable, ['true', 'false'])) {
2020
$draggable = $draggable === 'true' ? true : false;
2121
}
22-
$this->draggable = $draggable;
23-
$this->setAttribute('draggable', $draggable);
24-
// $this->delegated->setAttribute('draggable', $draggable);
22+
if ($draggable) {
23+
$this->draggable = $draggable;
24+
$this->setAttribute('draggable', $draggable);
25+
$this->delegated->setAttribute('draggable', $draggable);
26+
}
2527
return $this;
2628
}
2729

src/Trait/GlobalAttribute/HiddenTrait.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,19 @@ trait HiddenTrait
1717
*/
1818
public function setHidden(bool|string $hidden = true): static
1919
{
20-
$this->hidden = $hidden;
21-
$this->delegated->setAttribute('hidden', $hidden ? 'true' : 'false');
20+
// cast string to bool
21+
if (is_string($hidden)) {
22+
$hidden = match (strtolower($hidden)) {
23+
'true' => true,
24+
'false' => false,
25+
default => throw new \InvalidArgumentException('Hidden attribute can only be "true" or "false".'),
26+
};
27+
}
28+
if ($hidden) {
29+
$this->hidden = $hidden;
30+
$this->setAttribute('hidden', $hidden);
31+
$this->delegated->setAttribute('hidden', $hidden ? 'true' : 'false');
32+
}
2233
return $this;
2334
}
2435

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Html\Trait\GlobalAttribute;
6+
7+
/**
8+
* Id property not declared here, as it's part of PHPs HTMLElement class
9+
*/
10+
trait IdTrait
11+
{
12+
public function setId(string $id): static
13+
{
14+
// @todo shall we really throw an exception here? or simply ignore it?
15+
if (preg_match('/\s/', $id)) {
16+
throw new \InvalidArgumentException('ID attribute cannot contain whitespace characters.');
17+
}
18+
if (empty($id)) {
19+
throw new \InvalidArgumentException('ID attribute cannot be an empty string.');
20+
}
21+
$this->id = $id;
22+
$this->delegated->setAttribute('id', $id);
23+
return $this;
24+
}
25+
26+
public function getId(): ?string
27+
{
28+
return $this->id;
29+
}
30+
}

src/Trait/GlobalAttribute/PopoverTrait.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,18 @@ trait PopoverTrait
1313

1414
public function setPopover(bool|string $popover = true): static
1515
{
16-
$this->popover = $popover;
17-
$this->delegated->setAttribute('popover', $popover ? 'true' : 'false');
16+
if (is_string($popover)) {
17+
$popover = match (strtolower($popover)) {
18+
'true' => true,
19+
'false' => false,
20+
default => throw new \InvalidArgumentException('Popover attribute can only be "true" or "false".'),
21+
};
22+
}
23+
if ($popover) {
24+
$this->popover = $popover;
25+
$this->setAttribute('popover', $popover);
26+
$this->delegated->setAttribute('popover', $popover ? 'true' : 'false');
27+
}
1828
return $this;
1929
}
2030

src/Trait/GlobalAttributesTrait.php

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
trait GlobalAttributesTrait
1818
{
1919

20-
/**
21-
* Indicates whether the element can be edited in place
22-
*/
23-
protected ?ContentEditableEnum $contenteditable = null;
24-
2520
/**
2621
* Represents the spellchecking behavior of the element
2722
*/
@@ -33,72 +28,8 @@ trait GlobalAttributesTrait
3328
*/
3429
private ?InputModeEnum $inputmode = null;
3530

36-
public function setId(string $id): static
37-
{
38-
$this->id = $id;
39-
return $this;
40-
}
41-
42-
public function getId(): ?string
43-
{
44-
return $this->id;
45-
}
46-
47-
/**
48-
* @description Assigns CSS class names to an element.
49-
*/
50-
public function setClass(string $className): static
51-
{
52-
$this->className = $className;
53-
return $this;
54-
}
5531

56-
public function getClass(): ?string
57-
{
58-
return $this->className;
59-
}
6032

61-
/**
62-
* alias
63-
*/
64-
public function getClassName(): ?string
65-
{
66-
return $this->className;
67-
}
68-
69-
/**
70-
* alias
71-
*/
72-
public function setClassName(string $className): static
73-
{
74-
return $this->setClass($className);
75-
}
76-
77-
/**
78-
* @description Defines whether the content is editable by the user.
79-
*/
80-
public function setContentEditable(
81-
bool|string|ContentEditableEnum $contentEditable = ContentEditableEnum::TRUE
82-
): static {
83-
if (is_string($contentEditable) && ! in_array(
84-
$contentEditable,
85-
array_map(fn ($e) => $e->value, ContentEditableEnum::cases())
86-
)) {
87-
throw new InvalidArgumentException('Invalid value for contenteditable');
88-
}
89-
$contentEditable = is_bool(
90-
$contentEditable
91-
) ? ($contentEditable === true ? 'true' : 'false') : $contentEditable;
92-
$contentEditable = is_string($contentEditable) ? ContentEditableEnum::from($contentEditable) : $contentEditable;
93-
$this->contenteditable = $contentEditable;
94-
$this->delegated->setAttribute(ContentEditableEnum::getQualifiedName(), $contentEditable->value);
95-
return $this;
96-
}
97-
98-
public function getContentEditable(): ?ContentEditableEnum
99-
{
100-
return $this->contenteditable;
101-
}
10233

10334
/**
10435
* @description Suggests an input mode (e.g., numeric, email, tel).

0 commit comments

Comments
 (0)