Edgewall Software

Ticket #192: tag-join.patch

File tag-join.patch, 1.8 KB (added by Carsten Klein <carsten.klein@…>, 13 years ago)

Initial patch for this feature

  • builder.py

     
    242244    >>> print(Element(xhtml.html, lang='en'))
    243245    <html xmlns="http://www.w3.org/1999/xhtml" lang="en"/>
    244246    """
    245     __slots__ = ['tag', 'attrib']
     247    __slots__ = ['tag', 'attrib', 'siblings']
    246248
    247249    def __init__(self, tag_, **attrib):
    248250        Fragment.__init__(self)
    249251        self.tag = QName(tag_)
    250252        self.attrib = _kwargs_to_attrs(attrib)
     253        self.siblings = []
    251254
    252255    def __call__(self, *args, **kwargs):
    253256        """Append any positional arguments as child nodes, and keyword arguments
     
    270273            yield kind, data, pos
    271274        yield END, self.tag, (None, -1, -1)
    272275
     276        for sibling in self.siblings:
     277            if isinstance(sibling, Element):
     278                for event in sibling._generate():
     279                    yield event
     280            elif isinstance(sibling, Stream):
     281                for event in sibling:
     282                    yield event
     283            else:
     284                if not isinstance(sibling, basestring):
     285                    child = unicode(sibling)
     286                yield TEXT, sibling, (None, -1, -1)
     287
    273288    def generate(self):
    274289        """Return a markup event stream for the fragment.
    275290       
    276291        :rtype: `Stream`
    277292        """
     293        # FIXME:axn must be Stream(self._generate) to make it JIT
     294        # return Stream(self._generate)
    278295        return Stream(self._generate())
    279296
     297    def join(self, *args):
     298        for arg in args:
     299            if isinstance(arg, list):
     300                for sibling in arg:
     301                    self.siblings.append(sibling)
     302            else:
     303                self.siblings.append(arg)
     304        return self
    280305
     306
    281307class ElementFactory(object):
    282308    """Factory for `Element` objects.
    283309