Install
sudo gem install nokogiri
Contribute
github.com/tenderlove/nokogiri

An HTML, XML, SAX, & Reader parser with the ability to search documents via XPath or CSS3 selectors… and much more

Nokogiri

Class Nokogiri::CSS::Parser inherits from Nokogiri::CSS::GeneratedTokenizer

Attributes

cache_on RW

Turn on CSS parse caching

Public Class Methods

[](string) Show Source

Get the css selector in string from the cache

# File lib/nokogiri/css/parser.rb, line 17 17: def [] string 18: return unless @cache_on 19: @mutex.synchronize { @cache[string] } 20: end
[]=(string, value) Show Source

Set the css selector in string in the cache to value

# File lib/nokogiri/css/parser.rb, line 23 23: def []= string, value 24: return value unless @cache_on 25: @mutex.synchronize { @cache[string] = value } 26: end
clear_cache() Show Source

Clear the cache

# File lib/nokogiri/css/parser.rb, line 29 29: def clear_cache 30: @mutex.synchronize { @cache = {} } 31: end
new(namespaces = {}) Show Source

Create a new CSS parser with respect to namespaces

# File lib/nokogiri/css/parser.rb, line 54 54: def initialize namespaces = {} 55: @namespaces = namespaces 56: super() 57: end
parse(selector) Show Source
 

Parse this CSS selector in selector. Returns an AST.

# File lib/nokogiri/css/parser.rb, line 43 43: def parse selector 44: @warned ||= false 45: unless @warned 46: $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)') 47: @warned = true 48: end 49: new.parse selector 50: end
without_cache(&block) Show Source

Execute block without cache

# File lib/nokogiri/css/parser.rb, line 34 34: def without_cache &block 35: tmp = @cache_on 36: @cache_on = false 37: block.call 38: @cache_on = tmp 39: end

Public Instance Methods

on_error(error_token_id, error_value, value_stack) Show Source

On CSS parser error, raise an exception

# File lib/nokogiri/css/parser.rb, line 76 76: def on_error error_token_id, error_value, value_stack 77: after = value_stack.compact.last 78: raise SyntaxError.new("unexpected '#{error_value}' after '#{after}'") 79: end
xpath_for(string, options={}) Show Source

Get the xpath for string using options

# File lib/nokogiri/css/parser.rb, line 61 61: def xpath_for string, options={} 62: key = "#{string}#{options[:ns]}#{options[:prefix]}" 63: v = self.class[key] 64: return v if v 65: 66: args = [ 67: options[:prefix] || '//', 68: options[:visitor] || XPathVisitor.new 69: ] 70: self.class[key] = parse(string).map { |ast| 71: ast.to_xpath(*args) 72: } 73: end