From 67f342243891d8ba71a2827cd51d3dc7d4682e19 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 27 Jul 2018 22:14:50 +0530 Subject: [PATCH] Port addition of css for split marker --- src/calibre/gui2/__init__.py | 14 ------------ src/calibre/gui2/tweak_book/preview.py | 3 --- src/pyj/editor.pyj | 31 ++++++++++++++++++-------- 3 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index f354a93f7e..7fe34c7ef8 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -1361,20 +1361,6 @@ def secure_web_page(qwebpage_or_qwebsettings): return settings -def secure_webengine(view_or_page_or_settings, for_viewer=False): - s = view_or_page_or_settings.settings() if hasattr(view_or_page_or_settings, 'settings') else view_or_page_or_settings - a = s.setAttribute - a(s.PluginsEnabled, False) - if not for_viewer: - a(s.JavascriptEnabled, False) - s.setUnknownUrlSchemePolicy(s.DisallowUnknownUrlSchemes) - a(s.JavascriptCanOpenWindows, False) - a(s.JavascriptCanAccessClipboard, False) - a(s.LocalContentCanAccessFileUrls, False) # ensure javascript cannot read from local files - a(s.AllowWindowActivationFromJavaScript, False) - return s - - empty_model = QStringListModel(['']) empty_index = empty_model.index(0) diff --git a/src/calibre/gui2/tweak_book/preview.py b/src/calibre/gui2/tweak_book/preview.py index 4aad61454d..675fc5a1bb 100644 --- a/src/calibre/gui2/tweak_book/preview.py +++ b/src/calibre/gui2/tweak_book/preview.py @@ -7,7 +7,6 @@ from __future__ import absolute_import, division, print_function, unicode_litera # live css # check that clicking on both internal and external links works # check all buttons in preview panel -# pass user stylesheet with css for split import json import textwrap @@ -321,8 +320,6 @@ class WebPage(QWebEnginePage): self.bridge = Bridge(self) self.setWebChannel(c, QWebEngineScript.ApplicationWorld) c.registerObject('bridge', self.bridge) - # TODO: Implement this - # css = '[data-in-split-mode="1"] [data-is-block="1"]:hover { cursor: pointer !important; border-top: solid 5px green !important }' def javaScriptConsoleMessage(self, level, msg, linenumber, source_id): prints('%s:%s: %s' % (source_id, linenumber, msg)) diff --git a/src/pyj/editor.pyj b/src/pyj/editor.pyj index a315e3c8dd..f92e0b4615 100644 --- a/src/pyj/editor.pyj +++ b/src/pyj/editor.pyj @@ -3,6 +3,8 @@ # globals: CSSRule from __python__ import bound_methods, hash_literals +from elementmaker import E + def is_hidden(elem): while elem: if (elem.style and (elem.style.visibility is 'hidden' or elem.style.display is 'none')): @@ -18,7 +20,8 @@ def is_block(elem): def in_table(elem): while elem: - if elem.tagName?.toLowerCase() is 'table': + tn = elem.tagName.toLowerCase() if elem.tagName else '' + if tn is 'table': return True elem = elem.parentNode return False @@ -162,8 +165,12 @@ def get_style_properties(style, all_properties, node_style, is_ancestor): i = 0 properties = v'[]' while i < style.length: - property = style.item(i)?.toLowerCase() - val = style.getPropertyValue(property) + property = style.item(i) + if property: + property = property.toLowerCase() + val = style.getPropertyValue(property) + else: + val = None if property and val and (not is_ancestor or INHERITED_PROPS[property]): properties.push(v'[property, val, style.getPropertyPriority(property), get_color(property, val)]') if not all_properties.hasOwnProperty(property): @@ -248,7 +255,11 @@ class PreviewIntegration: self.in_split_mode = False if window is window.top: setTimeout(self.connect_channel, 10) - window.document.body.addEventListener('click', self.onclick, True) + document.body.addEventListener('click', self.onclick, True) + document.body.appendChild(E.style( + type='text/css', + '[data-in-split-mode="1"] [data-is-block="1"]:hover { cursor: pointer !important; border-top: solid 5px green !important }' + )) def go_to_line(self, lnum): for node in document.querySelectorAll(f'[data-lnum="{lnum}"]'): @@ -326,7 +337,7 @@ class PreviewIntegration: # Find the closest containing link, if any href = tn = '' while e and e != document.body and e != document and (tn is not 'a' or not href): - tn = e.tagName?.toLowerCase() + tn = e.tagName.toLowerCase() if e.tagName else '' href = e.getAttribute('href') e = e.parentNode self.bridge.request_sync(tn, href, JSON.stringify(address)) @@ -345,7 +356,8 @@ class PreviewIntegration: target = None i = 0 for node in document.querySelectorAll(f'[data-lnum="{sourceline}"]'): - if node.tagName?.toLowerCase() is not tags[i]: + tn = node.tagName.toLowerCase() if node.tagName else '' + if tn is not tags[i]: return JSON.stringify(None) i += 1 target = node @@ -358,10 +370,11 @@ class PreviewIntegration: css = get_matched_css(target, is_ancestor, all_properties) # We want to show the Matched CSS rules header even if no rules matched if css.length > 0 or not is_ancestor: + tn = target.tagName.toLowerCase() if target.tagName else '' ans.nodes.push({ - 'name':target.tagName?.toLowerCase(), - 'css':css, 'is_ancestor':is_ancestor, - 'sourceline':target.getAttribute('data-lnum') + 'name': tn, + 'css': css, 'is_ancestor': is_ancestor, + 'sourceline': target.getAttribute('data-lnum') }) target = target.parentNode is_ancestor = True