Reviewers: shindig-dev,

Description:
For OpenSocial Templates prototype:

IE interprets whitespace literally in calls to
document.createTextNode(). Special handling existed for this before, and
this patch fixes some issues with it. In particular, trailing/leading
spaces will be stripped off of <os:Render/>, and whitespace between tags
will now be properly detected.

Also added support for gadgets.util.registerOnLoadHandler() when it is
available.

JIRA issue: https://issues.apache.org/jira/browse/SHINDIG-649

Please review this at http://codereview.appspot.com/7450

Affected files:
  features/opensocial-templates/compiler.js
  features/opensocial-templates/container.js
  features/opensocial-templates/namespaces.js


Index: features/opensocial-templates/compiler.js
===================================================================
--- features/opensocial-templates/compiler.js   (revision 703205)
+++ features/opensocial-templates/compiler.js   (working copy)
@@ -50,7 +50,8 @@
     if (child.nodeType == DOM_ELEMENT_NODE) {
       nodes.push(os.compileNode_(child));
     } else if (child.nodeType == DOM_TEXT_NODE) {
-      if (!child.nodeValue.match(os.regExps_.onlyWhitespace)) {
+      if (child != node.firstChild ||
+          !child.nodeValue.match(os.regExps_.onlyWhitespace)) {
         var compiled = os.breakTextNode_(child);
         for (var i = 0; i < compiled.length; i++) {
           nodes.push(compiled[i]);
@@ -453,12 +454,12 @@
         var compiledChild = os.compileNode_(child);
         if (compiledChild) {
           if (!compiledChild.tagName &&
-            typeof(compiledChild.length) == 'number') {
+              typeof(compiledChild.length) == 'number') {
             for (var i = 0; i < compiledChild.length; i++) {
               output.appendChild(compiledChild[i]);
             }
           } else {
-            // If inserting a TRw into a TABLE, inject a TBODY element.
+            // If inserting a TR into a TABLE, inject a TBODY element.
if (compiledChild.tagName == 'TR' && output.tagName == 'TABLE') {
               var lastEl = output.lastChild;
               while (lastEl && lastEl.nodeType != DOM_ELEMENT_NODE &&
@@ -497,7 +498,7 @@
  */
 os.prepareTemplateXML_ = function(templateSrc) {
   var namespaces = os.getRequiredNamespaces(templateSrc);
-  return "<!DOCTYPE root [" + os.ENTITIES + "]><root " +
+ return "<!DOCTYPE root [" + os.ENTITIES + "]><root xml:space=\"preserve\"" +
       namespaces + ">" + templateSrc + "</root>";;
 };

Index: features/opensocial-templates/container.js
===================================================================
--- features/opensocial-templates/container.js  (revision 703205)
+++ features/opensocial-templates/container.js  (working copy)
@@ -67,7 +67,10 @@
  * @private
  */
 os.Container.registerDomLoadListener_ = function() {
-  if (navigator.product == 'Gecko') {
+  var gadgets = window['gadgets'];
+  if (gadgets && gadgets.util) {
+    gadgets.util.registerOnLoadHandler(os.Container.onDomLoad_);
+  } else if (navigator.product == 'Gecko') {
window.addEventListener("DOMContentLoaded", os.Container.onDomLoad_, false);
   } if (window.addEventListener) {
     window.addEventListener("load", os.Container.onDomLoad_, false);
Index: features/opensocial-templates/namespaces.js
===================================================================
--- features/opensocial-templates/namespaces.js (revision 703205)
+++ features/opensocial-templates/namespaces.js (working copy)
@@ -155,6 +155,21 @@
       }
       result = resultArray;
     }
+
+    // Trim away leading and trailing spaces on IE, which interprets them
+    // literally.
+    if (os.isIe) {
+      for (var i = 0; i < result.length; i++) {
+        if (result[i].nodeType == DOM_TEXT_NODE) {
+          var trimmed = os.trimWhitespaceForIE_(
+              result[i].nodeValue, (i == 0), (i == result.length - 1));
+          if (trimmed != result[i].nodeValue) {
+            result[i].parentNode.removeChild(result[i]);
+            result[i] = document.createTextNode(trimmed);
+          }
+        }
+      }
+    }

     return result;
   }


Reply via email to