Fixes RichTextArea's background color and font size on various versions of Safari.

Issue: 1507, 1508
Patch by: jgw
Review by: knorton (desk check)


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1328 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplSafari.java b/user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplSafari.java
index 556eac1..ae38a21 100644
--- a/user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplSafari.java
+++ b/user/src/com/google/gwt/user/client/ui/impl/RichTextAreaImplSafari.java
@@ -27,23 +27,50 @@
       "medium", "xx-small", "x-small", "small", "medium", "large", "x-large",
       "xx-large"};
 
-  public Element createElement() {
-    Element elem = super.createElement();
+  private static int webKitVersion = getWebKitVersion();
 
-    // Use this opportunity to check if this version of Safari has full rich
-    // text support or not.
-    capabilityTest(elem);
-    return elem;
+  /**
+   * WebKit v420 began suppporting full rich text editing.
+   */
+  private static boolean extendedEditingSupported = (webKitVersion >= 420);
+
+  /**
+   * WebKit v420 changed BackColor to HiliteColor.
+   */
+  private static boolean useHiliteColor = (webKitVersion >= 420);
+
+  /**
+   * WebKit version up to *and including* 420 require CSS font-size values
+   * (e.g. 'medium', 'x-large') rather than size numbers. All subsequent
+   * versions use size numbers like other browsers.
+   */
+  private static boolean oldSchoolSizeValues = (webKitVersion <= 420);
+
+  private static native int getWebKitVersion() /*-{
+    var exp = / AppleWebKit\/([\d]+)/;
+    var result = exp.exec(navigator.userAgent);
+    if (result) {
+      var version = parseInt(result[1]);
+      if (version) {
+        return version;
+      }
+    }
+
+    // Intentionally conservative fallback.
+    return 0;
+  }-*/;;
+
+  public Element createElement() {
+    return super.createElement();
   }
 
   public native boolean isBold() /*-{
     return !!this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__gwt_isBold;
   }-*/;
 
-  public native boolean isExtendedEditingSupported() /*-{
-    // __gwt_fullSupport is set in testCapability().
-    return this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__gwt_fullSupport;
-  }-*/;
+  public boolean isExtendedEditingSupported() {
+    return extendedEditingSupported;
+  }
 
   public native boolean isItalic() /*-{
     return !!this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__gwt_isItalic;
@@ -53,6 +80,14 @@
     return !!this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem.__gwt_isUnderlined;
   }-*/;
 
+  public void setBackColor(String color) {
+    if (useHiliteColor) {
+      execCommand("HiliteColor", color);
+    } else {
+      super.setBackColor(color);
+    }
+  }
+
   public native void setFocus(boolean focused) /*-{
     // Safari needs the *iframe* focused, not its window.
     var elem = this.@com.google.gwt.user.client.ui.impl.RichTextAreaImpl::elem;
@@ -67,10 +102,15 @@
   }-*/;
 
   public void setFontSize(FontSize fontSize) {
-    // Safari only accepts css-style 'small, medium, large, etc' values.
-    int number = fontSize.getNumber();
-    if ((number >= 0) && (number <= 7)) {
-      execCommand("FontSize", sizeNumberCSSValues[number]);
+    if (oldSchoolSizeValues) {
+      // Safari2 only accepts css-style 'small, medium, large, etc' values.
+      // Setting these doesn't seem to hurt Safari3.
+      int number = fontSize.getNumber();
+      if ((number >= 0) && (number <= 7)) {
+        execCommand("FontSize", sizeNumberCSSValues[number]);
+      }
+    } else {
+      super.setFontSize(fontSize);
     }
   }
 
@@ -172,8 +212,4 @@
     elem.onfocus = null;
     elem.onblur = null;
   }-*/;
-
-  private native void capabilityTest(Element elem) /*-{
-    elem.__gwt_fullSupport = $doc.queryCommandSupported('insertimage');
-  }-*/;
 }