From ffc74814c1c2a3d70bada9693ddda238f69cb238 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Mon, 12 May 2014 09:53:47 -0400
Subject: [PATCH] Optimize "empty" framed pages size (#1489792)

---
 CHANGELOG                              |    1 +
 program/include/rcmail_output_html.php |   58 +++++++++++++++++++++++++++++++++++-----------------------
 2 files changed, 36 insertions(+), 23 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 5729345..9ca544e 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -17,6 +17,7 @@
 - Removed redundant default_folders config option (#1489737)
 - Implemented IMAP SPECIAL-USE extension support [RFC6154] (#1487830)
 - Add configurable LDAP_OPT_DEREF option (#1489864)
+- Optimize framed pages size (#1489792)
 - Fix mbox files import
 
 RELEASE 1.0.1
diff --git a/program/include/rcmail_output_html.php b/program/include/rcmail_output_html.php
index 154bbef..2a90f6a 100644
--- a/program/include/rcmail_output_html.php
+++ b/program/include/rcmail_output_html.php
@@ -431,15 +431,17 @@
             $this->set_env('request_token', $this->app->get_request_token());
         }
 
-        // write all env variables to client
-        if ($commands = $this->get_js_commands()) {
-            if ($this->framed) {
-                $prefix = "if (window.parent) {\n";
-                $suffix = " }";
-            }
+        $commands = $this->get_js_commands($framed);
 
-            $this->add_script($prefix . $commands . $suffix, 'head_top');
+        // if all js commands go to parent window we can ignore all
+        // script files and skip rcube_webmail initialization (#1489792)
+        if ($framed) {
+            $this->scripts      = array();
+            $this->script_files = array();
         }
+
+        // write all javascript commands
+        $this->add_script($commands, 'head_top');
 
         // send clickjacking protection headers
         $iframe = $this->framed || $this->env['framed'];
@@ -567,17 +569,18 @@
      *
      * @return string $out
      */
-    protected function get_js_commands()
+    protected function get_js_commands(&$framed = null)
     {
-        $out = '';
-
         if (!$this->framed && !empty($this->js_env)) {
-            $out .= self::JS_OBJECT_NAME . '.set_env('.self::json_serialize($this->js_env).");\n";
+            $this->command('set_env', $this->js_env);
         }
 
         if (!empty($this->js_labels)) {
             $this->command('add_label', $this->js_labels);
         }
+
+        $out = '';
+        $parent_commands = 0;
 
         foreach ($this->js_commands as $i => $args) {
             $method = array_shift($args);
@@ -587,12 +590,26 @@
                 $args[$i] = self::json_serialize($arg);
             }
 
-            $out .= sprintf(
-                "%s.%s(%s);\n",
-                ($parent ? 'if (window.parent && parent.'.self::JS_OBJECT_NAME.') parent.' : '') . self::JS_OBJECT_NAME,
-                preg_replace('/^parent\./', '', $method),
-                implode(',', $args)
-            );
+            if ($parent) {
+                $parent_commands++;
+                $method        = preg_replace('/^parent\./', '', $method);
+                $parent_prefix = 'if (window.parent && parent.' . self::JS_OBJECT_NAME . ') parent.';
+                $method        = $parent_prefix . self::JS_OBJECT_NAME . '.' . $method;
+            }
+            else {
+                $method = self::JS_OBJECT_NAME . '.' . $method;
+            }
+
+            $out .= sprintf("%s(%s);\n", $method, implode(',', $args));
+        }
+
+        $framed = $parent_prefix && $parent_commands == count($this->js_commands);
+
+        // make the output more compact if all commands go to parent window
+        if ($framed) {
+            $out = "if (window.parent && parent." . self::JS_OBJECT_NAME . ") {\n"
+                . str_replace($parent_prefix, "\tparent.", $out)
+                . "}\n";
         }
 
         return $out;
@@ -1331,13 +1348,8 @@
         $output = trim($templ);
 
         if (empty($output)) {
-            $output   = $this->default_template;
+            $output   = html::doctype('html5') . "\n" . $this->default_template;
             $is_empty = true;
-        }
-
-        // set default page title
-        if (empty($this->pagetitle)) {
-            $this->pagetitle = 'Roundcube Mail';
         }
 
         // replace specialchars in content

--
Gitblit v1.9.1