Author: [EMAIL PROTECTED]
Date: Thu Sep 25 06:25:15 2008
New Revision: 3681

Modified:
    trunk/user/src/com/google/gwt/user/client/impl/HistoryImplSafari.java
    trunk/user/test/com/google/gwt/user/client/ui/HistoryTest.java

Log:
Fixes Safari history issues stemming from setting location.hash to the empty
string.

Patch by: jgw
Review by: jat
Issues: 2905, 2909



Modified:  
trunk/user/src/com/google/gwt/user/client/impl/HistoryImplSafari.java
==============================================================================
--- trunk/user/src/com/google/gwt/user/client/impl/HistoryImplSafari.java       
 
(original)
+++ trunk/user/src/com/google/gwt/user/client/impl/HistoryImplSafari.java       
 
Thu Sep 25 06:25:15 2008
@@ -59,9 +59,9 @@
    @Override
    protected void nativeUpdate(String historyToken) {
      if (isOldSafari) {
-      nativeUpdateImpl(historyToken);
+      oldNativeUpdate(historyToken);
      } else {
-      super.nativeUpdate(historyToken);
+      newNativeUpdate(historyToken);
      }
    }

@@ -79,7 +79,16 @@
       
@com.google.gwt.user.client.impl.HistoryImpl::fireHistoryChangedImpl(Ljava/lang/String;)($wnd.__gwt_historyToken);
    }-*/;

-  private native void nativeUpdateImpl(String historyToken) /*-{
+  private native void newNativeUpdate(String historyToken) /*-{
+    // Safari gets into a weird state (issue 2905) when setting the hash
+    // component of the url to an empty string, but works fine as long as  
you
+    // at least add a '#' to the end of the url. So we get around this by
+    // recreating the url, rather than just setting location.hash.
+    $wnd.location = $wnd.location.href.split('#')[0] + '#' +
+       
[EMAIL PROTECTED]::encodeFragment(Ljava/lang/String;)(historyToken);
+  }-*/;
+
+  private native void oldNativeUpdate(String historyToken) /*-{
      // Use a bizarre meta refresh trick to update the url's hash, without
      // creating a history entry.
      var meta = $doc.createElement('meta');

Modified: trunk/user/test/com/google/gwt/user/client/ui/HistoryTest.java
==============================================================================
--- trunk/user/test/com/google/gwt/user/client/ui/HistoryTest.java       
(original)
+++ trunk/user/test/com/google/gwt/user/client/ui/HistoryTest.java      Thu Sep 
 
25 06:25:15 2008
@@ -15,13 +15,15 @@
   */
  package com.google.gwt.user.client.ui;

+import java.util.ArrayList;
+
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.Element;
  import com.google.gwt.junit.client.GWTTestCase;
  import com.google.gwt.user.client.History;
  import com.google.gwt.user.client.HistoryListener;
  import com.google.gwt.user.client.Timer;

-import java.util.ArrayList;
-
  /**
   * Tests for the history system.
   *
@@ -275,6 +277,33 @@
        }
      });
      History.newItem(token);
+  }
+
+  /**
+   * Test that using an empty history token works properly. There have been
+   * problems (see issue 2905) with this in the past on Safari.
+   */
+  public void testEmptyHistoryToken() {
+    final ArrayList<Object> counter = new ArrayList<Object>();
+
+    addHistoryListenerImpl(new HistoryListener() {
+      public void onHistoryChanged(String historyToken) {
+        counter.add(new Object());
+        assertFalse("Browser is borked by empty history token",  
isBorked());
+      }
+    });
+
+    History.newItem("x");
+    History.newItem("");
+
+    assertEquals("Expected two history events", 2, counter.size());
+  }
+
+  // Used by testEmptyHistoryToken() to catch a bizarre failure mode on  
Safari.
+  private static boolean isBorked() {
+    Element e = Document.get().createDivElement();
+    e.setInnerHTML("string");
+    return e.getInnerHTML().length() == 0;
    }

    @Override

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

Reply via email to