summaryrefslogtreecommitdiff
path: root/src/lxml/classlookup.pxi
diff options
context:
space:
mode:
authorscoder <none@none>2009-02-20 19:30:55 +0100
committerscoder <none@none>2009-02-20 19:30:55 +0100
commitaf69c0c18b5c0493aa0592a2961c60c23924bace (patch)
tree1772ab00290804f9194559372ad3e2c3ba5b154d /src/lxml/classlookup.pxi
parent6a9de2d84d8d8872db9fbdd585cd89db5238b64a (diff)
downloadpython-lxml-af69c0c18b5c0493aa0592a2961c60c23924bace.tar.gz
[svn r4106] r5037@delle: sbehnel | 2009-02-20 19:27:56 +0100
extended ElementBase constructor: support text content, Element classes and instances --HG-- branch : trunk
Diffstat (limited to 'src/lxml/classlookup.pxi')
-rw-r--r--src/lxml/classlookup.pxi33
1 files changed, 27 insertions, 6 deletions
diff --git a/src/lxml/classlookup.pxi b/src/lxml/classlookup.pxi
index bb1ff6f0..7012dda3 100644
--- a/src/lxml/classlookup.pxi
+++ b/src/lxml/classlookup.pxi
@@ -44,12 +44,13 @@ cdef public class ElementBase(_Element) [ type LxmlElementBaseType,
"""
cdef bint is_html = 0
cdef _BaseParser parser
+ cdef _Element last_child
try:
namespace = _utf8(self.NAMESPACE)
except AttributeError:
namespace = None
try:
- tag, ns = _getNsTag(self.TAG)
+ ns, tag = _getNsTag(self.TAG)
if ns is not None:
namespace = ns
except AttributeError:
@@ -59,10 +60,11 @@ cdef public class ElementBase(_Element) [ type LxmlElementBaseType,
try:
parser = self.PARSER
except AttributeError:
- if python.PyTuple_GET_SIZE(children):
- parser = (<_Element>children[0])._doc._parser
- else:
- parser = None
+ parser = None
+ for child in children:
+ if isinstance(child, _Element):
+ parser = (<_Element>child)._doc._parser
+ break
if isinstance(parser, HTMLParser):
is_html = 1
if namespace is None:
@@ -72,8 +74,27 @@ cdef public class ElementBase(_Element) [ type LxmlElementBaseType,
pass
_initNewElement(self, is_html, tag, namespace, parser,
attrib, nsmap, _extra)
+ last_child = None
for child in children:
- _appendChild(self, child)
+ if _isString(child):
+ if last_child is None:
+ if _hasText(self._c_node):
+ self.text += child
+ else:
+ self.text = child
+ else:
+ if _hasTail(last_child._c_node):
+ last_child.tail += child
+ else:
+ last_child.tail = child
+ elif isinstance(child, _Element):
+ last_child = child
+ _appendChild(self, last_child)
+ elif isinstance(child, type) and issubclass(child, ElementBase):
+ last_child = child()
+ _appendChild(self, last_child)
+ else:
+ raise TypeError, "Invalid child type: %r" % type(child)
cdef class CommentBase(_Comment):
u"""All custom Comment classes must inherit from this one.