Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Selfclose tags automatically and correctly #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/HamlPHP/Lang/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -329,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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -635,8 +637,8 @@ public function useAttsHelper()
}

public function isSelfClosing() {
return $this->_selfClosing;
return $this->_selfClosing || in_array($this->getTag(), self::$_selfClosingTags);
}
}

?>
?>
4 changes: 2 additions & 2 deletions src/HamlPHP/Lang/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function atts($atts, $echo=true)

if($value === true)
{
$str .= " $name=\"$name\"";
$str .= " $name";
}
else
{
Expand Down Expand Up @@ -137,4 +137,4 @@ function class_for($obj, $prefix = '')
return $prefix.$obj->hamlObjRef();

return $prefix.s(get_class($obj))->underscorize();
}
}
18 changes: 17 additions & 1 deletion src/HamlPHP/Lang/Nodes/ElementNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', ";
}
}
}
}
}
Expand Down Expand Up @@ -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
{
Expand Down