From ed5fa13f00ca15d0b9455580d068a60e11bcfe2a Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Mon, 5 Oct 2015 17:17:41 +0100 Subject: [PATCH] Implement better URI class/ID on body tag, closes #242 --- src/Dispatcher/PageDispatcher.php | 32 ++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Dispatcher/PageDispatcher.php b/src/Dispatcher/PageDispatcher.php index 973d926f..f486b67d 100644 --- a/src/Dispatcher/PageDispatcher.php +++ b/src/Dispatcher/PageDispatcher.php @@ -225,21 +225,35 @@ public function setContentUri($uri, ResponseContent $content) { if(strrpos($uri, "/") === strlen($uri) - 1) { $uri .= "index"; } + $body = $content->querySelector("body"); $uri = trim($uri, "/"); - $uriPartTotal = ""; - foreach (explode("/", $uri) as $uriPart) { - $body->classList->add($uriPart); - if($uriPartTotal) { - $body->classList->add($uriPartTotal); + $classArray = []; + + foreach(explode("/", $uri) as $uriPart) { + $newClass = ""; + + foreach($classArray as $c) { + if(strlen($newClass) > 0) { + $newClass .= "_"; + } + $newClass .= $c; } - $uriPartTotal .= $uriPartTotal ? "_$uriPart" : $uriPart; + + if(strlen($newClass) > 0) { + $newClass .= "_"; + } + + $classArray []= $newClass . $uriPart; + } + + foreach($classArray as $c) { + $body->classList->add("dir--" . $c); } $uri = str_replace("/", "_", $uri); - $idUri = $uri; - $content->querySelector("body")->id = $idUri; + $body->id = "uri--" . $uri; } -}# \ No newline at end of file +}#