gawel / pyquery

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

append/appendTo breaks on text nodes #32

Open acdha opened 11 years ago

acdha commented 11 years ago

Using pyquery 1.2.4 with Python 3.3.0 on OS X 10.8:

Python 3.3.0 (default, Oct 22 2012, 11:17:43) 
[GCC 4.2.1 Compatible Apple Clang 4.0 ((tags/Apple/clang-421.0.57))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pyquery import PyQuery as PQ
>>> PQ('<div>').append('foobar').contents().appendTo(PQ('<div>'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/cadams/.virtualenvs/chris.improbable.org/lib/python3.3/site-packages/pyquery/pyquery.py", line 1068, in appendTo
    value.append(self)
  File "/Users/cadams/.virtualenvs/chris.improbable.org/lib/python3.3/site-packages/pyquery/pyquery.py", line 1061, in append
    tag.extend(root)
  File "lxml.etree.pyx", line 769, in lxml.etree._Element.extend (src/lxml/lxml.etree.c:38840)
TypeError: Cannot convert lxml.etree._ElementUnicodeResult to lxml.etree._Element
yodalee commented 10 years ago

There are two problem in this issue:

p = pq('<div>').append('footbar')
print(p) # this will be fine

However, first if you call

print(p.contents())

There will be error that: Type 'lxml.etree._ElementUnicodeResult' cannot be serialized. Since the p content will return a list in unicode result, and tostring cannot apply on it.


and the second one is the reported one: tag is a lxml.html.Element and root is a ElementUnicodeResult, and unicode object cannot convert to Element directly.