MOBI Output: Make parsing of CSS in style attributes more robust

This commit is contained in:
Kovid Goyal 2009-02-07 13:22:00 -08:00
commit 401a856458

View File

@ -17,6 +17,7 @@ import types
import re import re
import copy import copy
from itertools import izip from itertools import izip
from xml.dom import SyntaxErr as CSSSyntaxError
import cssutils import cssutils
from cssutils.css import CSSStyleRule, CSSPageRule, CSSStyleDeclaration, \ from cssutils.css import CSSStyleRule, CSSPageRule, CSSStyleDeclaration, \
CSSValueList, cssproperties CSSValueList, cssproperties
@ -288,15 +289,19 @@ class Style(object):
def _update_cssdict(self, cssdict): def _update_cssdict(self, cssdict):
self._style.update(cssdict) self._style.update(cssdict)
def _apply_style_attr(self): def _apply_style_attr(self):
attrib = self._element.attrib attrib = self._element.attrib
if 'style' in attrib: if 'style' not in attrib:
css = attrib['style'].split(';') return
css = filter(None, map(lambda x: x.strip(), css)) css = attrib['style'].split(';')
css = filter(None, (x.strip() for x in css))
try:
style = CSSStyleDeclaration('; '.join(css)) style = CSSStyleDeclaration('; '.join(css))
self._style.update(self._stylizer.flatten_style(style)) except CSSSyntaxError:
return
self._style.update(self._stylizer.flatten_style(style))
def _has_parent(self): def _has_parent(self):
return (self._element.getparent() is not None) return (self._element.getparent() is not None)