From 930db3b36d87cd084e918f5cce6bc8f5760663ca Mon Sep 17 00:00:00 2001
From: Christian A. Weber
Date: Mon, 23 Apr 2018 15:41:46 +0200
Subject: officially support htmltype => 'xml', don't short-close voidtags in
xhtml mode
---
it_html.class | 12 ++++++++----
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 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' => '',
'html' => '',
'xhtml' => '',
- 'xhtml-mobile' => ''
+ 'xhtml-mobile' => '',
+ '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' => "
", 'html' => "
", 'xhtml' => "
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(),
+ "\n",
+ "script may not be shortened (see xhtml spec)"
+);
+
+is(
+ h1(),
+ "