1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 00:21:15 +03:00

Support counter-set

Code was sleeping commented since December 2011 and could have been working in
WeasyPrint 0.3. Easiest feature ever thanks to @SimonSapin.

Fix #1019.
This commit is contained in:
Guillaume Ayoub 2020-01-06 22:38:17 +01:00
parent 5f28f83f36
commit cb610e88f0
6 changed files with 89 additions and 20 deletions

View File

@ -23,12 +23,6 @@ INITIAL_VALUES = {
'clear': 'none',
'clip': (), # computed value for 'auto'
'color': parse_color('black'), # chosen by the user agent
# Means 'none', but allow `display: list-item` to increment the
# list-item counter. If we ever have a way for authors to query
# computed values (JavaScript?), this value should serialize to 'none'.
'counter_increment': 'auto',
'counter_reset': (), # parsed value for 'none'
# 'counter_set': (), # parsed value for 'none'
'direction': 'ltr',
'display': 'inline',
'empty_cells': 'show',
@ -36,9 +30,6 @@ INITIAL_VALUES = {
'height': 'auto',
'left': 'auto',
'line_height': 'normal',
'list_style_image': ('none', None),
'list_style_position': 'outside',
'list_style_type': 'disc',
'margin_top': Dimension(0, 'px'),
'margin_right': Dimension(0, 'px'),
'margin_bottom': Dimension(0, 'px'),
@ -199,6 +190,17 @@ INITIAL_VALUES = {
'overflow': 'visible',
'text_overflow': 'clip',
# Lists Module 3 (WD): https://drafts.csswg.org/css-lists-3/
# Means 'none', but allow `display: list-item` to increment the
# list-item counter. If we ever have a way for authors to query
# computed values (JavaScript?), this value should serialize to 'none'.
'counter_increment': 'auto',
'counter_reset': (), # parsed value for 'none'
'counter_set': (), # parsed value for 'none'
'list_style_image': ('none', None),
'list_style_position': 'outside',
'list_style_type': 'disc',
# Proprietary
'anchor': None, # computed value of 'none'
'link': None, # computed value of 'none'
@ -297,6 +299,7 @@ TABLE_WRAPPER_BOX_PROPERTIES = {
'clear',
'counter_increment',
'counter_reset',
'counter_set',
'float',
'left',
'margin_top',

View File

@ -499,6 +499,12 @@ def counter_reset(tokens):
return counter(tokens, default_integer=0)
@property()
def counter_set(tokens):
"""``counter-set`` property validation."""
return counter(tokens, default_integer=0)
def counter(tokens, default_integer):
"""``counter-increment`` and ``counter-reset`` properties validation."""
if get_single_keyword(tokens) == 'none':

View File

@ -671,14 +671,13 @@ def update_counters(state, style):
sibling_scopes.add(name)
counter_values.setdefault(name, []).append(value)
# XXX Disabled for now, only exists in Lists3s editors draft.
# for name, value in style['counter_set']:
# values = counter_values.setdefault(name, [])
# if not values:
# assert name not in sibling_scopes
# sibling_scopes.add(name)
# values.append(0)
# values[-1] = value
for name, value in style['counter_set']:
values = counter_values.setdefault(name, [])
if not values:
assert name not in sibling_scopes
sibling_scopes.add(name)
values.append(0)
values[-1] = value
counter_increment = style['counter_increment']
if counter_increment == 'auto':

View File

@ -284,8 +284,7 @@ def _standardize_page_based_counters(style, pseudo_type):
"""
page_counter_touched = False
# XXX 'counter-set` not yet supported
for propname in ('counter_reset', 'counter_increment'):
for propname in ('counter_set', 'counter_reset', 'counter_increment'):
if style[propname] == 'auto':
style[propname] = ()
continue

View File

@ -503,6 +503,68 @@ def test_list_style_types(style_type, values):
assert li_4.children[0].children[0].children[0].text == values[3]
def test_counter_set():
page, = render_pages('''
<style>
body { counter-reset: h2 0 h3 4; font-size: 1px }
article { counter-reset: h2 2 }
h1 { counter-increment: h1 }
h1::before { content: counter(h1) }
h2 { counter-increment: h2; counter-set: h3 3 }
h2::before { content: counter(h2) }
h3 { counter-increment: h3 }
h3::before { content: counter(h3) }
</style>
<article>
<h1></h1>
</article>
<article>
<h2></h2>
<h3></h3>
</article>
<article>
<h3></h3>
</article>
<article>
<h2></h2>
</article>
<article>
<h3></h3>
<h3></h3>
</article>
<article>
<h1></h1>
<h2></h2>
<h3></h3>
</article>
''')
html, = page.children
body, = html.children
art_1, art_2, art_3, art_4, art_5, art_6 = body.children
h1, = art_1.children
assert h1.children[0].children[0].children[0].text == '1'
h2, h3, = art_2.children
assert h2.children[0].children[0].children[0].text == '3'
assert h3.children[0].children[0].children[0].text == '4'
h3, = art_3.children
assert h3.children[0].children[0].children[0].text == '5'
h2, = art_4.children
assert h2.children[0].children[0].children[0].text == '3'
h3_1, h3_2 = art_5.children
assert h3_1.children[0].children[0].children[0].text == '4'
assert h3_2.children[0].children[0].children[0].text == '5'
h1, h2, h3 = art_6.children
assert h1.children[0].children[0].children[0].text == '1'
assert h2.children[0].children[0].children[0].text == '3'
assert h3.children[0].children[0].children[0].text == '4'
def test_counter_multiple_extends():
# Inspired by W3C failing test system-extends-invalid
page, = render_pages('''

View File

@ -77,7 +77,7 @@ def test_function_invalid(rule):
('counter-increment: foo bar 2 baz', {
'counter_increment': (('foo', 1), ('bar', 2), ('baz', 1))}),
('counter-reset: foo', {'counter_reset': (('foo', 0),)}),
('counter-reset: FoO', {'counter_reset': (('FoO', 0),)}),
('counter-set: FoO', {'counter_set': (('FoO', 0),)}),
('counter-increment: foo bAr 2 Bar', {
'counter_increment': (('foo', 1), ('bAr', 2), ('Bar', 1))}),
('counter-reset: none', {'counter_reset': ()}),