Skip to content

Commit

Permalink
fix: shuffled-index in nested shuffle-contents
Browse files Browse the repository at this point in the history
  • Loading branch information
janbritz committed Dec 2, 2024
1 parent 3e228ed commit cb9b984
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
14 changes: 14 additions & 0 deletions questionpy_sdk/webserver/question_ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,24 @@ 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(element: etree._Element, index_element: etree._Element) -> bool:
ancestor = index_element.getparent()
while ancestor is not None and ancestor != element:
if f"{{{_QPY_NAMESPACE}}}shuffle-contents" in ancestor.attrib:
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(element, index_element):
# 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.
continue

format_style = index_element.get("format", "123")

if format_style == "123":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div xmlns="http://www.w3.org/1999/xhtml" xmlns:qpy="http://questionpy.org/ns/question" qpy:shuffle-contents="">
<p><qpy:shuffled-index format="iii"/>. A</p>
<p><qpy:shuffled-index format="iii"/>. B</p>
<div qpy:shuffle-contents="">
<p><qpy:shuffled-index format="iii"/>. C</p>
<p><qpy:shuffled-index format="iii"/>. D</p>
</div>
</div>
18 changes: 18 additions & 0 deletions tests/questionpy_sdk/webserver/test_question_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
<div>
<p><span>i</span>. B</p>
<p><span>ii</span>. A</p>
<div>
<p><span>i</span>. D</p>
<p><span>ii</span>. C</p>
</div>
</div>
"""
html, errors = renderer.render()
assert len(errors) == 0
assert_html_is_equal(html, expected)


@pytest.mark.render_params(
xml="""
<div xmlns:qpy="http://questionpy.org/ns/question">
Expand Down

0 comments on commit cb9b984

Please sign in to comment.