Class: Nokogiri::CSS::Parser
- Inherits:
-
GeneratedTokenizer
- Object
- Racc::Parser
- GeneratedParser
- GeneratedTokenizer
- Nokogiri::CSS::Parser
- 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
-
.cache_on ⇒ Object
(also: cache_on?)
Turn on CSS parse caching.
Attributes inherited from GeneratedTokenizer
Class Method Summary collapse
-
.[](string) ⇒ Object
Get the css selector in
string
from the cache. -
.[]=(string, value) ⇒ Object
Set the css selector in
string
in the cache tovalue
. -
.clear_cache ⇒ Object
Clear the cache.
-
.parse(selector) ⇒ Object
Parse this CSS selector in
selector
. -
.without_cache(&block) ⇒ Object
Execute
block
without cache.
Instance Method Summary collapse
-
#initialize(namespaces = {}) ⇒ Parser
constructor
Create a new CSS parser with respect to
namespaces
. -
#on_error(error_token_id, error_value, value_stack) ⇒ Object
On CSS parser error, raise an exception.
-
#xpath_for(string, options = {}) ⇒ Object
Get the xpath for
string
usingoptions
.
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_36, #_reduce_37, #_reduce_38, #_reduce_39, #_reduce_4, #_reduce_40, #_reduce_41, #_reduce_44, #_reduce_45, #_reduce_46, #_reduce_47, #_reduce_5, #_reduce_52, #_reduce_53, #_reduce_54, #_reduce_56, #_reduce_57, #_reduce_58, #_reduce_59, #_reduce_6, #_reduce_60, #_reduce_61, #_reduce_62, #_reduce_63, #_reduce_7, #_reduce_8, #_reduce_9, #_reduce_none
Constructor Details
#initialize(namespaces = {}) ⇒ Parser
Create a new CSS parser with respect to namespaces
55 56 57 58 |
# File 'lib/nokogiri/css/parser.rb', line 55 def initialize namespaces = {} @namespaces = namespaces super() end |
Class Attribute Details
.cache_on ⇒ Object Also known as: cache_on?
Turn on CSS parse caching
13 14 15 |
# File 'lib/nokogiri/css/parser.rb', line 13 def cache_on @cache_on end |
Class Method Details
.[](string) ⇒ Object
Get the css selector in string
from the cache
18 19 20 21 |
# File 'lib/nokogiri/css/parser.rb', line 18 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
24 25 26 27 |
# File 'lib/nokogiri/css/parser.rb', line 24 def []= string, value return value unless @cache_on @mutex.synchronize { @cache[string] = value } end |
.clear_cache ⇒ Object
Clear the cache
30 31 32 |
# File 'lib/nokogiri/css/parser.rb', line 30 def clear_cache @mutex.synchronize { @cache = {} } end |
.parse(selector) ⇒ Object
Parse this CSS selector in selector
. Returns an AST.
44 45 46 47 48 49 50 51 |
# File 'lib/nokogiri/css/parser.rb', line 44 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
35 36 37 38 39 40 |
# File 'lib/nokogiri/css/parser.rb', line 35 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
77 78 79 80 |
# File 'lib/nokogiri/css/parser.rb', line 77 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
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/nokogiri/css/parser.rb', line 62 def xpath_for string, ={} key = "#{string}#{[:ns]}#{[:prefix]}" v = self.class[key] return v if v args = [ [:prefix] || '//', [:visitor] || XPathVisitor.new ] self.class[key] = parse(string).map { |ast| ast.to_xpath(*args) } end |