Module: Puppeteer::PSelectorParser

Defined in:
lib/puppeteer/p_selector_parser.rb

Defined Under Namespace

Classes: PseudoSelector

Constant Summary collapse

PSEUDO_PREFIX =
'::-p-'

Class Method Summary collapse

Class Method Details

.parse(selector) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/puppeteer/p_selector_parser.rb', line 18

def self.parse(selector)
  is_pure_css = true
  has_pseudo_classes = false
  has_aria = false

  selectors = []
  compound = []
  complex = [compound]
  storage = +''

  i = 0
  length = selector.length
  in_quote = nil
  escaped = false
  paren_depth = 0

  while i < length
    char = selector[i]

    if escaped
      storage << char
      escaped = false
      i += 1
      next
    end

    if char == '\\'
      storage << char
      escaped = true
      i += 1
      next
    end

    if in_quote
      if char == in_quote
        in_quote = nil
      end
      storage << char
      i += 1
      next
    end

    if char == '"' || char == "'"
      in_quote = char
      storage << char
      i += 1
      next
    end

    if char == '('
      paren_depth += 1
      storage << char
      i += 1
      next
    end

    if char == ')'
      paren_depth = [paren_depth - 1, 0].max
      storage << char
      i += 1
      next
    end

    if paren_depth == 0
      if starts_with_at?(selector, i, '>>>>')
        flush_storage(storage, compound)
        complex << '>>>>'
        compound = []
        complex << compound
        is_pure_css = false
        i += 4
        next
      end

      if starts_with_at?(selector, i, '>>>')
        flush_storage(storage, compound)
        complex << '>>>'
        compound = []
        complex << compound
        is_pure_css = false
        i += 3
        next
      end

      if starts_with_at?(selector, i, PSEUDO_PREFIX)
        flush_storage(storage, compound)
        i += PSEUDO_PREFIX.length
        name, i = parse_name(selector, i)
        raise ArgumentError.new('Invalid PSelector name') if name.empty?

        i = skip_spaces(selector, i)
        value = ''
        if selector[i] == '('
          value, i = parse_argument(selector, i)
        end
        value = unquote(value.strip)

        compound << PseudoSelector.new(name: name, value: value)
        is_pure_css = false
        has_aria = true if name == 'aria'
        next
      end

      if char == ','
        flush_storage(storage, compound)
        selectors << complex
        compound = []
        complex = [compound]
        i += 1
        next
      end

      if char == ':' && selector[i, PSEUDO_PREFIX.length] != PSEUDO_PREFIX
        if selector[i + 1] != ':'
          has_pseudo_classes = true
        end
      end
    end

    storage << char
    i += 1
  end

  raise ArgumentError.new('Unterminated string in PSelector') if in_quote
  raise ArgumentError.new('Unterminated parentheses in PSelector') if paren_depth != 0

  flush_storage(storage, compound)
  selectors << complex if complex.any?

  [selectors, is_pure_css, has_pseudo_classes, has_aria]
end