Author: johnh
Date: Thu Jun 25 22:41:13 2009
New Revision: 788536

URL: http://svn.apache.org/viewvc?rev=788536&view=rev
Log:
dynamic-height fix for WebKit-based browsers. Makes it possible for 
dynamic-height to shrink, as well as expand, the IFRAME's height.

Code provided by Michael Hermanto. Many thanks!


Modified:
    
incubator/shindig/trunk/features/src/main/javascript/features/dynamic-height/dynamic-height.js

Modified: 
incubator/shindig/trunk/features/src/main/javascript/features/dynamic-height/dynamic-height.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/dynamic-height/dynamic-height.js?rev=788536&r1=788535&r2=788536&view=diff
==============================================================================
--- 
incubator/shindig/trunk/features/src/main/javascript/features/dynamic-height/dynamic-height.js
 (original)
+++ 
incubator/shindig/trunk/features/src/main/javascript/features/dynamic-height/dynamic-height.js
 Thu Jun 25 22:41:13 2009
@@ -38,6 +38,41 @@
   var oldHeight;
 
   /**
+   * Parse out the value (specified in px) for a CSS attribute of an element.
+   * @param {Object} elem the element with the attribute to look for.
+   * @param {String} attr the CSS attribute name of interest.
+   * @returns {int} the value of the px attr of the elem.
+   */
+  function parseIntFromElemPxAttribute(elem, attr) {
+    var style = window.getComputedStyle(elem, "");
+    var value = style.getPropertyValue(attr);
+    value.match(/^([0-9]+)/);
+    return parseInt(RegExp.$1, 10);
+  }
+
+  /**
+   * Calculate the height of the gadget iframe by iterating through the
+   * elements in the gadget body for Webkit.
+   * @returns {int} the height of the gadget.
+   */
+  function getHeightForWebkit() {
+    var result = 0;
+    var children = document.body.childNodes;
+    for (var i = 0; i < children.length; i++) {
+      if (children[i].offsetTop && children[i].offsetHeight) {
+        var bottom = children[i].offsetTop + children[i].scrollHeight
+            + parseIntFromElemPxAttribute(children[i], "margin-bottom")
+            + parseIntFromElemPxAttribute(children[i], "padding-bottom");
+        result = Math.max(result, bottom);
+      }
+    }
+    // Add margin and padding height specificed in the body (if any).
+    return result
+        + parseIntFromElemPxAttribute(document.body, "margin-bottom")
+        + parseIntFromElemPxAttribute(document.body, "padding-bottom");
+  }
+
+  /**
    * Detects the inner dimensions of a frame.
    * See: http://www.quirksmode.org/viewport/compatibility.html for more
    * information.
@@ -100,6 +135,13 @@
         // use the value that's NOT equal to the viewport height found above.
         newHeight = docEl.scrollHeight !== vh ?
                      docEl.scrollHeight : docEl.offsetHeight;
+      } else if (navigator.userAgent.indexOf('AppleWebKit') >= 0) {
+        // In Webkit:
+        // Property scrollHeight and offsetHeight will only increase in value.
+        // This will incorrectly calculate reduced height of a gadget
+        // (ie: made smaller). These properties also do not account margin and
+        // padding size of an element.
+        newHeight = getHeightForWebkit();
       } else {
         // In Quirks mode:
         // documentElement.clientHeight is equal to 
documentElement.offsetHeight


Reply via email to