diff --git a/.gitignore b/.gitignore
index bcc0b7a..f2ecd3c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,2 @@
.idea
-vendor/
\ No newline at end of file
+vendor/
diff --git a/.phpunit.result.cache b/.phpunit.result.cache
new file mode 100644
index 0000000..1f939d5
--- /dev/null
+++ b/.phpunit.result.cache
@@ -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;}}}
\ No newline at end of file
diff --git a/app/HtmlAttributes.php b/app/HtmlAttributes.php
index 8ad06fa..4dabc49 100644
--- a/app/HtmlAttributes.php
+++ b/app/HtmlAttributes.php
@@ -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);
diff --git a/app/HtmlElement.php b/app/HtmlElement.php
index 10f3aa8..8471cf5 100644
--- a/app/HtmlElement.php
+++ b/app/HtmlElement.php
@@ -1,7 +1,9 @@
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 . '>';
}
+
}
\ No newline at end of file
diff --git a/public/index.php b/public/index.php
index 409cfab..6d31893 100644
--- a/public/index.php
+++ b/public/index.php
@@ -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 "
";
-echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
+$element = new \App\HtmlElement('p',['id' => 'my_paragraph' ],'este es el contenido');
-echo '
';
+echo htmlentities($element->render(),ENT_QUOTES,'utf-8');
-$element = new \App\HtmlElement('p', ['id' => 'my_paragraph'], 'Este es el contenido');
+echo "
";
-echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
+$element = new \App\HtmlElement('p',['id' => 'my_paragraph','class' => 'paragraph' ],'este es el contenido');
-echo '
';
+echo htmlentities($element->render(),ENT_QUOTES,'utf-8');
-$element = new \App\HtmlElement('p', ['id' => 'my_paragraph', 'class' => 'paragraph'], 'Este es el contenido');
+echo "
";
-echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
+$element = new \App\HtmlElement('img',['src' => 'img/img.png']);
-echo '
';
+echo htmlentities($element->render(),ENT_QUOTES,'utf-8');
-$element = new \App\HtmlElement('img', ['src' => 'img/styde.png']);
+echo "
";
-echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
+$element = new \App\HtmlElement('img',['src' => 'img/img.png','title' => 'Curso de "Refactorizacion" en styde']);
-echo '
';
+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 "
";
-echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
+$element = new \App\HtmlElement('input',['required']);
-echo '
';
+echo htmlentities($element->render(),ENT_QUOTES,'utf-8');
-$element = new \App\HtmlElement('input', ['required']);
+echo "
";
-echo htmlentities($element->render(), ENT_QUOTES, 'UTF-8');
\ No newline at end of file
+$element = new \App\HtmlElement('input');
+
+echo htmlentities($element->render(),ENT_QUOTES,'utf-8');
\ No newline at end of file
diff --git a/tests/HtmlElementTest.php b/tests/HtmlElementTest.php
index 5b9524a..db60b07 100644
--- a/tests/HtmlElementTest.php
+++ b/tests/HtmlElementTest.php
@@ -1,88 +1,137 @@
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(
+ '
este es el contenido
', + $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( - 'Este es el contenido
', + 'este es el contenido
', $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( + 'este es el contenido
', + $element->render() ); + } + + function test_it_generates_a_tag_image() + { + $element = new HtmlElement('img',['src' => 'img/img.png']); $this->assertSame( - 'Este es el contenido
', + '
',
$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(
+ '
',
+ $element->render()
);
+ }
+
+ function test_it_generates_a_tag_input_with_boolean_attributes()
+ {
+ $element = new HtmlElement('input',['required']);
$this->assertSame(
- 'Este es el contenido
', + '', $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('
', $element->render());
+ $this->assertSame(
+ '',
+ $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(
- '
',
+ '<>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(
+ '', + (new HtmlElement('p'))->open() + ); + } - $this->assertSame('', $element->render()); + function test_it_check_close_tag() + { + $this->assertSame( + '
', + (new HtmlElement('p'))->close() + ); } } \ No newline at end of file