Skip to content

Commit ec19643

Browse files
author
Steve Canny
committed
style: add _LatentStyle on/off prop getters
1 parent 75006eb commit ec19643

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

docx/oxml/styles.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,19 @@ class CT_LsdException(BaseOxmlElement):
7878
``<w:lsdException>`` element, defining override visibility behaviors for
7979
a named latent style.
8080
"""
81+
locked = OptionalAttribute('w:locked', ST_OnOff)
8182
name = RequiredAttribute('w:name', ST_String)
83+
qFormat = OptionalAttribute('w:qFormat', ST_OnOff)
84+
semiHidden = OptionalAttribute('w:semiHidden', ST_OnOff)
8285
uiPriority = OptionalAttribute('w:uiPriority', ST_DecimalNumber)
86+
unhideWhenUsed = OptionalAttribute('w:unhideWhenUsed', ST_OnOff)
87+
88+
def on_off_prop(self, attr_name):
89+
"""
90+
Return the boolean value of the attribute having *attr_name*, or
91+
|None| if not present.
92+
"""
93+
return getattr(self, attr_name)
8394

8495

8596
class CT_Style(BaseOxmlElement):

docx/styles/latent.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,26 @@ class _LatentStyle(ElementProxy):
126126

127127
__slots__ = ()
128128

129+
@property
130+
def hidden(self):
131+
"""
132+
Tri-state value specifying whether this latent style should appear in
133+
the recommended list. |None| indicates the effective value is
134+
inherited from the parent ``<w:latentStyles>`` element.
135+
"""
136+
return self._element.on_off_prop('semiHidden')
137+
138+
@property
139+
def locked(self):
140+
"""
141+
Tri-state value specifying whether this latent styles is locked.
142+
A locked style does not appear in the styles panel or the style
143+
gallery and cannot be applied to document content. This behavior is
144+
only active when formatting protection is turned on for the document
145+
(via the Developer menu).
146+
"""
147+
return self._element.on_off_prop('locked')
148+
129149
@property
130150
def name(self):
131151
"""
@@ -143,3 +163,24 @@ def priority(self):
143163
@priority.setter
144164
def priority(self, value):
145165
self._element.uiPriority = value
166+
167+
@property
168+
def quick_style(self):
169+
"""
170+
Tri-state value specifying whether this latent style should appear in
171+
the Word styles gallery when not hidden. |None| indicates the
172+
effective value should be inherited from the default values in its
173+
parent |LatentStyles| object.
174+
"""
175+
return self._element.on_off_prop('qFormat')
176+
177+
@property
178+
def unhide_when_used(self):
179+
"""
180+
Tri-state value specifying whether this style should have its
181+
:attr:`hidden` attribute set |False| the next time the style is
182+
applied to content. |None| indicates the effective value should be
183+
inherited from the default specified by its parent |LatentStyles|
184+
object.
185+
"""
186+
return self._element.on_off_prop('unhideWhenUsed')

features/sty-latent-props.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Feature: Get and set latent style properties
6060
| 42 | None | None |
6161

6262

63-
@wip
6463
Scenario Outline: Get on/off latent style properties
6564
Given a latent style having <prop-name> set <setting>
6665
Then latent_style.<prop-name> is <value>

tests/styles/test_latent.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def it_can_change_its_priority(self, priority_set_fixture):
3030
latent_style.priority = new_value
3131
assert latent_style._element.xml == expected_xml
3232

33+
def it_knows_its_on_off_properties(self, on_off_get_fixture):
34+
latent_style, prop_name, expected_value = on_off_get_fixture
35+
actual_value = getattr(latent_style, prop_name)
36+
assert actual_value == expected_value
37+
3338
# fixtures -------------------------------------------------------
3439

3540
@pytest.fixture(params=[
@@ -40,6 +45,25 @@ def name_get_fixture(self, request):
4045
latent_style = _LatentStyle(element(lsdException_cxml))
4146
return latent_style, expected_value
4247

48+
@pytest.fixture(params=[
49+
('w:lsdException', 'hidden', None),
50+
('w:lsdException', 'locked', None),
51+
('w:lsdException', 'quick_style', None),
52+
('w:lsdException', 'unhide_when_used', None),
53+
('w:lsdException{w:semiHidden=1}', 'hidden', True),
54+
('w:lsdException{w:locked=1}', 'locked', True),
55+
('w:lsdException{w:qFormat=1}', 'quick_style', True),
56+
('w:lsdException{w:unhideWhenUsed=1}', 'unhide_when_used', True),
57+
('w:lsdException{w:semiHidden=0}', 'hidden', False),
58+
('w:lsdException{w:locked=0}', 'locked', False),
59+
('w:lsdException{w:qFormat=0}', 'quick_style', False),
60+
('w:lsdException{w:unhideWhenUsed=0}', 'unhide_when_used', False),
61+
])
62+
def on_off_get_fixture(self, request):
63+
lsdException_cxml, prop_name, expected_value = request.param
64+
latent_style = _LatentStyle(element(lsdException_cxml))
65+
return latent_style, prop_name, expected_value
66+
4367
@pytest.fixture(params=[
4468
('w:lsdException', None),
4569
('w:lsdException{w:uiPriority=42}', 42),

0 commit comments

Comments
 (0)