| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
# globals: CSSRule |
| 4 |
|
| 5 |
from __python__ import bound_methods, hash_literals |
| 6 |
|
| 7 |
|
| 8 |
INHERITED_PROPS = { # {{{ |
| 9 |
'azimuth': '2', |
| 10 |
'border-collapse': '2', |
| 11 |
'border-spacing': '2', |
| 12 |
'caption-side': '2', |
| 13 |
'color': '2', |
| 14 |
'cursor': '2', |
| 15 |
'direction': '2', |
| 16 |
'elevation': '2', |
| 17 |
'empty-cells': '2', |
| 18 |
'fit': '3', |
| 19 |
'fit-position': '3', |
| 20 |
'font': '2', |
| 21 |
'font-family': '2', |
| 22 |
'font-size': '2', |
| 23 |
'font-size-adjust': '2', |
| 24 |
'font-stretch': '2', |
| 25 |
'font-style': '2', |
| 26 |
'font-variant': '2', |
| 27 |
'font-weight': '2', |
| 28 |
'hanging-punctuation': '3', |
| 29 |
'hyphenate-after': '3', |
| 30 |
'hyphenate-before': '3', |
| 31 |
'hyphenate-character': '3', |
| 32 |
'hyphenate-lines': '3', |
| 33 |
'hyphenate-resource': '3', |
| 34 |
'hyphens': '3', |
| 35 |
'image-resolution': '3', |
| 36 |
'letter-spacing': '2', |
| 37 |
'line-height': '2', |
| 38 |
'line-stacking': '3', |
| 39 |
'line-stacking-ruby': '3', |
| 40 |
'line-stacking-shift': '3', |
| 41 |
'line-stacking-strategy': '3', |
| 42 |
'list-style': '2', |
| 43 |
'list-style-image': '2', |
| 44 |
'list-style-position': '2', |
| 45 |
'list-style-type': '2', |
| 46 |
'marquee-direction': '3', |
| 47 |
'orphans': '2', |
| 48 |
'overflow-style': '3', |
| 49 |
'page': '2', |
| 50 |
'page-break-inside': '2', |
| 51 |
'pitch': '2', |
| 52 |
'pitch-range': '2', |
| 53 |
'presentation-level': '3', |
| 54 |
'punctuation-trim': '3', |
| 55 |
'quotes': '2', |
| 56 |
'richness': '2', |
| 57 |
'ruby-align': '3', |
| 58 |
'ruby-overhang': '3', |
| 59 |
'ruby-position': '3', |
| 60 |
'speak': '2', |
| 61 |
'speak-header': '2', |
| 62 |
'speak-numeral': '2', |
| 63 |
'speak-punctuation': '2', |
| 64 |
'speech-rate': '2', |
| 65 |
'stress': '2', |
| 66 |
'text-align': '2', |
| 67 |
'text-align-last': '3', |
| 68 |
'text-emphasis': '3', |
| 69 |
'text-height': '3', |
| 70 |
'text-indent': '2', |
| 71 |
'text-justify': '3', |
| 72 |
'text-outline': '3', |
| 73 |
'text-replace': '?', |
| 74 |
'text-shadow': '3', |
| 75 |
'text-transform': '2', |
| 76 |
'text-wrap': '3', |
| 77 |
'visibility': '2', |
| 78 |
'voice-balance': '3', |
| 79 |
'voice-family': '2', |
| 80 |
'voice-rate': '3', |
| 81 |
'voice-pitch': '3', |
| 82 |
'voice-pitch-range': '3', |
| 83 |
'voice-stress': '3', |
| 84 |
'voice-volume': '3', |
| 85 |
'volume': '2', |
| 86 |
'white-space': '2', |
| 87 |
'white-space-collapse': '3', |
| 88 |
'widows': '2', |
| 89 |
'word-break': '3', |
| 90 |
'word-spacing': '2', |
| 91 |
'word-wrap': '3', |
| 92 |
# the mozilla extensions are all proprietary properties |
| 93 |
'-moz-force-broken-image-icon': 'm', |
| 94 |
'-moz-image-region': 'm', |
| 95 |
'-moz-stack-sizing': 'm', |
| 96 |
'-moz-user-input': 'm', |
| 97 |
'-x-system-font': 'm', |
| 98 |
# the opera extensions are all draft implementations of CSS3 properties |
| 99 |
'-xv-voice-balance': 'o', |
| 100 |
'-xv-voice-pitch': 'o', |
| 101 |
'-xv-voice-pitch-range': 'o', |
| 102 |
'-xv-voice-rate': 'o', |
| 103 |
'-xv-voice-stress': 'o', |
| 104 |
'-xv-voice-volume': 'o', |
| 105 |
# the explorer extensions are all draft implementations of CSS3 properties |
| 106 |
'-ms-text-align-last': 'e', |
| 107 |
'-ms-text-justify': 'e', |
| 108 |
'-ms-word-break': 'e', |
| 109 |
'-ms-word-wrap': 'e' |
| 110 |
} # }}} |
| 111 |
|
| 112 |
|
| 113 |
def get_sourceline_address(node): |
| 114 |
sourceline = parseInt(node.dataset.lnum) |
| 115 |
tags = [] |
| 116 |
for elem in document.querySelectorAll(f'[data-lnum="{sourceline}"]'): |
| 117 |
tags.push(elem.tagName.toLowerCase()) |
| 118 |
if elem is node: |
| 119 |
break |
| 120 |
return [sourceline, tags] |
| 121 |
|
| 122 |
|
| 123 |
def get_color(property, val): |
| 124 |
color = None |
| 125 |
if property.indexOf('color') > -1: |
| 126 |
try: |
| 127 |
color = window.parseCSSColor(val) # Use the csscolor library to get an rgba 4-tuple |
| 128 |
except: |
| 129 |
pass |
| 130 |
return color |
| 131 |
|
| 132 |
|
| 133 |
def get_style_properties(style, all_properties, node_style, is_ancestor): |
| 134 |
i = 0 |
| 135 |
properties = [] |
| 136 |
while i < style.length: |
| 137 |
property = style.item(i) |
| 138 |
if property: |
| 139 |
property = property.toLowerCase() |
| 140 |
val = style.getPropertyValue(property) |
| 141 |
else: |
| 142 |
val = None |
| 143 |
if property and val and (not is_ancestor or INHERITED_PROPS[property]): |
| 144 |
properties.push([property, val, style.getPropertyPriority(property), get_color(property, val)]) |
| 145 |
if not all_properties[property]: |
| 146 |
cval = node_style.getPropertyValue(property) |
| 147 |
cval |
| 148 |
all_properties[property] = [cval, get_color(property, cval)] |
| 149 |
i += 1 |
| 150 |
return properties |
| 151 |
|
| 152 |
|
| 153 |
def get_sheet_rules(sheet): |
| 154 |
if sheet.disabled or not sheet.cssRules: |
| 155 |
return [] |
| 156 |
sheet_media = sheet.media and sheet.media.mediaText |
| 157 |
if sheet_media and sheet_media.length and not window.matchMedia(sheet_media).matches: |
| 158 |
return [] |
| 159 |
return sheet.cssRules |
| 160 |
|
| 161 |
|
| 162 |
def selector_matches(node, selector): |
| 163 |
try: |
| 164 |
return node.matches(selector) |
| 165 |
except: |
| 166 |
# happens if the selector uses epub|type |
| 167 |
if 'epub|' in selector: |
| 168 |
sanitized_sel = str.replace(selector, 'epub|', '*|') |
| 169 |
try: |
| 170 |
# TODO: Actually parse the selector and extract the attribute |
| 171 |
# and check it manually |
| 172 |
return node.matches(sanitized_sel) |
| 173 |
except: |
| 174 |
return False |
| 175 |
return False |
| 176 |
|
| 177 |
|
| 178 |
def process_rules(node, rules, address, sheet, sheet_index, all_properties, node_style, is_ancestor, ans): |
| 179 |
offset = 0 |
| 180 |
for rule_index in range(rules.length): |
| 181 |
rule = rules[rule_index] |
| 182 |
rule_address = address.concat([rule_index - offset]) |
| 183 |
if rule.type is CSSRule.MEDIA_RULE: |
| 184 |
process_rules(node, rule.cssRules, rule_address, sheet, sheet_index, all_properties, node_style, is_ancestor, ans) |
| 185 |
continue |
| 186 |
if rule.type is not CSSRule.STYLE_RULE: |
| 187 |
if rule.type is CSSRule.NAMESPACE_RULE: |
| 188 |
offset += 1 |
| 189 |
continue |
| 190 |
st = rule.selectorText |
| 191 |
if not selector_matches(node, st): |
| 192 |
continue |
| 193 |
type = 'sheet' |
| 194 |
href = sheet.href |
| 195 |
if not href: |
| 196 |
href = get_sourceline_address(sheet.ownerNode) |
| 197 |
type = 'elem' |
| 198 |
# Technically, we should check for the highest specificity |
| 199 |
# matching selector, but we cheat and use the first one |
| 200 |
parts = st.split(',') |
| 201 |
if parts.length > 1: |
| 202 |
for q in parts: |
| 203 |
if selector_matches(node, q): |
| 204 |
st = q |
| 205 |
break |
| 206 |
properties = get_style_properties(rule.style, all_properties, node_style, is_ancestor) |
| 207 |
if properties.length > 0: |
| 208 |
data = {'selector': st, 'type': type, 'href': href, 'properties': properties, 'rule_address': rule_address, 'sheet_index': sheet_index} |
| 209 |
ans.push(data) |
| 210 |
|
| 211 |
|
| 212 |
def get_matched_css(node, is_ancestor, all_properties): |
| 213 |
ans = [] |
| 214 |
node_style = window.getComputedStyle(node) |
| 215 |
|
| 216 |
sheets = document.styleSheets |
| 217 |
for sheet_index in range(sheets.length): |
| 218 |
sheet = sheets[sheet_index] |
| 219 |
process_rules(node, get_sheet_rules(sheet), [], sheet, sheet_index, all_properties, node_style, is_ancestor, ans) |
| 220 |
|
| 221 |
if node.getAttribute('style'): |
| 222 |
properties = get_style_properties(node.style, all_properties, node_style, is_ancestor) |
| 223 |
if properties.length > 0: |
| 224 |
data = { |
| 225 |
'selector': None, |
| 226 |
'type': 'inline', |
| 227 |
'href': get_sourceline_address(node), |
| 228 |
'properties': properties, |
| 229 |
'rule_address': None, |
| 230 |
'sheet_index': None, |
| 231 |
} |
| 232 |
ans.push(data) |
| 233 |
|
| 234 |
return ans.reverse() |