Also check for errors in style attributes

This commit is contained in:
Kovid Goyal 2013-12-12 10:50:34 +05:30
parent b7efcea021
commit fc5dc6cc29
2 changed files with 14 additions and 2 deletions

View File

@ -49,6 +49,10 @@ def run_checks(container):
for style in root.xpath('//*[local-name()="style"]'): for style in root.xpath('//*[local-name()="style"]'):
if style.get('type', 'text/css') == 'text/css': if style.get('type', 'text/css') == 'text/css':
errors.extend(check_css_parsing(name, style.text, line_offset=style.sourceline - 1)) 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_links(container)
errors += check_fonts(container) errors += check_fonts(container)

View File

@ -11,6 +11,7 @@ import re
from lxml.etree import XMLParser, fromstring, XMLSyntaxError from lxml.etree import XMLParser, fromstring, XMLSyntaxError
import cssutils import cssutils
from calibre import force_unicode
from calibre.ebooks.html_entities import html5_entities 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.pretty import pretty_script_or_style as fix_style_tag
from calibre.ebooks.oeb.polish.utils import PositionFinder from calibre.ebooks.oeb.polish.utils import PositionFinder
@ -160,6 +161,10 @@ class CSSError(BaseError):
for style in root.xpath('//*[local-name()="style"]'): for style in root.xpath('//*[local-name()="style"]'):
if style.get('type', 'text/css') == 'text/css' and style.text and style.text.strip(): if style.get('type', 'text/css') == 'text/css' and style.text and style.text.strip():
fix_style_tag(container, style) 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 return True
pos_pats = (re.compile(r'\[(\d+):(\d+)'), re.compile(r'(\d+), (\d+)\)')) pos_pats = (re.compile(r'\[(\d+):(\d+)'), re.compile(r'(\d+), (\d+)\)'))
@ -196,10 +201,13 @@ class ErrorHandler(object):
self.__handle(WARN, *args) self.__handle(WARN, *args)
warning = warn 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) log = ErrorHandler(name)
parser = cssutils.CSSParser(fetcher=lambda x: (None, None), log=log) 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: for err in log.errors:
err.line += line_offset err.line += line_offset
return log.errors return log.errors