popstate is fired at the wrong time on load
https://bugs.webkit.org/show_bug.cgi?id=94265

Patch by Antoine Quint <graouts@apple.com> on 2015-10-02
Reviewed by Darin Adler.

Source/WebCore:

Don't fire popstate event on initial document load

According to the specification [1], a popstate event should only be fired
when the document has a "last entry" and the entry being navigated to isn't
it.  A document is created without a "last entry" and gets one just after
this check when it is first navigated to, so a popstate should be fired any
time a document is navigated to except for the first time after it has been
created.

Patch adapted from work by jl@opera.com on Blink [2].

[1] http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#traverse-the-history (step 12-14 in particular)
[2] https://src.chromium.org/viewvc/blink?revision=165221&view=revision

* dom/Document.cpp:
(WebCore::Document::implicitClose):

Source/WebKit2:

Ensure we have a valid page before trying to get to its drawingArea as this could lead
to a crash as observed with fast/loader/stateobjects/pushstate-object-types.html.

* WebProcess/WebCoreSupport/WebFrameLoaderClient.cpp:
(WebKit::WebFrameLoaderClient::restoreViewState):

LayoutTests:

Updating tests that relied on a "popstate" event being fired at page load.

* fast/history/same-document-iframes-changing-fragment-expected.txt:
* fast/history/same-document-iframes-changing-pushstate-expected.txt:
* fast/loader/javascript-url-iframe-remove-on-navigate.html:
* fast/loader/stateobjects/document-destroyed-navigate-back-with-fragment-scroll.html:
* fast/loader/stateobjects/document-destroyed-navigate-back.html:
* fast/loader/stateobjects/popstate-after-load-complete-addeventlistener.html:
* fast/loader/stateobjects/popstate-after-load-complete-body-attribute.html:
* fast/loader/stateobjects/popstate-after-load-complete-body-inline-attribute.html:
* fast/loader/stateobjects/popstate-after-load-complete-window-attribute.html:
* fast/loader/stateobjects/popstate-fires-on-history-traversal-expected.txt:
* fast/loader/stateobjects/popstate-fires-on-history-traversal.html:
* fast/loader/stateobjects/popstate-fires-with-page-cache-expected.txt:
* fast/loader/stateobjects/popstate-fires-with-page-cache.html:
* fast/loader/stateobjects/pushstate-object-types.html:
* fast/loader/stateobjects/pushstate-then-replacestate.html:
* fast/loader/stateobjects/pushstate-with-fragment-urls-and-hashchange.html:
* fast/loader/stateobjects/replacestate-then-pushstate.html:
* fast/loader/stateobjects/resources/popstate-fires-with-page-cache-1.html:
* fast/loader/stateobjects/resources/popstate-fires-with-page-cache-2.html:
* fast/loader/stateobjects/resources/replacestate-in-iframe-window-child.html:
* http/tests/history/popstate-fires-with-pending-requests.html:
* http/tests/navigation/redirect-on-back-updates-history-item-expected.txt:
* http/tests/navigation/redirect-on-reload-updates-history-item-expected.txt:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@190505 268f45cc-cd09-0410-ab3c-d52691b4dbfc
28 files changed