Skip to content

Commit d96f45f

Browse files
author
Steve Canny
committed
style: add name translation to latent styles
Use BabelFish to translate between UI and internal style names when necessary.
1 parent 656d356 commit d96f45f

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

docx/styles/latent.py

Lines changed: 5 additions & 3 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 . import BabelFish
1112
from ..shared import ElementProxy
1213

1314

@@ -24,7 +25,8 @@ def __getitem__(self, key):
2425
"""
2526
Enables dictionary-style access to a latent style by name.
2627
"""
27-
lsdException = self._element.get_by_name(key)
28+
style_name = BabelFish.ui2internal(key)
29+
lsdException = self._element.get_by_name(style_name)
2830
if lsdException is None:
2931
raise KeyError("no latent style with name '%s'" % key)
3032
return _LatentStyle(lsdException)
@@ -42,7 +44,7 @@ def add_latent_style(self, name):
4244
having *name*.
4345
"""
4446
lsdException = self._element.add_lsdException()
45-
lsdException.name = name
47+
lsdException.name = BabelFish.ui2internal(name)
4648
return _LatentStyle(lsdException)
4749

4850
@property
@@ -179,7 +181,7 @@ def name(self):
179181
"""
180182
The name of the built-in style this exception applies to.
181183
"""
182-
return self._element.name
184+
return BabelFish.internal2ui(self._element.name)
183185

184186
@property
185187
def priority(self):

tests/styles/test_latent.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def delete_fixture(self):
5656
return latent_style, latent_styles, expected_xml
5757

5858
@pytest.fixture(params=[
59-
('w:lsdException{w:name=Foobar}', 'Foobar'),
59+
('w:lsdException{w:name=heading 1}', 'Heading 1'),
6060
])
6161
def name_get_fixture(self, request):
6262
lsdException_cxml, expected_value = request.param
@@ -194,8 +194,8 @@ def it_can_change_its_boolean_properties(self, bool_prop_set_fixture):
194194
@pytest.fixture
195195
def add_fixture(self):
196196
latent_styles = LatentStyles(element('w:latentStyles'))
197-
name = 'Foobar'
198-
expected_xml = xml('w:latentStyles/w:lsdException{w:name=Foobar}')
197+
name = 'Heading 1'
198+
expected_xml = xml('w:latentStyles/w:lsdException{w:name=heading 1}')
199199
return latent_styles, name, expected_xml
200200

201201
@pytest.fixture(params=[
@@ -261,6 +261,7 @@ def count_set_fixture(self, request):
261261
('w:lsdException{w:name=Ab},w:lsdException,w:lsdException', 'Ab', 0),
262262
('w:lsdException,w:lsdException{w:name=Cd},w:lsdException', 'Cd', 1),
263263
('w:lsdException,w:lsdException,w:lsdException{w:name=Ef}', 'Ef', 2),
264+
('w:lsdException{w:name=heading 1}', 'Heading 1', 0),
264265
])
265266
def getitem_fixture(self, request):
266267
cxml, name, idx = request.param

0 commit comments

Comments
 (0)