Changeset 4414 in subversion


Ignore:
Timestamp:
Jan 12, 2011 3:56:31 PM (2 years ago)
Author:
alec
Message:
  • include css files before scripts for speedup (with parallel downloads) + some code cleanups
Location:
trunk/roundcubemail/program/include
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/roundcubemail/program/include/rcube_html_page.php

    r4410 r4414  
    2929    protected $scripts_path = ''; 
    3030    protected $script_files = array(); 
     31    protected $css_files = array(); 
    3132    protected $scripts = array(); 
    3233    protected $charset = RCMAIL_CHARSET; 
     
    3435    protected $script_tag_file = "<script type=\"text/javascript\" src=\"%s\"></script>\n"; 
    3536    protected $script_tag  =  "<script type=\"text/javascript\">\n/* <![CDATA[ */\n%s\n/* ]]> */\n</script>"; 
     37    protected $link_css_file = "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\" />\n"; 
    3638    protected $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>"; 
    3739 
     
    6264            return; 
    6365        } 
     66 
     67        $sa_files[] = $file; 
     68 
    6469        if (!is_array($this->script_files[$position])) { 
    6570            $this->script_files[$position] = array(); 
     
    8186            $this->scripts[$position] .= "\n".rtrim($script); 
    8287        } 
     88    } 
     89 
     90    /** 
     91     * Link an external css file 
     92     * 
     93     * @param string File URL 
     94     */ 
     95    public function include_css($file) 
     96    { 
     97        $this->css_files[] = $file; 
    8398    } 
    8499 
     
    164179 
    165180        // replace specialchars in content 
    166         $__page_title = Q($this->title, 'show', FALSE); 
    167         $__page_header = $__page_body = $__page_footer = ''; 
     181        $page_title  = Q($this->title, 'show', FALSE); 
     182        $page_header = ''; 
     183        $page_footer = ''; 
    168184 
    169185        // include meta tag with charset 
     
    172188                header('Content-Type: text/html; charset=' . $this->charset); 
    173189            } 
    174             $__page_header = '<meta http-equiv="content-type"'; 
    175             $__page_header.= ' content="text/html; charset='; 
    176             $__page_header.= $this->charset . '" />'."\n"; 
     190            $page_header = '<meta http-equiv="content-type"'; 
     191            $page_header.= ' content="text/html; charset='; 
     192            $page_header.= $this->charset . '" />'."\n"; 
    177193        } 
    178194 
     
    180196        if (is_array($this->script_files['head'])) { 
    181197            foreach ($this->script_files['head'] as $file) { 
    182                 $__page_header .= sprintf($this->script_tag_file, $file); 
     198                $page_header .= sprintf($this->script_tag_file, $file); 
    183199            } 
    184200        } 
     
    186202        $head_script = $this->scripts['head_top'] . $this->scripts['head']; 
    187203        if (!empty($head_script)) { 
    188             $__page_header .= sprintf($this->script_tag, $head_script); 
     204            $page_header .= sprintf($this->script_tag, $head_script); 
    189205        } 
    190206 
    191207        if (!empty($this->header)) { 
    192             $__page_header .= $this->header; 
     208            $page_header .= $this->header; 
    193209        } 
    194210 
    195211        if (is_array($this->script_files['foot'])) { 
    196212            foreach ($this->script_files['foot'] as $file) { 
    197                 $__page_footer .= sprintf($this->script_tag_file, $file); 
     213                $page_footer .= sprintf($this->script_tag_file, $file); 
    198214            } 
    199215        } 
    200216 
    201217        if (!empty($this->scripts['foot'])) { 
    202             $__page_footer .= sprintf($this->script_tag, $this->scripts['foot']); 
     218            $page_footer .= sprintf($this->script_tag, $this->scripts['foot']); 
    203219        } 
    204220 
    205221        if (!empty($this->footer)) { 
    206             $__page_footer .= $this->footer; 
     222            $page_footer .= $this->footer; 
    207223        } 
    208224 
    209225        // find page header 
    210226        if ($hpos = stripos($output, '</head>')) { 
    211             $__page_header .= "\n"; 
     227            $page_header .= "\n"; 
    212228        } 
    213229        else { 
     
    221237                $hpos++; 
    222238            } 
    223             $__page_header = "<head>\n<title>$__page_title</title>\n$__page_header\n</head>\n"; 
     239            $page_header = "<head>\n<title>$page_title</title>\n$page_header\n</head>\n"; 
    224240        } 
    225241 
    226242        // add page hader 
    227243        if ($hpos) { 
    228             $output = substr($output,0,$hpos) . $__page_header . substr($output,$hpos,strlen($output)); 
     244            $output = substr($output,0,$hpos) . $page_header . substr($output,$hpos,strlen($output)); 
    229245        } 
    230246        else { 
    231             $output = $__page_header . $output; 
    232         } 
    233  
    234         // find page body 
    235         if ($bpos = stripos($output, '<body')) { 
    236             while ($output[$bpos] != '>') { 
    237                 $bpos++; 
    238             } 
    239             $bpos++; 
     247            $output = $page_header . $output; 
     248        } 
     249 
     250        // add page footer 
     251        if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) { 
     252            $output = substr($output, 0, $fpos) . "$page_footer\n" . substr($output, $fpos); 
    240253        } 
    241254        else { 
    242             $bpos = stripos($output, '</head>')+7; 
    243         } 
    244  
    245         // add page body 
    246         if ($bpos && $__page_body) { 
    247             $output = substr($output,0,$bpos) . "\n$__page_body\n" . substr($output,$bpos,strlen($output)); 
    248         } 
    249  
    250         // find and add page footer 
    251         if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) { 
    252             $output = substr($output, 0, $fpos) . "$__page_footer\n" . substr($output, $fpos); 
    253         } 
    254         else { 
    255             $output .= "\n".$__page_footer; 
    256         } 
    257  
    258         // reset those global vars 
    259         $__page_header = $__page_footer = ''; 
     255            $output .= "\n".$page_footer; 
     256        } 
     257 
     258        // add css files in head, before scripts, for speed up with parallel downloads 
     259        if (!empty($this->css_files) &&  
     260            (($pos = stripos($output, '<script ')) || ($pos = stripos($output, '</head>'))) 
     261        ) { 
     262            $css = ''; 
     263            foreach ($this->css_files as $file) { 
     264                $css .= sprintf($this->link_css_file, $file); 
     265            } 
     266            $output = substr($output, 0, $pos) . $css . substr($output, $pos); 
     267        } 
    260268 
    261269            $this->base_path = $base_path; 
     270 
    262271        // correct absolute paths in images and other tags 
    263272            // add timestamp to .js and .css filename 
    264         $output = preg_replace_callback('!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i', 
    265             array($this, 'file_callback'), $output); 
     273        $output = preg_replace_callback( 
     274            '!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i', 
     275                array($this, 'file_callback'), $output); 
    266276        $output = str_replace('$__skin_path', $base_path, $output); 
    267277 
  • trunk/roundcubemail/program/include/rcube_plugin_api.php

    r4410 r4414  
    396396    if ($this->output->type == 'html') { 
    397397      $src = $this->resource_url($fn); 
    398       $this->output->add_header(html::tag('link', array('rel' => "stylesheet", 'type' => "text/css", 'href' => $src))); 
     398      $this->output->include_css($src); 
    399399    } 
    400400  } 
     
    438438 
    439439} 
    440  
Note: See TracChangeset for help on using the changeset viewer.