def match(element, first_only = false)
if matched = (!@tag_name || @tag_name == element.name)
for attr in @attributes
if element.attributes[attr[0]] !~ attr[1]
matched = false
break
end
end
end
if matched
for pseudo in @pseudo
unless pseudo.call(element)
matched = false
break
end
end
end
if matched && @negation
for negation in @negation
if negation[:tag_name] == element.name
matched = false
else
for attr in negation[:attributes]
if element.attributes[attr[0]] =~ attr[1]
matched = false
break
end
end
end
if matched
for pseudo in negation[:pseudo]
if pseudo.call(element)
matched = false
break
end
end
end
break unless matched
end
end
if matched && @depends
matches = @depends.call(element, first_only)
else
matches = matched ? [element] : nil
end
if !first_only || !matches
@alternates.each do |alternate|
break if matches && first_only
if subset = alternate.match(element, first_only)
if matches
matches.concat subset
else
matches = subset
end
end
end
end
matches
end