Open zhaoguixu opened 9 years ago
node must be a lxml element. doc.replace_with(node[0]) should work
It still does not work.
Traceback (most recent call last):
File "
It seems that you forget to check whether the parent of a tag is None?
Looks like I was wrong: must be text
https://github.com/gawel/pyquery/blob/master/tests/test_pyquery.py#L448
the docstring of the replace_with specifies it can be a PyQuery instance.
def replace_with(self, value):
"""replace nodes by value::
>>> doc = PyQuery("<html><div /></html>")
>>> node = PyQuery("<span />")
>>> child = doc.find('div')
>>> child.replace_with(node)
[<div>]
>>> print(doc)
<html><span/></html>
"""
I think the root cause is you fail to check the parent of the tag.
The parent should be checked, do you think so?
It still gives the traceback.
doc.replace_with('')
Traceback (most recent call last):
File "
Looks like you're deep in the code. Feel free to provide a fix. It will be merged except if it break tests. Even better if you provide new test(s)
I just simply check the code. Some other places also ignore the check. I will try. Thanks!
Traceback (most recent call last): File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/pyquery/pyquery.py", line 1324, in replace_with
self.class(tag).before(value + (tag.tail or ''))
File "/usr/local/lib/python2.7/dist-packages/pyquery/pyquery.py", line 1199, in before
if not parent.text:
AttributeError: 'NoneType' object has no attribute 'text'
Why give an attributeerror traceback?