Skip to content

Commit d4559b8

Browse files
author
Steve Canny
committed
xmlch: add BaseOxmlElement.remove_all()
1 parent 57892dd commit d4559b8

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

docx/oxml/xmlchemy.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ def insert_element_before(self, elm, *tagnames):
119119
self.append(elm)
120120
return elm
121121

122+
def remove_all(self, *tagnames):
123+
"""
124+
Remove all child elements whose tagname (e.g. 'a:p') appears in
125+
*tagnames*.
126+
"""
127+
for tagname in tagnames:
128+
matching = self.findall(qn(tagname))
129+
for child in matching:
130+
self.remove(child)
131+
122132
@property
123133
def xml(self):
124134
"""

tests/oxml/test_xmlchemy.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,29 @@ def it_can_insert_an_element_before_named_successors(
2929
element.insert_element_before(child, *tagnames)
3030
assert element.xml == expected_xml
3131

32+
def it_can_remove_all_children_with_name_in_sequence(
33+
self, remove_fixture):
34+
element, tagnames, expected_xml = remove_fixture
35+
element.remove_all(*tagnames)
36+
assert element.xml == expected_xml
37+
3238
# fixtures ---------------------------------------------
3339

40+
@pytest.fixture(params=[
41+
('biu', 'iu', 'i'),
42+
('bu', 'iu', 'u'),
43+
('bi', 'u', None),
44+
('b', 'iu', None),
45+
('iu', 'biu', 'i'),
46+
('', 'biu', None),
47+
])
48+
def first_fixture(self, request):
49+
present, matching, match = request.param
50+
element = self.rPr_bldr(present).element
51+
tagnames = self.nsptags(matching)
52+
matching_child = element.find(qn('w:%s' % match)) if match else None
53+
return element, tagnames, matching_child
54+
3455
@pytest.fixture(params=[
3556
('iu', 'b', 'iu', 'biu'),
3657
('u', 'b', 'iu', 'bu'),
@@ -49,30 +70,34 @@ def insert_fixture(self, request):
4970
return element, child, tagnames, expected_xml
5071

5172
@pytest.fixture(params=[
52-
('biu', 'iu', 'i'),
53-
('bu', 'iu', 'u'),
54-
('bi', 'u', None),
55-
('b', 'iu', None),
56-
('iu', 'biu', 'i'),
57-
('', 'biu', None),
73+
('biu', 'b', 'iu'), ('biu', 'bi', 'u'), ('bbiiuu', 'i', 'bbuu'),
74+
('biu', 'i', 'bu'), ('biu', 'bu', 'i'), ('bbiiuu', '', 'bbiiuu'),
75+
('biu', 'u', 'bi'), ('biu', 'ui', 'b'), ('bbiiuu', 'bi', 'uu'),
76+
('bu', 'i', 'bu'), ('', 'ui', ''),
5877
])
59-
def first_fixture(self, request):
60-
present, matching, match = request.param
78+
def remove_fixture(self, request):
79+
present, remove, after = request.param
6180
element = self.rPr_bldr(present).element
62-
tagnames = [('w:%s' % char) for char in matching]
63-
matching_child = element.find(qn('w:%s' % match)) if match else None
64-
return element, tagnames, matching_child
81+
tagnames = self.nsptags(remove)
82+
expected_xml = self.rPr_bldr(after).xml()
83+
return element, tagnames, expected_xml
6584

6685
# fixture components ---------------------------------------------
6786

87+
def nsptags(self, letters):
88+
return [('w:%s' % letter) for letter in letters]
89+
6890
def rPr_bldr(self, children):
6991
rPr_bldr = an_rPr().with_nsdecls()
70-
if 'b' in children:
71-
rPr_bldr.with_child(a_b())
72-
if 'i' in children:
73-
rPr_bldr.with_child(an_i())
74-
if 'u' in children:
75-
rPr_bldr.with_child(a_u())
92+
for char in children:
93+
if char == 'b':
94+
rPr_bldr.with_child(a_b())
95+
elif char == 'i':
96+
rPr_bldr.with_child(an_i())
97+
elif char == 'u':
98+
rPr_bldr.with_child(a_u())
99+
else:
100+
raise NotImplementedError("got '%s'" % char)
76101
return rPr_bldr
77102

78103

0 commit comments

Comments
 (0)