3
3
import sre_constants
4
4
from itertools import chain
5
5
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
+
7
11
from AnyQt .QtGui import QStandardItemModel , QStandardItem
8
12
from AnyQt .QtWidgets import (QListView , QSizePolicy , QTableView ,
9
13
QAbstractItemView , QHeaderView , QSplitter ,
@@ -115,6 +119,7 @@ def __init__(self):
115
119
116
120
# Document contents
117
121
self .doc_webview = gui .WebviewWidget (self .splitter , debug = False )
122
+ self .doc_webview .loadFinished .connect (self .highlight_docs )
118
123
119
124
self .mainArea .layout ().addWidget (self .splitter )
120
125
@@ -222,6 +227,12 @@ def show_docs(self):
222
227
<!doctype html>
223
228
<html>
224
229
<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>
225
236
<meta charset='utf-8'>
226
237
<style>
227
238
@@ -306,17 +317,8 @@ def show_docs(self):
306
317
token ) for token in tokens [row_ind ]))
307
318
308
319
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 )
320
322
321
323
def search_features_changed (self ):
322
324
self .regenerate_docs ()
@@ -338,11 +340,21 @@ def refresh_search(self):
338
340
self .update_info ()
339
341
self .commit ()
340
342
343
+ @Slot ()
341
344
def highlight_docs (self ):
342
345
search_keyword = self .regexp_filter .\
343
346
strip ('|' ).replace ('\\ ' , '\\ \\ ' ) # escape one \ to two for mark.js
347
+
344
348
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
+ )
346
358
347
359
def update_info (self ):
348
360
if self .corpus is not None :
0 commit comments