Module: Fdlint::Parser::HTML::Query

Included in:
Tag
Defined in:
lib/fdlint/parser/html/query.rb

Constant Summary collapse

WORD =
/[A-Za-z][-_\w]*/
CLASS =
%r(\.#{WORD})
ID =
%r(##{WORD})
PROP_PAIR =
%r(\s*,?\s*#{WORD}(==[^\s,])?)
PROP =
%r(\[#{WORD}.*?\])

Instance Method Summary collapse

Instance Method Details

#match?(str) ⇒ Boolean Also known as: ===

This method implemented CSS selector for HTML (like Sizzle) very simply. It is not fully supported CSS selector.

TODO: support full CSS3 selector

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
# File 'lib/fdlint/parser/html/query.rb', line 20

def match?(str)
  query       = query_obj(str)
  tag_query   = query[:tag]
  class_query = query[:classes]
  prop_query  = query[:properties]

  (tag_query.blank?   || match_tag?( tag_query )) &&
  (class_query.blank? || match_class?( class_query )) &&
  (prop_query.blank?  || match_prop?( prop_query ))
end

#query(selector, &block) ⇒ Object Also known as: *



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fdlint/parser/html/query.rb', line 92

def query( selector, &block )
  ret = []
  if match?(selector)
    ret << self 
    yield self if block_given?
  end

  children && children.each do |node|
    ret += node.query(selector, &block)
  end

  ret
end