Skip to content

Commit

Permalink
Made it work with DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
CHH committed Mar 9, 2012
1 parent 248c69d commit dda9014
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 52 deletions.
80 changes: 37 additions & 43 deletions lib/Jazz.php
Expand Up @@ -59,21 +59,8 @@ static function render($node, $document = null)
$document = new \DOMDocument;
}

if (is_array($node)) {
if (static::isTag($node)) {
$document->appendChild(static::renderHtmlTag($node, $document));
} else {
foreach ($node as $n) {
foreach (static::render($n, $document)->childNodes as $el) {
$document->appendChild($el);
}
}
}
} else {
$document->appendChild($document->createTextNode($node));
}

return $document;
$document->appendChild(static::renderNode($node, $document));
return $document->saveHTML();
}

# Checks if the provided node hash is a tag, by
Expand Down Expand Up @@ -101,43 +88,50 @@ protected static function isTag($node)
# node - Tag as Array.
#
# Returns the HTML as String.
protected static function renderHtmlTag($node, $document)
protected static function renderNode($node, $document)
{
# Strip the leading "#"
$tagName = substr(array_shift($node), 1);
$attributes = array();

if (sizeof($node) > 2) {
throw new \UnexpectedValueException(
"Tags must not consist of more than three elements"
);
if (is_string($node)) {
return $document->createTextNode($node);
}

foreach ($node as $el) {
# Is the tag argument an attributes array?
if (is_array($el) and count(array_filter(array_keys($el), "is_string")) > 0) {
$attributes = $el;
} else {
$content = $el;
if (static::isTag($node)) {
# Strip the leading "#"
$tagName = substr(array_shift($node), 1);
$attributes = array();

if (sizeof($node) > 2) {
throw new \UnexpectedValueException(
"Tags must not consist of more than three elements"
);
}

foreach ($node as $el) {
# Is the tag argument an attributes array?
if (is_array($el) and count(array_filter(array_keys($el), "is_string")) > 0) {
$attributes = $el;
} else {
$content = $el;
}
}
}

$out = $document->createElement($tagName);
$el = $document->createElement($tagName);

foreach ($attributes as $attr => $value) {
if (is_array($value)) $value = join(" ", $value);
$out->setAttribute($attr, $value);
}
foreach ($attributes as $attr => $value) {
if (is_array($value)) $value = join(" ", $value);
$el->setAttribute($attr, $value);
}

if (!isset($content)) {
return $out;
}
if (isset($content)) {
$el->appendChild(static::renderNode($content, $document));
}
} else {
$el = $document->createDocumentFragment();

if($content) {
foreach (static::render($content, $document)->childNodes as $node) {
$out->appendChild($node);
foreach ($node as $child) {
$el->appendChild(static::renderNode($child, $document));
}
}
return $out;

return $el;
}
}
20 changes: 11 additions & 9 deletions tests/JazzTest.php
Expand Up @@ -5,8 +5,8 @@ class JazzTest extends \PHPUnit_Framework_TestCase
function testShortTag()
{
$this->assertEquals(
"<br />",
Jazz::render(["#br"])->saveHTML()
"<br>\n",
Jazz::render(["#br"])
);
}

Expand All @@ -29,8 +29,8 @@ function testShortTagWithContent()
function testRendersEmptyBody()
{
$this->assertEquals(
'<p></p>',
Jazz::render(["#p", ""])->saveHTML()
"<p></p>\n",
Jazz::render(["#p", ""])
);
}

Expand All @@ -44,7 +44,7 @@ function testTagWithAttributesAndBody()
],
Jazz::render(
["#h1", ["role" => "banner"], "Unicorns and Rainbows"]
)->saveHTML()
)
);
}

Expand Down Expand Up @@ -93,11 +93,11 @@ function testNoEscape()
function testJoinsNodeListWithoutRootElement()
{
$this->assertEquals(
"<p>Foo</p><p>Bar</p>",
"<p>Foo</p><p>Bar</p>\n",
Jazz::render([
["#p", "Foo"],
["#p", "Bar"]
])->saveHTML()
])
);
}

Expand All @@ -115,10 +115,12 @@ function testJoinsMultipleAttributeValues()
"tag" => "p",
"attributes" => [
"class" => "foo bar baz"
]
],
"content" => "Foo"
];

$actual = Jazz::render(["#p", ["class" => ["foo", "bar", "baz"], "Foo"]]);
$actual = Jazz::render(["#p", ["class" => ["foo", "bar", "baz"]], "Foo"]);

$this->assertTag($expected, $actual);
}
}

0 comments on commit dda9014

Please sign in to comment.