1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 16:07:57 +03:00

Clean css/__init__

This commit is contained in:
Guillaume Ayoub 2020-05-30 01:41:16 +02:00
parent 259d686aec
commit 89651e2f04

View File

@ -307,26 +307,25 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
('height', 'top'), ('height', 'bottom'), ('height', 'top'), ('height', 'bottom'),
('width', 'left'), ('width', 'right')): ('width', 'left'), ('width', 'right')):
style_attribute = None style_attribute = None
for prop in ('margin%s' % part, '%smargin' % position): for prop in (f'margin{part}', f'{position}margin'):
if element.get(prop): if element.get(prop):
style_attribute = 'margin-%s:%spx' % ( style_attribute = (
position, element.get(prop)) f'margin-{position}:{element.get(prop)}px')
break break
if style_attribute: if style_attribute:
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('background'): if element.get('background'):
style_attribute = 'background-image:url(%s)' % ( style_attribute = (
element.get('background')) f'background-image:url({element.get("background")})')
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('bgcolor'): if element.get('bgcolor'):
style_attribute = 'background-color:%s' % ( style_attribute = f'background-color:{element.get("bgcolor")}'
element.get('bgcolor'))
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('text'): if element.get('text'):
style_attribute = 'color:%s' % element.get('text') style_attribute = f'color:{element.get("text")}'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
# TODO: we should support link, vlink, alink # TODO: we should support link, vlink, alink
@ -340,14 +339,14 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
element, 'text-align:center') element, 'text-align:center')
elif align in ('center', 'left', 'right', 'justify'): elif align in ('center', 'left', 'right', 'justify'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'text-align:%s' % align) element, f'text-align:{align}')
elif element.tag == 'font': elif element.tag == 'font':
if element.get('color'): if element.get('color'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'color:%s' % element.get('color')) element, f'color:{element.get("color")}')
if element.get('face'): if element.get('face'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'font-family:%s' % element.get('face')) element, f'font-family:{element.get("face")}')
if element.get('size'): if element.get('size'):
size = element.get('size').strip() size = element.get('size').strip()
relative_plus = size.startswith('+') relative_plus = size.startswith('+')
@ -374,12 +373,11 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
size -= 3 size -= 3
size = max(1, min(7, size)) size = max(1, min(7, size))
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'font-size:%s' % font_sizes[size]) element, f'font-size:{font_sizes[size]}')
elif element.tag == 'table': elif element.tag == 'table':
if element.get('cellspacing'): if element.get('cellspacing'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, element, f'border-spacing:{element.get("cellspacing")}px')
'border-spacing:%spx' % element.get('cellspacing'))
if element.get('cellpadding'): if element.get('cellpadding'):
cellpadding = element.get('cellpadding') cellpadding = element.get('cellpadding')
if cellpadding.isdigit(): if cellpadding.isdigit():
@ -389,53 +387,49 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
if subelement.tag in ('td', 'th'): if subelement.tag in ('td', 'th'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
subelement, subelement,
'padding-left:%s;padding-right:%s;' f'padding-left:{cellpadding};'
'padding-top:%s;padding-bottom:%s;' % ( f'padding-right:{cellpadding};'
4 * (cellpadding,))) f'padding-top:{cellpadding};'
f'padding-bottom:{cellpadding};')
if element.get('hspace'): if element.get('hspace'):
hspace = element.get('hspace') hspace = element.get('hspace')
if hspace.isdigit(): if hspace.isdigit():
hspace += 'px' hspace += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, element, f'margin-left:{hspace};margin-right:{hspace}')
'margin-left:%s;margin-right:%s' % (hspace, hspace))
if element.get('vspace'): if element.get('vspace'):
vspace = element.get('vspace') vspace = element.get('vspace')
if vspace.isdigit(): if vspace.isdigit():
vspace += 'px' vspace += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, element, f'margin-top:{vspace};margin-bottom:{vspace}')
'margin-top:%s;margin-bottom:%s' % (vspace, vspace))
if element.get('width'): if element.get('width'):
style_attribute = 'width:%s' % element.get('width') style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit(): if element.get('width').isdigit():
style_attribute += 'px' style_attribute += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('height'): if element.get('height'):
style_attribute = 'height:%s' % element.get('height') style_attribute = f'height:{element.get("height")}'
if element.get('height').isdigit(): if element.get('height').isdigit():
style_attribute += 'px' style_attribute += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('background'): if element.get('background'):
style_attribute = 'background-image:url(%s)' % ( style_attribute = (
element.get('background')) f'background-image:url({element.get("background")})')
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('bgcolor'): if element.get('bgcolor'):
style_attribute = 'background-color:%s' % ( style_attribute = f'background-color:{element.get("bgcolor")}'
element.get('bgcolor'))
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('bordercolor'): if element.get('bordercolor'):
style_attribute = 'border-color:%s' % ( style_attribute = f'border-color:{element.get("bordercolor")}'
element.get('bordercolor'))
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('border'): if element.get('border'):
style_attribute = 'border-width:%spx' % ( style_attribute = f'border-width:{element.get("border")}px'
element.get('border'))
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
elif element.tag in ('tr', 'td', 'th', 'thead', 'tbody', 'tfoot'): elif element.tag in ('tr', 'td', 'th', 'thead', 'tbody', 'tfoot'):
@ -446,27 +440,26 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
element, 'text-align:center') element, 'text-align:center')
elif align in ('center', 'left', 'right', 'justify'): elif align in ('center', 'left', 'right', 'justify'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'text-align:%s' % align) element, f'text-align:{align}')
if element.get('background'): if element.get('background'):
style_attribute = 'background-image:url(%s)' % ( style_attribute = (
element.get('background')) f'background-image:url({element.get("background")})')
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('bgcolor'): if element.get('bgcolor'):
style_attribute = 'background-color:%s' % ( style_attribute = f'background-color:{element.get("bgcolor")}'
element.get('bgcolor'))
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.tag in ('tr', 'td', 'th'): if element.tag in ('tr', 'td', 'th'):
if element.get('height'): if element.get('height'):
style_attribute = 'height:%s' % element.get('height') style_attribute = f'height:{element.get("height")}'
if element.get('height').isdigit(): if element.get('height').isdigit():
style_attribute += 'px' style_attribute += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.tag in ('td', 'th'): if element.tag in ('td', 'th'):
if element.get('width'): if element.get('width'):
style_attribute = 'width:%s' % element.get('width') style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit(): if element.get('width').isdigit():
style_attribute += 'px' style_attribute += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
@ -479,10 +472,10 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
element, 'text-align:center') element, 'text-align:center')
elif align in ('center', 'left', 'right', 'justify'): elif align in ('center', 'left', 'right', 'justify'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'text-align:%s' % align) element, f'text-align:{align}')
elif element.tag == 'col': elif element.tag == 'col':
if element.get('width'): if element.get('width'):
style_attribute = 'width:%s' % element.get('width') style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit(): if element.get('width').isdigit():
style_attribute += 'px' style_attribute += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
@ -497,22 +490,22 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
if (element.get('color'), element.get('noshade')) != (None, None): if (element.get('color'), element.get('noshade')) != (None, None):
if size >= 1: if size >= 1:
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'border-width:%spx' % (size / 2)) element, f'border-width:{size / 2}px')
elif size == 1: elif size == 1:
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'border-bottom-width:0') element, 'border-bottom-width:0')
elif size > 1: elif size > 1:
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'height:%spx' % (size - 2)) element, f'height:{size - 2}px')
if element.get('width'): if element.get('width'):
style_attribute = 'width:%s' % element.get('width') style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit(): if element.get('width').isdigit():
style_attribute += 'px' style_attribute += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('color'): if element.get('color'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, 'color:%s' % element.get('color')) element, f'color:{element.get("color")}')
elif element.tag in ( elif element.tag in (
'iframe', 'applet', 'embed', 'img', 'input', 'object'): 'iframe', 'applet', 'embed', 'img', 'input', 'object'):
if (element.tag != 'input' or if (element.tag != 'input' or
@ -527,25 +520,23 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
if hspace.isdigit(): if hspace.isdigit():
hspace += 'px' hspace += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, element, f'margin-left:{hspace};margin-right:{hspace}')
'margin-left:%s;margin-right:%s' % (hspace, hspace))
if element.get('vspace'): if element.get('vspace'):
vspace = element.get('vspace') vspace = element.get('vspace')
if vspace.isdigit(): if vspace.isdigit():
vspace += 'px' vspace += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, element, f'margin-top:{vspace};margin-bottom:{vspace}')
'margin-top:%s;margin-bottom:%s' % (vspace, vspace))
# TODO: img seems to be excluded for width and height, but a # TODO: img seems to be excluded for width and height, but a
# lot of W3C tests rely on this attribute being applied to img # lot of W3C tests rely on this attribute being applied to img
if element.get('width'): if element.get('width'):
style_attribute = 'width:%s' % element.get('width') style_attribute = f'width:{element.get("width")}'
if element.get('width').isdigit(): if element.get('width').isdigit():
style_attribute += 'px' style_attribute += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, style_attribute) element, style_attribute)
if element.get('height'): if element.get('height'):
style_attribute = 'height:%s' % element.get('height') style_attribute = f'height:{element.get("height")}'
if element.get('height').isdigit(): if element.get('height').isdigit():
style_attribute += 'px' style_attribute += 'px'
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
@ -554,22 +545,22 @@ def find_style_attributes(tree, presentational_hints=False, base_url=None):
if element.get('border'): if element.get('border'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, element,
'border-width:%spx;border-style:solid' % f'border-width:{element.get("border")}px;'
element.get('border')) f'border-style:solid')
elif element.tag == 'ol': elif element.tag == 'ol':
# From https://www.w3.org/TR/css-lists-3/#ua-stylesheet # From https://www.w3.org/TR/css-lists-3/#ua-stylesheet
if element.get('start'): if element.get('start'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, element,
'counter-reset:list-item %s;' f'counter-reset:list-item {element.get("start")};'
'counter-increment:list-item -1' % element.get('start')) 'counter-increment:list-item -1')
elif element.tag == 'li': elif element.tag == 'li':
# From https://www.w3.org/TR/css-lists-3/#ua-stylesheet # From https://www.w3.org/TR/css-lists-3/#ua-stylesheet
if element.get('value'): if element.get('value'):
yield specificity, check_style_attribute( yield specificity, check_style_attribute(
element, element,
'counter-reset:list-item %s;' f'counter-reset:list-item {element.get("value")};'
'counter-increment:none' % element.get('value')) 'counter-increment:none')
def declaration_precedence(origin, importance): def declaration_precedence(origin, importance):
@ -831,10 +822,11 @@ def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules,
elif rule.type == 'at-rule' and rule.lower_at_keyword == 'import': elif rule.type == 'at-rule' and rule.lower_at_keyword == 'import':
if ignore_imports: if ignore_imports:
LOGGER.warning('@import rule "%s" not at the beginning of the ' LOGGER.warning(
'the whole rule was ignored at %s:%s.', '@import rule "%s" not at the beginning of the '
tinycss2.serialize(rule.prelude), 'the whole rule was ignored at %s:%s.',
rule.source_line, rule.source_column) tinycss2.serialize(rule.prelude),
rule.source_line, rule.source_column)
continue continue
tokens = remove_whitespace(rule.prelude) tokens = remove_whitespace(rule.prelude)
@ -844,10 +836,11 @@ def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules,
continue continue
media = media_queries.parse_media_query(tokens[1:]) media = media_queries.parse_media_query(tokens[1:])
if media is None: if media is None:
LOGGER.warning('Invalid media type "%s" ' LOGGER.warning(
'the whole @import rule was ignored at %s:%s.', 'Invalid media type "%s" '
tinycss2.serialize(rule.prelude), 'the whole @import rule was ignored at %s:%s.',
rule.source_line, rule.source_column) tinycss2.serialize(rule.prelude),
rule.source_line, rule.source_column)
continue continue
if not media_queries.evaluate_media_query( if not media_queries.evaluate_media_query(
media, device_media_type): media, device_media_type):
@ -870,10 +863,11 @@ def preprocess_stylesheet(device_media_type, base_url, stylesheet_rules,
elif rule.type == 'at-rule' and rule.lower_at_keyword == 'media': elif rule.type == 'at-rule' and rule.lower_at_keyword == 'media':
media = media_queries.parse_media_query(rule.prelude) media = media_queries.parse_media_query(rule.prelude)
if media is None: if media is None:
LOGGER.warning('Invalid media type "%s" ' LOGGER.warning(
'the whole @media rule was ignored at %s:%s.', 'Invalid media type "%s" '
tinycss2.serialize(rule.prelude), 'the whole @media rule was ignored at %s:%s.',
rule.source_line, rule.source_column) tinycss2.serialize(rule.prelude),
rule.source_line, rule.source_column)
continue continue
ignore_imports = True ignore_imports = True
if not media_queries.evaluate_media_query( if not media_queries.evaluate_media_query(