Skip to content

Commit 1617257

Browse files
authored
Merge pull request #341 from ales-erjavec/corpus-webview-js
[FIX] owcorpusviewer: Avoid 'synchronous' evalJS, use 'runJavaScript' instead
2 parents ec1745a + 5589bb7 commit 1617257

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

orangecontrib/text/widgets/owcorpusviewer.py

+25-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import sre_constants
44
from itertools import chain
55

6-
from AnyQt.QtCore import Qt, QUrl, QItemSelection, QItemSelectionModel, QItemSelectionRange
6+
from AnyQt.QtCore import (
7+
Qt, QUrl, QItemSelection, QItemSelectionModel, QItemSelectionRange,
8+
pyqtSlot as Slot
9+
)
10+
711
from AnyQt.QtGui import QStandardItemModel, QStandardItem
812
from AnyQt.QtWidgets import (QListView, QSizePolicy, QTableView,
913
QAbstractItemView, QHeaderView, QSplitter,
@@ -115,6 +119,7 @@ def __init__(self):
115119

116120
# Document contents
117121
self.doc_webview = gui.WebviewWidget(self.splitter, debug=False)
122+
self.doc_webview.loadFinished.connect(self.highlight_docs)
118123

119124
self.mainArea.layout().addWidget(self.splitter)
120125

@@ -222,6 +227,12 @@ def show_docs(self):
222227
<!doctype html>
223228
<html>
224229
<head>
230+
<script type="text/javascript" src="resources/jquery-3.1.1.min.js">
231+
</script>
232+
<script type="text/javascript" src="resources/jquery.mark.min.js">
233+
</script>
234+
<script type="text/javascript" src="resources/highlighter.js">
235+
</script>
225236
<meta charset='utf-8'>
226237
<style>
227238
@@ -306,17 +317,8 @@ def show_docs(self):
306317
token) for token in tokens[row_ind]))
307318

308319
html += '</table>'
309-
310-
# QUrl is a workaround to allow local resources
311-
# https://bugreports.qt.io/browse/QTBUG-55902?focusedCommentId=335945
312-
self.doc_webview.setHtml(HTML.format(html), QUrl("file://"))
313-
self.load_js()
314-
self.highlight_docs()
315-
316-
def load_js(self):
317-
resources = os.path.join(os.path.dirname(__file__), 'resources')
318-
for script in ('jquery-3.1.1.min.js', 'jquery.mark.min.js', 'highlighter.js', ):
319-
self.doc_webview.evalJS(open(os.path.join(resources, script), encoding='utf-8').read())
320+
base = QUrl.fromLocalFile(__file__)
321+
self.doc_webview.setHtml(HTML.format(html), base)
320322

321323
def search_features_changed(self):
322324
self.regenerate_docs()
@@ -338,11 +340,21 @@ def refresh_search(self):
338340
self.update_info()
339341
self.commit()
340342

343+
@Slot()
341344
def highlight_docs(self):
342345
search_keyword = self.regexp_filter.\
343346
strip('|').replace('\\', '\\\\') # escape one \ to two for mark.js
347+
344348
if search_keyword:
345-
self.doc_webview.evalJS('mark("{}");'.format(search_keyword))
349+
# mark is undefined when clearing the view (`setHtml('')`). Maybe
350+
# set and template html with all the scripts, ... but no contents?
351+
self.doc_webview.runJavaScript(
352+
'''
353+
if (typeof mark !== "undefined") {{
354+
mark("{}");
355+
}}
356+
'''.format(search_keyword)
357+
)
346358

347359
def update_info(self):
348360
if self.corpus is not None:

0 commit comments

Comments
 (0)