Skip to content

Commit

Permalink
run: add Run.underline setter
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyjus authored and Steve Canny committed May 10, 2014
1 parent 4948b90 commit b84d64f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
50 changes: 50 additions & 0 deletions docx/oxml/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,11 @@ def underline(self):
return None
return rPr.underline

@underline.setter
def underline(self, value):
rPr = self.get_or_add_rPr()
rPr.underline = value

def _add_rPr(self):
"""
Return a newly added rPr child element. Assumes one is not present.
Expand Down Expand Up @@ -665,6 +670,11 @@ def remove_strike(self):
for strike in strike_lst:
self.remove(strike)

def remove_u(self):
u_lst = self.findall(qn('w:u'))
for u in u_lst:
self.remove(u)

def remove_vanish(self):
vanish_lst = self.findall(qn('w:vanish'))
for vanish in vanish_lst:
Expand Down Expand Up @@ -767,6 +777,13 @@ def underline(self):
return None
return u.val

@underline.setter
def underline(self, value):
self.remove_u()
if value is not None:
u = self._add_u()
u.val = value

@property
def vanish(self):
"""
Expand All @@ -786,6 +803,14 @@ def _add_rStyle(self, style):
self.insert(0, rStyle)
return rStyle

def _add_u(self):
"""
Return a newly added <w:u/> child element.
"""
u = OxmlElement('w:u')
self.insert(0, u)
return u


class CT_Text(OxmlBaseElement):
"""
Expand Down Expand Up @@ -833,3 +858,28 @@ def val(self):
}
val = self.get(qn('w:val'))
return underline_type_map[val]

@val.setter
def val(self, value):
underline_vals = {
True: 'single',
False: 'none',
WD_UNDERLINE.WORDS: 'words',
WD_UNDERLINE.DOUBLE: 'double',
WD_UNDERLINE.DOTTED: 'dotted',
WD_UNDERLINE.THICK: 'thick',
WD_UNDERLINE.DASH: 'dash',
WD_UNDERLINE.DOT_DASH: 'dotDash',
WD_UNDERLINE.DOT_DOT_DASH: 'dotDotDash',
WD_UNDERLINE.WAVY: 'wave',
WD_UNDERLINE.DOTTED_HEAVY: 'dottedHeavy',
WD_UNDERLINE.DASH_HEAVY: 'dashedHeavy',
WD_UNDERLINE.DOT_DASH_HEAVY: 'dashDotHeavy',
WD_UNDERLINE.DOT_DOT_DASH_HEAVY: 'dashDotDotHeavy',
WD_UNDERLINE.WAVY_HEAVY: 'wavyHeavy',
WD_UNDERLINE.DASH_LONG: 'dashLong',
WD_UNDERLINE.WAVY_DOUBLE: 'wavyDouble',
WD_UNDERLINE.DASH_LONG_HEAVY: 'dashLongHeavy',
}
val = underline_vals[value]
self.set(qn('w:val'), val)
4 changes: 4 additions & 0 deletions docx/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ def underline(self):
"""
return self._r.underline

@underline.setter
def underline(self, value):
self._r.underline = value

@boolproperty
def web_hidden(self):
"""
Expand Down
1 change: 0 additions & 1 deletion features/run-enum-props.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Feature: Query or apply enumerated run property
| double | WD_UNDERLINE.DOUBLE |


@wip
Scenario Outline: Change underline setting for a run
Given a run having <underline-type> underline
When I set the run underline to <new-underline-value>
Expand Down
24 changes: 24 additions & 0 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ def it_knows_its_underline_type(self, underline_get_fixture):
run, expected_value = underline_get_fixture
assert run.underline == expected_value

def it_can_change_its_underline_type(self, underline_set_fixture):
run, underline, expected_xml = underline_set_fixture
run.underline = underline
assert run._r.xml == expected_xml

def it_can_add_text(self, add_text_fixture):
run, text_str, expected_xml, Text_ = add_text_fixture
_text = run.add_text(text_str)
Expand Down Expand Up @@ -370,6 +375,25 @@ def underline_get_fixture(self, request):
run = Run(r)
return run, expected_prop_value

@pytest.fixture(params=[
(None, True, 'single'),
(None, False, 'none'),
(None, None, None),
(None, WD_UNDERLINE.SINGLE, 'single'),
(None, WD_UNDERLINE.WAVY, 'wave'),
('single', True, 'single'),
('single', False, 'none'),
('single', None, None),
('single', WD_UNDERLINE.SINGLE, 'single'),
('single', WD_UNDERLINE.DOTTED, 'dotted'),
])
def underline_set_fixture(self, request):
before_val, underline, expected_val = request.param
r = self.r_bldr_with_underline(before_val).element
run = Run(r)
expected_xml = self.r_bldr_with_underline(expected_val).xml()
return run, underline, expected_xml

# fixture components ---------------------------------------------

@pytest.fixture
Expand Down

0 comments on commit b84d64f

Please sign in to comment.