gawel / pyquery

A jquery-like library for python
http://pyquery.rtfd.org/
Other
2.3k stars 182 forks source link

html method replacing text after tag #102

Closed donuk closed 6 years ago

donuk commented 9 years ago

Apologies if I'm mis-understanding the way this should work but I'm running the following:

from pyquery import PyQuery
el = PyQuery("<html><span>Replace Me</span>Not Me</html>")
el.find('span').html('Replaced')
print el.outerHtml()

And it outputs the following:

<html><span>Replaced</span></html>

I would expect it to output:

<html><span>Replaced</span>Not Me</html>

I can fix it by removing a line from the html method, but I presume that line was there for a reason?:

diff --git a/pyquery/pyquery.py b/pyquery/pyquery.py
index 628a32d..15a4b2a 100644
--- a/pyquery/pyquery.py
+++ b/pyquery/pyquery.py
@@ -1020,7 +1020,6 @@ class PyQuery(list):
                 if children:
                     tag.extend(children)
                 tag.text = root.text
-                tag.tail = root.tail
         return self

     @with_camel_case_alias
djay commented 8 years ago

I'm having this issue too. That line seems to be a bug to me. Do you want a pull request @gawel ?

djay commented 8 years ago

btw, I think I see why tag.tail = root.tail was included. The thinking being that if I do .html("food<span/>bah") I want ensure that bah is included. However this is already taken care of, as the .tail of the <span/> will be bah. It's a misunderstanding of what .tail is.

gawel commented 8 years ago

I have no problem removing this line if it doesn't break tests. Feel free to provide a PR