From 7474aa7a3a6c2a7cacd3b3c5067201bb608029c4 Mon Sep 17 00:00:00 2001 From: capellancitizen Date: Wed, 28 Aug 2024 20:12:16 -0400 Subject: Fixed hidden objects being stitched out when cloned (Fix #3167) (#3171) Extracted Base.descendants into a util function --- tests/test_clone.py | 34 ++++++++++++++++++++++++++++- tests/test_elements_utils.py | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 tests/test_elements_utils.py (limited to 'tests') diff --git a/tests/test_clone.py b/tests/test_clone.py index 1c84f3e9..07683a16 100644 --- a/tests/test_clone.py +++ b/tests/test_clone.py @@ -1,6 +1,6 @@ from lib.elements import Clone, EmbroideryElement, FillStitch from lib.commands import add_commands -from lib.svg.tags import INKSTITCH_ATTRIBS, SVG_RECT_TAG +from lib.svg.tags import INKSTITCH_ATTRIBS, SVG_RECT_TAG, INKSCAPE_LABEL from lib.utils import cache_module from inkex import SvgDocumentElement, Rectangle, Circle, Group, Use, Transform, TextElement from inkex.tester import TestCase @@ -19,6 +19,7 @@ def element_fill_angle(element: EmbroideryElement) -> Optional[float]: class CloneElementTest(TestCase): + # Monkey-patch the cahce to forcibly disable it: We may need to refactor this out for tests. def setUp(self): from pytest import MonkeyPatch self.monkeypatch = MonkeyPatch() @@ -78,6 +79,37 @@ class CloneElementTest(TestCase): self.assertEqual(len(elements), 1) self.assertAlmostEqual(element_fill_angle(elements[0]), 30) + def test_hidden_cloned_elements_not_embroidered(self): + root = svg() + g = root.add(Group()) + g.add(Rectangle(attrib={ + INKSCAPE_LABEL: "NotHidden", + "width": "10", + "height": "10" + })) + g.add(Rectangle(attrib={ + INKSCAPE_LABEL: "Hidden", + "width": "10", + "height": "10", + "style": "display:none" + })) + hidden_group = g.add(Group(attrib={ + "style": "display:none" + })) + hidden_group.add(Rectangle(attrib={ + INKSCAPE_LABEL: "ChildOfHidden", + "width": "10", + "height": "10", + })) + use = root.add(Use()) + use.href = g + + clone = Clone(use) + + with clone.clone_elements() as elements: + self.assertEqual(len(elements), 1) + self.assertEqual(elements[0].node.get(INKSCAPE_LABEL), "NotHidden") + def test_angle_rotated(self): root: SvgDocumentElement = svg() rect = root.add(Rectangle(attrib={ diff --git a/tests/test_elements_utils.py b/tests/test_elements_utils.py new file mode 100644 index 00000000..651635be --- /dev/null +++ b/tests/test_elements_utils.py @@ -0,0 +1,52 @@ +from lib.elements import utils, FillStitch +from inkex import Rectangle, Group, Style +from inkex.tester import TestCase +from inkex.tester.svg import svg + + +class ElementsUtilsTest(TestCase): + # These tests test two functions at once, but they're sort of complimentary. + # Might suggest that they could be combined in a later refactor? + def test_iterate_nodes_to_elements(self): + root = svg() + g = root.add(Group()) + rect = g.add(Rectangle(attrib={ + "width": "10", + "height": "10" + })) + hidden_rect = g.add(Rectangle(attrib={ # noqa: F841 + "width": "10", + "height": "10", + "style": "display:none" + })) + hidden_group = g.add(Group(attrib={ + "style": "display:none" + })) + child_of_hidden = hidden_group.add(Rectangle(attrib={ # noqa: F841 + "width": "10", + "height": "10", + })) + + elements = utils.nodes_to_elements(utils.iterate_nodes(g)) + self.assertEqual(len(elements), 1) + self.assertEqual(type(elements[0]), FillStitch) + self.assertEqual(elements[0].node, rect) + + def test_iterate_nodes_to_elements_root_embroiderable(self): + """ Case where the root node is directly embroiderable """ + root = svg() + rect = root.add(Rectangle(attrib={ + "width": "10", + "height": "10" + })) + + elements = utils.nodes_to_elements(utils.iterate_nodes(rect)) + self.assertEqual(len(elements), 1) + self.assertEqual(type(elements[0]), FillStitch) + self.assertEqual(elements[0].node, rect) + + # Now make the element hidden: It shouldn't return an element + rect.style = rect.style + Style({"display": "none"}) + + elements = utils.nodes_to_elements(utils.iterate_nodes(rect)) + self.assertEqual(len(elements), 0) -- cgit v1.2.3