Changeset 2755:2fc601fa2051 in livinglogic.python.xist

Show
Ignore:
Timestamp:
07/04/07 14:39:33 (12 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Enhance nthchild and nthoftype so that "even" and "odd" are recognized as
legal index values (however their meaning is inverted from their CSS3
meaning as Python indexes start at 0).

Enhance nthoftype, so that the types for which a node is checked can be
passed as additional constructor arguments. If none are gives nthoftype
falls back to the old behaviour of using the type of the node itself.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/ll/xist/xfind.py

    r2750 r2755  
    616616            return False 
    617617        try: 
     618            if self.index in ("even", "odd"): 
     619                for (i, child) in enumerate(path[-2]): 
     620                    if child is path[-1]: 
     621                        return (i % 2) == (self.index == "odd") 
     622                return False # can't happen 
    618623            return path[-2][self.index] is path[-1] 
    619624        except IndexError: 
     
    625630 
    626631class nthoftype(Selector): 
    627     def __init__(self, index): 
     632    def __init__(self, index, *types): 
    628633        self.index = index 
     634        self.types = types 
    629635 
    630636    def match(self, path): 
     
    632638            return False 
    633639        try: 
    634             return path[-2][path[-1].__class__][self.index] is path[-1] 
     640            types = self.types if self.types else path[-1].__class__ 
     641            if self.index in ("even", "odd"): 
     642                i = 0 
     643                for child in path[-2]: 
     644                    if isinstance(child, types): 
     645                        if child is path[-1]: 
     646                            return (i % 2) == (self.index == "odd") 
     647                        i += 1 
     648            else: 
     649                i = 0 
     650                for child in path[-2]: 
     651                    if isinstance(child, types): 
     652                        if child is path[-1]: 
     653                            return i == self.index 
     654                        i += 1 
     655            return False 
    635656        except IndexError: 
    636657            return False 
    637658 
    638659    def __repr__(self): 
    639         return "%s(%r)" % (self.__class__.__name__, self.index) 
     660        if self.types: 
     661            return "%s(%r, %s)" % (self.__class__.__name__, self.index, ", ".join("%s.%s" % (type.__module__, type.__name__) for type in self.types)) 
     662        else: 
     663            return "%s(%r)" % (self.__class__.__name__, self.index) 
    640664 
    641665