Edit Book: Fix unused CSS removal tool inserting namespace declaration at the top of the stylesheet

This commit is contained in:
Kovid Goyal 2014-01-30 12:48:57 +05:30
parent 541611d509
commit efbab707db

View File

@ -106,7 +106,10 @@ def filter_used_rules(root, rules, log, pseudo_pat, cache):
def remove_unused_css(container, report):
from cssutils.css import CSSRule
sheets = {name:container.parsed(name) for name, mt in container.mime_map.iteritems() if mt in OEB_STYLES}
namespaced_sheets = set()
for sheet in sheets.itervalues():
if 'h' not in sheet.namespaces:
namespaced_sheets.add(sheet)
sheet.namespaces['h'] = XHTML_NS
style_rules = {name:tuple(sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE)) for name, sheet in sheets.iteritems()}
@ -121,12 +124,15 @@ def remove_unused_css(container, report):
for style in root.xpath('//*[local-name()="style"]'):
if style.get('type', 'text/css') == 'text/css' and style.text:
sheet = container.parse_css(style.text)
ns = 'h' not in sheet.namespaces
sheet.namespaces['h'] = XHTML_NS
rules = tuple(sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE))
unused_rules = tuple(filter_used_rules(root, rules, container.log, pseudo_pat, cache))
if unused_rules:
num_of_removed_rules += len(unused_rules)
[sheet.cssRules.remove(r) for r in unused_rules]
if ns:
del sheet.namespaces['h']
style.text = force_unicode(sheet.cssText, 'utf-8')
pretty_script_or_style(container, style)
container.dirty(name)
@ -137,6 +143,8 @@ def remove_unused_css(container, report):
style_rules[sname] = tuple(filter_used_rules(root, style_rules[sname], container.log, pseudo_pat, cache))
for name, sheet in sheets.iteritems():
if sheet in namespaced_sheets:
del sheet.namespaces['h']
unused_rules = style_rules[name]
if unused_rules:
num_of_removed_rules += len(unused_rules)