From 2e3d14ec7bf0cdfe3976158eb70216914cfdd843 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 18 May 2014 18:23:23 +0530 Subject: [PATCH] Handle media queries when finding matching CSS rules --- resources/compiled_coffeescript.zip | Bin 89571 -> 90366 bytes src/calibre/ebooks/oeb/polish/preview.coffee | 46 ++++++++++++------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/resources/compiled_coffeescript.zip b/resources/compiled_coffeescript.zip index 86458686e666fddfa76237422e18286866a2aaa4..125bd0cfa549210a82c77fa716073d6590f1e9de 100644 GIT binary patch delta 972 zcmah|O-~b16y=f#jt=of~@PfP@j2@R>!=__q4ZJjq2jhL`? zMZzTBxPqNKF~p<`HyW3^kVJm~acQD)>Bjh8>BI(&zQttb=Du^zJ?GBnS8YGvv~9eE z{vp@Lt-ZNVPd2r^U`xwhu%-+_2wYe($~xmbZB&bl*EA1gPINTaXg>_yiq83hY*0|k zW)0D=AkQ;qD!8hddcIIvNpn_Yx>+_jX>ssf7M&&T!-q~3=bmO@Rx>pP^9IW)SgNin zlDWbRvmj9=`{6VFn?RtO4=T*+)ZOjk21ao-V10i&>X2%zUEM?JUN|;qYfGgI%QJO4 zlms{u#en@S@oi**)8N1nOo>i$%R1P>+w;k}OS9>Pn^%*zZmGT54DKzJvkjqsoA(dh z|1_rk(3lw~Z6~3JHfNySdU4KSec5qRUjzo}SrWSHWgj<%RH@u=dwYZ3H|>!MIrNGS zj|aU}6&EjX(}(Np=xv_WUu2~fGmkM0$WE!=zs53m3g*1bjq$}sY68k9j>wC4BY?CY zfnNF=fdT4_!Zclp!YQlwgOh@BA#p1TF|y(?L_b8G-o>HA!A-?U6EW~mDh6&^lW(^Y z;6AG~B9UGq4mYbCp01B)2V*^d-$U*$aJj_l_$yVO_sXU5S+BqK*Xva7640kX7_`v` z=jm-27O78#1bGHwpk}GyXr29AUiDpV3M{q!zx_RF$0S8Rs*fNHr{fo)yLKiH*8rR6 zIyzOUS}fM?CIH<>{yN%Tf<$Xes&=phk6XQ}nj6oAGvnc$8V+T&Nj0QTj!k7V>O?>b SjxlX)YLaQWP?)xs;rcJ+4LEuL delta 412 zcmZXQPbdU&9L2r0TbeWpvEZP#us*;$s|nelI(l|RL0Ysd7P{44CPA{Pe-ZuC2R zZ)=X6l+?tQh>LP@;OZa;Il2nt;KJ?od9U~CeO~#VOTMjBv;^fNelmIyJD4G*tc(;V z8bkulY6wO(Jq}>fJD1}YU8Yb+`XHj-!o~_0xfWNM}2^?j^Blj z6#G{h{pri<2+_8VCMxO((~YajT`rFA>ZzE9U%ZaV!Y;{ i += 1 return properties +process_rules = (node, cssRules, address, sheet, matching_selectors, all_properties, node_style, is_ancestor, ans) -> + num = -1 + for rule in cssRules + num += 1 + rule_address = address + [num] + if rule.type == CSSRule.MEDIA_RULE + process_rules(node, rule.cssRules, rule_address, sheet, matching_selectors, all_properties, node_style, is_ancestor, ans) + continue + if rule.type != CSSRule.STYLE_RULE + continue + # As a performance improvement, instead of running the match on every + # rule, we simply check if its selector is one of the matching + # selectors returned by getMatchedCSSRules. However, + # getMatchedCSSRules ignores rules in media queries that dont apply, so we check them manually + st = rule.selectorText + if st and (matching_selectors.hasOwnProperty(st) or (rule_address.length > 1 and node.webkitMatchesSelector(st))) + type = 'sheet' + href = sheet.href + if href == null + href = get_sourceline_address(sheet.ownerNode) + type = 'elem' + properties = get_style_properties(rule.style, all_properties, node_style, is_ancestor) + if properties.length > 0 + data = {'selector':st, 'type':type, 'href':href, 'properties':properties, 'is_ancestor':is_ancestor, 'rule_address':rule_address} + ans.push(data) + get_matched_css = (node, is_ancestor, all_properties) -> # WebKit sets parentStyleSheet == null for rules returned by getMatchedCSSRules so we cannot use them directly rules = node.ownerDocument.defaultView.getMatchedCSSRules(node, '') @@ -176,31 +202,17 @@ get_matched_css = (node, is_ancestor, all_properties) -> for sheet in document.styleSheets if sheet.disabled continue - for rule in sheet.cssRules - if rule.type != CSSRule.STYLE_RULE - continue - # Either use matching_selectors for speed or matches for - # correctness - # if rule.selectorText and node.webkitMatchesSelector(rule.selectorText) - if rule.selectorText and matching_selectors.hasOwnProperty(rule.selectorText) - type = 'sheet' - href = sheet.href - if href == null - href = get_sourceline_address(sheet.ownerNode) - type = 'elem' - properties = get_style_properties(rule.style, all_properties, node_style, is_ancestor) - if properties.length > 0 - data = {'selector':rule.selectorText, 'type':type, 'href':href, 'properties':properties, 'is_ancestor':is_ancestor} - ans.push(data) + process_rules(node, sheet.cssRules, [], sheet, matching_selectors, all_properties, node_style, is_ancestor, ans) if node.getAttribute('style') properties = get_style_properties(node.style, all_properties, node_style, is_ancestor) if properties.length > 0 - data = {'selector':null, 'type':'inline', 'href':get_sourceline_address(node), 'properties':properties, 'is_ancestor':is_ancestor} + data = {'selector':null, 'type':'inline', 'href':get_sourceline_address(node), 'properties':properties, 'is_ancestor':is_ancestor, 'rule_address':null} ans.push(data) return ans.reverse() + class PreviewIntegration ###