From ad91e2b50a2b5d2fdc704dd3b594f01386b460fd Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Sun, 24 Dec 2006 01:08:27 +0000 Subject: faster, pussycat! --- html.class | 48 +++++++++++++++++++++--------------------------- 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/html.class b/html.class index 8281560..8bde4dd 100644 --- a/html.class +++ b/html.class @@ -35,6 +35,7 @@ class it_html 'show_boot_dom' => true, ); var $tags_seen = array('body' => true); # body always counts as seen + var $_hasnonewline = array(); /** * Create a HTML object and global functions for all methods (exlcluding @@ -61,36 +62,22 @@ function it_html($config = array()) if (isset($this->_oldhtml) && !$this->_htmltype) $this->_htmltype = $this->_oldhtml ? "html" : "xhtml"; $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'] - $methods = get_class_methods(get_class($this)); $funcs = array_unique(array_merge($methods, explode(',', $this->_tags), explode(',', $this->_moretags))); - $nonewlinetags = explode(',', $this->_nonewlinetags); - foreach($funcs as $func) - { - # Do not globalise 'private' functions starting in '_' or for our constructor - if (preg_match('/^_/', $func) || is_a($this, $func) || empty($func) || function_exists($func)) - continue; - - $funcargs = array(); - if (in_array($func, $nonewlinetags)) - $funcargs += array('nonewline' => true); - - $funcconfig = $funcargs ? 'array_push($args, '.strtr(var_export($funcargs, true), array("\n" => '')).'); ' : ''; - - $code = "function $func() { \$args = func_get_args(); $funcconfig"; - if (!in_array($func, $methods)) # No special case: generate call to tag() - { - $code .= "array_unshift(\$args, '$func');"; - $func = 'tag'; - } + # 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 .= "return call_user_func_array(array(&\$GLOBALS['$this->_name'], '$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);}"; - debug(get_class($this).': code: '.$code, 8); - eval($code); - } + eval(join("", array_values($code))); # putting things in assoc array prevents redeclarations # Since name is given as param, it is our duty to store it, not our caller's. $GLOBALS[$this->_name] =& $this; @@ -108,6 +95,14 @@ function tag(/* $name, ... */) $args = func_get_args(); $name = array_shift($args); + return $this->_tag($name, $args); +} + +/** + * Internal: Create html tag from name and args array + */ +function _tag($name, $args) +{ $data = null; $attr = array(); @@ -129,8 +124,7 @@ function tag(/* $name, ... */) $data .= $arg; } - $newline = empty($attr['nonewline']) ? "\n" : ""; - unset($attr['nonewline']); + $newline = $this->_hasnonewline[$name] ? "" : "\n"; # Ultra XML PrettyPrinter 3000 [\] by SCA if ($this->_prettyprint && $newline && (substr($data, -1, 1) == "\n") && (strpos($data, '&\"'\n\x80-\x9f]/", $value) ? str_replace("\n", " ", it_html::Q($value)) : $value) . '"'; else # true: tag without value $result .= $this->_oldhtml ? " $key" : " $key=\"$key\""; } -- cgit v1.2.3