Class: Nokogiri::CSS::Node
- Inherits:
-
Object
- Object
- Nokogiri::CSS::Node
- Defined in:
- lib/nokogiri/css/node.rb
Instance Attribute Summary collapse
-
#type ⇒ Object
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #accept(visitor) ⇒ Object
- #find_by_type(types) ⇒ Object
-
#initialize(type, value) ⇒ Node
constructor
A new instance of Node.
- #preprocess! ⇒ Object
- #to_a ⇒ Object
- #to_type ⇒ Object
- #to_xpath(prefix = '//', preprocess = true) ⇒ Object
Constructor Details
#initialize(type, value) ⇒ Node
Returns a new instance of Node.
5 6 7 8 |
# File 'lib/nokogiri/css/node.rb', line 5 def initialize type, value @type = type @value = value end |
Instance Attribute Details
#type ⇒ Object
Returns the value of attribute type.
4 5 6 |
# File 'lib/nokogiri/css/node.rb', line 4 def type @type end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'lib/nokogiri/css/node.rb', line 4 def value @value end |
Instance Method Details
#accept(visitor) ⇒ Object
10 11 12 |
# File 'lib/nokogiri/css/node.rb', line 10 def accept visitor visitor.send(:"visit_#{type.to_s.downcase}", self) end |
#find_by_type(types) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/nokogiri/css/node.rb', line 75 def find_by_type(types) matches = [] matches << self if to_type == types @value.each do |v| matches += v.find_by_type(types) if v.respond_to?(:find_by_type) end matches end |
#preprocess! ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/nokogiri/css/node.rb', line 19 def preprocess! ### Deal with nth-child matches = find_by_type( [:CONDITIONAL_SELECTOR, [:ELEMENT_NAME], [:PSEUDO_CLASS, [:FUNCTION] ] ] ) matches.each do |match| if match.value[1].value[0].value[0] =~ /^nth-child/ tag_name = match.value[0].value.first match.value[0].value = ['*'] match.value[1] = Node.new(:COMBINATOR, [ match.value[1].value[0], Node.new(:FUNCTION, ['self(', tag_name]) ]) end if match.value[1].value[0].value[0] =~ /^nth-last-child/ tag_name = match.value[0].value.first match.value[0].value = ['*'] match.value[1] = Node.new(:COMBINATOR, [ match.value[1].value[0], Node.new(:FUNCTION, ['self(', tag_name]) ]) end end ### Deal with first-child, last-child matches = find_by_type( [:CONDITIONAL_SELECTOR, [:ELEMENT_NAME], [:PSEUDO_CLASS] ]) matches.each do |match| if ['first-child', 'last-child'].include?(match.value[1].value.first) which = match.value[1].value.first.gsub(/-\w*$/, '') tag_name = match.value[0].value.first match.value[0].value = ['*'] match.value[1] = Node.new(:COMBINATOR, [ Node.new(:FUNCTION, ["#{which}("]), Node.new(:FUNCTION, ['self(', tag_name]) ]) elsif 'only-child' == match.value[1].value.first tag_name = match.value[0].value.first match.value[0].value = ['*'] match.value[1] = Node.new(:COMBINATOR, [ Node.new(:FUNCTION, ["#{match.value[1].value.first}("]), Node.new(:FUNCTION, ['self(', tag_name]) ]) end end self end |
#to_a ⇒ Object
90 91 92 |
# File 'lib/nokogiri/css/node.rb', line 90 def to_a [@type] + @value.map { |n| n.to_a } end |
#to_type ⇒ Object
84 85 86 87 88 |
# File 'lib/nokogiri/css/node.rb', line 84 def to_type [@type] + @value.map { |n| n.to_type if n.respond_to?(:to_type) }.compact end |
#to_xpath(prefix = '//', preprocess = true) ⇒ Object
14 15 16 17 |
# File 'lib/nokogiri/css/node.rb', line 14 def to_xpath prefix = '//', preprocess = true self.preprocess! if preprocess prefix + XPathVisitor.new.accept(self) end |