diff --git a/questionpy_sdk/webserver/question_ui/__init__.py b/questionpy_sdk/webserver/question_ui/__init__.py index b311f3ba..2c5505a0 100644 --- a/questionpy_sdk/webserver/question_ui/__init__.py +++ b/questionpy_sdk/webserver/question_ui/__init__.py @@ -76,10 +76,25 @@ def _set_element_value(element: etree._Element, value: str, name: str, xpath: et element.set("value", value) +def _check_shuffled_index_is_in_nested_shuffle_contents(index_element: etree._Element) -> bool: + ancestor = index_element.getparent() + while ancestor is not None and ancestor != index_element: + if f"{{{_QPY_NAMESPACE}}}shuffle-contents" in ancestor.attrib: + # The index element is in a nested shuffle-contents. + # We want it to be replaced with the index of the inner shuffle, so we ignore it for now. + return True + + ancestor = ancestor.getparent() + return False + + def _replace_shuffled_indices(element: etree._Element, index: int) -> None: for index_element in _assert_element_list( element.xpath(".//qpy:shuffled-index", namespaces={"qpy": _QPY_NAMESPACE}) ): + if _check_shuffled_index_is_in_nested_shuffle_contents(index_element): + continue + format_style = index_element.get("format", "123") if format_style == "123": diff --git a/tests/questionpy_sdk/webserver/test_question_ui.py b/tests/questionpy_sdk/webserver/test_question_ui.py index cea57767..d613b0fe 100644 --- a/tests/questionpy_sdk/webserver/test_question_ui.py +++ b/tests/questionpy_sdk/webserver/test_question_ui.py @@ -319,6 +319,24 @@ def test_should_replace_shuffled_index(renderer: QuestionUIRenderer) -> None: assert_html_is_equal(html, expected) +@pytest.mark.ui_file("shuffled-index-nested") +@pytest.mark.render_params(seed=42) +def test_should_replace_shuffled_index_in_nested(renderer: QuestionUIRenderer) -> None: + expected = """ +
i. B
+ii. A
+i. D
+ii. C
+