From af69c0c18b5c0493aa0592a2961c60c23924bace Mon Sep 17 00:00:00 2001 From: scoder Date: Fri, 20 Feb 2009 19:30:55 +0100 Subject: [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 --- src/lxml/classlookup.pxi | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) (limited to 'src/lxml/classlookup.pxi') 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. -- cgit v1.2.1