Skip to content

Commit 400b4e5

Browse files
author
Steve Canny
committed
style: add Styles.__contains__()
1 parent f933976 commit 400b4e5

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

docx/styles/styles.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ class Styles(ElementProxy):
2121

2222
__slots__ = ()
2323

24+
def __contains__(self, name):
25+
"""
26+
Enables `in` operator on style name.
27+
"""
28+
name = self._translate_special_case_names(name)
29+
for style in self._element.style_lst:
30+
if style.name_val == name:
31+
return True
32+
return False
33+
2434
def __getitem__(self, key):
2535
"""
2636
Enables dictionary-style access by style id or UI name.

tests/styles/test_styles.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
class DescribeStyles(object):
2222

23+
def it_supports_the_in_operator_on_style_name(self, in_fixture):
24+
styles, name, expected_value = in_fixture
25+
assert (name in styles) is expected_value
26+
2327
def it_knows_its_length(self, len_fixture):
2428
styles, expected_value = len_fixture
2529
assert len(styles) == expected_value
@@ -250,6 +254,17 @@ def id_style_raises_fixture(self, style_):
250254
style_type = 2
251255
return styles, style_, style_type
252256

257+
@pytest.fixture(params=[
258+
('w:styles/w:style/w:name{w:val=heading 1}', 'Heading 1', True),
259+
('w:styles/w:style/w:name{w:val=Foo Bar}', 'Foo Bar', True),
260+
('w:styles/w:style/w:name{w:val=heading 1}', 'Foobar', False),
261+
('w:styles', 'Foobar', False),
262+
])
263+
def in_fixture(self, request):
264+
styles_cxml, name, expected_value = request.param
265+
styles = Styles(element(styles_cxml))
266+
return styles, name, expected_value
267+
253268
@pytest.fixture(params=[
254269
('w:styles', 0),
255270
('w:styles/w:style', 1),

0 commit comments

Comments
 (0)