Class: REXML::CSSSelector::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rexml/css_selector/parser.rb

Overview

Parser is a CSS selector parser.

Constant Summary collapse

RE_NEWLINE =

:stopdoc:

/\r\n|[\n\r\f]/
RE_WHITESPACE =
/#{RE_NEWLINE}|[ \t]/
RE_WS =
/(?:#{RE_WHITESPACE})*/
RE_ESCAPE =
/\\(?:(?!#{RE_NEWLINE})\H|\h{1,6}(?:#{RE_WHITESPACE})?)/
RE_ESCAPE_NEWLINE =
/#{RE_ESCAPE}|\\#{RE_NEWLINE}/
RE_IDENT_START =
/[a-zA-Z_\P{ASCII}]|#{RE_ESCAPE}/
RE_IDENT_PART =
/(?:[-a-zA-Z0-9_\P{ASCII}]|#{RE_ESCAPE})*/
RE_IDENT =
/(?:-?(?:#{RE_IDENT_START})|--)(?:#{RE_IDENT_PART})*/
RE_STRING =
/
  "(?:(?!#{RE_NEWLINE})[^"\\]|#{RE_ESCAPE_NEWLINE})*"
| '(?:(?!#{RE_NEWLINE})[^'\\]|#{RE_ESCAPE_NEWLINE})*'
/x
RE_SUBSTITUTE =
/\$#{RE_IDENT}/
RE_VALUE =
/#{RE_IDENT}|#{RE_STRING}|#{RE_SUBSTITUTE}/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**config) ⇒ Parser

Returns a new instance of Parser.



14
15
16
17
# File 'lib/rexml/css_selector/parser.rb', line 14

def initialize(**config)
  @config = config
  @config[:pseudo_classes] ||= {}
end

Class Method Details

.unescape_ident(ident) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rexml/css_selector/parser.rb', line 48

def self.unescape_ident(ident)
  return nil unless ident

  ident.gsub(RE_ESCAPE) do |escape|
    if escape[1] =~ /\H/
      escape[1]
    else
      escape[1..].to_i(16).chr
    end
  end
end

.unescape_namespace(value) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/rexml/css_selector/parser.rb', line 80

def self.unescape_namespace(value)
  return nil unless value

  if value == "*"
    UniversalNamespace[]
  else
    Namespace[name: unescape_ident(value)]
  end
end

.unescape_value(value) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rexml/css_selector/parser.rb', line 60

def self.unescape_value(value)
  if value[0] == '"' || value[0] == "'"
    value =
      value[1...-1].gsub(RE_ESCAPE_NEWLINE) do |escape|
        if escape[1] =~ /[\n\r\f]/
          ""
        elsif escape[1] =~ /\H/
          escape[1]
        else
          escape[1..].to_i(16).chr
        end
      end
    String[value]
  elsif value[0] == "$"
    Substitution[unescape_ident(value[1..])]
  else
    Ident[unescape_ident(value)]
  end
end

Instance Method Details

#parse(source) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/rexml/css_selector/parser.rb', line 19

def parse(source)
  old_scanner = @scanner
  @scanner = StringScanner.new(source)
  @scanner.scan RE_WS
  parse_selector_list
ensure
  @scanner = old_scanner
end