Class: Nokogiri::CSS::Parser

### Home Classes Methods

### In Files

- lib/nokogiri/css/parser.rb
- lib/nokogiri/css/parser_extras.rb

### Parent

Racc::Parser

### Methods

- ::[]
- ::[]=
- ::clear_cache
- ::new
- ::parse
- ::without_cache
- #_reduce_1
- #_reduce_10
- #_reduce_11
- #_reduce_12
- #_reduce_14
- #_reduce_15
- #_reduce_16
- #_reduce_17
- #_reduce_18
- #_reduce_19
- #_reduce_2
- #_reduce_21
- #_reduce_23
- #_reduce_24
- #_reduce_25
- #_reduce_26
- #_reduce_28
- #_reduce_29
- #_reduce_3
- #_reduce_30
- #_reduce_31
- #_reduce_32
- #_reduce_33
- #_reduce_34
- #_reduce_35
- #_reduce_36
- #_reduce_37
- #_reduce_38
- #_reduce_39
- #_reduce_4
- #_reduce_40
- #_reduce_43
- #_reduce_44
- #_reduce_45
- #_reduce_46
- #_reduce_47
- #_reduce_48
- #_reduce_5
- #_reduce_51
- #_reduce_52
- #_reduce_53
- #_reduce_54
- #_reduce_59
- #_reduce_6
- #_reduce_60
- #_reduce_61
- #_reduce_63
- #_reduce_64
- #_reduce_65
- #_reduce_66
- #_reduce_67
- #_reduce_68
- #_reduce_69
- #_reduce_7
- #_reduce_70
- #_reduce_8
- #_reduce_9
- #_reduce_none
- #next_token
- #on_error
- #parse
- #xpath_for

### 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::Parser

### Constants

Racc_arg
Racc_debug_parser
Racc_token_to_s_table

### Attributes

cache_on[RW]

Turn on CSS parse caching

cache_on?[RW]

Turn on CSS parse caching

set_cache[RW]

Turn on CSS parse caching

### Public Class Methods

[](string) click to toggle source

Get the css selector in `string` from the cache

    # File lib/nokogiri/css/parser_extras.rb, line 17
    def [] string
      return unless @cache_on
      @mutex.synchronize { @cache[string] }
    end

[]=(string, value) click to toggle source

Set the css selector in `string` in the cache to `value`

    # File lib/nokogiri/css/parser_extras.rb, line 23
    def []= string, value
      return value unless @cache_on
      @mutex.synchronize { @cache[string] = value }
    end

clear_cache() click to toggle source

Clear the cache

    # File lib/nokogiri/css/parser_extras.rb, line 29
    def clear_cache
      @mutex.synchronize { @cache = {} }
    end

new(namespaces = {}) click to toggle source

Create a new CSS parser with respect to `namespaces`

    # File lib/nokogiri/css/parser_extras.rb, line 54
    def initialize namespaces = {}
      @tokenizer  = Tokenizer.new
      @namespaces = namespaces
      super()
    end

parse(selector) click to toggle source

Parse this CSS selector in `selector`. Returns an AST.

    # File lib/nokogiri/css/parser_extras.rb, line 43
    def parse selector
      @warned ||= false
      unless @warned
        $stderr.puts('Nokogiri::CSS::Parser.parse is deprecated, call Nokogiri::CSS.parse(), this will be removed August 1st or version 1.4.0 (whichever is first)')
        @warned = true
      end
      new.parse selector
    end

without_cache(&block) click to toggle source

Execute `block` without cache

    # File lib/nokogiri/css/parser_extras.rb, line 34
    def without_cache &block
      tmp = @cache_on
      @cache_on = false
      block.call
      @cache_on = tmp
    end

### Public Instance Methods

_reduce_1(val, _values, result) click to toggle source

reduce 0 omitted

    # File lib/nokogiri/css/parser.rb, line 303
    def _reduce_1(val, _values, result)
            result = [val.first, val.last].flatten

        result
    end

_reduce_10(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 349
    def _reduce_10(val, _values, result)
            result =  if val[1].nil?
                        val.first
                      else
                        Node.new(:CONDITIONAL_SELECTOR, [val.first, val[1]])
                      end

        result
    end

_reduce_11(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 359
    def _reduce_11(val, _values, result)
            result = Node.new(:CONDITIONAL_SELECTOR,
              [
                val.first,
                Node.new(:COMBINATOR, [val[1], val.last])
              ]
            )

        result
    end

_reduce_12(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 370
    def _reduce_12(val, _values, result)
            result = Node.new(:CONDITIONAL_SELECTOR, val)

        result
    end

_reduce_14(val, _values, result) click to toggle source

reduce 13 omitted

    # File lib/nokogiri/css/parser.rb, line 378
    def _reduce_14(val, _values, result)
            result = Node.new(:CONDITIONAL_SELECTOR, val)

        result
    end

_reduce_15(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 384
    def _reduce_15(val, _values, result)
            result = Node.new(:CONDITIONAL_SELECTOR, val)

        result
    end

_reduce_16(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 390
    def _reduce_16(val, _values, result)
            result = Node.new(:CONDITIONAL_SELECTOR,
              [
                Node.new(:ELEMENT_NAME, ['*']),
                Node.new(:COMBINATOR, val)
              ]
            )

        result
    end

_reduce_17(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 401
    def _reduce_17(val, _values, result)
            result = Node.new(:CONDITIONAL_SELECTOR,
              [Node.new(:ELEMENT_NAME, ['*']), val.first]
            )

        result
    end

_reduce_18(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 409
    def _reduce_18(val, _values, result)
            result = Node.new(val.first, [nil, val.last])

        result
    end

_reduce_19(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 415
    def _reduce_19(val, _values, result)
            result = Node.new(val[1], [val.first, val.last])

        result
    end

_reduce_2(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 309
    def _reduce_2(val, _values, result)
     result = val.flatten
        result
    end

_reduce_21(val, _values, result) click to toggle source

reduce 20 omitted

    # File lib/nokogiri/css/parser.rb, line 423
    def _reduce_21(val, _values, result)
     result = Node.new(:CLASS_CONDITION, [val[1]])
        result
    end

_reduce_23(val, _values, result) click to toggle source

reduce 22 omitted

    # File lib/nokogiri/css/parser.rb, line 430
    def _reduce_23(val, _values, result)
     result = Node.new(:ELEMENT_NAME, val)
        result
    end

_reduce_24(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 435
    def _reduce_24(val, _values, result)
            result = Node.new(:ELEMENT_NAME,
              [[val.first, val.last].compact.join(':')]
            )

        result
    end

_reduce_25(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 443
    def _reduce_25(val, _values, result)
            name = @namespaces.key?('xmlns') ? "xmlns:#{val.first}" : val.first
            result = Node.new(:ELEMENT_NAME, [name])

        result
    end

_reduce_26(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 450
    def _reduce_26(val, _values, result)
     result = val[0]
        result
    end

_reduce_28(val, _values, result) click to toggle source

reduce 27 omitted

    # File lib/nokogiri/css/parser.rb, line 457
    def _reduce_28(val, _values, result)
            result = Node.new(:ATTRIBUTE_CONDITION,
              [val[1]] + (val[2] || [])
            )

        result
    end

_reduce_29(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 465
    def _reduce_29(val, _values, result)
            result = Node.new(:ATTRIBUTE_CONDITION,
              [val[1]] + (val[2] || [])
            )

        result
    end

_reduce_3(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 314
    def _reduce_3(val, _values, result)
     result = val.flatten
        result
    end

_reduce_30(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 473
    def _reduce_30(val, _values, result)
            # Non standard, but hpricot supports it.
            result = Node.new(:PSEUDO_CLASS,
              [Node.new(:FUNCTION, ['nth-child(', val[1]])]
            )

        result
    end

_reduce_31(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 482
    def _reduce_31(val, _values, result)
            result = Node.new(:ELEMENT_NAME,
              [[val.first, val.last].compact.join(':')]
            )

        result
    end

_reduce_32(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 490
    def _reduce_32(val, _values, result)
            # Default namespace is not applied to attributes.
            # So we don't add prefix "xmlns:" as in namespaced_ident.
            result = Node.new(:ELEMENT_NAME, [val.first])

        result
    end

_reduce_33(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 498
    def _reduce_33(val, _values, result)
            result = Node.new(:FUNCTION, [val.first.strip])

        result
    end

_reduce_34(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 504
    def _reduce_34(val, _values, result)
            result = Node.new(:FUNCTION, [val.first.strip, val[1]].flatten)

        result
    end

_reduce_35(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 510
    def _reduce_35(val, _values, result)
            result = Node.new(:FUNCTION, [val.first.strip, val[1]].flatten)

        result
    end

_reduce_36(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 516
    def _reduce_36(val, _values, result)
            result = Node.new(:FUNCTION, [val.first.strip, val[1]].flatten)

        result
    end

_reduce_37(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 522
    def _reduce_37(val, _values, result)
            result = Node.new(:FUNCTION, [val.first.strip, val[1]].flatten)

        result
    end

_reduce_38(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 528
    def _reduce_38(val, _values, result)
     result = [val.first, val.last]
        result
    end

_reduce_39(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 533
    def _reduce_39(val, _values, result)
     result = [val.first, val.last]
        result
    end

_reduce_4(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 319
    def _reduce_4(val, _values, result)
     result = :DIRECT_ADJACENT_SELECTOR
        result
    end

_reduce_40(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 538
    def _reduce_40(val, _values, result)
     result = [val.first, val.last]
        result
    end

_reduce_43(val, _values, result) click to toggle source

reduce 42 omitted

    # File lib/nokogiri/css/parser.rb, line 547
    def _reduce_43(val, _values, result)
            if val[0] == 'even'
              val = ["2","n","+","0"]
              result = Node.new(:AN_PLUS_B, val)
            elsif val[0] == 'odd'
              val = ["2","n","+","1"]
              result = Node.new(:AN_PLUS_B, val)
            else
              # This is not CSS standard.  It allows us to support this:
              # assert_xpath("//a[foo(., @href)]", @parser.parse('a:foo(@href)'))
              # assert_xpath("//a[foo(., @a, b)]", @parser.parse('a:foo(@a, b)'))
              # assert_xpath("//a[foo(., a, 10)]", @parser.parse('a:foo(a, 10)'))
              result = val
            end

        result
    end

_reduce_44(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 565
    def _reduce_44(val, _values, result)
            if val[1] == 'n'
              result = Node.new(:AN_PLUS_B, val)
            else
              raise Racc::ParseError, "parse error on IDENT '#{val[1]}'"
            end

        result
    end

_reduce_45(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 575
    def _reduce_45(val, _values, result)
                   # n+3, -n+3
            if val[0] == 'n'
              val.unshift("1")
              result = Node.new(:AN_PLUS_B, val)
            elsif val[0] == '-n'
              val[0] = 'n'
              val.unshift("-1")
              result = Node.new(:AN_PLUS_B, val)
            else
              raise Racc::ParseError, "parse error on IDENT '#{val[1]}'"
            end

        result
    end

_reduce_46(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 591
    def _reduce_46(val, _values, result)
            if val[1] == 'n'
              val << "+"
              val << "0"
              result = Node.new(:AN_PLUS_B, val)
            else
              raise Racc::ParseError, "parse error on IDENT '#{val[1]}'"
            end

        result
    end

_reduce_47(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 603
    def _reduce_47(val, _values, result)
            result = Node.new(:PSEUDO_CLASS, [val[1]])

        result
    end

_reduce_48(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 609
    def _reduce_48(val, _values, result)
     result = Node.new(:PSEUDO_CLASS, [val[1]])
        result
    end

_reduce_5(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 324
    def _reduce_5(val, _values, result)
     result = :CHILD_SELECTOR
        result
    end

_reduce_51(val, _values, result) click to toggle source

reduce 50 omitted

    # File lib/nokogiri/css/parser.rb, line 618
    def _reduce_51(val, _values, result)
            result = Node.new(:COMBINATOR, val)

        result
    end

_reduce_52(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 624
    def _reduce_52(val, _values, result)
            result = Node.new(:COMBINATOR, val)

        result
    end

_reduce_53(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 630
    def _reduce_53(val, _values, result)
            result = Node.new(:COMBINATOR, val)

        result
    end

_reduce_54(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 636
    def _reduce_54(val, _values, result)
            result = Node.new(:COMBINATOR, val)

        result
    end

_reduce_59(val, _values, result) click to toggle source

reduce 58 omitted

    # File lib/nokogiri/css/parser.rb, line 650
    def _reduce_59(val, _values, result)
     result = Node.new(:ID, val)
        result
    end

_reduce_6(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 329
    def _reduce_6(val, _values, result)
     result = :FOLLOWING_SELECTOR
        result
    end

_reduce_60(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 655
    def _reduce_60(val, _values, result)
     result = [val.first, val[1]]
        result
    end

_reduce_61(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 660
    def _reduce_61(val, _values, result)
     result = [val.first, val[1]]
        result
    end

_reduce_63(val, _values, result) click to toggle source

reduce 62 omitted

    # File lib/nokogiri/css/parser.rb, line 667
    def _reduce_63(val, _values, result)
     result = :equal
        result
    end

_reduce_64(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 672
    def _reduce_64(val, _values, result)
     result = :prefix_match
        result
    end

_reduce_65(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 677
    def _reduce_65(val, _values, result)
     result = :suffix_match
        result
    end

_reduce_66(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 682
    def _reduce_66(val, _values, result)
     result = :substring_match
        result
    end

_reduce_67(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 687
    def _reduce_67(val, _values, result)
     result = :not_equal
        result
    end

_reduce_68(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 692
    def _reduce_68(val, _values, result)
     result = :includes
        result
    end

_reduce_69(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 697
    def _reduce_69(val, _values, result)
     result = :dash_match
        result
    end

_reduce_7(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 334
    def _reduce_7(val, _values, result)
     result = :DESCENDANT_SELECTOR
        result
    end

_reduce_70(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 702
    def _reduce_70(val, _values, result)
            result = Node.new(:NOT, [val[1]])

        result
    end

_reduce_8(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 339
    def _reduce_8(val, _values, result)
     result = :DESCENDANT_SELECTOR
        result
    end

_reduce_9(val, _values, result) click to toggle source

    # File lib/nokogiri/css/parser.rb, line 344
    def _reduce_9(val, _values, result)
     result = :CHILD_SELECTOR
        result
    end

_reduce_none(val, _values, result) click to toggle source

reduce 73 omitted

    # File lib/nokogiri/css/parser.rb, line 714
    def _reduce_none(val, _values, result)
      val[0]
    end

next_token() click to toggle source

    # File lib/nokogiri/css/parser_extras.rb, line 65
    def next_token
      @tokenizer.next_token
    end

on_error(error_token_id, error_value, value_stack) click to toggle source

On CSS parser error, raise an exception

    # File lib/nokogiri/css/parser_extras.rb, line 85
    def on_error error_token_id, error_value, value_stack
      after = value_stack.compact.last
      raise SyntaxError.new("unexpected '#{error_value}' after '#{after}'")
    end

parse(string) click to toggle source

    # File lib/nokogiri/css/parser_extras.rb, line 60
    def parse string
      @tokenizer.scan_setup string
      do_parse
    end

xpath_for(string, options={}) click to toggle source

Get the xpath for `string` using `options`

    # File lib/nokogiri/css/parser_extras.rb, line 70
    def xpath_for string, options={}
      key = "#{string}#{options[:ns]}#{options[:prefix]}"
      v = self.class[key]
      return v if v

      args = [
        options[:prefix] || '//',
        options[:visitor] || XPathVisitor.new
      ]
      self.class[key] = parse(string).map { |ast|
        ast.to_xpath(*args)
      }
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.