Reviewers: rjrjr,

Description:
Handle SafeHtml as return type in ui:text

Review by: rj...@google.com

Please review this at http://gwt-code-reviews.appspot.com/1409802/

Affected files:
M user/src/com/google/gwt/uibinder/elementparsers/ComputedAttributeInterpreter.java
  M user/src/com/google/gwt/uibinder/elementparsers/UiTextInterpreter.java
  M user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java
  M user/src/com/google/gwt/uibinder/rebind/XMLAttribute.java
  M user/src/com/google/gwt/uibinder/rebind/XMLElement.java


Index: user/src/com/google/gwt/uibinder/elementparsers/ComputedAttributeInterpreter.java
===================================================================
--- user/src/com/google/gwt/uibinder/elementparsers/ComputedAttributeInterpreter.java (revision 9906) +++ user/src/com/google/gwt/uibinder/elementparsers/ComputedAttributeInterpreter.java (working copy)
@@ -58,7 +58,16 @@
       }

       if (att.hasComputedValue()) {
- String attToken = writer.tokenForStringExpression(att.consumeStringValue());
+        String attToken;
+ String returnValue = att.computedReturnValue(writer.getFieldManager(),
+            writer.getLogger());
+
+        if (returnValue.equals("SafeHtml")) {
+ attToken = writer.tokenForSafeHtmlMethod(att.consumeSafeHtmlValue());
+        } else {
+ attToken = writer.tokenForStringExpression(att.consumeStringValue());
+        }
+
         attNameToToken.put(att.getName(), attToken);
       } else {
         /*
@@ -77,5 +86,5 @@

     // Return null because we don't want to replace the dom element
     return null;
-  }
+  }
 }
Index: user/src/com/google/gwt/uibinder/elementparsers/UiTextInterpreter.java
===================================================================
--- user/src/com/google/gwt/uibinder/elementparsers/UiTextInterpreter.java (revision 9906) +++ user/src/com/google/gwt/uibinder/elementparsers/UiTextInterpreter.java (working copy)
@@ -44,8 +44,9 @@
       if (fieldRef == null) {
         logger.die(elem, "Attribute 'from' not found.");
       }
+
       return "\" + " + fieldRef + " + \"";
-    }
+   }
     return null;
   }
 }
Index: user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java
===================================================================
--- user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java (revision 9906) +++ user/src/com/google/gwt/uibinder/rebind/UiBinderWriter.java (working copy)
@@ -575,6 +575,13 @@
   }

   /**
+   * Returns the fieldManager.
+   */
+  public FieldManager getFieldManager() {
+    return fieldManager;
+  }
+
+  /**
* Returns the logger, at least until we get get it handed off to parsers via
    * constructor args.
    */
@@ -673,7 +680,7 @@
     setFieldInitializer(fieldName, formatCode("new %s(%s)",
         type.getQualifiedSourceName(), asCommaSeparatedList(args)));
   }
-
+
   /**
* Like {@link #tokenForStringExpression}, but used for runtime expressions * that we trust to be safe to interpret at runtime as HTML without escaping,
@@ -691,6 +698,18 @@
     return token;
   }

+  /**
+ * Like {@link #tokenForStringExpression}, but used for runtime methods that + * return a value of type {@link com.google.gwt.safehtml.shared.SafeHtml SafeHtml}.
+   *
+   * @param expression
+   */
+  public String tokenForSafeHtmlMethod(String expression) {
+    String token =  tokenator.nextToken(expression);
+    htmlTemplates.noteSafeConstant(expression);
+    return token;
+  }
+
   /**
    * Returns a string token that can be used in place the given expression
    * inside any string literals. Before the generated code is written, the
Index: user/src/com/google/gwt/uibinder/rebind/XMLAttribute.java
===================================================================
--- user/src/com/google/gwt/uibinder/rebind/XMLAttribute.java (revision 9906)
+++ user/src/com/google/gwt/uibinder/rebind/XMLAttribute.java   (working copy)
@@ -15,7 +15,9 @@
  */
 package com.google.gwt.uibinder.rebind;

+import com.google.gwt.core.ext.TreeLogger.Type;
 import com.google.gwt.core.ext.UnableToCompleteException;
+import com.google.gwt.core.ext.typeinfo.JType;
 import com.google.gwt.uibinder.attributeparsers.FieldReferenceConverter;

 import org.w3c.dom.Attr;
@@ -33,8 +35,23 @@
     this.w3cAttr = attr;
   }

+  public String computedReturnValue(FieldManager fieldManager,
+      MortalLogger logger) {
+    String attrValue = w3cAttr.getValue();
+ String[] parts = attrValue.substring(1, attrValue.length() - 1).split("\\.");
+    JType type = fieldManager.lookup(parts[0]).getReturnType(parts,
+        new MonitoredLogger(logger.getTreeLogger().branch(Type.TRACE,
+            "retrieving return type for " + attrValue)));
+
+    return type.getSimpleSourceName();
+  }
+
   public String consumeRawValue() {
     return xmlElem.consumeRawAttribute(w3cAttr.getName());
+  }
+
+  public String consumeSafeHtmlValue() throws UnableToCompleteException {
+    return xmlElem.consumeSafeHtmlAttribute(w3cAttr.getName());
   }

   public String consumeStringValue() throws UnableToCompleteException {
Index: user/src/com/google/gwt/uibinder/rebind/XMLElement.java
===================================================================
--- user/src/com/google/gwt/uibinder/rebind/XMLElement.java     (revision 9906)
+++ user/src/com/google/gwt/uibinder/rebind/XMLElement.java     (working copy)
@@ -22,6 +22,7 @@
 import com.google.gwt.core.ext.typeinfo.TypeOracleException;
 import com.google.gwt.dom.client.Style.Unit;
 import com.google.gwt.resources.client.ImageResource;
+import com.google.gwt.safehtml.shared.SafeHtml;
 import com.google.gwt.uibinder.attributeparsers.AttributeParser;
 import com.google.gwt.uibinder.attributeparsers.AttributeParsers;

@@ -145,6 +146,7 @@
   private JType doubleType;
   private JType intType;
   private JType stringType;
+  private JType safeHtmlType;

   {
     // from com/google/gxp/compiler/schema/html.xml
@@ -649,6 +651,20 @@
   }

   /**
+   * Convenience method for parsing the named attribute as a
+ * {@link com.google.gwt.safehtml.shared.SafeHtml SafeHtml} value or reference.
+   *
+   * @return an expression that will evaluate to a
+   * {@link com.google.gwt.safehtml.shared.SafeHtml SafeHtml} value in
+   * the generated code, or null if there is no such attribute
+   * @throws UnableToCompleteException on unparseable value
+   */
+  public String consumeSafeHtmlAttribute(String name)
+      throws UnableToCompleteException {
+    return consumeAttribute(name, getSafeHtmlType());
+  }
+
+  /**
* Consumes a single child element, ignoring any text nodes and throwing an * exception if no child is found, or more than one child element is found.
    *
@@ -698,7 +714,7 @@
     designTime.putAttribute(this, name, strings);
     return strings;
   }
-
+
   /**
* Convenience method for parsing the named attribute as a String value or
    * reference.
@@ -933,6 +949,13 @@
     return rtn;
   }

+  private JType getSafeHtmlType() {
+    if (safeHtmlType == null) {
+      safeHtmlType = oracle.findType(SafeHtml.class.getName());
+    }
+    return safeHtmlType;
+  }
+
   private JType getStringType() {
     if (stringType == null) {
       stringType = oracle.findType(String.class.getCanonicalName());


--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to