diff options
-rw-r--r-- | it_html.class | 12 | ||||
-rwxr-xr-x | tests/it_html.t | 21 |
2 files changed, 27 insertions, 6 deletions
diff --git a/it_html.class b/it_html.class index abd674f..58e0b84 100644 --- a/it_html.class +++ b/it_html.class @@ -54,7 +54,8 @@ function it_parse_args($args) class it_html { - static $voidtags = array('area' => 1, 'base' => 1, 'br' => 1, 'col' => 1, 'command' => 1, 'embed' => 1, 'hr' => 1, 'img' => 1, 'input' => 1, 'keygen' => 1, 'link' => 1, 'meta' => 1, 'param' => 1, 'source' => 1, 'track' => 1, 'wbr' => 1); # need no closing tag + # these tags have no content and need no closing tag in html or can be shortened in xhtml + static $voidtags = array('area' => 1, 'base' => 1, 'br' => 1, 'col' => 1, 'command' => 1, 'embed' => 1, 'hr' => 1, 'img' => 1, 'input' => 1, 'keygen' => 1, 'link' => 1, 'meta' => 1, 'param' => 1, 'source' => 1, 'track' => 1, 'wbr' => 1); var $p; # constructor params plus defaults @@ -72,7 +73,7 @@ function __construct($p = array()) 'charset' => ini_get('default_charset') ?: 'iso-8859-1', 'doctype' => null, # Custom doctype (will usually be calculated from htmltype) 'head' => '', # Code to put into head() section - 'htmltype' => 'html5', # 'html5', 'html' (=old-style), 'xhtml' or 'xhtml-mobile' + 'htmltype' => 'html5', # 'html5', 'html' (=old-style), 'xhtml' or 'xhtml-mobile' for xhtml, or 'xml' for plain xml without magic 'lang' => 'de', # Language code to use in <html lang="..."> tag 'moretags' => '', # Comma-separated list of tag-functions to generate additionally to 'tags' 'name' => 'it_html', # Name of global variable $this is assigned to (string), XXX Copy and paste in configure() to keep PHP4 compatibility @@ -95,7 +96,8 @@ function __construct($p = array()) 'html5' => '<!DOCTYPE html>', 'html' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">', 'xhtml' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', - 'xhtml-mobile' => '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">' + 'xhtml-mobile' => '<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">', + 'xml' => '' ); # Since name is given as param, it is our duty to store it, not our caller's. @@ -316,8 +318,10 @@ function _tag($name, $args) } # close tag according to html dialect - if ($this->p['htmltype'][0] == 'x') # xhtml + if ($this->p['htmltype'] == 'xml') # plain xml $result .= isset($data) ? ">$data</$name>$newline" : " />$newline"; + elseif ($this->p['htmltype'][0] == 'x') # xhtml: only voidtags can be shortened + $result .= isset($data) || !self::$voidtags[$name] ? ">$data</$name>$newline" : " />$newline"; else $result .= isset($data) || !self::$voidtags[$name] ? ">$data</$name>$newline" : ">$newline"; diff --git a/tests/it_html.t b/tests/it_html.t index 0c419b8..bcfd40d 100755 --- a/tests/it_html.t +++ b/tests/it_html.t @@ -108,9 +108,26 @@ foreach (array('html5' => "<br flag>", 'html' => "<br flag>", 'xhtml' => "<br fl is (trim(br(array('flag' => true))), $value, "Check empty tag and attribute for $type"); } +# XHTML generation +unset($GLOBALS['it_html']); +new it_html(array('htmltype' => "xhtml", 'tags' => "script")); + +is( + script(), + "<script></script>\n", + "script may not be shortened (see xhtml spec)" +); + +is( + h1(), + "<h1></h1>\n", + "empty h1 tag in xhtml context" +); + + # XML generation unset($GLOBALS['it_html']); -new it_html(array('htmltype' => "xhtml", 'tags' => "xmltest")); +new it_html(array('htmltype' => "xml", 'tags' => "xmltest")); is( xmltest(), @@ -121,7 +138,7 @@ is( is( xmltest("foo"), "<xmltest>foo</xmltest>\n", - "empty xmltest tag" + "non-empty xmltest tag" ); is( |