Skip to content

Commit

Permalink
added raw() function
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Dec 5, 2019
1 parent 764f28f commit 346cdea
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [0.1.5] - 2019-12-05
### Added
- New `raw()` function to add raw unescaped html.

## [0.1.4] - 2019-12-01
### Fixed
- The elements `area`, `track`, `embed`, `param`, `source` and `col` are [empty elements](https://developer.mozilla.org/en-US/docs/Glossary/empty_element) so they should use the `SelfClosingElement` class.
Expand All @@ -26,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## 0.1.0 - 2019-04-27
First version

[0.1.5]: https://github.com/oscarotero/html/compare/v0.1.4...v0.1.5
[0.1.4]: https://github.com/oscarotero/html/compare/v0.1.3...v0.1.4
[0.1.3]: https://github.com/oscarotero/html/compare/v0.1.2...v0.1.3
[0.1.2]: https://github.com/oscarotero/html/compare/v0.1.1...v0.1.2
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ echo div('Hello world');
echo div('Hello', ' world');
//<div>Hello world</div>

//HTML entities are converted
//HTML entities are escaped
echo div('Hello', ' <world>');
//<div>Hello &lt;world&gt;</div>

//Use the function raw() to do not escape html entities
echo div('Hello', raw(' <world>'));
//<div>Hello <world></div>

//Create a div with html content
echo div('Hello ', strong('world'));
//<div>Hello <strong>world</strong></div>
Expand Down
54 changes: 54 additions & 0 deletions src/StringElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
declare(strict_types = 1);

namespace Html;

final class StringElement implements ElementInterface
{
private $children = [];

public static function create(string ...$children)
{
return new static(...$children);
}

public function __construct(string ...$children)
{
$this->children = $children;
}

public function __call(string $name, array $arguments): ElementInterface
{
return $this;
}

public function __toString(): string
{
return implode('', $this->children);
}

public function data(string $key, $value): ElementInterface
{
return $this;
}

public function jsonSerialize()
{
return [
'tag' => 'raw',
'children' => $this->children,
];
}

public function serialize()
{
return serialize($this->jsonSerialize());
}

public function unserialize($data)
{
$data = unserialize($data);

$this->children = $data['children'];
}
}
10 changes: 9 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ function array2Html(array $data): ElementInterface
);
}

$args = [$data['attributes'] ?? []];
if ($data['tag'] === 'raw') {
$args = [];
} else {
$args = [$data['attributes'] ?? []];
}

if (isset($data['children'])) {
foreach ($data['children'] as $child) {
Expand Down Expand Up @@ -477,3 +481,7 @@ function template(...$args): ElementInterface
{
return Element::create('template', $args);
}
function raw(string ...$strings): ElementInterface
{
return StringElement::create(...$strings);
}
8 changes: 8 additions & 0 deletions tests/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
use function Html\div;
use Html\ElementInterface;
use function Html\input;
use function Html\raw;
use function Html\span;
use function Html\strong;
use PHPUnit\Framework\TestCase;

class HtmlTest extends TestCase
{
public function testRaw()
{
$raw = raw('<b>foo</b>');
$this->assertSame('<b>foo</b>', (string) $raw);
$this->assertInstanceOf(ElementInterface::class, $raw);
}

public function testDiv()
{
$div = div();
Expand Down
12 changes: 9 additions & 3 deletions tests/SerializableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

use function Html\array2Html;
use function Html\div;
use function Html\raw;
use function Html\strong;
use PHPUnit\Framework\TestCase;

class SerializableTest extends TestCase
{
public function testJson()
{
$div = div('Hello ', strong('World'))->hidden();
$div = div('Hello ', strong(raw('<em>World</em>')))->hidden();
$expected = [
'tag' => 'div',
'attributes' => [
Expand All @@ -24,7 +25,12 @@ public function testJson()
'tag' => 'strong',
'attributes' => [],
'children' => [
'World',
[
'tag' => 'raw',
'children' => [
'<em>World</em>',
],
],
],
],
],
Expand All @@ -39,7 +45,7 @@ public function testJson()

public function testSerialize()
{
$div = div('Hello ', strong('World'))->hidden();
$div = div('Hello ', strong(raw('<em>World</em>')))->hidden();

$ser = \serialize($div);
$div2 = \unserialize($ser);
Expand Down

0 comments on commit 346cdea

Please sign in to comment.