From 5976bef77527cdb9fcf9f4830061266936ab092b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20P=C3=A5lsson?= Date: Tue, 4 Mar 2014 10:41:29 +0100 Subject: Improve detection of text input elements --- browser/browserview.cpp | 16 +++++++++++++--- browser/browserview.h | 18 +++++++++++++----- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/browser/browserview.cpp b/browser/browserview.cpp index 947efb0..af32053 100644 --- a/browser/browserview.cpp +++ b/browser/browserview.cpp @@ -103,9 +103,19 @@ void BrowserView::loadFinished(bool ok) m_webview.page()->mainFrame()->addToJavaScriptWindowObject("inputHandler", &m_inputHandler); m_webview.page()->mainFrame()->evaluateJavaScript( - "document.addEventListener('focus', function(e){" - " window.inputHandler.setCurrentFocus(e.target);" - "}, true);"); + "(function() {" + "var pocCurrentElement = null;" + "document.addEventListener('focus', function(e){" + " if (pocCurrentElement != e.target) {" + " window.inputHandler.setCurrentFocus(e.target);" + " pocCurrentElement = e.target;" + " }" + "}, true);" + "document.addEventListener('focusout', function(e){" + " pocCurrentElement = e.relatedTarget;" + "}, true);" + "})()" + ); m_webview.page()->mainFrame()->evaluateJavaScript( "document.addEventListener('scroll', function(){" diff --git a/browser/browserview.h b/browser/browserview.h index 17e0fde..edd3659 100644 --- a/browser/browserview.h +++ b/browser/browserview.h @@ -37,16 +37,23 @@ public: class InputHandler : public QObject { Q_OBJECT +public: + InputHandler () : m_textElements (new QStringList()) { + *m_textElements << "date" << "datetime" << "datetime-local" << "email" + << "month" << "number" << "password" << "range" << "tel" << "text" + << "time" << "url" << "week"; + } public slots: void setScrollPosition (const int &x, const int &y) { emit onScroll ((uint)x,(uint)y); } void setCurrentFocus (const QWebElement &elem) { - // Check whether this element was already selected - if (elem == m_elem) - return; - - if (elem.tagName().compare("INPUT", Qt::CaseInsensitive) == 0){ + QString attrType = elem.attribute("type", "text"); + if ((elem.tagName().compare("INPUT", Qt::CaseInsensitive) == 0 && + m_textElements->contains(attrType)) + || + elem.tagName().compare("TEXTAREA", Qt::CaseInsensitive) == 0) + { emit onInputText (elem.attribute("name"), elem.attribute("value"), elem.attribute("type", "0").toInt(), elem.attribute("maxlength", "0").toInt(), @@ -85,6 +92,7 @@ signals: private: QWebElement m_elem; + QStringList *m_textElements; }; class BrowserView : public QGraphicsView -- cgit v1.2.1