| +-----------------------------------------------------------------------+ $Id$ */ /** * Class for HTML code creation * * @package HTML */ class html { protected $tagname; protected $attrib = array(); protected $allowed = array(); protected $content; public static $doctype = 'xhtml'; public static $lc_tags = true; public static $common_attrib = array('id','class','style','title','align'); public static $containers = array('iframe','div','span','p','h1','h2','h3','form','textarea','table','thead','tbody','tr','th','td','style','script'); /** * Constructor * * @param array $attrib Hash array with tag attributes */ public function __construct($attrib = array()) { if (is_array($attrib)) { $this->attrib = $attrib; } } /** * Return the tag code * * @return string The finally composed HTML tag */ public function show() { return self::tag($this->tagname, $this->attrib, $this->content, array_merge(self::$common_attrib, $this->allowed)); } /****** STATIC METHODS *******/ /** * Generic method to create a HTML tag * * @param string $tagname Tag name * @param array $attrib Tag attributes as key/value pairs * @param string $content Optinal Tag content (creates a container tag) * @param array $allowed_attrib List with allowed attributes, omit to allow all * @return string The XHTML tag */ public static function tag($tagname, $attrib = array(), $content = null, $allowed_attrib = null) { if (is_string($attrib)) $attrib = array('class' => $attrib); $inline_tags = array('a','span','img'); $suffix = $attrib['nl'] || ($content && $attrib['nl'] !== false && !in_array($tagname, $inline_tags)) ? "\n" : ''; $tagname = self::$lc_tags ? strtolower($tagname) : $tagname; if (isset($content) || in_array($tagname, self::$containers)) { $suffix = $attrib['noclose'] ? $suffix : '' . $tagname . '>' . $suffix; unset($attrib['noclose'], $attrib['nl']); return '<' . $tagname . self::attrib_string($attrib, $allowed_attrib) . '>' . $content . $suffix; } else { return '<' . $tagname . self::attrib_string($attrib, $allowed_attrib) . '>' . $suffix; } } /** * */ public static function doctype($type) { $doctypes = array( 'html5' => '', 'xhtml' => '', 'xhtml-trans' => '', 'xhtml-strict' => '', ); if ($doctypes[$type]) { self::$doctype = preg_replace('/-\w+$/', '', $type); return $doctypes[$type]; } return ''; } /** * Derrived method for
blocks * * @param mixed $attr Hash array with tag attributes or string with class name * @param string $cont Paragraph content * @return string HTML code * @see html::tag() */ public static function p($attr = null, $cont = null) { if (is_string($attr)) { $attr = array('class' => $attr); } return self::tag('p', $attr, $cont, self::$common_attrib); } /** * Derrived method to create * * @param mixed $attr Hash array with tag attributes or string with image source (src) * @return string HTML code * @see html::tag() */ public static function img($attr = null) { if (is_string($attr)) { $attr = array('src' => $attr); } return self::tag('img', $attr + array('alt' => ''), null, array_merge(self::$common_attrib, array('src','alt','width','height','border','usemap','onclick'))); } /** * Derrived method for link tags * * @param mixed $attr Hash array with tag attributes or string with link location (href) * @param string $cont Link content * @return string HTML code * @see html::tag() */ public static function a($attr, $cont) { if (is_string($attr)) { $attr = array('href' => $attr); } return self::tag('a', $attr, $cont, array_merge(self::$common_attrib, array('href','target','name','rel','onclick','onmouseover','onmouseout','onmousedown','onmouseup'))); } /** * Derrived method for inline span tags * * @param mixed $attr Hash array with tag attributes or string with class name * @param string $cont Tag content * @return string HTML code * @see html::tag() */ public static function span($attr, $cont) { if (is_string($attr)) { $attr = array('class' => $attr); } return self::tag('span', $attr, $cont, self::$common_attrib); } /** * Derrived method for form element labels * * @param mixed $attr Hash array with tag attributes or string with 'for' attrib * @param string $cont Tag content * @return string HTML code * @see html::tag() */ public static function label($attr, $cont) { if (is_string($attr)) { $attr = array('for' => $attr); } return self::tag('label', $attr, $cont, array_merge(self::$common_attrib, array('for'))); } /** * Derrived method to create * * @param mixed $attr Hash array with tag attributes or string with frame source (src) * @return string HTML code * @see html::tag() */ public static function iframe($attr = null, $cont = null) { if (is_string($attr)) { $attr = array('src' => $attr); } return self::tag('iframe', $attr, $cont, array_merge(self::$common_attrib, array('src','name','width','height','border','frameborder'))); } /** * Derrived method to create