Class: Nokogiri::CSS::Node

### Home Classes Methods

### In Files

- lib/nokogiri/css/node.rb

### Parent

Object

### Methods

- ::new
- #accept
- #find_by_type
- #preprocess!
- #to_a
- #to_type
- #to_xpath

### Files

- CHANGELOG.ja.rdoc
- CHANGELOG.rdoc
- C_CODING_STYLE.rdoc
- Manifest.txt
- README.ja.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- Nokogiri
- Nokogiri::CSS
- Nokogiri::CSS::Node
- Nokogiri::CSS::Parser
- Nokogiri::CSS::SyntaxError
- Nokogiri::CSS::Tokenizer
- Nokogiri::CSS::Tokenizer::ScanError
- Nokogiri::Decorators
- Nokogiri::Decorators::Slop
- Nokogiri::EncodingHandler
- Nokogiri::HTML
- Nokogiri::HTML::Builder
- Nokogiri::HTML::Document
- Nokogiri::HTML::Document::EncodingReader
- Nokogiri::HTML::Document::EncodingReader::JumpSAXHandler
- Nokogiri::HTML::DocumentFragment
- Nokogiri::HTML::ElementDescription
- Nokogiri::HTML::EntityDescription
- Nokogiri::HTML::EntityLookup
- Nokogiri::HTML::SAX
- Nokogiri::HTML::SAX::Parser
- Nokogiri::HTML::SAX::ParserContext
- Nokogiri::HTML::SAX::PushParser
- Nokogiri::SyntaxError
- Nokogiri::XML
- Nokogiri::XML::Attr
- Nokogiri::XML::AttributeDecl
- Nokogiri::XML::Builder
- Nokogiri::XML::CDATA
- Nokogiri::XML::CharacterData
- Nokogiri::XML::Comment
- Nokogiri::XML::DTD
- Nokogiri::XML::Document
- Nokogiri::XML::DocumentFragment
- Nokogiri::XML::Element
- Nokogiri::XML::ElementContent
- Nokogiri::XML::ElementDecl
- Nokogiri::XML::EntityDecl
- Nokogiri::XML::EntityReference
- Nokogiri::XML::Namespace
- Nokogiri::XML::Node
- Nokogiri::XML::Node::SaveOptions
- Nokogiri::XML::NodeSet
- Nokogiri::XML::Notation
- Nokogiri::XML::PP
- Nokogiri::XML::PP::CharacterData
- Nokogiri::XML::PP::Node
- Nokogiri::XML::ParseOptions
- Nokogiri::XML::ProcessingInstruction
- Nokogiri::XML::Reader
- Nokogiri::XML::RelaxNG
- Nokogiri::XML::SAX
- Nokogiri::XML::SAX::Document
- Nokogiri::XML::SAX::Parser
- Nokogiri::XML::SAX::Parser::Attribute
- Nokogiri::XML::SAX::ParserContext
- Nokogiri::XML::SAX::PushParser
- Nokogiri::XML::Schema
- Nokogiri::XML::SyntaxError
- Nokogiri::XML::Text
- Nokogiri::XML::XPath
- Nokogiri::XML::XPath::SyntaxError
- Nokogiri::XML::XPathContext
- Nokogiri::XSLT
- Nokogiri::XSLT::Stylesheet
- XSD
- XSD::XMLParser
- XSD::XMLParser::Nokogiri
- Object

No matching classes.

# Nokogiri::CSS::Node

### Constants

ALLOW_COMBINATOR_ON_SELF

### Attributes

type[RW]

Get the type of this node

value[RW]

Get the value of this node

### Public Class Methods

new(type, value) click to toggle source

Create a new Node with `type` and `value`

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

### Public Instance Methods

accept(visitor) click to toggle source

Accept `visitor`

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

find_by_type(types) click to toggle source

Find a node by type using `types`

    # File lib/nokogiri/css/node.rb, line 80
    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!() click to toggle source

Preprocess this node tree

    # File lib/nokogiri/css/node.rb, line 31
    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() click to toggle source

Convert to array

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

to_type() click to toggle source

Convert to_type

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

to_xpath(prefix = '//', visitor = XPathVisitor.new) click to toggle source

Convert this CSS node to xpath with `prefix` using `visitor`

    # File lib/nokogiri/css/node.rb, line 24
    def to_xpath prefix = '//', visitor = XPathVisitor.new
      self.preprocess!
      prefix = '.' if ALLOW_COMBINATOR_ON_SELF.include?(type) && value.first.nil?
      prefix + visitor.accept(self)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.