oga 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/doc/css_selectors.md +1 -1
  3. data/lib/oga.rb +6 -1
  4. data/lib/oga/blacklist.rb +0 -10
  5. data/lib/oga/css/lexer.rb +530 -255
  6. data/lib/oga/css/parser.rb +232 -230
  7. data/lib/oga/entity_decoder.rb +0 -4
  8. data/lib/oga/html/entities.rb +0 -4
  9. data/lib/oga/html/parser.rb +0 -4
  10. data/lib/oga/html/sax_parser.rb +0 -4
  11. data/lib/oga/lru.rb +0 -26
  12. data/lib/oga/oga.rb +0 -8
  13. data/lib/oga/ruby/generator.rb +225 -0
  14. data/lib/oga/ruby/node.rb +189 -0
  15. data/lib/oga/version.rb +1 -1
  16. data/lib/oga/whitelist.rb +0 -6
  17. data/lib/oga/xml/attribute.rb +13 -20
  18. data/lib/oga/xml/cdata.rb +0 -4
  19. data/lib/oga/xml/character_node.rb +0 -8
  20. data/lib/oga/xml/comment.rb +0 -4
  21. data/lib/oga/xml/default_namespace.rb +0 -2
  22. data/lib/oga/xml/doctype.rb +0 -8
  23. data/lib/oga/xml/document.rb +10 -14
  24. data/lib/oga/xml/element.rb +1 -52
  25. data/lib/oga/xml/entities.rb +0 -26
  26. data/lib/oga/xml/expanded_name.rb +12 -0
  27. data/lib/oga/xml/html_void_elements.rb +0 -2
  28. data/lib/oga/xml/lexer.rb +0 -86
  29. data/lib/oga/xml/namespace.rb +0 -10
  30. data/lib/oga/xml/node.rb +18 -34
  31. data/lib/oga/xml/node_set.rb +0 -50
  32. data/lib/oga/xml/parser.rb +13 -50
  33. data/lib/oga/xml/processing_instruction.rb +0 -8
  34. data/lib/oga/xml/pull_parser.rb +0 -18
  35. data/lib/oga/xml/querying.rb +58 -19
  36. data/lib/oga/xml/sax_parser.rb +0 -18
  37. data/lib/oga/xml/text.rb +0 -12
  38. data/lib/oga/xml/traversal.rb +0 -4
  39. data/lib/oga/xml/xml_declaration.rb +0 -8
  40. data/lib/oga/xpath/compiler.rb +1568 -0
  41. data/lib/oga/xpath/conversion.rb +102 -0
  42. data/lib/oga/xpath/lexer.rb +1844 -1238
  43. data/lib/oga/xpath/parser.rb +182 -153
  44. metadata +7 -3
  45. data/lib/oga/xpath/evaluator.rb +0 -1800
@@ -5,7 +5,6 @@ require 'll/setup'
5
5
 
6
6
  module Oga
7
7
  module CSS
8
- ##
9
8
  # AST parser for CSS expressions.
10
9
  #
11
10
  # This parser does _not_ build a CSS specific AST, instead it directly produces
@@ -53,31 +52,31 @@ class Parser < LL::Driver
53
52
  CONFIG.rules = [
54
53
  [3, 0, 0, 1], # 0
55
54
  [3, 1, 2, 0], # 1
56
- [3, 2, 4, 31, 6, 0, 0, 3], # 2
55
+ [3, 2, 4, 30, 6, 0, 0, 3], # 2
57
56
  [3, 3, 0, 3, 1, 26], # 3
58
- [3, 4, 4, 32, 6, 0, 0, 5], # 4
59
- [3, 5, 0, 5, 1, 6], # 5
60
- [3, 6, 8, 33, 0, 6], # 6
61
- [3, 7, 8, 34, 0, 7], # 7
62
- [3, 8, 0, 12], # 8
63
- [3, 9, 0, 9], # 9
64
- [3, 10, 0, 8, 1, 16], # 10
65
- [3, 11, 0, 8, 1, 17], # 11
66
- [3, 12, 0, 8, 1, 18], # 12
67
- [3, 13, 0, 9], # 13
68
- [3, 14, 0, 7], # 14
69
- [3, 15, 0, 10], # 15
70
- [3, 16, 8, 35, 1, 1], # 16
71
- [3, 17, 1, 1, 1, 2], # 17
72
- [3, 18, 4, 36, 6, 0, 0, 13], # 18
73
- [3, 19, 0, 19], # 19
74
- [3, 20, 0, 20], # 20
75
- [3, 21, 0, 21], # 21
76
- [3, 22, 0, 14], # 22
77
- [3, 23, 1, 4, 0, 15, 1, 3], # 23
78
- [3, 24, 0, 17], # 24
79
- [3, 25, 0, 10], # 25
80
- [3, 26, 8, 37, 0, 16], # 26
57
+ [3, 4, 8, 32, 8, 31, 0, 8], # 4
58
+ [3, 5, 0, 5], # 5
59
+ [3, 6, 8, 33, 0, 11], # 6
60
+ [3, 7, 0, 3, 1, 6], # 7
61
+ [3, 8, 0, 5], # 8
62
+ [3, 9, 8, 34, 0, 6, 1, 16], # 9
63
+ [3, 10, 8, 35, 0, 6, 1, 18], # 10
64
+ [3, 11, 8, 36, 0, 6, 1, 17], # 11
65
+ [3, 12, 8, 37, 0, 8], # 12
66
+ [3, 13, 0, 11], # 13
67
+ [3, 14, 0, 19], # 14
68
+ [3, 15, 0, 18], # 15
69
+ [3, 16, 0, 20], # 16
70
+ [3, 17, 0, 9], # 17
71
+ [3, 18, 8, 38, 1, 1], # 18
72
+ [3, 19, 1, 1, 1, 2], # 19
73
+ [3, 20, 4, 39, 6, 0, 0, 12], # 20
74
+ [3, 21, 0, 7], # 21
75
+ [3, 22, 0, 13], # 22
76
+ [3, 23, 1, 4, 0, 14, 1, 3], # 23
77
+ [3, 24, 0, 16], # 24
78
+ [3, 25, 0, 9], # 25
79
+ [3, 26, 8, 40, 0, 15], # 26
81
80
  [3, 27, 1, 10], # 27
82
81
  [3, 28, 1, 11], # 28
83
82
  [3, 29, 1, 12], # 29
@@ -86,79 +85,86 @@ class Parser < LL::Driver
86
85
  [3, 32, 1, 15], # 32
87
86
  [3, 33, 1, 1, 1, 24], # 33
88
87
  [3, 34, 1, 1, 1, 25], # 34
89
- [3, 35, 8, 38, 0, 22], # 35
88
+ [3, 35, 8, 41, 0, 21], # 35
90
89
  [3, 36, 1, 1, 1, 5], # 36
91
- [3, 37, 1, 8, 0, 24, 1, 7], # 37
92
- [3, 38, 0, 29], # 38
93
- [3, 39, 0, 30], # 39
94
- [3, 40, 0, 27], # 40
95
- [3, 41, 0, 5], # 41
90
+ [3, 37, 1, 8, 0, 23, 1, 7], # 37
91
+ [3, 38, 0, 28], # 38
92
+ [3, 39, 0, 29], # 39
93
+ [3, 40, 0, 26], # 40
94
+ [3, 41, 0, 3], # 41
96
95
  [3, 42, 1, 21], # 42
97
96
  [3, 43, 1, 20], # 43
98
- [3, 44, 8, 39, 0, 28], # 44
99
- [3, 45, 8, 40, 0, 28, 1, 9], # 45
100
- [3, 46, 8, 42, 8, 41, 0, 26], # 46
97
+ [3, 44, 8, 42, 0, 27], # 44
98
+ [3, 45, 8, 43, 0, 27, 1, 9], # 45
99
+ [3, 46, 8, 45, 8, 44, 0, 25], # 46
101
100
  [3, 47, 1, 19], # 47
102
101
  [3, 48, 1, 22], # 48
103
102
  [3, 49, 1, 23], # 49
104
103
  [3, 50, 0, 2], # 50
105
- [3, 51, 0, 4], # 51
106
- [3, 52, 0, 12], # 52
107
- [3, 53, 0, 12], # 53
108
- [3, 54, 0, 11], # 54
109
- [3, 55, 0, 13], # 55
110
- [3, 56, 0, 25, 0, 18], # 56
111
- [3, 57, 0, 23], # 57
112
- [3, 58, 0, 26], # 58
113
- [3, 59, 0, 26], # 59
114
- [3, 60, 0, 28], # 60
115
- [3, 61, 0, 26], # 61
104
+ [3, 51, 0, 11], # 51
105
+ [3, 52, 0, 4], # 52
106
+ [3, 53, 0, 4], # 53
107
+ [3, 54, 0, 4], # 54
108
+ [3, 55, 0, 4], # 55
109
+ [3, 56, 0, 4], # 56
110
+ [3, 57, 0, 11], # 57
111
+ [3, 58, 0, 10], # 58
112
+ [3, 59, 0, 12], # 59
113
+ [3, 60, 0, 24, 0, 17], # 60
114
+ [3, 61, 0, 22], # 61
115
+ [3, 62, 0, 25], # 62
116
+ [3, 63, 0, 25], # 63
117
+ [3, 64, 0, 27], # 64
118
+ [3, 65, 0, 25], # 65
116
119
  ].freeze
117
120
 
118
121
  CONFIG.table = [
119
122
  [1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1], # 0
120
123
  [-1, 2, -1, 2, -1, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, 2, -1, -1, -1, -1, -1, 2, 2, -1], # 1
121
124
  [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3], # 2
122
- [-1, 4, -1, 4, -1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 4, 4, 4, -1, -1, -1, -1, -1, 4, 4, -1], # 3
123
- [-1, -1, -1, -1, -1, -1, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 4
124
- [-1, 6, -1, 8, -1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 7, 7, 7, -1, -1, -1, -1, -1, 8, 8, -1], # 5
125
- [-1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 6
126
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, -1, -1, -1, -1, -1, -1, -1, -1], # 7
127
- [-1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 14, 14, 14, -1, -1, -1, -1, -1, -1, -1, -1], # 8
128
- [-1, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 9
129
- [-1, 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 10
130
- [-1, -1, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 11
131
- [-1, -1, -1, 18, -1, 18, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 18, 18, -1], # 12
132
- [-1, -1, -1, 22, -1, 21, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 19, 20, -1], # 13
133
- [-1, -1, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 14
134
- [-1, 24, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 15
135
- [-1, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 16
136
- [-1, 26, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 17
137
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 18
138
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 33, -1, -1], # 19
139
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 34, -1], # 20
140
- [-1, -1, -1, -1, -1, 35, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 21
141
- [-1, -1, -1, -1, -1, 36, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 22
142
- [-1, -1, -1, -1, -1, -1, -1, 37, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 23
143
- [-1, 41, -1, 41, -1, 41, -1, -1, -1, 40, -1, -1, -1, -1, -1, -1, 41, 41, 41, 40, 40, -1, 38, 39, 41, 41, -1], # 24
144
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 42, -1, -1, -1, -1, -1], # 25
145
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 43, -1, -1, -1, -1, -1, -1], # 26
146
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, 44, 46, -1, -1, -1, -1, -1, -1], # 27
147
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 47, -1, -1, -1, -1, -1, -1, -1], # 28
148
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 48, -1, -1, -1, -1], # 29
149
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 49, -1, -1, -1], # 30
150
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 50], # 31
151
- [-1, -1, -1, -1, -1, -1, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 32
152
- [-1, -1, -1, 52, -1, 52, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 52, 52, -1], # 33
153
- [-1, -1, -1, 53, -1, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 53, 53, -1], # 34
154
- [-1, -1, 54, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 35
155
- [-1, -1, -1, 55, -1, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 55, 55, -1], # 36
156
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 56, 56, 56, 56, 56, 56, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 37
157
- [-1, -1, -1, -1, -1, -1, -1, 57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 38
158
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 58, -1, -1, -1, -1, -1, -1], # 39
159
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 59, -1, -1, -1, -1, -1, -1], # 40
160
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 60, -1, -1, -1, -1, -1, -1, -1], # 41
161
- [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 61, -1, -1, -1, -1, -1, -1], # 42
125
+ [-1, 4, -1, 6, -1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 5, 5, 5, -1, -1, -1, -1, -1, 6, 6, -1], # 3
126
+ [-1, -1, -1, -1, -1, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, 8, 8, 8, -1, -1, -1, -1, -1, -1, -1, -1], # 4
127
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 9, 11, 10, -1, -1, -1, -1, -1, -1, -1, -1], # 5
128
+ [-1, 12, -1, 13, -1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 13, 13, -1], # 6
129
+ [-1, -1, -1, -1, -1, 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 15, 14, -1], # 7
130
+ [-1, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 8
131
+ [-1, 18, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 9
132
+ [-1, -1, 19, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 10
133
+ [-1, -1, -1, 20, -1, 20, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 20, 20, -1], # 11
134
+ [-1, -1, -1, 22, -1, 21, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 21, 21, -1], # 12
135
+ [-1, -1, -1, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 13
136
+ [-1, 24, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 14
137
+ [-1, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 15
138
+ [-1, 26, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 16
139
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 17
140
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 33, -1, -1], # 18
141
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 34, -1], # 19
142
+ [-1, -1, -1, -1, -1, 35, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 20
143
+ [-1, -1, -1, -1, -1, 36, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 21
144
+ [-1, -1, -1, -1, -1, -1, -1, 37, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 22
145
+ [-1, 41, -1, 41, -1, 41, -1, -1, -1, 40, -1, -1, -1, -1, -1, -1, 41, 41, 41, 40, 40, -1, 38, 39, 41, 41, -1], # 23
146
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 42, -1, -1, -1, -1, -1], # 24
147
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 43, -1, -1, -1, -1, -1, -1], # 25
148
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, 45, -1, -1, -1, -1, -1, -1, -1, -1, -1, 44, 46, -1, -1, -1, -1, -1, -1], # 26
149
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 47, -1, -1, -1, -1, -1, -1, -1], # 27
150
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 48, -1, -1, -1, -1], # 28
151
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 49, -1, -1, -1], # 29
152
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 50], # 30
153
+ [-1, -1, -1, 51, -1, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 51, 51, -1], # 31
154
+ [-1, -1, -1, -1, -1, -1, 52, -1, -1, -1, -1, -1, -1, -1, -1, -1, 52, 52, 52, -1, -1, -1, -1, -1, -1, -1, -1], # 32
155
+ [-1, -1, -1, -1, -1, -1, 53, -1, -1, -1, -1, -1, -1, -1, -1, -1, 53, 53, 53, -1, -1, -1, -1, -1, -1, -1, -1], # 33
156
+ [-1, -1, -1, -1, -1, -1, 54, -1, -1, -1, -1, -1, -1, -1, -1, -1, 54, 54, 54, -1, -1, -1, -1, -1, -1, -1, -1], # 34
157
+ [-1, -1, -1, -1, -1, -1, 55, -1, -1, -1, -1, -1, -1, -1, -1, -1, 55, 55, 55, -1, -1, -1, -1, -1, -1, -1, -1], # 35
158
+ [-1, -1, -1, -1, -1, -1, 56, -1, -1, -1, -1, -1, -1, -1, -1, -1, 56, 56, 56, -1, -1, -1, -1, -1, -1, -1, -1], # 36
159
+ [-1, -1, -1, 57, -1, 57, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 57, 57, -1], # 37
160
+ [-1, -1, 58, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 38
161
+ [-1, -1, -1, 59, -1, 59, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 59, 59, -1], # 39
162
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 60, 60, 60, 60, 60, 60, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 40
163
+ [-1, -1, -1, -1, -1, -1, -1, 61, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1], # 41
164
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, -1, -1, -1], # 42
165
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, -1, -1, -1, -1], # 43
166
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 64, -1, -1, -1, -1, -1, -1, -1], # 44
167
+ [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 65, -1, -1, -1, -1, -1, -1], # 45
162
168
  ].freeze
163
169
 
164
170
  CONFIG.actions = [
@@ -166,23 +172,23 @@ class Parser < LL::Driver
166
172
  [:_rule_1, 0], # 1
167
173
  [:_rule_2, 2], # 2
168
174
  [:_rule_3, 2], # 3
169
- [:_rule_4, 2], # 4
170
- [:_rule_5, 2], # 5
175
+ [:_rule_4, 3], # 4
176
+ [:_rule_5, 1], # 5
171
177
  [:_rule_6, 2], # 6
172
178
  [:_rule_7, 2], # 7
173
179
  [:_rule_8, 1], # 8
174
- [:_rule_9, 1], # 9
175
- [:_rule_10, 2], # 10
176
- [:_rule_11, 2], # 11
180
+ [:_rule_9, 3], # 9
181
+ [:_rule_10, 3], # 10
182
+ [:_rule_11, 3], # 11
177
183
  [:_rule_12, 2], # 12
178
184
  [:_rule_13, 1], # 13
179
185
  [:_rule_14, 1], # 14
180
186
  [:_rule_15, 1], # 15
181
- [:_rule_16, 2], # 16
182
- [:_rule_17, 2], # 17
187
+ [:_rule_16, 1], # 16
188
+ [:_rule_17, 1], # 17
183
189
  [:_rule_18, 2], # 18
184
- [:_rule_19, 1], # 19
185
- [:_rule_20, 1], # 20
190
+ [:_rule_19, 2], # 19
191
+ [:_rule_20, 2], # 20
186
192
  [:_rule_21, 1], # 21
187
193
  [:_rule_22, 1], # 22
188
194
  [:_rule_23, 3], # 23
@@ -218,55 +224,47 @@ class Parser < LL::Driver
218
224
  [:_rule_53, 1], # 53
219
225
  [:_rule_54, 1], # 54
220
226
  [:_rule_55, 1], # 55
221
- [:_rule_56, 2], # 56
227
+ [:_rule_56, 1], # 56
222
228
  [:_rule_57, 1], # 57
223
229
  [:_rule_58, 1], # 58
224
230
  [:_rule_59, 1], # 59
225
- [:_rule_60, 1], # 60
231
+ [:_rule_60, 2], # 60
226
232
  [:_rule_61, 1], # 61
233
+ [:_rule_62, 1], # 62
234
+ [:_rule_63, 1], # 63
235
+ [:_rule_64, 1], # 64
236
+ [:_rule_65, 1], # 65
227
237
  ].freeze
228
238
 
229
- ##
230
239
  # @return [Oga::LRU]
231
- #
232
240
  CACHE = LRU.new
233
241
 
234
- ##
235
242
  # @param [String] data
236
243
  # @return [AST::Node]
237
- #
238
244
  def self.parse_with_cache(data)
239
- return CACHE.get_or_set(data) { new(data).parse }
245
+ CACHE.get_or_set(data) { new(data).parse }
240
246
  end
241
247
 
242
- ##
243
248
  # @param [String] data The input to parse.
244
- #
245
249
  def initialize(data)
246
250
  @lexer = Lexer.new(data)
247
251
  end
248
252
 
249
- ##
250
253
  # Resets the internal state of the parser.
251
- #
252
254
  def reset
253
255
  @current_element = nil
254
256
  end
255
257
 
256
- ##
257
258
  # @param [Symbol] type
258
259
  # @param [Array] children
259
260
  # @return [AST::Node]
260
- #
261
261
  def s(type, *children)
262
- return AST::Node.new(type, children)
262
+ AST::Node.new(type, children)
263
263
  end
264
264
 
265
- ##
266
265
  # Yields the next token from the lexer.
267
266
  #
268
267
  # @yieldparam [Array]
269
- #
270
268
  def each_token
271
269
  @lexer.advance do |*args|
272
270
  yield args
@@ -275,16 +273,13 @@ class Parser < LL::Driver
275
273
  yield [-1, -1]
276
274
  end
277
275
 
278
- ##
279
276
  # Returns the node test for the current element.
280
277
  #
281
278
  # @return [AST::Node]
282
- #
283
279
  def current_element
284
280
  @current_element ||= s(:test, nil, '*')
285
281
  end
286
282
 
287
- ##
288
283
  # Parses the input and returns the corresponding AST.
289
284
  #
290
285
  # @example
@@ -292,175 +287,158 @@ class Parser < LL::Driver
292
287
  # ast = parser.parse
293
288
  #
294
289
  # @return [AST::Node]
295
- #
296
290
  def parse
297
291
  reset
298
292
 
299
293
  super
300
294
  end
301
295
 
302
- ##
303
296
  # Generates the AST for a node test.
304
297
  #
305
298
  # @param [String] namespace
306
299
  # @param [String] name
307
300
  # @return [AST::Node]
308
- #
309
301
  def on_test(namespace, name)
310
302
  @current_element = s(:test, namespace, name)
311
303
  end
312
304
 
313
- ##
314
305
  # @param [String] name
315
306
  # @param [AST::Node] arg
316
307
  # @return [AST::Node]
317
- #
318
308
  def on_pseudo_class(name, arg = nil)
319
309
  handler = "on_pseudo_class_#{name.gsub('-', '_')}"
320
310
 
321
311
  arg ? send(handler, arg) : send(handler)
322
312
  end
323
313
 
324
- ##
325
314
  # Generates the AST for the `root` pseudo class.
326
315
  #
327
316
  # @return [AST::Node]
328
- #
329
317
  def on_pseudo_class_root
330
318
  s(:call, 'not', s(:axis, 'parent', s(:test, nil, '*')))
331
319
  end
332
320
 
333
- ##
334
321
  # Generates the AST for the `nth-child` pseudo class.
335
322
  #
336
323
  # @param [AST::Node] arg
337
324
  # @return [AST::Node]
338
- #
339
325
  def on_pseudo_class_nth_child(arg)
340
326
  generate_nth_child('preceding-sibling', arg)
341
327
  end
342
328
 
343
- ##
344
329
  # Generates the AST for the `nth-last-child` pseudo class.
345
330
  #
346
331
  # @param [AST::Node] arg
347
332
  # @return [AST::Node]
348
- #
349
333
  def on_pseudo_class_nth_last_child(arg)
350
334
  generate_nth_child('following-sibling', arg)
351
335
  end
352
336
 
353
- ##
354
337
  # Generates the AST for the `nth-of-type` pseudo class.
355
338
  #
356
339
  # @param [AST::Node] arg
357
340
  # @return [AST::Node]
358
- #
359
341
  def on_pseudo_class_nth_of_type(arg)
360
342
  generate_nth_child('preceding-sibling', arg, current_element)
361
343
  end
362
344
 
363
- ##
364
345
  # Generates the AST for the `nth-last-of-type` pseudo class.
365
346
  #
366
347
  # @param [AST::Node] arg
367
348
  # @return [AST::Node]
368
- #
369
349
  def on_pseudo_class_nth_last_of_type(arg)
370
350
  generate_nth_child('following-sibling', arg, current_element)
371
351
  end
372
352
 
373
- ##
374
353
  # Generates the AST for the `nth` pseudo class.
375
354
  #
376
355
  # @param [AST::Node] arg
377
356
  # @return [AST::Node]
378
- #
379
357
  def on_pseudo_class_nth(arg)
380
358
  s(:eq, s(:call, 'position'), arg)
381
359
  end
382
360
 
383
- ##
384
361
  # Generates the AST for the `:first-child` selector.
385
362
  #
386
363
  # @return [AST::Node]
387
- #
388
364
  def on_pseudo_class_first_child
389
365
  generate_no_siblings('preceding-sibling')
390
366
  end
391
367
 
392
- ##
393
368
  # Generates the AST for the `:last-child` selector.
394
369
  #
395
370
  # @return [AST::Node]
396
- #
397
371
  def on_pseudo_class_last_child
398
372
  generate_no_siblings('following-sibling')
399
373
  end
400
374
 
401
- ##
402
375
  # Generates the AST for the `:first-of-type` selector.
403
376
  #
404
377
  # @return [AST::Node]
405
- #
406
378
  def on_pseudo_class_first_of_type
407
379
  generate_no_siblings('preceding-sibling', current_element)
408
380
  end
409
381
 
410
- ##
411
382
  # Generates the AST for the `:last-of-type` selector.
412
383
  #
413
384
  # @return [AST::Node]
414
- #
415
385
  def on_pseudo_class_last_of_type
416
386
  generate_no_siblings('following-sibling', current_element)
417
387
  end
418
388
 
419
- ##
420
389
  # Generates the AST for the `:only-child` selector.
421
390
  #
422
391
  # @return [AST::Node]
423
- #
424
392
  def on_pseudo_class_only_child
425
393
  s(:and, on_pseudo_class_first_child, on_pseudo_class_last_child)
426
394
  end
427
395
 
428
- ##
429
396
  # Generates the AST for the `:only-of-type` selector.
430
397
  #
431
398
  # @return [AST::Node]
432
- #
433
399
  def on_pseudo_class_only_of_type
434
400
  s(:and, on_pseudo_class_first_of_type, on_pseudo_class_last_of_type)
435
401
  end
436
402
 
437
- ##
438
403
  # Generates the AST for the `:empty` selector.
439
404
  #
440
405
  # @return [AST::Node]
441
- #
442
406
  def on_pseudo_class_empty
443
407
  s(:call, 'not', s(:axis, 'child', s(:type_test, 'node')))
444
408
  end
445
409
 
446
- ##
410
+ # Generates the AST for the `:not` selector.
411
+ #
412
+ # @param [AST::Node] arg
413
+ # @return [AST::Node]
414
+ def on_pseudo_class_not(arg)
415
+ # Unpacks (axis "descendant" (test nil "x")) into just (test nil "x") as
416
+ # in this case we want to wrap the (test) node in a (axis "self") node.
417
+ if arg.type == :axis
418
+ arg = s(:axis, 'self', arg.children[1])
419
+
420
+ # Unpack (predicate (eq ...)) into just (eq)
421
+ elsif arg.type == :predicate
422
+ arg = arg.children[1]
423
+ end
424
+
425
+ s(:call, 'not', arg)
426
+ end
427
+
447
428
  # Generates the AST for the `=` operator.
448
429
  #
449
430
  # @param [AST::Node] attr
450
431
  # @param [AST::Node] value
451
432
  # @return [AST::Node]
452
- #
453
433
  def on_op_eq(attr, value)
454
434
  s(:eq, attr, value)
455
435
  end
456
436
 
457
- ##
458
437
  # Generates the AST for the `~=` operator.
459
438
  #
460
439
  # @param [AST::Node] attr
461
440
  # @param [AST::Node] value
462
441
  # @return [AST::Node]
463
- #
464
442
  def on_op_space_in(attr, value)
465
443
  s(
466
444
  :call,
@@ -470,24 +448,20 @@ class Parser < LL::Driver
470
448
  )
471
449
  end
472
450
 
473
- ##
474
451
  # Generates the AST for the `^=` operator.
475
452
  #
476
453
  # @param [AST::Node] attr
477
454
  # @param [AST::Node] value
478
455
  # @return [AST::Node]
479
- #
480
456
  def on_op_starts_with(attr, value)
481
457
  s(:call, 'starts-with', attr, value)
482
458
  end
483
459
 
484
- ##
485
460
  # Generates the AST for the `$=` operator.
486
461
  #
487
462
  # @param [AST::Node] attr
488
463
  # @param [AST::Node] value
489
464
  # @return [AST::Node]
490
- #
491
465
  def on_op_ends_with(attr, value)
492
466
  s(
493
467
  :eq,
@@ -510,24 +484,20 @@ class Parser < LL::Driver
510
484
  )
511
485
  end
512
486
 
513
- ##
514
487
  # Generates the AST for the `*=` operator.
515
488
  #
516
489
  # @param [AST::Node] attr
517
490
  # @param [AST::Node] value
518
491
  # @return [AST::Node]
519
- #
520
492
  def on_op_in(attr, value)
521
493
  s(:call, 'contains', attr, value)
522
494
  end
523
495
 
524
- ##
525
496
  # Generates the AST for the `|=` operator.
526
497
  #
527
498
  # @param [AST::Node] attr
528
499
  # @param [AST::Node] value
529
500
  # @return [AST::Node]
530
- #
531
501
  def on_op_hyphen_in(attr, value)
532
502
  s(
533
503
  :or,
@@ -543,12 +513,10 @@ class Parser < LL::Driver
543
513
 
544
514
  private
545
515
 
546
- ##
547
516
  # @param [String] count_axis
548
517
  # @param [AST::Node] arg
549
518
  # @param [AST::Node] count_test
550
519
  # @return [AST::Node]
551
- #
552
520
  def generate_nth_child(count_axis, arg, count_test = s(:test, nil, '*'))
553
521
  count_call = s(:call, 'count', s(:axis, count_axis, count_test))
554
522
 
@@ -575,46 +543,36 @@ class Parser < LL::Driver
575
543
  end
576
544
  end
577
545
 
578
- return node
546
+ node
579
547
  end
580
548
 
581
- ##
582
549
  # @param [String] axis
583
550
  # @param [AST::Node] test
584
551
  # @return [AST::Node]
585
- #
586
552
  def generate_no_siblings(axis, test = s(:test, nil, '*'))
587
553
  s(:eq, s(:call, 'count', s(:axis, axis, test)), s(:int, 0))
588
554
  end
589
555
 
590
- ##
591
556
  # @param [AST::Node] node
592
557
  # @return [TrueClass|FalseClass]
593
- #
594
558
  def int_node?(node)
595
559
  node.type.equal?(:int)
596
560
  end
597
561
 
598
- ##
599
562
  # @param [AST::Node] node
600
563
  # @return [TrueClass|FalseClass]
601
- #
602
564
  def non_positive_number?(node)
603
565
  node.children[0] <= 0
604
566
  end
605
567
 
606
- ##
607
568
  # @param [AST::Node] node
608
569
  # @return [Symbol]
609
- #
610
570
  def step_comparison(node)
611
571
  node.children[0] >= 0 ? :gte : :lte
612
572
  end
613
573
 
614
- ##
615
574
  # @param [AST::Node] step
616
575
  # @return [AST::Node]
617
- #
618
576
  def step_modulo_value(step)
619
577
  # -2n
620
578
  if step and non_positive_number?(step)
@@ -631,6 +589,34 @@ class Parser < LL::Driver
631
589
  mod_val
632
590
  end
633
591
 
592
+ # @param [String] name
593
+ # @param [AST::Node] test
594
+ # @param [AST::Node] predicates
595
+ # @param [AST::Node] more
596
+ # @return [AST::Node]
597
+ def generate_axis(name, test = nil, predicates = nil, more = nil)
598
+ if test
599
+ node = s(:axis, name, test)
600
+ else
601
+ node = s(:axis, name, on_test(nil, '*'))
602
+ end
603
+
604
+ if predicates
605
+ node = s(:predicate, node, predicates)
606
+ end
607
+
608
+ node = add_child(node, more) if more
609
+
610
+ node
611
+ end
612
+
613
+ # @param [AST::Node] node
614
+ # @param [AST::Node] child
615
+ # @return [AST::Node]
616
+ def add_child(node, child)
617
+ node.updated(nil, node.children + [child])
618
+ end
619
+
634
620
  def _rule_0(val)
635
621
  val[0]
636
622
  end
@@ -641,13 +627,13 @@ class Parser < LL::Driver
641
627
 
642
628
  def _rule_2(val)
643
629
 
644
- path = val[0]
630
+ query = val[0]
645
631
 
646
632
  val[1].each do |chunk|
647
- path = s(:pipe, path, chunk)
633
+ query = s(:pipe, query, chunk)
648
634
  end
649
635
 
650
- path
636
+ query
651
637
 
652
638
  end
653
639
 
@@ -657,88 +643,88 @@ class Parser < LL::Driver
657
643
 
658
644
  def _rule_4(val)
659
645
 
660
- # Single selector
661
- if val[1].empty?
662
- ret = val[0]
646
+ node = s(:axis, 'descendant', val[0])
647
+ preds = val[1]
648
+ more = val[2]
663
649
 
664
- if ret.is_a?(Array)
665
- ret = s(:path, *ret)
666
- end
667
-
668
- # Multiple selectors
669
- else
670
- steps = [val[0]]
671
-
672
- val[1].each do |step|
673
- # "+ foo" is broken up into two steps.
674
- if step.is_a?(Array)
675
- # Using Array#+ or Array#| would require allocating an extra Array
676
- step.each { |sub| steps << sub }
677
- else
678
- steps << step
679
- end
680
- end
681
-
682
- ret = s(:path, *steps)
650
+ if preds
651
+ node = s(:predicate, node, preds)
683
652
  end
684
653
 
685
- ret
654
+ node = add_child(node, more) if more
655
+
656
+ node
686
657
 
687
658
  end
688
659
 
689
660
  def _rule_5(val)
690
- val[1]
661
+ val[0]
691
662
  end
692
663
 
693
664
  def _rule_6(val)
694
665
 
695
- val[1] ? s(:predicate, val[0], val[1]) : val[0]
666
+ node = s(:axis, 'descendant', on_test(nil, '*'))
667
+ preds = val[0]
668
+ more = val[1]
669
+
670
+ if preds
671
+ node = s(:predicate, node, preds)
672
+ else
673
+ node = s(:predicate, node)
674
+ end
675
+
676
+ node = add_child(node, more) if more
677
+
678
+ node
696
679
 
697
680
  end
698
681
 
699
682
  def _rule_7(val)
700
-
701
- val[1] ? s(:predicate, val[0], val[1]) : val[0]
702
-
683
+ val[1]
703
684
  end
704
685
 
705
686
  def _rule_8(val)
706
-
707
- s(:predicate, s(:axis, 'descendant', on_test(nil, '*')), val[0])
708
-
687
+ val[0]
709
688
  end
710
689
 
711
690
  def _rule_9(val)
712
- s(:axis, 'descendant', val[0])
691
+
692
+ test, preds = val[1]
693
+ more = val[2]
694
+
695
+ generate_axis('child', test, preds, more)
696
+
713
697
  end
714
698
 
715
699
  def _rule_10(val)
716
700
 
717
- s(:axis, 'child', val[1])
701
+ test, preds = val[1]
702
+ more = val[2]
703
+
704
+ s(
705
+ :predicate,
706
+ s(:axis, 'following-sibling', on_test(nil, '*')),
707
+ s(:int, 1),
708
+ generate_axis('self', test, preds, more)
709
+ )
718
710
 
719
711
  end
720
712
 
721
713
  def _rule_11(val)
722
714
 
723
- s(:axis, 'following-sibling', val[1])
715
+ test, preds = val[1]
716
+ more = val[2]
717
+
718
+ generate_axis('following-sibling', test, preds, more)
724
719
 
725
720
  end
726
721
 
727
722
  def _rule_12(val)
728
-
729
- [
730
- s(
731
- :predicate,
732
- s(:axis, 'following-sibling', on_test(nil, '*')),
733
- s(:int, 1)
734
- ),
735
- s(:axis, 'self', val[1])
736
- ]
737
-
723
+ val
738
724
  end
739
725
 
740
726
  def _rule_13(val)
741
- val[0]
727
+ [nil, val[0]]
742
728
  end
743
729
 
744
730
  def _rule_14(val)
@@ -746,18 +732,26 @@ class Parser < LL::Driver
746
732
  end
747
733
 
748
734
  def _rule_15(val)
749
- on_test(*val[0])
735
+ val[0]
750
736
  end
751
737
 
752
738
  def _rule_16(val)
753
- val[1] ? [val[0], val[1]] : [nil, val[0]]
739
+ val[0]
754
740
  end
755
741
 
756
742
  def _rule_17(val)
757
- val[1]
743
+ on_test(*val[0])
758
744
  end
759
745
 
760
746
  def _rule_18(val)
747
+ val[1] ? [val[0], val[1]] : [nil, val[0]]
748
+ end
749
+
750
+ def _rule_19(val)
751
+ val[1]
752
+ end
753
+
754
+ def _rule_20(val)
761
755
 
762
756
  ret = val[0]
763
757
 
@@ -769,14 +763,6 @@ class Parser < LL::Driver
769
763
 
770
764
  end
771
765
 
772
- def _rule_19(val)
773
- val[0]
774
- end
775
-
776
- def _rule_20(val)
777
- val[0]
778
- end
779
-
780
766
  def _rule_21(val)
781
767
  val[0]
782
768
  end
@@ -989,7 +975,7 @@ class Parser < LL::Driver
989
975
  end
990
976
 
991
977
  def _rule_56(val)
992
- val
978
+ val[0]
993
979
  end
994
980
 
995
981
  def _rule_57(val)
@@ -1005,12 +991,28 @@ class Parser < LL::Driver
1005
991
  end
1006
992
 
1007
993
  def _rule_60(val)
1008
- val[0]
994
+ val
1009
995
  end
1010
996
 
1011
997
  def _rule_61(val)
1012
998
  val[0]
1013
999
  end
1000
+
1001
+ def _rule_62(val)
1002
+ val[0]
1003
+ end
1004
+
1005
+ def _rule_63(val)
1006
+ val[0]
1007
+ end
1008
+
1009
+ def _rule_64(val)
1010
+ val[0]
1011
+ end
1012
+
1013
+ def _rule_65(val)
1014
+ val[0]
1015
+ end
1014
1016
  end
1015
1017
  end
1016
1018
  end