Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👌 IMPROVE: Make tab ids deterministic #93

Merged
merged 1 commit into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions sphinx_design/tabs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from typing import List
from uuid import uuid4

from docutils import nodes
from docutils.parsers.rst import directives
Expand Down Expand Up @@ -208,15 +207,20 @@ class TabSetHtmlTransform(SphinxPostTransform):
default_priority = 200
formats = ("html",)

def get_unique_key(self):
return str(uuid4())

def run(self) -> None:
"""Run the transform."""

# setup id generators
tab_set_id_base = "sd-tab-set-"
tab_set_id_num = 0
tab_item_id_base = "sd-tab-item-"
tab_item_id_num = 0

for tab_set in self.document.traverse(
lambda node: is_component(node, "tab-set")
):
tab_set_identity = self.get_unique_key()
tab_set_identity = tab_set_id_base + str(tab_set_id_num)
tab_set_id_num += 1
children = []
# get the first selected node
selected_idx = None
Expand All @@ -239,7 +243,8 @@ def run(self) -> None:
except ValueError:
print(tab_item)
raise
tab_item_identity = self.get_unique_key()
tab_item_identity = tab_item_id_base + str(tab_item_id_num)
tab_item_id_num += 1

# create: <input checked="checked" id="id" type="radio">
input_node = sd_tab_input(
Expand Down
5 changes: 0 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from sphinx.testing.util import SphinxTestApp

from sphinx_design._compat import findall
from sphinx_design.tabs import TabSetHtmlTransform

pytest_plugins = "sphinx.testing.fixtures"

Expand Down Expand Up @@ -63,10 +62,6 @@ def get_doctree(

@pytest.fixture()
def sphinx_builder(tmp_path: Path, make_app, monkeypatch):

# make sure tabbed id keys are reproducible across test runs
monkeypatch.setattr(TabSetHtmlTransform, "get_unique_key", lambda self: "mock-uuid")

def _create_project(
buildername: str = "html", conf_kwargs: Optional[Dict[str, Any]] = None
):
Expand Down
8 changes: 4 additions & 4 deletions tests/test_snippets/snippet_post_tab-basic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<title>
Heading
<container classes="sd-tab-set" design_component="tab-set" is_div="True">
<sd_tab_input checked="True" id="mock-uuid" set_id="mock-uuid" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="mock-uuid">
<sd_tab_input checked="True" id="sd-tab-item-0" set_id="sd-tab-set-0" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-0">
Label1
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
<paragraph>
Content 1
<sd_tab_input checked="False" id="mock-uuid" set_id="mock-uuid" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="mock-uuid">
<sd_tab_input checked="False" id="sd-tab-item-1" set_id="sd-tab-set-0" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-1">
Label2
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
<paragraph>
Expand Down
8 changes: 4 additions & 4 deletions tests/test_snippets/snippet_post_tab-code-set.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<title>
Heading
<container classes="sd-tab-set" design_component="tab-set" is_div="True">
<sd_tab_input checked="True" id="mock-uuid" set_id="mock-uuid" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="mock-uuid" sync_id="tabcode-python">
<sd_tab_input checked="True" id="sd-tab-item-0" set_id="sd-tab-set-0" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-0" sync_id="tabcode-python">
PYTHON
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
<literal_block force="False" highlight_args="{'linenostart': 1}" language="python" linenos="False" source="snippet.py" xml:space="preserve">
a = 1
<sd_tab_input checked="False" id="mock-uuid" set_id="mock-uuid" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="mock-uuid" sync_id="tabcode-javascript">
<sd_tab_input checked="False" id="sd-tab-item-1" set_id="sd-tab-set-0" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-1" sync_id="tabcode-javascript">
JAVASCRIPT
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
<literal_block force="False" highlight_args="{}" language="javascript" linenos="False" xml:space="preserve">
Expand Down
4 changes: 2 additions & 2 deletions tests/test_snippets/snippet_post_tab-options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<title>
Heading
<container classes="sd-tab-set class-set" design_component="tab-set" is_div="True">
<sd_tab_input checked="True" id="mock-uuid" set_id="mock-uuid" type="radio">
<sd_tab_label classes="sd-tab-label class-label" ids="target" input_id="mock-uuid">
<sd_tab_input checked="True" id="sd-tab-item-0" set_id="sd-tab-set-0" type="radio">
<sd_tab_label classes="sd-tab-label class-label" ids="target" input_id="sd-tab-item-0">
Label
<container classes="sd-tab-content class-content" design_component="tab-content" is_div="True">
<paragraph>
Expand Down
16 changes: 8 additions & 8 deletions tests/test_snippets/snippet_post_tab-sync.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
<title>
Heading
<container classes="sd-tab-set" design_component="tab-set" is_div="True">
<sd_tab_input checked="True" id="mock-uuid" set_id="mock-uuid" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="mock-uuid" sync_id="key1">
<sd_tab_input checked="True" id="sd-tab-item-0" set_id="sd-tab-set-0" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-0" sync_id="key1">
Label1
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
<paragraph>
Content 1
<sd_tab_input checked="False" id="mock-uuid" set_id="mock-uuid" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="mock-uuid" sync_id="key2">
<sd_tab_input checked="False" id="sd-tab-item-1" set_id="sd-tab-set-0" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-1" sync_id="key2">
Label2
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
<paragraph>
Content 2
<container classes="sd-tab-set" design_component="tab-set" is_div="True">
<sd_tab_input checked="True" id="mock-uuid" set_id="mock-uuid" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="mock-uuid" sync_id="key1">
<sd_tab_input checked="True" id="sd-tab-item-2" set_id="sd-tab-set-1" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-2" sync_id="key1">
Label1
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
<paragraph>
Content 1
<sd_tab_input checked="False" id="mock-uuid" set_id="mock-uuid" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="mock-uuid" sync_id="key2">
<sd_tab_input checked="False" id="sd-tab-item-3" set_id="sd-tab-set-1" type="radio">
<sd_tab_label classes="sd-tab-label" input_id="sd-tab-item-3" sync_id="key2">
Label2
<container classes="sd-tab-content" design_component="tab-content" is_div="True">
<paragraph>
Expand Down