#-- # Copyright (c) 2006 Assaf Arkin (http://labnotes.org) # Under MIT and/or CC By license. #++ module HTML # Selects HTML elements using CSS 2 selectors. # # The +Selector+ class uses CSS selector expressions to match and select # HTML elements. # # For example: # selector = HTML::Selector.new "form.login[action=/login]" # creates a new selector that matches any +form+ element with the class # +login+ and an attribute +action+ with the value /login. # # === Matching Elements # # Use the #match method to determine if an element matches the selector. # # For simple selectors, the method returns an array with that element, # or +nil+ if the element does not match. For complex selectors (see below) # the method returns an array with all matched elements, of +nil+ if no # match found. # # For example: # if selector.match(element) # puts "Element is a login form" # end # # === Selecting Elements # # Use the #select method to select all matching elements starting with # one element and going through all children in depth-first order. # # This method returns an array of all matching elements, an empty array # if no match is found # # For example: # selector = HTML::Selector.new "input[type=text]" # matches = selector.select(element) # matches.each do |match| # puts "Found text field with name #{match.attributes['name']}" # end # # === Expressions # # Selectors can match elements using any of the following criteria: # * name -- Match an element based on its name (tag name). # For example, p to match a paragraph. You can use * # to match any element. # * #id -- Match an element based on its identifier (the # id attribute). For example, #page. # * .class -- Match an element based on its class name, all # class names if more than one specified. # * [attr] -- Match an element that has the specified attribute. # * [attr=value] -- Match an element that has the specified # attribute and value. (More operators are supported see below) # * :pseudo-class -- Match an element based on a pseudo class, # such as :nth-child and :empty. # * :not(expr) -- Match an element that does not match the # negation expression. # # When using a combination of the above, the element name comes first # followed by identifier, class names, attributes, pseudo classes and # negation in any order. Do not separate these parts with spaces! # Space separation is used for descendant selectors. # # For example: # selector = HTML::Selector.new "form.login[action=/login]" # The matched element must be of type +form+ and have the class +login+. # It may have other classes, but the class +login+ is required to match. # It must also have an attribute called +action+ with the value # /login. # # This selector will match the following element: #