Next / Previous / Contents / Shipman's homepage

14.6. addText(): Add text content to an element

See Section 13.5, “addText(): Adding text content to an element”. To simplify the caller's job, we do nothing if s is None, as may be the case with the .text or .tail attribute of an et.Element.

etbuilder.py
# - - -   a d d T e x t

def addText(node, s):
    '''Add text content to an element.

      [ (node is an Element) and (s is a string) ->
          if node has any children ->
              last child's .tail  +:=  s
          else ->
              node.text +:= s ]
    '''
    #-- 1 --
    if not s:
        return

    #-- 2 --
    if len(node) == 0:
        node.text = (node.text or "") + s
    else:
        lastChild = node[-1]
        lastChild.tail = (lastChild.tail or "") + s