diff --git a/weasyprint/formatting_structure/build.py b/weasyprint/formatting_structure/build.py index 9a116debda..1e7b30de65 100644 --- a/weasyprint/formatting_structure/build.py +++ b/weasyprint/formatting_structure/build.py @@ -209,7 +209,7 @@ def before_after_to_box(element, pseudo_type, state, style_for, def content_to_boxes(style, parent_box, quote_depth, counter_values, - get_image_from_uri, context=None): + get_image_from_uri, context=None, page=None): """Takes the value of a ``content`` property and yield boxes.""" texts = [] for type_, value in style.content: @@ -233,8 +233,8 @@ def content_to_boxes(style, parent_box, quote_depth, counter_values, counters.format(counter_value, counter_style) for counter_value in counter_values.get(counter_name, [0]) )) - elif type_ == 'string' and context is not None: - text = context.get_string_set_for(*value) + elif type_ == 'string' and context is not None and page is not None: + text = context.get_string_set_for(page, *value) texts.append(text) else: assert type_ == 'QUOTE' diff --git a/weasyprint/layout/__init__.py b/weasyprint/layout/__init__.py index e375fbf13e..62803681aa 100644 --- a/weasyprint/layout/__init__.py +++ b/weasyprint/layout/__init__.py @@ -25,6 +25,7 @@ from .pages import make_all_pages, make_margin_boxes from .backgrounds import layout_backgrounds from ..compat import xrange +from ..formatting_structure import boxes def layout_fixed_boxes(context, pages): @@ -100,7 +101,7 @@ def finish_block_formatting_context(self, root_box): else: self.excluded_shapes = None - def get_string_set_for(self, name, keyword=None): + def get_string_set_for(self, page, name, keyword='first'): """Resolve value of string function (as set by string set). We'll have something like this that represents all assignments on a @@ -119,19 +120,28 @@ def get_string_set_for(self, name, keyword=None): :returns: text """ - last = keyword == "last" if self.current_page in self.string_set[name]: - # a value was assigned on this page - if keyword == 'first-except': - # 'first-except' excludes the page it was assinged on - return "" - elif last: - # use the most recent assignment - return self.string_set[name][self.current_page][-1] - return self.string_set[name][self.current_page][0] - else: - # search backwards through previous pages - for previous_page in xrange(self.current_page, 0, -1): - if previous_page in self.string_set[name]: - return self.string_set[name][previous_page][-1] - return "" + # A value was assigned on this page + first_string = self.string_set[name][self.current_page][0] + last_string = self.string_set[name][self.current_page][-1] + if keyword == 'first': + return first_string + elif keyword == 'start': + element = page + while element: + if element.style['string_set'] != 'none': + for (string_name, _) in element.style['string_set']: + if string_name == name: + return first_string + if isinstance(element, boxes.ParentBox): + if element.children: + element = element.children[0] + continue + break + elif keyword == 'last': + return last_string + # Search backwards through previous pages + for previous_page in xrange(self.current_page - 1, 0, -1): + if previous_page in self.string_set[name]: + return self.string_set[name][previous_page][-1] + return '' diff --git a/weasyprint/layout/pages.py b/weasyprint/layout/pages.py index 7f54cc174b..43a8459cf5 100644 --- a/weasyprint/layout/pages.py +++ b/weasyprint/layout/pages.py @@ -310,7 +310,7 @@ def make_box(at_keyword, containing_block): quote_depth = [0] box.children = build.content_to_boxes( box.style, box, quote_depth, counter_values, - context.get_image_from_uri, context) + context.get_image_from_uri, context, page) # content_to_boxes() only produces inline-level boxes, no need to # run other post-processors from build.build_formatting_structure() box = build.inline_in_block(box)