Skip to content

Commit cca918b

Browse files
committed
test_text: Update, cleanup
Cleaned up code to follow standards.
1 parent 48e6bb2 commit cca918b

8 files changed

Lines changed: 70 additions & 51 deletions

File tree

docx/oxml/shared.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,11 @@ def val(self, val):
342342
return self.set(qn('w:val'), val)
343343

344344

345-
class CT_Shading(OxmlBaseElement):
345+
class CT_Shd(OxmlBaseElement):
346346
@classmethod
347347
def new(cls, nsptagname, valDict):
348348
"""
349-
Return a new ``CT_Shading`` element with tagname *nsptagname* and
349+
Return a new ``CT_Shd`` element with tagname *nsptagname* and
350350
attributes set by key, value pairs in *valDict*.
351351
"""
352352
attrsDict = {}

docx/oxml/table.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from docx.oxml.shared import OxmlBaseElement, OxmlElement, qn
1010

1111
from .exceptions import ValidationError
12-
from .shared import CT_String, CT_DecimalNumber, CT_Shading
12+
from .shared import CT_String, CT_DecimalNumber, CT_Shd
1313
from .text import CT_P
1414

1515

@@ -287,7 +287,7 @@ def add_gridSpan(self, span):
287287
the existing child elements.gridSpan
288288
"""
289289
gridSpan = CT_DecimalNumber.new('w:gridSpan', span)
290-
self.append(gridSpan) # append or insert?
290+
self.insert(0, gridSpan)
291291
return gridSpan
292292

293293
@property
@@ -302,8 +302,8 @@ def add_shading(self, argDict):
302302
Return a new <w:shd> element newly inserted in sequence among
303303
the existing child elements.gridSpan
304304
"""
305-
shading = CT_Shading.new('w:shd', argDict)
306-
self.append(shading) # append or insert?
305+
shading = CT_Shd.new('w:shd', argDict)
306+
self.insert(0, shading)
307307
return shading
308308

309309
@property

docx/oxml/text.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,20 +288,7 @@ class CT_RPr(OxmlBaseElement):
288288
``<w:rPr>`` element, containing the properties for a run.
289289
"""
290290

291-
@property
292-
def underline(self):
293-
"""
294-
First ``<w:u>`` child element or None if none are present.
295-
"""
296-
return self.find(qn('w:u'))
297-
298-
def add_underline(self, style):
299-
"""
300-
Return a newly added <w:u w:val=style/> child element.
301-
"""
302-
u = CT_String.new('w:u', WD_UNDERLINE.stringDict[style])
303-
self.insert(0, u)
304-
return u
291+
305292

306293
def add_b(self):
307294
"""
@@ -462,6 +449,14 @@ def add_webHidden(self):
462449
webHidden = OxmlElement('w:webHidden')
463450
self.insert(0, webHidden)
464451
return webHidden
452+
453+
def add_underline(self, utype):
454+
"""
455+
Return a newly added <w:u w:val=utype/> child element.
456+
"""
457+
u = CT_String.new('w:u', WD_UNDERLINE.stringDict[utype])
458+
self.insert(0, u)
459+
return u
465460

466461
@property
467462
def b(self):
@@ -653,6 +648,11 @@ def remove_webHidden(self):
653648
webHidden_lst = self.findall(qn('w:webHidden'))
654649
for webHidden in webHidden_lst:
655650
self.remove(webHidden)
651+
652+
def remove_underline(self):
653+
underline_lst = self.findall(qn('w:u'))
654+
for underline in underline_lst:
655+
self.remove(underline)
656656

657657
@property
658658
def rtl(self):
@@ -709,6 +709,13 @@ def webHidden(self):
709709
First ``<w:webHidden>`` child element or None if none are present.
710710
"""
711711
return self.find(qn('w:webHidden'))
712+
713+
@property
714+
def underline(self):
715+
"""
716+
First ``<w:u>`` child element or None if none are present.
717+
"""
718+
return self.find(qn('w:u'))
712719

713720

714721
class CT_Text(OxmlBaseElement):

docx/table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def set_shading(self, val = None, color = None, themeColor = None,
156156
themeFillShade = None, argDict = None):
157157
"""
158158
Convienence method for setting individual values of the shading dict.
159-
It only replaces values that are not `None`.
159+
It only replaces values that are not None.
160160
"""
161161
if self.shading is None:
162162
valDict = {}

docx/text.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,18 +230,18 @@ def underline(self):
230230
return u.val
231231

232232
@underline.setter
233-
def underline(self, style):
233+
def underline(self, utype):
234234
# interperate style
235-
if style is None:
236-
style = WD_UNDERLINE.NONE
237-
if type(style)==bool:
238-
style = WD_UNDERLINE.SINGLE if style else WD_UNDERLINE.NONE
235+
if utype is None:
236+
utype = WD_UNDERLINE.NONE
237+
if type(utype)==bool:
238+
utype = WD_UNDERLINE.SINGLE if utype else WD_UNDERLINE.NONE
239239

240240
u = self._r.get_or_add_rPr().underline
241241
if u is None:
242-
self._r.get_or_add_rPr().add_underline(style)
242+
self._r.get_or_add_rPr().add_underline(utype)
243243
else:
244-
u.val = style
244+
u.val = utype
245245

246246
@boolproperty
247247
def imprint(self):

tests/oxml/unitdata/shared.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ def __init__(self, tag):
3131
def with_val(self, value):
3232
self._set_xmlattr('w:val', str(value))
3333
return self
34+
35+
class CT_Underline(BaseBuilder):
36+
__nspfxs__ = ('w',)
37+
__attrs__ = ('w:val')
38+
39+
def __init__(self, tag):
40+
self.__tag__ = tag
41+
super(CT_Underline, self).__init__()
42+
43+
def with_val(self, value):
44+
self._set_xmlattr('w:val', str(value))
45+
return self

tests/oxml/unitdata/text.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
from ...unitdata import BaseBuilder
8-
from .shared import CT_OnOffBuilder, CT_StringBuilder
8+
from .shared import CT_OnOffBuilder, CT_StringBuilder, CT_Underline
99

1010

1111
class CT_BrBuilder(BaseBuilder):
@@ -137,8 +137,8 @@ def an_emboss():
137137
def an_i():
138138
return CT_OnOffBuilder('w:i')
139139

140-
def an_u():
141-
return CT_StringBuilder('w:u')
140+
def a_u():
141+
return CT_Underline('w:u')
142142

143143
def an_iCs():
144144
return CT_OnOffBuilder('w:iCs')

tests/test_text.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
a_b, a_bCs, a_br, a_caps, a_cs, a_dstrike, a_p, a_shadow, a_smallCaps,
2121
a_snapToGrid, a_specVanish, a_strike, a_t, a_vanish, a_webHidden,
2222
an_emboss, an_i, an_iCs, an_imprint, an_oMath, a_noProof, an_outline,
23-
an_r, an_rPr, an_rtl, an_u
23+
an_r, an_rPr, an_rtl, a_u
2424
)
2525
from .unitutil import class_mock, instance_mock
2626

@@ -164,26 +164,26 @@ def it_knows_the_text_it_contains(self, text_prop_fixture):
164164
])
165165
def add_underline_fixture(self, request, run):
166166
underline_type = {
167-
'dash':WD_UNDERLINE.DASH,
168-
'dashHeavy':WD_UNDERLINE.DASH_HEAVY,
169-
'dashLong':WD_UNDERLINE.DASH_LONG,
170-
'longHeavy':WD_UNDERLINE.LONG_HEAVY,
171-
'dotDash':WD_UNDERLINE.DOT_DASH,
172-
'dotDashHeavy':WD_UNDERLINE.DOT_DASH_HEAVY,
173-
'dotDotDash':WD_UNDERLINE.DOT_DOT_DASH,
174-
'dotDotDashHeavy':WD_UNDERLINE.DOT_DOT_DASH_HEAVY,
175-
'dotted':WD_UNDERLINE.DOTTED,
176-
'dottedHeavy':WD_UNDERLINE.DOTTED_HEAVY,
177-
'double':WD_UNDERLINE.DOUBLE,
178-
'none':WD_UNDERLINE.NONE,
179-
'single':WD_UNDERLINE.SINGLE,
180-
'thick':WD_UNDERLINE.THICK,
181-
'wavy':WD_UNDERLINE.WAVY,
182-
'wavyDouble':WD_UNDERLINE.WAVY_DOUBLE,
183-
'wavyHeavy':WD_UNDERLINE.WAVY_HEAVY,
184-
'words':WD_UNDERLINE.WORDS,
167+
'dash': WD_UNDERLINE.DASH,
168+
'dashHeavy': WD_UNDERLINE.DASH_HEAVY,
169+
'dashLong': WD_UNDERLINE.DASH_LONG,
170+
'longHeavy': WD_UNDERLINE.LONG_HEAVY,
171+
'dotDash': WD_UNDERLINE.DOT_DASH,
172+
'dotDashHeavy': WD_UNDERLINE.DOT_DASH_HEAVY,
173+
'dotDotDash': WD_UNDERLINE.DOT_DOT_DASH,
174+
'dotDotDashHeavy': WD_UNDERLINE.DOT_DOT_DASH_HEAVY,
175+
'dotted': WD_UNDERLINE.DOTTED,
176+
'dottedHeavy': WD_UNDERLINE.DOTTED_HEAVY,
177+
'double': WD_UNDERLINE.DOUBLE,
178+
'none': WD_UNDERLINE.NONE,
179+
'single': WD_UNDERLINE.SINGLE,
180+
'thick': WD_UNDERLINE.THICK,
181+
'wavy': WD_UNDERLINE.WAVY,
182+
'wavyDouble': WD_UNDERLINE.WAVY_DOUBLE,
183+
'wavyHeavy': WD_UNDERLINE.WAVY_HEAVY,
184+
'words': WD_UNDERLINE.WORDS,
185185
}[request.param]
186-
u_bldr = an_u()
186+
u_bldr = a_u()
187187
expected_xml = an_r().with_nsdecls().with_child(u_bldr).xml()
188188
return run, underline_type, expected_xml
189189

0 commit comments

Comments
 (0)