Skip to content

Commit

Permalink
Maintain m_currentFrame as opposed to using QWebPage's currentFrame().
Browse files Browse the repository at this point in the history
Currently focused frame made available with page.focusedFrameName and page.switchToFocusedFrame.

http://code.google.com/p/phantomjs/issues/detail?id=683
  • Loading branch information
Harry Waye authored and n1k0 committed Sep 24, 2012
1 parent af07b04 commit b4696f6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 18 deletions.
53 changes: 35 additions & 18 deletions src/webpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ WebPage::WebPage(QObject *parent, const QUrl &baseUrl)
m_customWebPage = new CustomPage(this);
m_mainFrame = m_customWebPage->mainFrame();
m_mainFrame->setHtml(BLANK_HTML, baseUrl);
changeCurrentFrame(m_mainFrame);

Config *phantomCfg = Phantom::instance()->config();

Expand Down Expand Up @@ -347,7 +348,7 @@ QString WebPage::content() const

QString WebPage::frameContent() const
{
return m_customWebPage->currentFrame()->toHtml();
return m_currentFrame->toHtml();
}

void WebPage::setContent(const QString &content)
Expand All @@ -357,7 +358,7 @@ void WebPage::setContent(const QString &content)

void WebPage::setFrameContent(const QString &content)
{
m_customWebPage->currentFrame()->setHtml(content);
m_currentFrame->setHtml(content);
}

QString WebPage::url() const
Expand All @@ -367,7 +368,7 @@ QString WebPage::url() const

QString WebPage::frameUrl() const
{
return m_customWebPage->currentFrame()->url().toString();
return m_currentFrame->url().toString();
}

QString WebPage::plainText() const
Expand All @@ -377,7 +378,7 @@ QString WebPage::plainText() const

QString WebPage::framePlainText() const
{
return m_customWebPage->currentFrame()->toPlainText();
return m_currentFrame->toPlainText();
}

QString WebPage::libraryPath() const
Expand Down Expand Up @@ -518,7 +519,7 @@ QVariantMap WebPage::paperSize() const
QVariant WebPage::evaluateJavaScript(const QString &code)
{
QString function = "(" + code + ")()";
return m_customWebPage->currentFrame()->evaluateJavaScript(
return m_currentFrame->evaluateJavaScript(
function,
QString("phantomjs://webpage.evaluate()"));
}
Expand Down Expand Up @@ -964,7 +965,7 @@ QString WebPage::footer(int page, int numPages)

void WebPage::uploadFile(const QString &selector, const QString &fileName)
{
QWebElement el = m_customWebPage->currentFrame()->findFirstElement(selector);
QWebElement el = m_currentFrame->findFirstElement(selector);
if (el.isNull())
return;

Expand All @@ -973,11 +974,11 @@ void WebPage::uploadFile(const QString &selector, const QString &fileName)
}

bool WebPage::injectJs(const QString &jsFilePath) {
return Utils::injectJsInFrame(jsFilePath, m_libraryPath, m_customWebPage->currentFrame());
return Utils::injectJsInFrame(jsFilePath, m_libraryPath, m_currentFrame);
}

void WebPage::_appendScriptElement(const QString &scriptUrl) {
m_customWebPage->currentFrame()->evaluateJavaScript(QString(JS_APPEND_SCRIPT_ELEMENT).arg(scriptUrl), scriptUrl);
m_currentFrame->evaluateJavaScript(QString(JS_APPEND_SCRIPT_ELEMENT).arg(scriptUrl), scriptUrl);
}

QObject *WebPage::_getGenericCallback() {
Expand Down Expand Up @@ -1158,7 +1159,7 @@ void WebPage::setOwnsPages(const bool owns)

int WebPage::framesCount() const
{
return m_customWebPage->currentFrame()->childFrames().count();
return m_currentFrame->childFrames().count();
}

int WebPage::childFramesCount() const //< deprecated
Expand All @@ -1170,7 +1171,7 @@ QStringList WebPage::framesName() const
{
QStringList framesName;

foreach(QWebFrame *f, m_customWebPage->currentFrame()->childFrames()) {
foreach(QWebFrame *f, m_currentFrame->childFrames()) {
framesName << f->frameName();
}
return framesName;
Expand All @@ -1181,11 +1182,16 @@ QStringList WebPage::childFramesName() const //< deprecated
return this->framesName();
}

void WebPage::changeCurrentFrame(QWebFrame * const frame)
{
m_currentFrame = frame;
}

bool WebPage::switchToFrame(const QString &frameName)
{
foreach(QWebFrame * f, m_customWebPage->currentFrame()->childFrames()) {
foreach(QWebFrame * f, m_currentFrame->childFrames()) {
if (f->frameName() == frameName) {
f->setFocus();
this->changeCurrentFrame(f);
return true;
}
}
Expand All @@ -1199,9 +1205,9 @@ bool WebPage::switchToChildFrame(const QString &frameName) //< deprecated

bool WebPage::switchToFrame(const int framePosition)
{
QList<QWebFrame *> childFrames = m_customWebPage->currentFrame()->childFrames();
QList<QWebFrame *> childFrames = m_currentFrame->childFrames();
if (framePosition >= 0 && framePosition < childFrames.size()) {
childFrames.at(framePosition)->setFocus();
this->changeCurrentFrame(childFrames.at(framePosition));
return true;
}
return false;
Expand All @@ -1214,28 +1220,38 @@ bool WebPage::switchToChildFrame(const int framePosition) //< deprecated

void WebPage::switchToMainFrame()
{
m_mainFrame->setFocus();
this->changeCurrentFrame(m_mainFrame);
}

bool WebPage::switchToParentFrame()
{
if (m_customWebPage->currentFrame()->parentFrame() != NULL) {
m_customWebPage->currentFrame()->parentFrame()->setFocus();
if (m_currentFrame->parentFrame() != NULL) {
this->changeCurrentFrame(m_currentFrame->parentFrame());
return true;
}
return false;
}

void WebPage::switchToFocusedFrame()
{
this->changeCurrentFrame(m_customWebPage->currentFrame());
}

QString WebPage::frameName() const
{
return m_customWebPage->currentFrame()->frameName();
return m_currentFrame->frameName();
}

QString WebPage::currentFrameName() const //< deprecated
{
return this->frameName();
}

QString WebPage::focusedFrameName() const
{
return m_customWebPage->currentFrame()->frameName();
}

void WebPage::handleJavaScriptWindowObjectCleared()
{
// Create Callbacks Holder object, if not already present for this page
Expand Down Expand Up @@ -1288,6 +1304,7 @@ void WebPage::initCompletions()
addCompletion("switchToFrame");
addCompletion("switchToMainFrame");
addCompletion("switchToParentFrame");
addCompletion("switchToFocusedFrame");
addCompletion("addCookie");
addCompletion("deleteCookie");
addCompletion("clearCookies");
Expand Down
24 changes: 24 additions & 0 deletions src/webpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class WebPage: public REPLCompletable, public QWebFrame::PrintCallback
Q_PROPERTY(QStringList framesName READ framesName)
Q_PROPERTY(QString frameName READ frameName)
Q_PROPERTY(int framesCount READ framesCount)
Q_PROPERTY(QString focusedFrameName READ focusedFrameName)

public:
WebPage(QObject *parent, const QUrl &baseUrl = QUrl());
Expand Down Expand Up @@ -213,6 +214,13 @@ class WebPage: public REPLCompletable, public QWebFrame::PrintCallback
* @return Name of the Current Frame
*/
QString frameName() const;
/**
* Returns the currently focused Frame's name.
*
* @brief focusedFrameName
* @return Frame
*/
QString focusedFrameName() const;

public slots:
void openUrl(const QString &address, const QVariant &op, const QVariantMap &settings);
Expand Down Expand Up @@ -320,6 +328,13 @@ public slots:
* @return "true" if the Current Frame is not a Main Frame, "false" otherwise (i.e. there is no parent frame to switch to)
*/
bool switchToParentFrame();
/**
* Switches to the currently focused frame, as per QWebPage. This is the frame whose
* window element was last focus()ed, and is currently the target of key events.
*
* @brief switchToFocusedFrame
*/
void switchToFocusedFrame();
/**
* Returns the name of the Current Frame (if it has one)
*
Expand Down Expand Up @@ -401,6 +416,14 @@ private slots:
void applySettings(const QVariantMap &defaultSettings);
QString userAgent() const;

/**
* Switches focus from the Current Frame to the Child Frame, identified by `frame`.
*
* @brief changeCurrentFrame
* @param frame The Child frame
*/
void changeCurrentFrame(QWebFrame * const frame);

bool javaScriptConfirm(const QString &msg);
bool javaScriptPrompt(const QString &msg, const QString &defaultValue, QString *result);

Expand All @@ -410,6 +433,7 @@ private slots:
CustomPage *m_customWebPage;
NetworkAccessManager *m_networkAccessManager;
QWebFrame *m_mainFrame;
QWebFrame *m_currentFrame;
QRect m_clipRect;
QPoint m_scrollPosition;
QVariantMap m_paperSize; // For PDF output via render()
Expand Down
40 changes: 40 additions & 0 deletions test/webpage-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,46 @@ describe("WebPage switch frame of execution", function(){
expect(p.framesCount).toEqual(3);
expect(p.framesName).toEqual(["frame2-1", "frame2-2", "frame2-3"]);
});

it("should have top level as focused frame", function(){
expect(p.focusedFrameName).toEqual("");
});

it("should move focus to level 1 frame", function(){
p.evaluate(function(){
window.focus();
});
expect(p.focusedFrameName).toEqual("frame2");
});

it("should move focus to level 2 frame", function(){
expect(p.switchToFrame("frame2-1")).toBeTruthy();
p.evaluate(function(){
window.focus();
});
expect(p.focusedFrameName).toEqual("frame2-1");
});

it("should move focus back to main frame", function(){
expect(p.switchToMainFrame()).toBeUndefined();
p.evaluate(function(){
window.focus();
});
expect(p.focusedFrameName).toEqual("");
});

it("should maintain focus but move current frame", function(){
p.evaluate(function(){
window.frames[0].focus();
});
expect(p.focusedFrameName).toEqual("frame1");
expect(p.frameName).toEqual("");
});

it("should change current frame to focused frame", function(){
expect(p.switchToFocusedFrame()).toBeUndefined();
expect(p.frameName).toEqual("frame1");
});
});

describe("WebPage opening and closing of windows/child-pages", function(){
Expand Down

0 comments on commit b4696f6

Please sign in to comment.