select_nth_of_type

returns elements that are the nth child of their parents.

Arguments

Source


def select_nth_of_type(self, i):
    parents = list()
    ret = pyNodeList()
    tag = self[0].name()
    for node in self:
        duplicated = False
        if len(parents) != 0:
            for p in parents:
                if p == node.parent():
                    duplicated = True
                    break
        if not duplicated: parents.append(node.parent())
        if tag != node.name():
            return ret
    ret = pyNodeList()
    if len(parents) != 0:
        for parent in parents:
            ch = parent.child(tag)
            if isinstance(ch, pyNodeList) and len(ch) != 0:
                n = ch.eq(i - 1)
                if n is not None: ret.append(n)
    if len(ret) == 1: return ret[0]
    else: return ret