oga 1.2.3-java → 1.3.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/doc/css_selectors.md +1 -1
  3. data/lib/liboga.jar +0 -0
  4. data/lib/oga.rb +6 -1
  5. data/lib/oga/blacklist.rb +0 -10
  6. data/lib/oga/css/lexer.rb +530 -255
  7. data/lib/oga/css/parser.rb +232 -230
  8. data/lib/oga/entity_decoder.rb +0 -4
  9. data/lib/oga/html/entities.rb +0 -4
  10. data/lib/oga/html/parser.rb +0 -4
  11. data/lib/oga/html/sax_parser.rb +0 -4
  12. data/lib/oga/lru.rb +0 -26
  13. data/lib/oga/oga.rb +0 -8
  14. data/lib/oga/ruby/generator.rb +225 -0
  15. data/lib/oga/ruby/node.rb +189 -0
  16. data/lib/oga/version.rb +1 -1
  17. data/lib/oga/whitelist.rb +0 -6
  18. data/lib/oga/xml/attribute.rb +13 -20
  19. data/lib/oga/xml/cdata.rb +0 -4
  20. data/lib/oga/xml/character_node.rb +0 -8
  21. data/lib/oga/xml/comment.rb +0 -4
  22. data/lib/oga/xml/default_namespace.rb +0 -2
  23. data/lib/oga/xml/doctype.rb +0 -8
  24. data/lib/oga/xml/document.rb +10 -14
  25. data/lib/oga/xml/element.rb +1 -52
  26. data/lib/oga/xml/entities.rb +0 -26
  27. data/lib/oga/xml/expanded_name.rb +12 -0
  28. data/lib/oga/xml/html_void_elements.rb +0 -2
  29. data/lib/oga/xml/lexer.rb +0 -86
  30. data/lib/oga/xml/namespace.rb +0 -10
  31. data/lib/oga/xml/node.rb +18 -34
  32. data/lib/oga/xml/node_set.rb +0 -50
  33. data/lib/oga/xml/parser.rb +13 -50
  34. data/lib/oga/xml/processing_instruction.rb +0 -8
  35. data/lib/oga/xml/pull_parser.rb +0 -18
  36. data/lib/oga/xml/querying.rb +58 -19
  37. data/lib/oga/xml/sax_parser.rb +0 -18
  38. data/lib/oga/xml/text.rb +0 -12
  39. data/lib/oga/xml/traversal.rb +0 -4
  40. data/lib/oga/xml/xml_declaration.rb +0 -8
  41. data/lib/oga/xpath/compiler.rb +1568 -0
  42. data/lib/oga/xpath/conversion.rb +102 -0
  43. data/lib/oga/xpath/lexer.rb +1844 -1238
  44. data/lib/oga/xpath/parser.rb +182 -153
  45. metadata +7 -3
  46. data/lib/oga/xpath/evaluator.rb +0 -1800
@@ -1,17 +1,14 @@
1
1
  module Oga
2
2
  module XML
3
- ##
4
3
  # A generic XML node. Instances of this class can belong to a
5
4
  # {Oga::XML::NodeSet} and can be used to query surrounding and parent
6
5
  # nodes.
7
- #
8
6
  class Node
9
7
  include Traversal
10
8
 
11
9
  # @return [Oga::XML::NodeSet]
12
10
  attr_reader :node_set
13
11
 
14
- ##
15
12
  # @param [Hash] options
16
13
  #
17
14
  # @option options [Oga::XML::NodeSet] :node_set The node set that this
@@ -19,35 +16,28 @@ module Oga
19
16
  #
20
17
  # @option options [Oga::XML::NodeSet|Array] :children The child nodes of
21
18
  # the current node.
22
- #
23
19
  def initialize(options = {})
24
20
  self.node_set = options[:node_set]
25
21
  self.children = options[:children] if options[:children]
26
22
  end
27
23
 
28
- ##
29
24
  # @param [Oga::XML::NodeSet] set
30
- #
31
25
  def node_set=(set)
32
26
  @node_set = set
33
27
  @root_node = nil
34
28
  @html_p = nil
35
29
  end
36
30
 
37
- ##
38
31
  # Returns the child nodes of the current node.
39
32
  #
40
33
  # @return [Oga::XML::NodeSet]
41
- #
42
34
  def children
43
35
  @children ||= NodeSet.new([], self)
44
36
  end
45
37
 
46
- ##
47
38
  # Sets the child nodes of the element.
48
39
  #
49
40
  # @param [Oga::XML::NodeSet|Array] nodes
50
- #
51
41
  def children=(nodes)
52
42
  if nodes.is_a?(NodeSet)
53
43
  @children = nodes
@@ -56,32 +46,26 @@ module Oga
56
46
  end
57
47
  end
58
48
 
59
- ##
60
49
  # Returns the parent node of the current node. If there is no parent node
61
50
  # `nil` is returned instead.
62
51
  #
63
52
  # @return [Oga::XML::Node]
64
- #
65
53
  def parent
66
54
  node_set ? node_set.owner : nil
67
55
  end
68
56
 
69
- ##
70
57
  # Returns the preceding node, or nil if there is none.
71
58
  #
72
59
  # @return [Oga::XML::Node]
73
- #
74
60
  def previous
75
61
  index = node_set.index(self) - 1
76
62
 
77
63
  index >= 0 ? node_set[index] : nil
78
64
  end
79
65
 
80
- ##
81
66
  # Returns the following node, or nil if there is none.
82
67
  #
83
68
  # @return [Oga::XML::Node]
84
- #
85
69
  def next
86
70
  index = node_set.index(self) + 1
87
71
  length = node_set.length
@@ -89,11 +73,9 @@ module Oga
89
73
  index <= length ? node_set[index] : nil
90
74
  end
91
75
 
92
- ##
93
76
  # Returns the previous element node or nil if there is none.
94
77
  #
95
78
  # @return [Oga::XML::Element]
96
- #
97
79
  def previous_element
98
80
  node = self
99
81
 
@@ -104,11 +86,9 @@ module Oga
104
86
  return
105
87
  end
106
88
 
107
- ##
108
89
  # Returns the next element node or nil if there is none.
109
90
  #
110
91
  # @return [Oga::XML::Element]
111
- #
112
92
  def next_element
113
93
  node = self
114
94
 
@@ -119,12 +99,10 @@ module Oga
119
99
  return
120
100
  end
121
101
 
122
- ##
123
102
  # Returns the root document/node of the current node. The node is
124
103
  # retrieved by traversing upwards in the DOM tree from the current node.
125
104
  #
126
105
  # @return [Oga::XML::Document|Oga::XML::Node]
127
- #
128
106
  def root_node
129
107
  unless @root_node
130
108
  node = self
@@ -143,16 +121,13 @@ module Oga
143
121
  @root_node
144
122
  end
145
123
 
146
- ##
147
124
  # Removes the current node from the owning node set.
148
125
  #
149
126
  # @return [Oga::XML::Node]
150
- #
151
127
  def remove
152
128
  return node_set.delete(self) if node_set
153
129
  end
154
130
 
155
- ##
156
131
  # Replaces the current node with another.
157
132
  #
158
133
  # @example Replacing with an element
@@ -163,7 +138,6 @@ module Oga
163
138
  # some_node.replace('this will replace the current node with a text node')
164
139
  #
165
140
  # @param [String|Oga::XML::Node] other
166
- #
167
141
  def replace(other)
168
142
  if other.is_a?(String)
169
143
  other = Text.new(:text => other)
@@ -173,31 +147,25 @@ module Oga
173
147
  remove
174
148
  end
175
149
 
176
- ##
177
150
  # Inserts the given node before the current node.
178
151
  #
179
152
  # @param [Oga::XML::Node] other
180
- #
181
153
  def before(other)
182
154
  index = node_set.index(self)
183
155
 
184
156
  node_set.insert(index, other)
185
157
  end
186
158
 
187
- ##
188
159
  # Inserts the given node after the current node.
189
160
  #
190
161
  # @param [Oga::XML::Node] other
191
- #
192
162
  def after(other)
193
163
  index = node_set.index(self) + 1
194
164
 
195
165
  node_set.insert(index, other)
196
166
  end
197
167
 
198
- ##
199
168
  # @return [TrueClass|FalseClass]
200
- #
201
169
  def html?
202
170
  if @html_p.nil?
203
171
  root = root_node
@@ -208,12 +176,28 @@ module Oga
208
176
  @html_p
209
177
  end
210
178
 
211
- ##
212
179
  # @return [TrueClass|FalseClass]
213
- #
214
180
  def xml?
215
181
  !html?
216
182
  end
183
+
184
+ # Yields all ancestor elements of the current node.
185
+ #
186
+ # @example
187
+ # some_element.each_ancestor do |node|
188
+ # # ...
189
+ # end
190
+ #
191
+ # @yieldparam [Oga::XML::Node]
192
+ def each_ancestor
193
+ node = parent
194
+
195
+ while node.is_a?(XML::Element)
196
+ yield node
197
+
198
+ node = node.parent
199
+ end
200
+ end
217
201
  end # Element
218
202
  end # XML
219
203
  end # Oga
@@ -1,6 +1,5 @@
1
1
  module Oga
2
2
  module XML
3
- ##
4
3
  # The NodeSet class contains a set of unique {Oga::XML::Node} instances that
5
4
  # can be queried and modified. Optionally NodeSet instances can take
6
5
  # ownership of a node (besides just containing it). This allows the nodes to
@@ -30,17 +29,14 @@ module Oga
30
29
  #
31
30
  # If ownership was not handled then you'd have to manually set the
32
31
  # `element` variable's `node_set` attribute after pushing it into a set.
33
- #
34
32
  class NodeSet
35
33
  include Enumerable
36
34
 
37
35
  # @return [Oga::XML::Node]
38
36
  attr_accessor :owner
39
37
 
40
- ##
41
38
  # @param [Array] nodes The nodes to add to the set.
42
39
  # @param [Oga::XML::NodeSet] owner The owner of the set.
43
- #
44
40
  def initialize(nodes = [], owner = nil)
45
41
  @nodes = nodes
46
42
  @owner = owner
@@ -53,38 +49,30 @@ module Oga
53
49
  end
54
50
  end
55
51
 
56
- ##
57
52
  # Yields the supplied block for every node.
58
53
  #
59
54
  # @yieldparam [Oga::XML::Node]
60
- #
61
55
  def each
62
56
  @nodes.each { |node| yield node }
63
57
  end
64
58
 
65
- ##
66
59
  # Returns the last node in the set.
67
60
  #
68
61
  # @return [Oga::XML::Node]
69
- #
70
62
  def last
71
63
  @nodes[-1]
72
64
  end
73
65
 
74
- ##
75
66
  # Returns `true` if the set is empty.
76
67
  #
77
68
  # @return [TrueClass|FalseClass]
78
- #
79
69
  def empty?
80
70
  @nodes.empty?
81
71
  end
82
72
 
83
- ##
84
73
  # Returns the amount of nodes in the set.
85
74
  #
86
75
  # @return [Fixnum]
87
- #
88
76
  def length
89
77
  @nodes.length
90
78
  end
@@ -92,21 +80,17 @@ module Oga
92
80
  alias_method :count, :length
93
81
  alias_method :size, :length
94
82
 
95
- ##
96
83
  # Returns the index of the given node.
97
84
  #
98
85
  # @param [Oga::XML::Node] node
99
86
  # @return [Fixnum]
100
- #
101
87
  def index(node)
102
88
  @nodes.index(node)
103
89
  end
104
90
 
105
- ##
106
91
  # Pushes the node at the end of the set.
107
92
  #
108
93
  # @param [Oga::XML::Node] node
109
- #
110
94
  def push(node)
111
95
  return if exists?(node)
112
96
 
@@ -119,11 +103,9 @@ module Oga
119
103
 
120
104
  alias_method :<<, :push
121
105
 
122
- ##
123
106
  # Pushes the node at the start of the set.
124
107
  #
125
108
  # @param [Oga::XML::Node] node
126
- #
127
109
  def unshift(node)
128
110
  return if exists?(node)
129
111
 
@@ -134,11 +116,9 @@ module Oga
134
116
  take_ownership(node) if @owner
135
117
  end
136
118
 
137
- ##
138
119
  # Shifts a node from the start of the set.
139
120
  #
140
121
  # @return [Oga::XML::Node]
141
- #
142
122
  def shift
143
123
  node = @nodes.shift
144
124
 
@@ -151,11 +131,9 @@ module Oga
151
131
  node
152
132
  end
153
133
 
154
- ##
155
134
  # Pops a node from the end of the set.
156
135
  #
157
136
  # @return [Oga::XML::Node]
158
- #
159
137
  def pop
160
138
  node = @nodes.pop
161
139
 
@@ -168,12 +146,10 @@ module Oga
168
146
  node
169
147
  end
170
148
 
171
- ##
172
149
  # Inserts a node into the set at the given index.
173
150
  #
174
151
  # @param [Fixnum] index The index to insert the node at.
175
152
  # @param [Oga::XML::Node] node
176
- #
177
153
  def insert(index, node)
178
154
  return if exists?(node)
179
155
 
@@ -184,73 +160,59 @@ module Oga
184
160
  take_ownership(node) if @owner
185
161
  end
186
162
 
187
- ##
188
163
  # Returns the node for the given index.
189
164
  #
190
165
  # @param [Fixnum] index
191
166
  # @return [Oga::XML::Node]
192
- #
193
167
  def [](index)
194
168
  @nodes[index]
195
169
  end
196
170
 
197
- ##
198
171
  # Converts the current set to an Array.
199
172
  #
200
173
  # @return [Array]
201
- #
202
174
  def to_a
203
175
  @nodes
204
176
  end
205
177
 
206
- ##
207
178
  # Creates a new set based on the current and the specified set. The newly
208
179
  # created set does not inherit ownership rules of the current set.
209
180
  #
210
181
  # @param [Oga::XML::NodeSet] other
211
182
  # @return [Oga::XML::NodeSet]
212
- #
213
183
  def +(other)
214
184
  self.class.new(to_a | other.to_a)
215
185
  end
216
186
 
217
- ##
218
187
  # Returns `true` if the current node set and the one given in `other` are
219
188
  # equal to each other.
220
189
  #
221
190
  # @param [Oga::XML::NodeSet] other
222
- #
223
191
  def ==(other)
224
192
  other.is_a?(NodeSet) && other.equal_nodes?(@nodes)
225
193
  end
226
194
 
227
- ##
228
195
  # Returns `true` if the nodes given in `nodes` are equal to those
229
196
  # specified in the current `@nodes` variable. This method allows two
230
197
  # NodeSet instances to compare each other without the need of exposing
231
198
  # `@nodes` to the public.
232
199
  #
233
200
  # @param [Array<Oga::XML::Node>] nodes
234
- #
235
201
  def equal_nodes?(nodes)
236
202
  @nodes == nodes
237
203
  end
238
204
 
239
- ##
240
205
  # Adds the nodes of the given node set to the current node set.
241
206
  #
242
207
  # @param [Oga::XML::NodeSet] other
243
- #
244
208
  def concat(other)
245
209
  other.each { |node| push(node) }
246
210
  end
247
211
 
248
- ##
249
212
  # Removes the current nodes from their owning set. The nodes are *not*
250
213
  # removed from the current set.
251
214
  #
252
215
  # This method is intended to remove nodes from an XML document/node.
253
- #
254
216
  def remove
255
217
  sets = []
256
218
 
@@ -270,9 +232,7 @@ module Oga
270
232
  end
271
233
  end
272
234
 
273
- ##
274
235
  # Removes a node from the current set only.
275
- #
276
236
  def delete(node)
277
237
  removed = @nodes.delete(node)
278
238
 
@@ -285,12 +245,10 @@ module Oga
285
245
  removed
286
246
  end
287
247
 
288
- ##
289
248
  # Returns the values of the given attribute.
290
249
  #
291
250
  # @param [String|Symbol] name The name of the attribute.
292
251
  # @return [Array]
293
- #
294
252
  def attribute(name)
295
253
  values = []
296
254
 
@@ -305,11 +263,9 @@ module Oga
305
263
 
306
264
  alias_method :attr, :attribute
307
265
 
308
- ##
309
266
  # Returns the text of all nodes in the set, ignoring comment nodes.
310
267
  #
311
268
  # @return [String]
312
- #
313
269
  def text
314
270
  text = ''
315
271
 
@@ -322,9 +278,7 @@ module Oga
322
278
  text
323
279
  end
324
280
 
325
- ##
326
281
  # @return [String]
327
- #
328
282
  def inspect
329
283
  values = @nodes.map(&:inspect).join(', ')
330
284
 
@@ -333,21 +287,17 @@ module Oga
333
287
 
334
288
  private
335
289
 
336
- ##
337
290
  # Takes ownership of the given node. This only occurs when the current
338
291
  # set has an owner.
339
292
  #
340
293
  # @param [Oga::XML::Node] node
341
- #
342
294
  def take_ownership(node)
343
295
  node.node_set = self
344
296
  end
345
297
 
346
- ##
347
298
  # Removes ownership of the node if it belongs to the current set.
348
299
  #
349
300
  # @param [Oga::XML::Node] node
350
- #
351
301
  def remove_ownership(node)
352
302
  node.node_set = nil if node.node_set == self
353
303
  end