Skip to content

Commit 9d45201

Browse files
Copilotchrisjsewell
andcommitted
🐛 FIX: Fix ValueError in tab-set with invalid children (#243)
Co-authored-by: chrisjsewell <2997570+chrisjsewell@users.noreply.github.com>
1 parent b22c820 commit 9d45201

3 files changed

Lines changed: 73 additions & 5 deletions

File tree

sphinx_design/tabs.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def run_with_defaults(self) -> list[nodes.Node]:
3535
)
3636
self.set_source_info(tab_set)
3737
self.state.nested_parse(self.content, self.content_offset, tab_set)
38+
valid_children = []
3839
for item in tab_set.children:
3940
if not is_component(item, "tab-item"):
4041
LOGGER.warning(
@@ -44,9 +45,12 @@ def run_with_defaults(self) -> list[nodes.Node]:
4445
type=WARNING_TYPE,
4546
subtype="tab",
4647
)
47-
break
48+
continue # Skip invalid children instead of breaking
4849
if "sync_id" in item.children[0]:
4950
item.children[0]["sync_group"] = self.options.get("sync-group", "tab")
51+
valid_children.append(item)
52+
53+
tab_set.children = valid_children
5054
return [tab_set]
5155

5256

@@ -240,10 +244,17 @@ def run(self) -> None:
240244
selected_idx = 0 if selected_idx is None else selected_idx
241245

242246
for idx, tab_item in enumerate(tab_set.children):
243-
try:
244-
tab_label, tab_content = tab_item.children
245-
except ValueError:
246-
raise
247+
if not is_component(tab_item, "tab-item"):
248+
continue # Skip non tab-item children
249+
if len(tab_item.children) != 2:
250+
LOGGER.warning(
251+
f"Malformed 'tab-item' directive [{WARNING_TYPE}.tab]",
252+
location=tab_item,
253+
type=WARNING_TYPE,
254+
subtype="tab",
255+
)
256+
continue
257+
tab_label, tab_content = tab_item.children
247258
tab_item_identity = tab_item_id_base + str(tab_item_id_num)
248259
tab_item_id_num += 1
249260

tests/test_misc.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,43 @@ def test_material(style, file_regression):
3030
for name in sorted(data):
3131
content += f"{name}: {','.join(data[name]['heights'])}\n"
3232
file_regression.check(content)
33+
34+
35+
def test_tab_set_with_invalid_children(sphinx_builder, file_regression, normalize_doctree_xml):
36+
"""Test that tab-set with invalid children does not crash.
37+
38+
This reproduces the issue from https://github.com/executablebooks/sphinx-design/issues/243
39+
where a ValueError was raised when a tab-set contained non-tab-item children.
40+
"""
41+
builder = sphinx_builder(conf_kwargs={"extensions": ["sphinx_design"]})
42+
builder.src_path.joinpath("index.rst").write_text(
43+
"""
44+
Tab Test document
45+
=================
46+
47+
.. tab-set::
48+
49+
.. tab-item:: A
50+
51+
A content
52+
53+
foo
54+
55+
.. tab-item:: B
56+
57+
B content
58+
""",
59+
encoding="utf8",
60+
)
61+
# Build should not crash, but should produce a warning
62+
builder.build(assert_pass=False)
63+
assert "All children of a 'tab-set' should be 'tab-item'" in builder.warnings
64+
65+
# Valid tab items should still be processed
66+
doctree = builder.get_doctree("index", post_transforms=True)
67+
doctree.attributes.pop("translation_progress", None)
68+
file_regression.check(
69+
normalize_doctree_xml(doctree.pformat()),
70+
extension=".xml",
71+
encoding="utf8",
72+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<document source="index">
2+
<section ids="tab-test-document" names="tab\ test\ document">
3+
<title>
4+
Tab Test document
5+
<container classes="sd-tab-set" design_component="tab-set" is_div="True">
6+
<sd_tab_input checked="True" id="sd-tab-item-0" set_id="sd-tab-set-0" type="radio">
7+
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-0">
8+
A
9+
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
10+
<paragraph>
11+
A content
12+
<sd_tab_input checked="False" id="sd-tab-item-1" set_id="sd-tab-set-0" type="radio">
13+
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-1">
14+
B
15+
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
16+
<paragraph>
17+
B content

0 commit comments

Comments
 (0)