Class: Nokogiri::CSS::Parser

Inherits:
GeneratedTokenizer show all
Defined in:
lib/nokogiri/css/parser.rb

Constant Summary

Constants inherited from GeneratedParser

GeneratedParser::Racc_arg, GeneratedParser::Racc_debug_parser, GeneratedParser::Racc_token_to_s_table

Class Attribute Summary collapse

Attributes inherited from GeneratedTokenizer

#filename, #lineno, #state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GeneratedTokenizer

#action, #load_file, #next_token, #scan_file, #scan_setup, #scan_str

Methods inherited from GeneratedParser

#_reduce_1, #_reduce_10, #_reduce_11, #_reduce_13, #_reduce_14, #_reduce_15, #_reduce_16, #_reduce_18, #_reduce_19, #_reduce_2, #_reduce_20, #_reduce_21, #_reduce_22, #_reduce_24, #_reduce_25, #_reduce_26, #_reduce_27, #_reduce_28, #_reduce_29, #_reduce_3, #_reduce_30, #_reduce_31, #_reduce_32, #_reduce_33, #_reduce_34, #_reduce_37, #_reduce_38, #_reduce_39, #_reduce_4, #_reduce_40, #_reduce_41, #_reduce_42, #_reduce_45, #_reduce_46, #_reduce_47, #_reduce_48, #_reduce_5, #_reduce_53, #_reduce_54, #_reduce_55, #_reduce_57, #_reduce_58, #_reduce_59, #_reduce_6, #_reduce_60, #_reduce_61, #_reduce_62, #_reduce_63, #_reduce_64, #_reduce_7, #_reduce_8, #_reduce_9, #_reduce_none

Constructor Details

#initialize(namespaces = {}) ⇒ Parser

Create a new CSS parser with respect to namespaces



54
55
56
57
# File 'lib/nokogiri/css/parser.rb', line 54

def initialize namespaces = {}
  @namespaces = namespaces
  super()
end

Class Attribute Details

.cache_onObject Also known as: cache_on?

Turn on CSS parse caching



12
13
14
# File 'lib/nokogiri/css/parser.rb', line 12

def cache_on
  @cache_on
end

Class Method Details

.[](string) ⇒ Object

Get the css selector in string from the cache



17
18
19
20
# File 'lib/nokogiri/css/parser.rb', line 17

def [] string
  return unless @cache_on
  @mutex.synchronize { @cache[string] }
end

.[]=(string, value) ⇒ Object

Set the css selector in string in the cache to value



23
24
25
26
# File 'lib/nokogiri/css/parser.rb', line 23

def []= string, value
  return value unless @cache_on
  @mutex.synchronize { @cache[string] = value }
end

.clear_cacheObject

Clear the cache



29
30
31
# File 'lib/nokogiri/css/parser.rb', line 29

def clear_cache
  @mutex.synchronize { @cache = {} }
end

.parse(selector) ⇒ Object

Parse this CSS selector in selector. Returns an AST.



43
44
45
46
47
48
49
50
# File 'lib/nokogiri/css/parser.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) ⇒ Object

Execute block without cache



34
35
36
37
38
39
# File 'lib/nokogiri/css/parser.rb', line 34

def without_cache &block
  tmp = @cache_on
  @cache_on = false
  block.call
  @cache_on = tmp
end

Instance Method Details

#on_error(error_token_id, error_value, value_stack) ⇒ Object

On CSS parser error, raise an exception

Raises:



76
77
78
79
# File 'lib/nokogiri/css/parser.rb', line 76

def on_error error_token_id, error_value, value_stack
  after = value_stack.compact.last
  raise SyntaxError.new("unexpected '#{error_value}' after '#{after}'")
end

#xpath_for(string, options = {}) ⇒ Object

Get the xpath for string using options



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nokogiri/css/parser.rb', line 61

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