# File lib/html/selector.rb, line 719
719:     def nth_child(a, b, of_type, reverse)
720:       # a = 0 means select at index b, if b = 0 nothing selected
721:       return lambda { |element| false } if a == 0 and b == 0
722:       # a < 0 and b < 0 will never match against an index
723:       return lambda { |element| false } if a < 0 and b < 0
724:       b = a + b + 1 if b < 0   # b < 0 just picks last element from each group
725:       b -= 1 unless b == 0  # b == 0 is same as b == 1, otherwise zero based
726:       lambda do |element|
727:         # Element must be inside parent element.
728:         return false unless element.parent and element.parent.tag?
729:         index = 0
730:         # Get siblings, reverse if counting from last.
731:         siblings = element.parent.children
732:         siblings = siblings.reverse if reverse
733:         # Match element name if of-type, otherwise ignore name.
734:         name = of_type ? element.name : nil
735:         found = false
736:         for child in siblings
737:           # Skip text nodes/comments.
738:           if child.tag? and (name == nil or child.name == name)
739:             if a == 0
740:               # Shortcut when a == 0 no need to go past count
741:               if index == b
742:                 found = child.equal?(element)
743:                 break
744:               end
745:             elsif a < 0
746:               # Only look for first b elements
747:               break if index > b
748:               if child.equal?(element)
749:                 found = (index % a) == 0
750:                 break
751:               end
752:             else
753:               # Otherwise, break if child found and count ==  an+b
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