diff options
author | Urban Müller | 2006-12-26 20:24:55 +0000 |
---|---|---|
committer | Urban Müller | 2006-12-26 20:24:55 +0000 |
commit | 5f9d4842bf0a625214e80facb7dbdde934495432 (patch) | |
tree | 947b753e12e466a17478108bdf97a8d1fd65d20e /html.class | |
parent | 3ab5a20ae3a9b62c1094396e8fa8217171b12f99 (diff) | |
download | itools-5f9d4842bf0a625214e80facb7dbdde934495432.tar.gz itools-5f9d4842bf0a625214e80facb7dbdde934495432.tar.bz2 itools-5f9d4842bf0a625214e80facb7dbdde934495432.zip |
use arrays instead of varargs for method calls (by cschneid)
Diffstat (limited to 'html.class')
-rw-r--r-- | html.class | 115 |
1 files changed, 41 insertions, 74 deletions
@@ -36,6 +36,7 @@ class it_html ); var $tags_seen = array('body' => true); # body always counts as seen var $_hasnonewline = array(); + var $_staticallycallable = "q,u,select"; # Those methods are statically callable (have same arguments as global stubs) but are a bit slower /** * Create a HTML object and global functions for all methods (exlcluding @@ -64,20 +65,25 @@ function it_html($config = array()) $this->_oldhtml = $this->_htmltype == "html"; $this->_hasnonewline = array_flip(explode(',', "dummy," . $this->_nonewlinetags)); # dummy keeps values >0 - # Create global functions from all methods and names in $config['tags'] and $config['moretags'] - $funcs = array_unique(array_merge($methods, explode(',', $this->_tags), explode(',', $this->_moretags))); - # Create global functions for _tags foreach (array_merge(explode(',', $this->_tags), explode(',', $this->_moretags)) as $func) + { if (!function_exists($func) && $func) - $code[$func] = "function $func() { \$args = func_get_args(); return \$GLOBALS['it_html']->_tag('$func', \$args);}"; + $code[$func] = "function $func() { \$args = func_get_args(); return \$GLOBALS['$this->_name']->_tag('$func', \$args);}"; + } # Create global functions for it_html methods foreach (get_class_methods(get_class($this)) as $func) + { if (!preg_match('/^_/', $func) && !is_a($this, $func) && $func && !function_exists($func)) - $code[$func] = "function $func() { \$args = func_get_args(); return call_user_func_array(array(&\$GLOBALS['$this->_name'], '$func'), \$args);}"; + $code[$func] = "function $func() { \$args = func_get_args(); return \$GLOBALS['$this->_name']->$func(\$args); }"; + } + + # Create global functions for methods that are statically callable (have same arguments as global stubs) + foreach (explode(",", $this->_staticallycallable) as $func) + $code[$func] = "function $func() { \$args = func_get_args(); return call_user_func_array(array(&\$GLOBALS['$this->_name'], '$func'), \$args); }"; - eval(join("", array_values($code))); # putting things in assoc array prevents redeclarations + eval(join("", $code)); # Since name is given as param, it is our duty to store it, not our caller's. $GLOBALS[$this->_name] =& $this; @@ -90,9 +96,8 @@ function it_html($config = array()) * @param ... any number optional data or array of key => value arguments * @return string containing XML/HTML tag */ -function tag(/* $name, ... */) +function tag($args) { - $args = func_get_args(); $name = array_shift($args); return $this->_tag($name, $args); @@ -170,15 +175,12 @@ function _tag($name, $args) * @param ... any number optional data or array of key => value arguments * @return <div class="$class"...>...</div> */ -function div(/* $class, ... */) +function div($args) { - $args = func_get_args(); - if (!is_array($args[0]) && ($class = array_shift($args)) !== null) array_unshift($args, compact('class')); - array_unshift($args, 'div'); - return call_user_func_array(array(&$this, 'tag'), $args); + return $this->_tag("div", $args); } @@ -187,10 +189,8 @@ function div(/* $class, ... */) * @param ... any number optional data or array of key => value arguments * @return <img ... /> */ -function img(/* ... */) +function img($args) { - $args = func_get_args(); - if ($this->_ie_png_fix && preg_match('/MSIE [56]/', $_SERVER['HTTP_USER_AGENT'])) { foreach($args as $id => $arg) @@ -201,8 +201,7 @@ function img(/* ... */) } } - array_unshift($args, 'img'); - return call_user_func_array(array(&$this, 'tag'), $args); + return $this->_tag("img", $args); } @@ -212,15 +211,12 @@ function img(/* ... */) * @param ... any number optional data or array of key => value arguments * @return <span class="$class"...>...</span> */ -function span(/* $class, ... */) +function span($args) { - $args = func_get_args(); - if (!is_array($args[0]) && ($class = array_shift($args)) !== null) array_unshift($args, compact('class')); - array_unshift($args, 'span'); - return call_user_func_array(array(&$this, 'tag'), $args); + return $this->_tag("span", $args); } @@ -229,7 +225,7 @@ function span(/* $class, ... */) * @param $string String to encode with htmlspecialchars() * @return htmlspecialchars($string) */ -function Q($string) +function q($string) { if ($GLOBALS['it_html']->_charset == "iso-8859-1") $string = preg_replace('/[\x80-\x9f]/', ' ', strtr($string, array("\x80"=>"EUR", "\x82"=>"'", "\x84"=>"\"", "\x85"=>"...", "\x8a"=>"S", "\x8c"=>"OE", "\x8e"=>"Z", "\x91"=>"'", "\x92"=>"'", "\x93"=>"\"", "\x94"=>"\"", "\x96"=>"-", "\x97"=>"-", "\x9a"=>"s", "\x9e"=>"z"))); @@ -242,7 +238,7 @@ function Q($string) * Build a complete url from base-url and params * @param ... scalar args and numeric indices build base-url, rest as params */ -function U(/* ... */) +function u(/* ... */) { $args = func_get_args(); $base = null; @@ -320,14 +316,14 @@ function select($tags, $options, $selected = null) { $grouphtml = ""; foreach($option as $optval => $opt) - $grouphtml .= $this->tag('option', array('value' => $optval, 'selected' => isset($selected) ? $optval == $selected : false), it_html::Q($opt)); - $html .= $this->tag('optgroup', array('label' => $value), $grouphtml); + $grouphtml .= $this->_tag("option", array(array('value' => $optval, 'selected' => isset($selected) ? $optval == $selected : false), it_html::Q($opt))); + $html .= $this->_tag("optgroup", array(array('label' => $value, $grouphtml))); } else - $html .= $this->tag('option', array('value' => $value, 'selected' => isset($selected) ? $value == $selected : false, 'disabled' => $option === ""), (trim($option) === "") ? " " : it_html::Q($option)); + $html .= $this->_tag("option", array(array('value' => $value, 'selected' => isset($selected) ? $value == $selected : false, 'disabled' => $option === ""), (trim($option) === "") ? " " : it_html::Q($option))); } - return $this->tag('select', $tags, $html); + return $this->_tag("select", array($tags, $html)); } @@ -336,71 +332,45 @@ function select($tags, $options, $selected = null) * @param ... any number optional data or array of key => value arguments * @return <script type="text/javascript"...>...</script> */ -function js(/* ... */) +function js($args) { - $args = func_get_args(); - if (!$this->_oldhtml && isset($args[0])) { array_unshift($args, "<!--//--><![CDATA[//><!--\n"); $args[] = "\n//--><!]]>"; } - array_unshift($args, 'script', array('type' => 'text/javascript')); - return call_user_func_array(array(&$this, 'tag'), $args); + array_unshift($args, array('type' => 'text/javascript')); + return $this->_tag("script", $args); } /** * Include javascript code or generate HTML to include it */ -function itjs(/* ... */) +function _itjs($files, $mode) { $result = ""; - $files = array(); - $p = array(); - - $args = func_get_args(); - - foreach ($args as $arg) - { - if (isset($arg)) - { - if (!is_array($arg)) - $files[] = $arg; - else - $p += $arg; - } - } if ($files) { - if ($p['mode'] == "files") + if ($mode == "files") { - foreach ($files as $filelist) - { - foreach (itjs::filenames($filelist) as $file) - $result .= tag('script', array('type' => "text/javascript", 'src' => "/itjs/" . basename($file) . "?v=" . itjs::checksum($file))); - } + foreach (itjs::filenames($files) as $file) + $result .= tag('script', array('type' => "text/javascript", 'src' => "/itjs/" . basename($file) . "?v=" . itjs::checksum($file))); } - else if ($p['mode'] == "inline") + else if ($mode == "inline") { $jsfile = ""; - foreach ($files as $filelist) - { - foreach (itjs::filenames($filelist) as $file) - $jsfile .= @file_get_contents($file); - } + foreach (itjs::filenames($files) as $file) + $jsfile .= @file_get_contents($file); $result .= itjs::strip($jsfile); } else { - $filenames = array(); - - foreach ($files as $filelist) - $filenames = array_merge($filenames, itjs::filenames($filelist)); + $filenames = itjs::filenames($files); $result .= tag('script', array('type' => "text/javascript", 'src' => "/itjs/" . join(",", $files) . "?v=" . itjs::checksum($filenames))); } @@ -422,11 +392,10 @@ function itjs(/* ... */) * 'lang' optional language (defaults to 'de') * 'stylesheets' optional array mediatype => url of styleshests */ -function head(/* ... */) +function head($args = array()) { if (!$this->head_sent++) { - $args = func_get_args(); $p = array(); foreach ($args as $arg) @@ -495,24 +464,22 @@ function head(/* ... */) if ($p['js']) { - $js .= $this->itjs("boot.js", array('mode' => "inline")); + $js .= $this->_itjs("boot.js", "inline"); $js .= "function it_boot_start(){ " . trim($p['jsboot']) . " }\n"; $js .= "it_boot('/itjs/" . $p['js'] . "');\n"; } - $js .= $this->itjs($p['jsinline'], array('mode' => 'inline')); + $js .= $this->_itjs($p['jsinline'], 'inline'); if ($js) - $header .= $this->js($js); + $header .= $this->js(array($js)); return "$prefix$header$data</head>"; } } -function body(/* ... */) +function body($args) { - $args = func_get_args(); - foreach ($args as $arg) { if (is_array($arg)) |