fixed issue 1631:elements(..., replace=None) deletes the first element only
This commit is contained in:
@@ -1 +1 @@
|
|||||||
Version 2.6.0-development+timestamp.2013.08.15.09.21.06
|
Version 2.6.0-development+timestamp.2013.08.15.10.05.27
|
||||||
|
|||||||
+12
-10
@@ -1075,10 +1075,8 @@ class DIV(XmlComponent):
|
|||||||
matches = []
|
matches = []
|
||||||
# check if the component has an attribute with the same
|
# check if the component has an attribute with the same
|
||||||
# value as provided
|
# value as provided
|
||||||
check = True
|
|
||||||
tag = getattr(self, 'tag').replace('/', '')
|
tag = getattr(self, 'tag').replace('/', '')
|
||||||
if args and tag not in args:
|
check = not (args and tag not in args)
|
||||||
check = False
|
|
||||||
for (key, value) in kargs.iteritems():
|
for (key, value) in kargs.iteritems():
|
||||||
if key not in ['first_only', 'replace', 'find_text']:
|
if key not in ['first_only', 'replace', 'find_text']:
|
||||||
if isinstance(value, (str, int)):
|
if isinstance(value, (str, int)):
|
||||||
@@ -1109,24 +1107,28 @@ class DIV(XmlComponent):
|
|||||||
def replace_component(i):
|
def replace_component(i):
|
||||||
if replace is None:
|
if replace is None:
|
||||||
del self[i]
|
del self[i]
|
||||||
elif callable(replace):
|
return i
|
||||||
self[i] = replace(self[i])
|
|
||||||
else:
|
else:
|
||||||
self[i] = replace
|
self[i] = replace(self[i]) if callable(replace) else replace
|
||||||
|
return i+1
|
||||||
# loop the components
|
# loop the components
|
||||||
if find_text or find_components:
|
if find_text or find_components:
|
||||||
for i, c in enumerate(self.components):
|
i = 0
|
||||||
|
while i<len(self.components):
|
||||||
|
c = self[i]
|
||||||
|
j = i+1
|
||||||
if check and find_text and isinstance(c, str) and \
|
if check and find_text and isinstance(c, str) and \
|
||||||
((is_regex and find_text.search(c)) or (str(find_text) in c)):
|
((is_regex and find_text.search(c)) or (str(find_text) in c)):
|
||||||
replace_component(i)
|
j = replace_component(i)
|
||||||
if find_components and isinstance(c, XmlComponent):
|
elif find_components and isinstance(c, XmlComponent):
|
||||||
child_matches = c.elements(*args, **kargs)
|
child_matches = c.elements(*args, **kargs)
|
||||||
if len(child_matches):
|
if len(child_matches):
|
||||||
if not find_text and replace is not False and child_matches[0] is c:
|
if not find_text and replace is not False and child_matches[0] is c:
|
||||||
replace_component(i)
|
j = replace_component(i)
|
||||||
if first_only:
|
if first_only:
|
||||||
return child_matches
|
return child_matches
|
||||||
matches.extend(child_matches)
|
matches.extend(child_matches)
|
||||||
|
i = j
|
||||||
return matches
|
return matches
|
||||||
|
|
||||||
def element(self, *args, **kargs):
|
def element(self, *args, **kargs):
|
||||||
|
|||||||
Reference in New Issue
Block a user