diff --git a/src/calibre/ebooks/oeb/polish/cascade.py b/src/calibre/ebooks/oeb/polish/cascade.py index f6a6f07f3b..58169a6c52 100644 --- a/src/calibre/ebooks/oeb/polish/cascade.py +++ b/src/calibre/ebooks/oeb/polish/cascade.py @@ -108,6 +108,7 @@ class Values(tuple): @property def cssText(self): + ' This will return either a string or a tuple of strings ' if len(self) == 1: return self[0].cssText return tuple(x.cssText for x in self) @@ -136,7 +137,7 @@ def resolve_declarations(decls): ans[name] = first_val return ans -def resolve_styles(container, name, select=None): +def resolve_styles(container, name, select=None, sheet_callback=None): root = container.parsed(name) select = select or Select(root, ignore_inappropriate_pseudo_classes=True) style_map = defaultdict(list) @@ -145,6 +146,8 @@ def resolve_styles(container, name, select=None): pseudo_pat = re.compile(ur':{1,2}(%s)' % ('|'.join(INAPPROPRIATE_PSEUDO_CLASSES)), re.I) def process_sheet(sheet, sheet_name): + if sheet_callback is not None: + sheet_callback(sheet, sheet_name) for rule, sheet_name, rule_index in iterrules(container, sheet_name, rules=sheet, rule_index_counter=rule_index_counter, rule_type='STYLE_RULE'): for selector in rule.selectorList: text = selector.selectorText diff --git a/src/calibre/ebooks/oeb/polish/tests/cascade.py b/src/calibre/ebooks/oeb/polish/tests/cascade.py index f616582c47..25a4e7bf8d 100644 --- a/src/calibre/ebooks/oeb/polish/tests/cascade.py +++ b/src/calibre/ebooks/oeb/polish/tests/cascade.py @@ -74,7 +74,7 @@ class CascadeTest(BaseTest): def get_maps(html, styles=None): html = '
{}'.format(html) - c = VirtualContainer({'index.html':html, 'styles.css':styles or 'body { color: red }'}) + c = VirtualContainer({'index.html':html, 'styles.css':styles or 'body { color: red; font-family: "Kovid Goyal", sans-serif }'}) style_map, pseudo_style_map, select = resolve_styles(c, 'index.html') tp = partial(test_property, select, style_map) return tp @@ -87,6 +87,20 @@ class CascadeTest(BaseTest): t('b', 'margin-top') t('body', 'display', 'block') t('b', 'display', 'inline') + t('body', 'font-family', ('"Kovid Goyal"', 'sans-serif')) for e in ('body', 'p', 'b'): for prop in 'background-color text-indent'.split(): t(e, prop) + + t = get_maps('xxx
', 'p {color: red}') + t('p', 'color', 'blue') + t = get_maps('xxx
', 'p {color: red}') + t('p', 'color', 'blue') + t = get_maps('xxx
', 'p {color: red !important}') + t('p', 'color', 'red') + t = get_maps('xxx
', '#p { color: blue } p {color: red}') + t('p', 'color', 'blue') + t = get_maps('xxx
', 'p {color: red; color: blue}') + t('p', 'color', 'blue') + t = get_maps('xxx
', 'p {color: red; margin:11pt}') + t('p', 'margin-top', '11pt')