Install
sudo gem install nokogiri
Contribute
github.com/tenderlove/nokogiri

An HTML, XML, SAX, & Reader parser with the ability to search documents via XPath or CSS3 selectors… and much more

Nokogiri

Class Nokogiri::CSS::Node inherits from Object

Attributes

type RW

Get the type of this node

value RW

Get the value of this node

Public Class Methods

new(type, value) Show Source

Create a new Node with type and value

# File lib/nokogiri/css/node.rb, line 10 def initialize type, value @type = type @value = value end

Public Instance Methods

accept(visitor) Show Source

Accept visitor

# File lib/nokogiri/css/node.rb, line 16 def accept visitor visitor.send(:"visit_#{type.to_s.downcase}", self) end
find_by_type(types) Show Source

Find a node by type using types

# File lib/nokogiri/css/node.rb, line 77 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!() Show Source

Preprocess this node tree

# File lib/nokogiri/css/node.rb, line 28 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-(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() Show Source

Convert to array

# File lib/nokogiri/css/node.rb, line 94 def to_a [@type] + @value.map { |n| n.respond_to?(:to_a) ? n.to_a : [n] } end
to_type() Show Source

Convert to_type

# File lib/nokogiri/css/node.rb, line 87 def to_type [@type] + @value.map { |n| n.to_type if n.respond_to?(:to_type) }.compact end
to_xpath(prefix = '//', visitor = XPathVisitor.new) Show Source

Convert this CSS node to xpath with prefix using visitor

# File lib/nokogiri/css/node.rb, line 22 def to_xpath prefix = '//', visitor = XPathVisitor.new self.preprocess! prefix + visitor.accept(self) end