Class: Nokogiri::CSS::Parser

Inherits:
Racc::Parser
  • Object
show all
Defined in:
lib/nokogiri/css/parser_extras.rb

Constant Summary collapse

CACHE_SWITCH_NAME =
:nokogiri_css_parser_cache_is_off

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespaces = {}) ⇒ Parser

Create a new CSS parser with respect to namespaces



57
58
59
60
61
# File 'lib/nokogiri/css/parser_extras.rb', line 57

def initialize(namespaces = {})
  @tokenizer = Tokenizer.new
  @namespaces = namespaces
  super()
end

Class Method Details

.[](string) ⇒ Object

Get the css selector in string from the cache



24
25
26
27
# File 'lib/nokogiri/css/parser_extras.rb', line 24

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



30
31
32
33
# File 'lib/nokogiri/css/parser_extras.rb', line 30

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

.cache_on?Boolean

Return a thread-local boolean indicating whether the CSS-to-XPath cache is active. (Default is `true`.)

Returns:

  • (Boolean)


14
15
16
# File 'lib/nokogiri/css/parser_extras.rb', line 14

def cache_on?
  !Thread.current[CACHE_SWITCH_NAME]
end

.clear_cache(create_new_object = false) ⇒ Object

Clear the cache



36
37
38
39
40
41
42
43
44
# File 'lib/nokogiri/css/parser_extras.rb', line 36

def clear_cache(create_new_object = false)
  @mutex.synchronize do
    if create_new_object
      @cache = {}
    else
      @cache.clear
    end
  end
end

.set_cache(value) ⇒ Object

Set a thread-local boolean to turn cacheing on and off. Truthy values turn the cache on, falsey values turn the cache off.



19
20
21
# File 'lib/nokogiri/css/parser_extras.rb', line 19

def set_cache(value)
  Thread.current[CACHE_SWITCH_NAME] = !value
end

.without_cache(&block) ⇒ Object

Execute block without cache



47
48
49
50
51
52
53
# File 'lib/nokogiri/css/parser_extras.rb', line 47

def without_cache(&block)
  original_cache_setting = cache_on?
  set_cache false
  block.call
ensure
  set_cache original_cache_setting
end

Instance Method Details

#next_tokenObject



68
69
70
# File 'lib/nokogiri/css/parser_extras.rb', line 68

def next_token
  @tokenizer.next_token
end

#on_error(error_token_id, error_value, value_stack) ⇒ Object

On CSS parser error, raise an exception

Raises:



88
89
90
91
# File 'lib/nokogiri/css/parser_extras.rb', line 88

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) ⇒ Object



63
64
65
66
# File 'lib/nokogiri/css/parser_extras.rb', line 63

def parse(string)
  @tokenizer.scan_setup string
  do_parse
end

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

Get the xpath for string using options



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nokogiri/css/parser_extras.rb', line 73

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