719: def nth_child(a, b, of_type, reverse)
720:
721: return lambda { |element| false } if a == 0 and b == 0
722:
723: return lambda { |element| false } if a < 0 and b < 0
724: b = a + b + 1 if b < 0
725: b -= 1 unless b == 0
726: lambda do |element|
727:
728: return false unless element.parent and element.parent.tag?
729: index = 0
730:
731: siblings = element.parent.children
732: siblings = siblings.reverse if reverse
733:
734: name = of_type ? element.name : nil
735: found = false
736: for child in siblings
737:
738: if child.tag? and (name == nil or child.name == name)
739: if a == 0
740:
741: if index == b
742: found = child.equal?(element)
743: break
744: end
745: elsif a < 0
746:
747: break if index > b
748: if child.equal?(element)
749: found = (index % a) == 0
750: break
751: end
752: else
753:
754: if child.equal?(element)
755: found = (index % a) == b
756: break
757: end
758: end
759: index += 1
760: end
761: end
762: found
763: end
764: end