WebBrowser: Fixed some sender() related issues.

Sun, 04 Mar 2018 14:52:32 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 04 Mar 2018 14:52:32 +0100
changeset 6174
bb2989d750ce
parent 6173
cb1488b062a9
child 6175
330ffed830a4

WebBrowser: Fixed some sender() related issues.

WebBrowser/OpenSearch/OpenSearchManager.py file | annotate | diff | comparison | revisions
WebBrowser/Tools/Scripts.py file | annotate | diff | comparison | revisions
WebBrowser/Tools/WebHitTestResult.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserPage.py file | annotate | diff | comparison | revisions
WebBrowser/WebBrowserWebSearchWidget.py file | annotate | diff | comparison | revisions
--- a/WebBrowser/OpenSearch/OpenSearchManager.py	Sun Mar 04 13:04:58 2018 +0100
+++ b/WebBrowser/OpenSearch/OpenSearchManager.py	Sun Mar 04 14:52:32 2018 +0100
@@ -480,31 +480,31 @@
         @param reply reference to the network reply
         @type QNetworkReply
         """
-        if reply.error() != QNetworkReply.NoError:
-            reply.close()
-            if reply in self.__replies:
-                self.__replies.remove(reply)
-            return
-        
-        from .OpenSearchReader import OpenSearchReader
-        reader = OpenSearchReader()
-        engine = reader.read(reply)
-        
         reply.close()
         if reply in self.__replies:
             self.__replies.remove(reply)
         
-        if not engine.isValid():
-            return
-        
-        if self.engineExists(engine.name()):
-            return
-        
-        if not self.__confirmAddition(engine):
-            return
-        
-        if not self.__addEngineByEngine(engine):
-            return
+        if reply.error() == QNetworkReply.NoError:
+            from .OpenSearchReader import OpenSearchReader
+            reader = OpenSearchReader()
+            engine = reader.read(reply)
+            
+            if not engine.isValid():
+                return
+            
+            if self.engineExists(engine.name()):
+                return
+            
+            if not self.__confirmAddition(engine):
+                return
+            
+            if not self.__addEngineByEngine(engine):
+                return
+        else:
+            # some error happened
+            from WebBrowser.WebBrowserWindow import WebBrowserWindow
+            WebBrowserWindow.getWindow().statusBar().showMessage(
+                reply.errorString(), 10000)
     
     def convertKeywordSearchToUrl(self, keywordSearch):
         """
--- a/WebBrowser/Tools/Scripts.py	Sun Mar 04 13:04:58 2018 +0100
+++ b/WebBrowser/Tools/Scripts.py	Sun Mar 04 14:52:32 2018 +0100
@@ -95,13 +95,18 @@
     """
     source = """
 (function() {
-var external = {};
-
-window.external = external;
-
-window.print = function() {
-    window.location = 'eric:PrintPage';
-};
+    var external = {};
+    external.AddSearchProvider = function(url) {
+        window.location = 'eric:AddSearchProvider?url=' + url;
+    };
+    external.IsSearchProviderInstalled = function(url) {
+        console.warn('NOT IMPLEMENTED: IsSearchProviderInstalled()');
+        return false;
+    };
+    window.external = external;
+    window.print = function() {
+        window.location = 'eric:PrintPage';
+    };
 })()"""
     
     return source
@@ -140,11 +145,11 @@
     source = """
 (function() {{
     var e = document.elementFromPoint({0}, {1});
-    if (!e || e.tagName != 'INPUT')
+    if (!e || e.tagName.toLowerCase() != 'input')
         return;
     var fe = e.parentElement;
     while (fe) {{
-        if (fe.tagName == 'FORM')
+        if (fe.tagName.toLowerCase() != 'form')
             break;
         fe = fe.parentElement;
     }}
@@ -336,10 +341,10 @@
         registerForm(document.forms[i]);
     
     var observer = new MutationObserver(function(mutations) {
-        for (var i = 0; i < mutations.length; ++i)
-            for (var j = 0; j < mutations[i].addedNodes.length; ++j)
-                if (mutations[i].addedNodes[j].tagName == 'FORM')
-                    registerForm(mutations[i].addedNodes[j]);
+        for (var mutation of mutations)
+            for (var node of mutation.addedNodes)
+                if (node.tagName && node.tagName.toLowerCase() == 'form')
+                    registerForm(node);
     });
     observer.observe(document.documentElement, {
         childList: true, subtree: true
@@ -375,8 +380,10 @@
             if (type != 'text' && type != 'password' &&
                 type != 'email')
                 continue;
-            if (input.name == key)
+            if (input.name == key) {{
                 input.value = val;
+                input.dispatchEvent(new Event('change'));
+            }}
         }}
     }}
     
--- a/WebBrowser/Tools/WebHitTestResult.py	Sun Mar 04 13:04:58 2018 +0100
+++ b/WebBrowser/Tools/WebHitTestResult.py	Sun Mar 04 14:52:32 2018 +0100
@@ -52,12 +52,14 @@
                 if (!e)
                     return;
                 function isMediaElement(e) {{
-                    return e.tagName == 'AUDIO' || e.tagName == 'VIDEO';
+                    return e.tagName.toLowerCase() == 'audio' ||
+                           e.tagName.toLowerCase() == 'video';
                 }}
                 function isEditableElement(e) {{
                     if (e.isContentEditable)
                         return true;
-                    if (e.tagName == 'INPUT' || e.tagName == 'TEXTAREA')
+                    if (e.tagName.toLowerCase() == 'input' ||
+                        e.tagName.toLowerCase() == 'textarea')
                         return e.getAttribute('readonly') != 'readonly';
                     return false;
                 }}
@@ -83,16 +85,16 @@
                 }};
                 var r = e.getBoundingClientRect();
                 res.boundingRect = [r.top, r.left, r.width, r.height];
-                if (e.tagName == 'IMG')
+                if (e.tagName.toLowerCase() == 'img')
                     res.imageUrl = e.getAttribute('src');
-                if (e.tagName == 'A') {{
+                if (e.tagName.toLowerCase() == 'a') {{
                     res.linkTitle = e.text;
                     res.linkUrl = e.getAttribute('href');
                 }}
                 while (e) {{
-                    if (res.linkTitle == '' && e.tagName == 'A')
+                    if (res.linkTitle == '' && e.tagName.toLowerCase() == 'a')
                         res.linkTitle = e.text;
-                    if (res.linkUrl == '' && e.tagName == 'A')
+                    if (res.linkUrl == '' && e.tagName.toLowerCase() == 'a')
                         res.linkUrl = e.getAttribute('href');
                     if (res.mediaUrl == '' && isMediaElement(e)) {{
                         res.mediaUrl = e.currentSrc;
--- a/WebBrowser/WebBrowserPage.py	Sun Mar 04 13:04:58 2018 +0100
+++ b/WebBrowser/WebBrowserPage.py	Sun Mar 04 14:52:32 2018 +0100
@@ -14,8 +14,8 @@
 except NameError:
     pass
 
-from PyQt5.QtCore import pyqtSlot, pyqtSignal, QUrl, QTimer, QEventLoop, \
-    QPoint, QPointF
+from PyQt5.QtCore import pyqtSlot, pyqtSignal, QUrl, QUrlQuery, QTimer, \
+    QEventLoop, QPoint, QPointF
 from PyQt5.QtGui import QDesktopServices
 from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings, \
     QWebEngineScript
@@ -86,7 +86,7 @@
         self.__printer = None
         self.__badSite = False
         
-        if qVersionTuple() == (5, 10, 0):
+        if qVersionTuple()[0:2] == (5, 10):
             self.loadProgress.connect(self.__loadProgressSlot)
         
         # Workaround for changing webchannel world inside
@@ -148,7 +148,12 @@
             return False
         
         if url.scheme() == "eric":
-            if url.path() == "PrintPage":
+            if url.path() == "AddSearchProvider":
+                query = QUrlQuery(url)
+                self.view().mainWindow().openSearchManager().addEngine(
+                    QUrl(query.queryItemValue("url")))
+                return False
+            elif url.path() == "PrintPage":
                 self.printRequested.emit()
                 return False
         
--- a/WebBrowser/WebBrowserWebSearchWidget.py	Sun Mar 04 13:04:58 2018 +0100
+++ b/WebBrowser/WebBrowserWebSearchWidget.py	Sun Mar 04 14:52:32 2018 +0100
@@ -55,7 +55,8 @@
         self.__currentEngine = ""
         
         self.__enginesMenu = QMenu(self)
-        self.__enginesMenu.triggered.connect(self.__changeCurrentEngine)
+        self.__enginesMenu.triggered.connect(
+            self.__handleEnginesMenuActionTriggered)
         
         self.__engineButton = E5LineEditButton(self)
         self.__engineButton.setMenu(self.__enginesMenu)
@@ -249,8 +250,6 @@
             engine = self.__openSearchManager.engine(engineName)
             action = OpenSearchEngineAction(engine, self.__enginesMenu)
             action.setData(engineName)
-##            action.triggered.connect(
-##                lambda: self.__changeCurrentEngine(action))
             self.__enginesMenu.addAction(action)
             
             if self.__openSearchManager.currentEngineName() == engineName:
@@ -285,38 +284,32 @@
                     self.tr("Add '{0}'").format(title))
                 action.setData(url)
                 action.setIcon(cb.icon())
-            action.triggered.connect(
-                lambda: self.__addEngineFromUrl(action))
         
         self.__enginesMenu.addSeparator()
         self.__enginesMenu.addAction(self.__mw.searchEnginesAction())
         
         if self.__recentSearches:
-            self.__enginesMenu.addAction(self.tr("Clear Recent Searches"),
-                                         self.clear)
+            act = self.__enginesMenu.addAction(
+                self.tr("Clear Recent Searches"))
+            act.setData("@@CLEAR@@")
     
-    def __changeCurrentEngine(self, action):
+    def __handleEnginesMenuActionTriggered(self, action):
         """
-        Private slot to handle the selection of a search engine.
+        Private slot to handle an action of the menu being triggered.
         
         @param action reference to the action that triggered
         @type QAction
         """
-        name = action.data()
-        self.__openSearchManager.setCurrentEngineName(name)
-    
-    def __addEngineFromUrl(self, action):
-        """
-        Private slot to add a search engine given its URL.
-        
-        @param action reference to the action that triggered
-        @type QAction
-        """
-        url = action.data()
-        if not isinstance(url, QUrl):
-            return
-        
-        self.__openSearchManager.addEngine(url)
+        actData = action.data()
+        if isinstance(actData, QUrl):
+            # add search engine
+            self.__openSearchManager.addEngine(actData)
+        elif isinstance(actData, str):
+            # engine name or special action
+            if actData == "@@CLEAR@@":
+                self.clear()
+            else:
+                self.__openSearchManager.setCurrentEngineName(actData)
     
     def __searchButtonClicked(self):
         """

eric ide

mercurial