Switch to using cssutils 0.9.9 for improved performance and robustness

This commit is contained in:
Kovid Goyal 2012-04-26 15:11:22 +05:30
parent 5c91febe06
commit aee69f304a
5 changed files with 18 additions and 22 deletions

View File

@ -78,7 +78,7 @@ class KF8Writer(object):
# in-memory CSSStylesheet, as deepcopy doesn't work (raises an # in-memory CSSStylesheet, as deepcopy doesn't work (raises an
# exception) # exception)
self._data_cache[item.href] = cssutils.parseString( self._data_cache[item.href] = cssutils.parseString(
item.data.cssText) item.data.cssText, validate=False)
def data(self, item): def data(self, item):
return self._data_cache.get(item.href, item.data) return self._data_cache.get(item.href, item.data)
@ -114,7 +114,7 @@ class KF8Writer(object):
for tag in XPath('//h:style')(root): for tag in XPath('//h:style')(root):
if tag.text: if tag.text:
sheet = cssutils.parseString(tag.text) sheet = cssutils.parseString(tag.text, validate=False)
replacer = partial(pointer, item) replacer = partial(pointer, item)
cssutils.replaceUrls(sheet, replacer, cssutils.replaceUrls(sheet, replacer,
ignoreImportRules=True) ignoreImportRules=True)

View File

@ -116,7 +116,7 @@ class Extract(ODF2XHTML):
def do_filter_css(self, css): def do_filter_css(self, css):
from cssutils import parseString from cssutils import parseString
from cssutils.css import CSSRule from cssutils.css import CSSRule
sheet = parseString(css) sheet = parseString(css, validate=False)
rules = list(sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE)) rules = list(sheet.cssRules.rulesOfType(CSSRule.STYLE_RULE))
sel_map = {} sel_map = {}
count = 0 count = 0

View File

@ -212,7 +212,7 @@ def rewrite_links(root, link_repl_func, resolve_base_href=False):
if tag == XHTML('style') and el.text and \ if tag == XHTML('style') and el.text and \
(_css_url_re.search(el.text) is not None or '@import' in (_css_url_re.search(el.text) is not None or '@import' in
el.text): el.text):
stylesheet = parseString(el.text) stylesheet = parseString(el.text, validate=False)
replaceUrls(stylesheet, link_repl_func) replaceUrls(stylesheet, link_repl_func)
repl = stylesheet.cssText repl = stylesheet.cssText
if isbytestring(repl): if isbytestring(repl):
@ -223,7 +223,7 @@ def rewrite_links(root, link_repl_func, resolve_base_href=False):
text = el.attrib['style'] text = el.attrib['style']
if _css_url_re.search(text) is not None: if _css_url_re.search(text) is not None:
try: try:
stext = parseStyle(text) stext = parseStyle(text, validate=False)
except: except:
# Parsing errors are raised by cssutils # Parsing errors are raised by cssutils
continue continue
@ -861,7 +861,7 @@ class Manifest(object):
parser = CSSParser(loglevel=logging.WARNING, parser = CSSParser(loglevel=logging.WARNING,
fetcher=self.override_css_fetch or self._fetch_css, fetcher=self.override_css_fetch or self._fetch_css,
log=_css_logger) log=_css_logger)
data = parser.parseString(data, href=self.href) data = parser.parseString(data, href=self.href, validate=False)
data = resolveImports(data) data = resolveImports(data)
data.namespaces['h'] = XHTML_NS data.namespaces['h'] = XHTML_NS
return data return data

View File

@ -14,11 +14,9 @@ from xml.dom import SyntaxErr as CSSSyntaxError
from cssutils.css import (CSSStyleRule, CSSPageRule, CSSFontFaceRule, from cssutils.css import (CSSStyleRule, CSSPageRule, CSSFontFaceRule,
cssproperties) cssproperties)
try: try:
from cssutils.css import CSSValueList from cssutils.css import PropertyValue
CSSValueList
except ImportError: except ImportError:
# cssutils >= 0.9.8 raise RuntimeError('You need cssutils >= 0.9.9 for calibre')
from cssutils.css import PropertyValue as CSSValueList
from cssutils import (profile as cssprofiles, parseString, parseStyle, log as from cssutils import (profile as cssprofiles, parseString, parseStyle, log as
cssutils_log, CSSParser, profiles, replaceUrls) cssutils_log, CSSParser, profiles, replaceUrls)
from lxml import etree from lxml import etree
@ -37,7 +35,7 @@ def html_css_stylesheet():
global _html_css_stylesheet global _html_css_stylesheet
if _html_css_stylesheet is None: if _html_css_stylesheet is None:
html_css = open(P('templates/html.css'), 'rb').read() html_css = open(P('templates/html.css'), 'rb').read()
_html_css_stylesheet = parseString(html_css) _html_css_stylesheet = parseString(html_css, validate=False)
_html_css_stylesheet.namespaces['h'] = XHTML_NS _html_css_stylesheet.namespaces['h'] = XHTML_NS
return _html_css_stylesheet return _html_css_stylesheet
@ -218,7 +216,8 @@ class Stylizer(object):
if text: if text:
text = XHTML_CSS_NAMESPACE + text text = XHTML_CSS_NAMESPACE + text
text = oeb.css_preprocessor(text) text = oeb.css_preprocessor(text)
stylesheet = parser.parseString(text, href=cssname) stylesheet = parser.parseString(text, href=cssname,
validate=False)
stylesheet.namespaces['h'] = XHTML_NS stylesheet.namespaces['h'] = XHTML_NS
stylesheets.append(stylesheet) stylesheets.append(stylesheet)
# Make links to resources absolute, since these rules will # Make links to resources absolute, since these rules will
@ -247,7 +246,8 @@ class Stylizer(object):
if x: if x:
try: try:
text = XHTML_CSS_NAMESPACE + x text = XHTML_CSS_NAMESPACE + x
stylesheet = parser.parseString(text, href=cssname) stylesheet = parser.parseString(text, href=cssname,
validate=False)
stylesheet.namespaces['h'] = XHTML_NS stylesheet.namespaces['h'] = XHTML_NS
stylesheets.append(stylesheet) stylesheets.append(stylesheet)
except: except:
@ -374,7 +374,7 @@ class Stylizer(object):
def _normalize_edge(self, cssvalue, name): def _normalize_edge(self, cssvalue, name):
style = {} style = {}
if isinstance(cssvalue, CSSValueList): if isinstance(cssvalue, PropertyValue):
primitives = [v.cssText for v in cssvalue] primitives = [v.cssText for v in cssvalue]
else: else:
primitives = [cssvalue.cssText] primitives = [cssvalue.cssText]
@ -507,15 +507,11 @@ class Style(object):
css = [x for x in css if self.MS_PAT.match(x) is None] css = [x for x in css if self.MS_PAT.match(x) is None]
css = '; '.join(css) css = '; '.join(css)
try: try:
style = parseStyle(css) style = parseStyle(css, validate=False)
except CSSSyntaxError: except CSSSyntaxError:
return return
if url_replacer is not None: if url_replacer is not None:
# Fool replaceUrls into processing our style declaration replaceUrls(style, url_replacer, ignoreImportRules=True)
class Fool:
def __init__(self, s):
self.style = s
replaceUrls(Fool(style), url_replacer, ignoreImportRules=True)
self._style.update(self._stylizer.flatten_style(style)) self._style.update(self._stylizer.flatten_style(style))
def _has_parent(self): def _has_parent(self):
@ -579,7 +575,7 @@ class Style(object):
val = self._style.get('background', None) val = self._style.get('background', None)
if val is not None: if val is not None:
try: try:
style = parseStyle('background: '+val) style = parseStyle('background: '+val, validate=False)
val = style.getProperty('background').cssValue val = style.getProperty('background').cssValue
try: try:
val = list(val) val = list(val)

View File

@ -404,7 +404,7 @@ class CSSFlattener(object):
rules = [r.cssText for r in stylizer.font_face_rules] rules = [r.cssText for r in stylizer.font_face_rules]
raw = '\n\n'.join(rules) raw = '\n\n'.join(rules)
# Make URLs referring to fonts relative to this item # Make URLs referring to fonts relative to this item
sheet = cssutils.parseString(raw) sheet = cssutils.parseString(raw, validate=False)
cssutils.replaceUrls(sheet, item.relhref, ignoreImportRules=True) cssutils.replaceUrls(sheet, item.relhref, ignoreImportRules=True)
style.text += '\n' + sheet.cssText style.text += '\n' + sheet.cssText