Skip to content

Commit

Permalink
Fix a few edge-cases
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Aug 16, 2017
1 parent 764d9b7 commit 97efb1a
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Pre\Phpx;

use Exception;

function tokens($code) {
$tokens = [];

Expand Down Expand Up @@ -119,7 +121,7 @@ function tokens($code) {

$tokens[] = trim($code);

return array_filter($tokens);
return $tokens;
}

function nodes($tokens) {
Expand All @@ -132,7 +134,11 @@ function nodes($tokens) {
while ($cursor < $length) {
$token =& $tokens[$cursor];

if (is_array($token) && !empty($token["tag"]) && $token["tag"][1] !== "/") {
if (!is_array($token)) {
$token = ["text" => $token];
}

if (isset($token["tag"]) && $token["tag"][1] !== "/") {
preg_match("#^<([a-zA-Z]+)#", $token["tag"], $matches);

if ($current !== null) {
Expand Down Expand Up @@ -161,7 +167,7 @@ function nodes($tokens) {
}

foreach ($item as $value) {
if (!empty($value["token"])) {
if (!empty($value["text"])) {
return $value;
}
}
Expand All @@ -172,7 +178,7 @@ function nodes($tokens) {
}
}

else if (is_array($token) && !empty($token["tag"]) && $token["tag"][1] === "/") {
else if (isset($token["tag"]) && $token["tag"][1] === "/") {
preg_match("#^</([a-zA-Z]+)#", $token["tag"], $matches);

if ($current === null) {
Expand All @@ -189,16 +195,12 @@ function nodes($tokens) {
}

else if ($current !== null) {
array_push($current["children"], [
"parent" => &$current,
"token" => &$token,
]);
$token["parent"] =& $current;
$current["children"][] =& $token;
}

else {
array_push($nodes, [
"token" => $token,
]);
$nodes[] =& $token;
}

$cursor++;
Expand All @@ -211,30 +213,23 @@ function parse($nodes) {
$code = "";

foreach ($nodes as $node) {
if (isset($node["token"])) {
if (is_array($node["token"])) {
continue;
}

$code .= $node["token"] . PHP_EOL;
if (isset($node["text"])) {
$code .= $node["text"] . PHP_EOL;
}

if (isset($node["tag"])) {
$props = [];
$attributes = [];

if (isset($node["attributes"])) {
foreach ($node["attributes"] as $key => $value) {
if (isset($value["token"])) {
$attributes["attr_{$key}"] = $value["token"];
}
$node["attributes"] = array_filter($node["attributes"]);

else if (isset($value["tag"])) {
foreach ($node["attributes"] as $key => $value) {
if (isset($value["tag"])) {
$attributes["attr_{$key}"] = parse([$value]);
}

else {
throw new Exception("attribute not token or tag");
$attributes["attr_{$key}"] = $value["text"];
}
}
}
Expand All @@ -259,7 +254,7 @@ function parse($nodes) {
}

else {
$children[] = "\"" . addslashes($child["token"]) . "\"";
$children[] = "\"" . addslashes($child["text"]) . "\"";
}
}

Expand Down Expand Up @@ -289,7 +284,7 @@ function parse($nodes) {
}
}

$code .= "])" . PHP_EOL;
$code .= "])";
}
}

Expand Down

0 comments on commit 97efb1a

Please sign in to comment.