You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by le...@google.com on 2008/10/06 20:39:42 UTC

Inlining of ${Msg} values at compile time, whitespace trimming.

Reviewers: shindig-dev,

Description:
Any ${Msg.foo} reference will be inlined into the template during
compilation, which will result in all the ${} style markup in the
message body to be properly processed.

So if there is a message that contains "Hello ${Viewer.displayName}", it
will work as expected. This will allow more flexibility in
internationalization.

Additionally, added trimming of string contents of template source
obtained from DOM nodes.

JIRA Issue: https://issues.apache.org/jira/browse/SHINDIG-645

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

Affected files:
   features/opensocial-templates/base.js
   features/opensocial-templates/compiler.js
   features/opensocial-templates/util.js


Index: features/opensocial-templates/base.js
===================================================================
--- features/opensocial-templates/base.js	(revision 702173)
+++ features/opensocial-templates/base.js	(working copy)
@@ -108,6 +108,7 @@

    opt_id = opt_id || node.id;
    var src = node.value || node.innerHTML;
+  src = os.trim(src);
    var template = os.compileTemplateString(src, opt_id);
    return template;
  };
@@ -119,7 +120,7 @@
   * @return {os.Template} A compiled Template object.
   */
  os.compileTemplateString = function(src, opt_id) {
-  var src = os.prepareTemplateXML_(src);
+  src = os.prepareTemplateXML_(src);
    var doc = os.parseXML_(src);
    return os.compileXMLDoc(doc, opt_id);
  };
@@ -197,14 +198,6 @@
  };

  /**
- * A convenience function for identifier resolver to be able to use
- * getPrefMessage() both as ${Msg.foo} and ${Msg('foo')}.
- */
-os.getPrefMessage.get = function(key) {
-  return os.getPrefMessage(key);
-};
-
-/**
   * Globally disallowed dynamic attributes. These are the attributes where
   * ${} notation will be ignored reguardless of the tag.
   */
Index: features/opensocial-templates/compiler.js
===================================================================
--- features/opensocial-templates/compiler.js	(revision 702173)
+++ features/opensocial-templates/compiler.js	(working copy)
@@ -518,7 +518,6 @@
    var context = JsEvalContext.create(data);
    context.setVariable(os.VAR_callbacks, []);
    context.setVariable(os.VAR_identifierresolver, os.getFromContext);
-  context.setVariable(os.VAR_msg, os.getPrefMessage);
    if (opt_globals) {
      for (var global in opt_globals) {
        context.setVariable(global, opt_globals[global]);
@@ -853,6 +852,17 @@
    if (!os.canStartIdentifier(token.charAt(0))) {
      return token;
    }
+
+  // If the identifier is accessing a message
+  // (and gadget messages are obtaibable), inline it here.
+  // TODO: This is inefficient for times when the message contains no  
markup -
+  // such cases should be optimized.
+  if (token.substring(0, os.VAR_msg.length) == os.VAR_msg &&  
os.gadgetPrefs_) {
+    var key = token.split(".")[1];
+    var msg = os.getPrefMessage(key) || '';
+    return os.parseAttribute_(msg) || os.transformLiteral_(msg);
+  }
+
    var identifiers = os.tokenToIdentifiers(token);
    var parts = false;
    var buffer = [];
Index: features/opensocial-templates/util.js
===================================================================
--- features/opensocial-templates/util.js	(revision 702173)
+++ features/opensocial-templates/util.js	(working copy)
@@ -20,7 +20,17 @@
   * @fileoverview Provides various utility functions used throughout the  
library.
   */

+
  /**
+ * Trims leading and trailing whitespace from a string.
+ * @param {string} string The input string.
+ * @returns {string} Input with leading and trailing whitespace removed.
+ */
+os.trim = function(string) {
+  return string.replace(/^\s+/, '').replace(/\s+$/, '');
+};
+
+/**
   * Checks whether or not a given character is alpha-numeric.
   * @param {string} ch Character to check.
   * @return {boolean} This character is alpha-numeric.