Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
.idea
vendor/
vendor/
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":2591:{a:2:{s:7:"defects";a:15:{s:54:"Tests\HtmlElementTest::test_it_generates_html_elements";i:5;s:46:"Tests\HtmlElementTest::test_it_check_close_tag";i:4;s:51:"Tests\HtmlElementTest::test_it_generates_attributes";i:4;s:84:"Tests\HtmlElementTest::test_it_generates_a_paragraph_with_content_and_once_attribute";i:4;s:85:"Tests\HtmlElementTest::test_it_generates_a_paragraph_with_content_and_twice_attribute";i:4;s:52:"Tests\HtmlElementTest::test_it_generates_a_tag_image";i:4;s:83:"Tests\HtmlElementTest::test_it_generates_a_tag_image_it_escapes_the_html_attributes";i:4;s:76:"Tests\HtmlElementTest::test_it_generates_a_tag_input_with_boolean_attributes";i:4;s:65:"Tests\HtmlElementTest::test_it_checks_if_a_element_is_void_or_not";i:4;s:65:"Tests\HtmlElementTest::test_it_generates_a_paragraph_with_content";i:4;s:79:"Tests\HtmlElementTest::test_it_generates_a_tag_input_without_boolean_attributes";i:4;s:57:"Tests\HtmlElementTest::test_it_generates_html_without_tag";i:4;s:45:"Tests\HtmlElementTest::test_it_check_open_tag";i:4;s:42:"Tests\HtmlElementTest::test_check_is_empty";i:3;s:53:"Tests\HtmlElementTest::test_check_attributes_is_empty";i:4;}s:5:"times";a:19:{s:32:"Tests\DummyTest::it_asserts_true";d:0.006;s:54:"Tests\HtmlElementTest::test_it_generates_html_elements";d:0.007;s:65:"Tests\HtmlElementTest::test_it_generates_a_paragraph_with_content";d:0;s:84:"Tests\HtmlElementTest::test_it_generates_a_paragraph_with_content_and_once_attribute";d:0;s:85:"Tests\HtmlElementTest::test_it_generates_a_paragraph_with_content_and_twice_attribute";d:0;s:52:"Tests\HtmlElementTest::test_it_generates_a_tag_image";d:0;s:68:"Tests\HtmlElementTest::test_it_generates_a_tag_image_with_attributes";d:0;s:68:"Tests\HtmlElementTest::test_it_generates_a_tag_input_with_attributes";d:0;s:71:"Tests\HtmlElementTest::test_it_generates_a_tag_input_without_attributes";d:0;s:83:"Tests\HtmlElementTest::test_it_generates_a_tag_image_it_escapes_the_html_attributes";d:0;s:76:"Tests\HtmlElementTest::test_it_generates_a_tag_input_with_boolean_attributes";d:0;s:79:"Tests\HtmlElementTest::test_it_generates_a_tag_input_without_boolean_attributes";d:0;s:57:"Tests\HtmlElementTest::test_it_generates_html_without_tag";d:0.001;s:65:"Tests\HtmlElementTest::test_it_checks_if_a_element_is_void_or_not";d:0.008;s:46:"Tests\HtmlElementTest::test_it_check_close_tag";d:0;s:45:"Tests\HtmlElementTest::test_it_check_open_tag";d:0;s:51:"Tests\HtmlElementTest::test_it_generates_attributes";d:0.001;s:42:"Tests\HtmlElementTest::test_check_is_empty";d:0.008;s:53:"Tests\HtmlElementTest::test_check_attributes_is_empty";d:0;}}}
2 changes: 1 addition & 1 deletion app/HtmlAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(array $attributes = [])
$this->attributes = $attributes;
}

public function render(): string
public function render()
{
return array_reduce(array_keys($this->attributes), function ($result, $attribute) {
return $result . $this->renderAttribute($attribute);
Expand Down
80 changes: 67 additions & 13 deletions app/HtmlElement.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php


namespace App;


class HtmlElement
{
/**
Expand All @@ -17,44 +19,96 @@ class HtmlElement
*/
private $attributes;

public function __construct(string $name, array $attributes = [], $content = null)
public function __construct(string $name, array $attributes =[], $content = null)
{
$this->name = $name;
$this->attributes = new HtmlAttributes($attributes);
$this->content = $content;
$this->attributes = new HtmlAttributes($attributes);
}
public function render()

public function render()
{
if ($this->isVoid()) {
if ($this->isVoid()){
return $this->open();
}

return $this->open().$this->content().$this->close();
}

public function open(): string
public function open()
{
return '<'.$this->name.$this->attributes().'>';
if ($this->hasAttributes()){
return '<'.$this->name.$this->attributes->render().'>';
}else{
return '<'.$this->name.'>';
}
}

public function attributes(): string
public function attributes()
{
return $this->attributes->render();
}

public function isVoid(): bool
/**
* @return bool
*/
public function hasAttributes()
{
return in_array($this->name, ['br', 'hr', 'img', 'input', 'meta']);
return !empty($this->getAttributes());
}

public function content(): string
/*public function attributes()
{

return array_reduce(array_keys($this->getAttributes()),function ($result,$attribute){
return $result . $this->attributes->render($attribute);
},'');

/*$htmlAttributes = '';

foreach ($this->attributes as $attribute => $value){

$htmlAttributes .= $this->renderAttribute($attribute,$value);

}

return $htmlAttributes;
}*/

/*protected function renderAttribute($attribute)
{
if (is_numeric($attribute)){
return ' '.$this->getAttributes()[$attribute];
}

return ' '.$attribute.'="'.htmlentities($this->getAttributes()[$attribute],ENT_QUOTES,'UTF-8').'"';//
}*/

protected function getAttributes()
{
return $this->attributes->attributes;
}


public function isVoid()
{
return in_array($this->name, ['img','br','hr','input','meta']);
}

/**
* @return string
*/
public function content()
{
return htmlentities($this->content, ENT_QUOTES, 'UTF-8');
}

public function close(): string
/**
* @return string
*/
public function close()
{
return '</'.$this->name.'>';
return '</' . $this->name . '>';
}

}
40 changes: 21 additions & 19 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,42 @@

require '../vendor/autoload.php';

$element = new \App\HtmlElement('p', [], 'Este es el contenido');
$element = new \App\HtmlElement('p',[],'este es el contenido');

echo $element->open().'Cualquier contenido'.$element->close();
echo htmlentities($element->render(),ENT_QUOTES,'utf-8');

exit;
echo "<br><br>";

echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
$element = new \App\HtmlElement('p',['id' => 'my_paragraph' ],'este es el contenido');

echo '<br><br>';
echo htmlentities($element->render(),ENT_QUOTES,'utf-8');

$element = new \App\HtmlElement('p', ['id' => 'my_paragraph'], 'Este es el contenido');
echo "<br><br>";

echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
$element = new \App\HtmlElement('p',['id' => 'my_paragraph','class' => 'paragraph' ],'este es el contenido');

echo '<br><br>';
echo htmlentities($element->render(),ENT_QUOTES,'utf-8');

$element = new \App\HtmlElement('p', ['id' => 'my_paragraph', 'class' => 'paragraph'], 'Este es el contenido');
echo "<br><br>";

echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
$element = new \App\HtmlElement('img',['src' => 'img/img.png']);

echo '<br><br>';
echo htmlentities($element->render(),ENT_QUOTES,'utf-8');

$element = new \App\HtmlElement('img', ['src' => 'img/styde.png']);
echo "<br><br>";

echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
$element = new \App\HtmlElement('img',['src' => 'img/img.png','title' => 'Curso de "Refactorizacion" en styde']);

echo '<br><br>';
echo htmlentities($element->render(),ENT_QUOTES,'utf-8');

$element = new \App\HtmlElement('img', ['src' => 'img/styde.png', 'title' => 'Curso de "Refactorización" en Styde']);
echo "<br><br>";

echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
$element = new \App\HtmlElement('input',['required']);

echo '<br><br>';
echo htmlentities($element->render(),ENT_QUOTES,'utf-8');

$element = new \App\HtmlElement('input', ['required']);
echo "<br><br>";

echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
$element = new \App\HtmlElement('input');

echo htmlentities($element->render(),ENT_QUOTES,'utf-8');
115 changes: 82 additions & 33 deletions tests/HtmlElementTest.php
Original file line number Diff line number Diff line change
@@ -1,88 +1,137 @@
<?php


namespace Tests;


use App\HtmlElement;

class HtmlElementTest extends TestCase
{
/** @test */
function it_checks_if_a_element_is_void_or_not()
function test_it_checks_if_a_element_is_void_or_not()
{
$this->assertFalse((new HtmlElement('p'))->isVoid());

$this->assertTrue((new HtmlElement('img'))->isVoid());

$this->assertTrue((new HtmlElement('input'))->isVoid());
}

function test_it_generates_attributes()
{
$element = new HtmlElement('span',['class' => 'a_spam', 'id' => 'the_spam' ]);

$this->assertSame(
' class="a_spam" id="the_spam"',
$element->attributes()

);
}

/** @test */
function it_generates_attributes()

function test_it_generates_a_paragraph_with_content()
{
$element = new HtmlElement('span', ['class' => 'a_span', 'id' => 'the_span']);
$element = new HtmlElement('p',[],'este es el contenido');

$this->assertSame(' class="a_span" id="the_span"', $element->attributes());
$this->assertSame(
'<p>este es el contenido</p>',
$element->render()
);
}

/** @test */
function it_generates_a_paragraph_with_content()
function test_it_generates_a_paragraph_with_content_and_once_attribute()
{
$element = new HtmlElement('p', [], 'Este es el contenido');
$element = new HtmlElement('p',['id' => 'my_paragraph' ],'este es el contenido');

$this->assertSame(
'<p>Este es el contenido</p>',
'<p id="my_paragraph">este es el contenido</p>',
$element->render()
);
}

/** @test */
function it_generates_a_paragraph_with_content_and_an_id_attribute()
function test_it_generates_a_paragraph_with_content_and_twice_attribute()
{
$element = new HtmlElement(
'p', ['id' => 'my_paragraph'], 'Este es el contenido'
$element = new HtmlElement('p',
['id' => 'my_paragraph','class' => 'paragraph' ],'este es el contenido');

$this->assertSame(
'<p id="my_paragraph" class="paragraph">este es el contenido</p>',
$element->render()
);
}

function test_it_generates_a_tag_image()
{
$element = new HtmlElement('img',['src' => 'img/img.png']);

$this->assertSame(
'<p id="my_paragraph">Este es el contenido</p>',
'<img src="img/img.png">',
$element->render()
);
}

/** @test */
function it_generates_a_paragraph_with_multiple_attributes()
function test_it_generates_a_tag_image_it_escapes_the_html_attributes()
{
$element = new HtmlElement(
'p', ['id' => 'my_paragraph', 'class' => 'paragraph'], 'Este es el contenido'
$element = new HtmlElement('img',
['src' => 'img/img.png','title' => 'Curso de "Refactorizacion" en styde']);

$this->assertSame(
'<img src="img/img.png" title="Curso de &quot;Refactorizacion&quot; en styde">',
$element->render()
);
}

function test_it_generates_a_tag_input_with_boolean_attributes()
{
$element = new HtmlElement('input',['required']);

$this->assertSame(
'<p id="my_paragraph" class="paragraph">Este es el contenido</p>',
'<input required>',
$element->render()
);
}

/** @test */
function it_generates_an_img_tag()
function test_check_attributes_is_empty()
{
$element = new HtmlElement('p',[]);
$this->assertFalse(
$element->hasAttributes()
);

}

function test_it_generates_a_tag_input_without_boolean_attributes()
{
$element = new HtmlElement('img', ['src' => 'img/styde.png']);
$element = new HtmlElement('input');

$this->assertSame('<img src="img/styde.png">', $element->render());
$this->assertSame(
'<input>',
$element->render()
);
}

/** @test */
function it_escapes_the_html_attributes()
function test_it_generates_html_without_tag()
{
$element = new HtmlElement('img', ['src' => 'img/styde.png', 'title' => 'Curso de "Refactorización" en Styde']);
$element = new HtmlElement('',[],'este es el contenido');

$this->assertSame(
'<img src="img/styde.png" title="Curso de &quot;Refactorizaci&oacute;n&quot; en Styde">',
'<>este es el contenido</>',
$element->render()
);
}

/** @test */
function it_generates_elements_with_boolean_attributes()
function test_it_check_open_tag()
{
$element = new HtmlElement('input', ['required']);
$this->assertSame(
'<p>',
(new HtmlElement('p'))->open()
);
}

$this->assertSame('<input required>', $element->render());
function test_it_check_close_tag()
{
$this->assertSame(
'</p>',
(new HtmlElement('p'))->close()
);
}
}