Skip to content

Commit bb16965

Browse files
author
Steve Canny
committed
style: add Styles.latent_styles
1 parent b8b7367 commit bb16965

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

docx/oxml/styles.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ class CT_Styles(BaseOxmlElement):
202202
``<w:styles>`` element, the root element of a styles part, i.e.
203203
styles.xml
204204
"""
205+
_tag_seq = ('w:docDefaults', 'w:latentStyles', 'w:style')
206+
latentStyles = ZeroOrOne('w:latentStyles', successors=_tag_seq[2:])
205207
style = ZeroOrMore('w:style', successors=())
208+
del _tag_seq
206209

207210
def add_style_of_type(self, name, style_type, builtin):
208211
"""

docx/styles/styles.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
absolute_import, division, print_function, unicode_literals
99
)
1010

11+
from .latent import LatentStyles
1112
from ..shared import ElementProxy
1213
from .style import BaseStyle, StyleFactory
1314

@@ -96,6 +97,16 @@ def get_style_id(self, style_or_name, style_type):
9697
else:
9798
return self._get_style_id_from_name(style_or_name, style_type)
9899

100+
@property
101+
def latent_styles(self):
102+
"""
103+
A |LatentStyles| object providing access to the default behaviors for
104+
latent styles and the collection of |_LatentStyle| objects that
105+
define overrides of those defaults for a particular named latent
106+
style.
107+
"""
108+
return LatentStyles(self._element.get_or_add_latentStyles())
109+
99110
def _get_by_id(self, style_id, style_type):
100111
"""
101112
Return the style of *style_type* matching *style_id*. Returns the

tests/styles/test_styles.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212

1313
from docx.enum.style import WD_STYLE_TYPE
1414
from docx.oxml.styles import CT_Style, CT_Styles
15+
from docx.styles.latent import LatentStyles
1516
from docx.styles.style import BaseStyle
1617
from docx.styles.styles import Styles
1718

1819
from ..unitutil.cxml import element
19-
from ..unitutil.mock import call, function_mock, instance_mock, method_mock
20+
from ..unitutil.mock import (
21+
call, class_mock, function_mock, instance_mock, method_mock
22+
)
2023

2124

2225
class DescribeStyles(object):
@@ -135,6 +138,12 @@ def it_raises_on_style_type_mismatch(self, id_style_raises_fixture):
135138
with pytest.raises(ValueError):
136139
styles._get_style_id_from_style(style_, style_type)
137140

141+
def it_provides_access_to_the_latent_styles(self, latent_styles_fixture):
142+
styles, LatentStyles_, latent_styles_ = latent_styles_fixture
143+
latent_styles = styles.latent_styles
144+
LatentStyles_.assert_called_once_with(styles._element.latentStyles)
145+
assert latent_styles is latent_styles_
146+
138147
# fixture --------------------------------------------------------
139148

140149
@pytest.fixture(params=[
@@ -319,6 +328,11 @@ def iter_fixture(self, request, StyleFactory_, style_):
319328
StyleFactory_.return_value = style_
320329
return styles, expected_count, style_, StyleFactory_, expected_calls
321330

331+
@pytest.fixture
332+
def latent_styles_fixture(self, LatentStyles_, latent_styles_):
333+
styles = Styles(element('w:styles/w:latentStyles'))
334+
return styles, LatentStyles_, latent_styles_
335+
322336
@pytest.fixture(params=[
323337
('w:styles', 0),
324338
('w:styles/w:style', 1),
@@ -352,6 +366,17 @@ def _get_style_id_from_name_(self, request):
352366
def _get_style_id_from_style_(self, request):
353367
return method_mock(request, Styles, '_get_style_id_from_style')
354368

369+
@pytest.fixture
370+
def LatentStyles_(self, request, latent_styles_):
371+
return class_mock(
372+
request, 'docx.styles.styles.LatentStyles',
373+
return_value=latent_styles_
374+
)
375+
376+
@pytest.fixture
377+
def latent_styles_(self, request):
378+
return instance_mock(request, LatentStyles)
379+
355380
@pytest.fixture
356381
def style_(self, request):
357382
return instance_mock(request, BaseStyle)

0 commit comments

Comments
 (0)