fix for mixed content model representation in ElementTree
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1507 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
b49cc08b36
commit
113924fb11
2 changed files with 17 additions and 7 deletions
|
@ -28,7 +28,8 @@ The elements generator lets you easily create snippets of XML or XHTML:
|
|||
</body>
|
||||
</html>
|
||||
|
||||
An XML element thus created may be converted to an ElementTree:
|
||||
An XML element thus created may be converted to an ElementTree (the standard
|
||||
implementation in fact uses an lxml.etree structure as its basis):
|
||||
|
||||
>>> doc.renderTree()
|
||||
'<html><head><title>Page Title</title></head><body><div class="top">The top bar</div><div class="body">The body stuff</div></body></html>'
|
||||
|
@ -92,3 +93,11 @@ just by accessing an element's attributes:
|
|||
>>> print doc.render()
|
||||
<html>...<p...>...</p>...
|
||||
|
||||
>>> x = p('Some more text').b('bold text')
|
||||
>>> x = p('and normal again')
|
||||
>>> print p.render()
|
||||
<p...>...home...Some more text
|
||||
<b>...bold text...</b>
|
||||
...and normal again
|
||||
</p>
|
||||
|
||||
|
|
|
@ -36,8 +36,8 @@ class Element(object):
|
|||
result.append(base.text)
|
||||
for c in base.getchildren():
|
||||
result.append(Element(c))
|
||||
if base.tail:
|
||||
result.append(base.tail)
|
||||
if c.tail: # the children's tails belong to this element
|
||||
result.append(c.tail)
|
||||
return result
|
||||
|
||||
@property
|
||||
|
@ -59,10 +59,11 @@ class Element(object):
|
|||
for c in children:
|
||||
if isinstance(c, Element):
|
||||
base.append(c.baseElement)
|
||||
elif not base:
|
||||
base.text = base.text and '\n'.join(base.text, c) or c
|
||||
else:
|
||||
base.tail = base.tail and '\n'.join(base.tail, c) or c
|
||||
elif not base: # no children yet, so it's the first text node
|
||||
base.text = base.text and ' '.join((base.text, c)) or c
|
||||
else: # if there are children, append text to the last child's tail
|
||||
lastChild = base.getchildren()[-1]
|
||||
lastChild.tail = lastChild.tail and ' '.join((lastChild.tail, c)) or c
|
||||
for a in attributes:
|
||||
if a.endswith('_'):
|
||||
attr = a[:-1]
|
||||
|
|
Loading…
Add table
Reference in a new issue