From 9ba13ba989c5e910a6bc684dbe5460f7a4953bed Mon Sep 17 00:00:00 2001 From: Ivan De Marino Date: Sun, 18 Nov 2012 00:08:54 +0000 Subject: [PATCH] Implementing "goBack", "goForward" and "go". Completing work for [Issue #808](http://code.google.com/p/phantomjs/issues/detail?id=808). --- src/webpage.cpp | 26 ++++++++++++++++++++++---- src/webpage.h | 19 +++++++++++++++---- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/webpage.cpp b/src/webpage.cpp index 7cabd1b1..ec1bfcfe 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -425,10 +426,10 @@ bool WebPage::canGoBack() return m_customWebPage->history()->canGoBack(); } -bool WebPage::back() +bool WebPage::goBack() { if (canGoBack()) { - m_customWebPage->triggerAction(QWebPage::Back); + m_customWebPage->history()->back(); return true; } return false; @@ -439,15 +440,32 @@ bool WebPage::canGoForward() return m_customWebPage->history()->canGoForward(); } -bool WebPage::forward() +bool WebPage::goForward() { if (canGoForward()) { - m_customWebPage->triggerAction(QWebPage::Forward); + m_customWebPage->history()->forward(); return true; } return false; } +bool WebPage::go(int historyItemRelativeIndex) +{ + // Convert the relative index to absolute + int historyItemIndex = m_customWebPage->history()->currentItemIndex() + historyItemRelativeIndex; + + // Fetch the right item from the history + QWebHistoryItem historyItem = m_customWebPage->history()->itemAt(historyItemIndex); + + // Go to the history item, if it's valid + if (historyItem.isValid()) { + m_customWebPage->history()->goToItem(historyItem); + return true; + } + + return false; +} + void WebPage::reload() { m_customWebPage->triggerAction(QWebPage::Reload); diff --git a/src/webpage.h b/src/webpage.h index 6a745bb9..e9cb406a 100644 --- a/src/webpage.h +++ b/src/webpage.h @@ -412,10 +412,10 @@ public slots: bool canGoBack(); /** * Goes back in the Navigation History - * @brief back + * @brief goBack * @return "true" if it does go back in the Navigation History, "false" otherwise */ - bool back(); + bool goBack(); /** * Checks if this Page can go forward in the Navigation History (i.e. next URL) * @brief canGoForward @@ -424,10 +424,21 @@ public slots: bool canGoForward(); /** * Goes forward in the Navigation History - * @brief forward + * @brief goForward * @return "true" if it does go forward in the Navigation History, "false" otherwise */ - bool forward(); + bool goForward(); + /** + * Go to the page identified by its relative location to the current page. + * For example '-1' for the previous page or 1 for the next page. + * + * Modelled after JavaScript "window.go(num)" method: + * {@see https://developer.mozilla.org/en-US/docs/DOM/window.history#Syntax}. + * @brief go + * @param historyRelativeIndex + * @return "true" if it does go forward/backgward in the Navigation History, "false" otherwise + */ + bool go(int historyRelativeIndex); /** * Reload current page * @brief reload