diff --git a/src/calibre/ebooks/oeb/polish/check/main.py b/src/calibre/ebooks/oeb/polish/check/main.py index 4bc61e4deb..146d847171 100644 --- a/src/calibre/ebooks/oeb/polish/check/main.py +++ b/src/calibre/ebooks/oeb/polish/check/main.py @@ -49,6 +49,10 @@ def run_checks(container): for style in root.xpath('//*[local-name()="style"]'): if style.get('type', 'text/css') == 'text/css': errors.extend(check_css_parsing(name, style.text, line_offset=style.sourceline - 1)) + for elem in root.xpath('//*[@style]'): + raw = elem.get('style') + if raw: + errors.extend(check_css_parsing(name, raw, line_offset=elem.sourceline - 1, is_declaration=True)) errors += check_links(container) errors += check_fonts(container) diff --git a/src/calibre/ebooks/oeb/polish/check/parsing.py b/src/calibre/ebooks/oeb/polish/check/parsing.py index 303c6237c6..35910542fd 100644 --- a/src/calibre/ebooks/oeb/polish/check/parsing.py +++ b/src/calibre/ebooks/oeb/polish/check/parsing.py @@ -11,6 +11,7 @@ import re from lxml.etree import XMLParser, fromstring, XMLSyntaxError import cssutils +from calibre import force_unicode from calibre.ebooks.html_entities import html5_entities from calibre.ebooks.oeb.polish.pretty import pretty_script_or_style as fix_style_tag from calibre.ebooks.oeb.polish.utils import PositionFinder @@ -160,6 +161,10 @@ class CSSError(BaseError): for style in root.xpath('//*[local-name()="style"]'): if style.get('type', 'text/css') == 'text/css' and style.text and style.text.strip(): fix_style_tag(container, style) + for elem in root.xpath('//*[@style]'): + raw = elem.get('style') + if raw: + elem.set('style', force_unicode(container.parse_css(raw, is_declaration=True).cssText, 'utf-8').replace('\n', ' ')) return True pos_pats = (re.compile(r'\[(\d+):(\d+)'), re.compile(r'(\d+), (\d+)\)')) @@ -196,10 +201,13 @@ class ErrorHandler(object): self.__handle(WARN, *args) warning = warn -def check_css_parsing(name, raw, line_offset=0): +def check_css_parsing(name, raw, line_offset=0, is_declaration=False): log = ErrorHandler(name) parser = cssutils.CSSParser(fetcher=lambda x: (None, None), log=log) - parser.parseString(raw, validate=True) + if is_declaration: + parser.parseStyle(raw, validate=True) + else: + parser.parseString(raw, validate=True) for err in log.errors: err.line += line_offset return log.errors