From be26c7210fab7bb1f95304a769d775b72dbb5959 Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Fri, 25 Jan 2013 16:22:15 +0100 Subject: [PATCH 1/2] Selfclose tags automatically and correctly based on http://xahlee.info/js/html5_non-closing_tag.html --- src/HamlPHP/Lang/Element.php | 10 ++++++---- src/HamlPHP/Lang/Helpers.php | 4 ++-- src/HamlPHP/Lang/Nodes/ElementNode.php | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/HamlPHP/Lang/Element.php b/src/HamlPHP/Lang/Element.php index 08208e7..48ac17d 100644 --- a/src/HamlPHP/Lang/Element.php +++ b/src/HamlPHP/Lang/Element.php @@ -13,6 +13,8 @@ class Element const ID = '#'; const KLASS = '.'; + private static $_selfClosingTags = array( 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr' ); + private $_haml = null; private $_tag = null; private $_id = null; @@ -490,11 +492,11 @@ private function _parseHashAttrs(StringScanner $scanner) { if ($scanner[0] == 'true') $atts[$name] = array( - 't' => 'static' , 'v' => true + 't' => 'boolean' , 'v' => true ); else $atts[$name] = array( - 't' => 'static' , 'v' => false + 't' => 'boolean' , 'v' => false ); } else @@ -635,8 +637,8 @@ public function useAttsHelper() } public function isSelfClosing() { - return $this->_selfClosing; + return $this->_selfClosing || in_array($this->getTag(), self::$_selfClosingTags); } } -?> \ No newline at end of file +?> diff --git a/src/HamlPHP/Lang/Helpers.php b/src/HamlPHP/Lang/Helpers.php index 8aabfe3..1f589b2 100644 --- a/src/HamlPHP/Lang/Helpers.php +++ b/src/HamlPHP/Lang/Helpers.php @@ -61,7 +61,7 @@ function atts($atts, $echo=true) if($value === true) { - $str .= " $name=\"$name\""; + $str .= " $name"; } else { @@ -137,4 +137,4 @@ function class_for($obj, $prefix = '') return $prefix.$obj->hamlObjRef(); return $prefix.s(get_class($obj))->underscorize(); -} \ No newline at end of file +} diff --git a/src/HamlPHP/Lang/Nodes/ElementNode.php b/src/HamlPHP/Lang/Nodes/ElementNode.php index 558a13d..77b70ec 100644 --- a/src/HamlPHP/Lang/Nodes/ElementNode.php +++ b/src/HamlPHP/Lang/Nodes/ElementNode.php @@ -78,6 +78,14 @@ private function renderHtml(Element $element) case 'function': $output .= "{$att['v']}, "; continue; + case 'boolean': + if( Config::$format == 'html5' ) { + $output .= "'$name' => ".($att['v'] ? 'true' : 'false').", "; + } else { + if( $att['v'] == true ) { + $output .= "'$name' => '$name', "; + } + } } } } @@ -125,7 +133,15 @@ private function renderHtml(Element $element) if($this->_el->isSelfClosing()) { - $output .= ' />'; + switch( Config::$format ) { + case 'html5': + case 'html4': + $output .= '>'; + break; + default: + $output .= '/>'; + break; + } } else { From bb4d3a79f71dafdb32c1ab6c5420d28df555bb76 Mon Sep 17 00:00:00 2001 From: Markus Doits Date: Wed, 11 Sep 2013 14:28:19 +0200 Subject: [PATCH 2/2] add forgotten change to boolean in _parseHtmlAttrs() --- src/HamlPHP/Lang/Element.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/HamlPHP/Lang/Element.php b/src/HamlPHP/Lang/Element.php index 48ac17d..6c9749e 100644 --- a/src/HamlPHP/Lang/Element.php +++ b/src/HamlPHP/Lang/Element.php @@ -331,11 +331,11 @@ private function _parseHtmlAttrs(StringScanner $scanner) { if ($scanner[0] == 'true') $atts[$name] = array( - 't' => 'static' , 'v' => true + 't' => 'boolean' , 'v' => true ); else $atts[$name] = array( - 't' => 'static' , 'v' => false + 't' => 'boolean' , 'v' => false ); } else