~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/oeb/polish/css.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-02-27 07:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 74.
  • Revision ID: package-import@ubuntu.com-20140227074806-64wdebb3ptosxhhx
Tags: upstream-1.25.0+dfsg
ImportĀ upstreamĀ versionĀ 1.25.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
103
103
        if not used:
104
104
            yield rule
105
105
 
 
106
def process_namespaces(sheet):
 
107
    # Find the namespace prefix (if any) for the XHTML namespace, so that we
 
108
    # can preserve it after processing
 
109
    for prefix in sheet.namespaces:
 
110
        if sheet.namespaces[prefix] == XHTML_NS:
 
111
            return prefix
 
112
 
 
113
def preserve_htmlns_prefix(sheet, prefix):
 
114
    if prefix is None:
 
115
        while 'h' in sheet.namespaces:
 
116
            del sheet.namespaces['h']
 
117
    else:
 
118
        sheet.namespaces[prefix] = XHTML_NS
 
119
 
106
120
def remove_unused_css(container, report):
107
121
    from cssutils.css import CSSRule
108
122
    sheets = {name:container.parsed(name) for name, mt in container.mime_map.iteritems() if mt in OEB_STYLES}
109
 
    namespaced_sheets = set()
 
123
    sheet_namespace = {}
110
124
    for sheet in sheets.itervalues():
111
 
        if 'h' not in sheet.namespaces:
112
 
            namespaced_sheets.add(sheet)
 
125
        sheet_namespace[sheet] = process_namespaces(sheet)
113
126
        sheet.namespaces['h'] = XHTML_NS
114
127
    style_rules = {name:tuple(sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE)) for name, sheet in sheets.iteritems()}
115
128
 
124
137
        for style in root.xpath('//*[local-name()="style"]'):
125
138
            if style.get('type', 'text/css') == 'text/css' and style.text:
126
139
                sheet = container.parse_css(style.text)
127
 
                ns = 'h' not in sheet.namespaces
 
140
                ns = process_namespaces(sheet)
128
141
                sheet.namespaces['h'] = XHTML_NS
129
142
                rules = tuple(sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE))
130
143
                unused_rules = tuple(filter_used_rules(root, rules, container.log, pseudo_pat, cache))
131
144
                if unused_rules:
132
145
                    num_of_removed_rules += len(unused_rules)
133
146
                    [sheet.cssRules.remove(r) for r in unused_rules]
134
 
                    if ns:
135
 
                        del sheet.namespaces['h']
 
147
                    preserve_htmlns_prefix(sheet, ns)
136
148
                    style.text = force_unicode(sheet.cssText, 'utf-8')
137
149
                    pretty_script_or_style(container, style)
138
150
                    container.dirty(name)
143
155
                style_rules[sname] = tuple(filter_used_rules(root, style_rules[sname], container.log, pseudo_pat, cache))
144
156
 
145
157
    for name, sheet in sheets.iteritems():
146
 
        if sheet in namespaced_sheets:
147
 
            del sheet.namespaces['h']
 
158
        preserve_htmlns_prefix(sheet, sheet_namespace[sheet])
148
159
        unused_rules = style_rules[name]
149
160
        if unused_rules:
150
161
            num_of_removed_rules += len(unused_rules)