match
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0 (0)
- 2.2.1 (0)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-2)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-1)
- 4.1.8 (0)
- 4.2.1
- 4.2.7
- 4.2.9
- 5.0.0.1
- 5.1.7
- 5.2.3
- 6.0.0
- 6.1.3.1
- 6.1.7.7
- 7.0.0
- 7.1.3.2
- What's this?
match(conditions)
public
Returns true if the node meets any of the given conditions. The conditions parameter must be a hash of any of the following keys (all are optional):
-
:tag: the node name must match the corresponding value
-
:attributes: a hash. The node’s values must match the corresponding values in the hash.
-
:parent: a hash. The node’s parent must match the corresponding hash.
-
:child: a hash. At least one of the node’s immediate children must meet the criteria described by the hash.
-
:ancestor: a hash. At least one of the node’s ancestors must meet the criteria described by the hash.
-
:descendant: a hash. At least one of the node’s descendants must meet the criteria described by the hash.
-
:sibling: a hash. At least one of the node’s siblings must meet the criteria described by the hash.
-
:after: a hash. The node must be after any sibling meeting the criteria described by the hash, and at least one sibling must match.
-
:before: a hash. The node must be before any sibling meeting the criteria described by the hash, and at least one sibling must match.
-
:children: a hash, for counting children of a node. Accepts the keys:
** :count: either a number or a range which must equal (or
include) the number of children that match.
** :less_than: the number of matching children must be less than
this number.
** :greater_than: the number of matching children must be
greater than this number.
** :only: another hash consisting of the keys to use
to match on the children, and only matching children will be counted.
Conditions are matched using the following algorithm:
-
if the condition is a string, it must be a substring of the value.
-
if the condition is a regexp, it must match the value.
-
if the condition is a number, the value must match number.to_s.
-
if the condition is true, the value must not be nil.
-
if the condition is false or nil, the value must be nil.
Usage:
# test if the node is a "span" tag node.match :tag => "span" # test if the node's parent is a "div" node.match :parent => { :tag => "div" } # test if any of the node's ancestors are "table" tags node.match :ancestor => { :tag => "table" } # test if any of the node's immediate children are "em" tags node.match :child => { :tag => "em" } # test if any of the node's descendants are "strong" tags node.match :descendant => { :tag => "strong" } # test if the node has between 2 and 4 span tags as immediate children node.match :children => { :count => 2..4, :only => { :tag => "span" } } # get funky: test to see if the node is a "div", has a "ul" ancestor # and an "li" parent (with "class" = "enum"), and whether or not it has # a "span" descendant that contains # text matching /hello world/: node.match :tag => "div", :ancestor => { :tag => "ul" }, :parent => { :tag => "li", :attributes => { :class => "enum" } }, :descendant => { :tag => "span", :child => /hello world/ }