Skip to content

Commit 3912043

Browse files
author
Steve Canny
committed
style: add LatentStyles.__len__()
1 parent bb16965 commit 3912043

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

docx/oxml/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ def OxmlElement(nsptag_str, attrs=None, nsdecls=None):
113113
register_element_cls('wp:extent', CT_PositiveSize2D)
114114
register_element_cls('wp:inline', CT_Inline)
115115

116-
from .styles import CT_Style, CT_Styles
116+
from .styles import CT_LatentStyles, CT_Style, CT_Styles
117117
register_element_cls('w:basedOn', CT_String)
118+
register_element_cls('w:latentStyles', CT_LatentStyles)
118119
register_element_cls('w:locked', CT_OnOff)
119120
register_element_cls('w:name', CT_String)
120121
register_element_cls('w:qFormat', CT_OnOff)

docx/oxml/styles.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ def styleId_from_name(name):
3030
}.get(name, name.replace(' ', ''))
3131

3232

33+
class CT_LatentStyles(BaseOxmlElement):
34+
"""
35+
`w:latentStyles` element, defining behavior defaults for latent styles
36+
and containing `w:lsdException` child elements that each override those
37+
defaults for a named latent style.
38+
"""
39+
lsdException = ZeroOrMore('w:lsdException', successors=())
40+
41+
3342
class CT_Style(BaseOxmlElement):
3443
"""
3544
A ``<w:style>`` element, representing a style definition

docx/styles/latent.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class LatentStyles(ElementProxy):
2020

2121
__slots__ = ()
2222

23+
def __len__(self):
24+
return len(self._element.lsdException_lst)
25+
2326

2427
class _LatentStyle(ElementProxy):
2528
"""

features/sty-access-latent-styles.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ Feature: Access latent styles for a document
44
I need access to the latent styles collection
55

66

7-
@wip
87
Scenario: Access latent styles collection
98
Given the style collection of a document
109
Then styles.latent_styles is the LatentStyles object for the document

tests/styles/test_latent.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Unit test suite for the docx.styles.latent module
5+
"""
6+
7+
from __future__ import (
8+
absolute_import, division, print_function, unicode_literals
9+
)
10+
11+
import pytest
12+
13+
from docx.styles.latent import LatentStyles
14+
15+
from ..unitutil.cxml import element
16+
17+
18+
class DescribeLatentStyles(object):
19+
20+
def it_knows_how_many_latent_styles_it_contains(self, len_fixture):
21+
latent_styles, expected_value = len_fixture
22+
assert len(latent_styles) == expected_value
23+
24+
# fixture --------------------------------------------------------
25+
26+
@pytest.fixture(params=[
27+
('w:latentStyles', 0),
28+
('w:latentStyles/w:lsdException', 1),
29+
('w:latentStyles/(w:lsdException,w:lsdException)', 2),
30+
])
31+
def len_fixture(self, request):
32+
latentStyles_cxml, count = request.param
33+
latent_styles = LatentStyles(element(latentStyles_cxml))
34+
return latent_styles, count

0 commit comments

Comments
 (0)