Skip to content

Commit

Permalink
Agregar validación para elementos en linea. (#11)
Browse files Browse the repository at this point in the history
* Agregar validación para elementos en linea.
  • Loading branch information
terabytesoftw committed Jun 2, 2022
1 parent 85ef10e commit 007da4c
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 36 deletions.
127 changes: 99 additions & 28 deletions src/Tag/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,83 @@

final class Tag
{
/** @var array<array-key, string> */
private const INLINE_ELEMENTS = [
'a',
'abbr',
'acronym',
'audio',
'b',
'bdi',
'bdo',
'big',
'br',
'button',
'canvas',
'cite',
'code',
'data',
'datalist',
'del',
'dfn',
'em',
'embed',
'i',
'iframe',
'img',
'input',
'ins',
'kbd',
'label',
'map',
'mark',
'meter',
'noscript',
'object',
'output',
'picture',
'progress',
'q',
'ruby',
's',
'samp',
'script',
'select',
'slot',
'small',
'span',
'strong',
'sub',
'sup',
'svg',
'template',
'textarea',
'time',
'u',
'tt',
'var',
'video',
'wbr',
];

/** @var array<array-key, string> */
private const VOID_ELEMENT = [
'area' => '',
'base' => '',
'br' => '',
'col' => '',
'command' => '',
'embed' => '',
'hr' => '',
'img' => '',
'input' => '',
'keygen' => '',
'link' => '',
'meta' => '',
'param' => '',
'source' => '',
'track' => '',
'wbr' => '',
'area',
'base',
'br',
'col',
'command',
'embed',
'hr',
'img',
'input',
'keygen',
'link',
'meta',
'param',
'source',
'track',
'wbr',
];


Expand All @@ -54,25 +113,37 @@ public function end(string $tag): string
public function create(string $tag, string $content = '', array $attributes = []): string
{
$tag = $this->validateTag($tag);
$html = "<$tag" . $this->attributes->render($attributes) . '>';
$content = $content === '' ? '' : PHP_EOL . $content . PHP_EOL;
return $this->voidElements($tag) !== '' ? $html : $html . $content . '</' . $tag . '>';
$voidElement = "<$tag" . $this->attributes->render($attributes) . '>';

if ($this->inlinedElements($tag) === false) {
$voidElement .= PHP_EOL;
}

if ($this->inlinedElements($tag) === false && $content !== '') {
$content = $content . PHP_EOL;
}

return $this->voidElements($tag) ? $voidElement : $voidElement . $content . '</' . $tag . '>';
}

/**
* {@see http://www.w3.org/TR/html-markup/syntax.html#void-element}
* @return bool True if tag is inlined element.
*
* @return string list of void elements (element name => '').
* @link https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements
*/
private function voidElements(string $tag): string
private function inlinedElements(string $tag): bool
{
$result = '';

if (array_key_exists($tag, self::VOID_ELEMENT)) {
$result = $tag;
}
return in_array($tag, self::INLINE_ELEMENTS, true) ? true : false;
}

return $result;
/**
* @return bool True if tag is void element.
*
* @link http://www.w3.org/TR/html-markup/syntax.html#void-element
*/
private function voidElements(string $tag): bool
{
return in_array($tag, self::VOID_ELEMENT, true) ? true : false;
}

private function validateTag(string $tag): string
Expand Down
2 changes: 1 addition & 1 deletion tests/Tag/Element/ATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function createProvider(): array
return [
[[], '', '<a></a>'],
[['class' => 'class'], '', '<a class="class"></a>'],
[[], 'Content', '<a>' . PHP_EOL . 'Content' . PHP_EOL . '</a>'],
[[], 'Content', '<a>Content</a>'],
[['disabled' => true], '', '<a disabled></a>'],
[
['download' => true, 'href' => '/images/myw3schoolsimage.jpg'],
Expand Down
2 changes: 1 addition & 1 deletion tests/Tag/Element/ButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function createProvider(): array
return [
[[], '', '<button type="button"></button>'],
[['class' => 'class'], '', '<button type="button" class="class"></button>'],
[[], 'Content', '<button type="button">' . PHP_EOL . 'Content' . PHP_EOL . '</button>'],
[[], 'Content', '<button type="button">Content</button>'],
[['disabled' => true], '', '<button type="button" disabled></button>'],
[
['download' => true, 'href' => '/images/myw3schoolsimage.jpg'],
Expand Down
6 changes: 3 additions & 3 deletions tests/Tag/Element/LiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ final class LiTest extends TestCase
public function createProvider(): array
{
return [
[[], '', '<li></li>'],
[['class' => 'class'], '', '<li class="class"></li>'],
[[], '', '<li>' . PHP_EOL . '</li>'],
[['class' => 'class'], '', '<li class="class">' . PHP_EOL . '</li>'],
[[], 'Content', '<li>' . PHP_EOL . 'Content' . PHP_EOL . '</li>'],
[['disabled' => true], '', '<li disabled></li>'],
[['disabled' => true], '', '<li disabled>' . PHP_EOL . '</li>'],
];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Tag/Element/UlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ final class UlTest extends TestCase
public function createProvider(): array
{
return [
[[], [], '<ul></ul>'],
[['class' => 'class'], [], '<ul class="class"></ul>'],
[[], [], '<ul>' . PHP_EOL . '</ul>'],
[['class' => 'class'], [], '<ul class="class">' . PHP_EOL . '</ul>'],
[
[],
['0' => ['content' => 'Uno'], '1' => ['content' => 'Dos'], '2' => ['content' => 'Tres']],
Expand Down
7 changes: 6 additions & 1 deletion tests/Tag/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ public function testBegin(): void
public function createProvider(): array
{
return [
['article', '', ['id' => 'id-1', 'class' => 'class'], '<article id="id-1" class="class"></article>'],
[
'article',
'',
['id' => 'id-1', 'class' => 'class'],
'<article id="id-1" class="class">' . PHP_EOL . '</article>',
],
['br', '', [], '<br>'],
['BR', '', [], '<br>'],
['div', 'Content', [], '<div>' . PHP_EOL . 'Content' . PHP_EOL . '</div>'],
Expand Down

0 comments on commit 007da4c

Please sign in to comment.