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
@@ -0,0 +1,189 @@
1
+ module Oga
2
+ module Ruby
3
+ # Class representing a single node in a Ruby AST.
4
+ #
5
+ # The setup of this class is roughly based on the "ast" Gem. The "ast" Gem
6
+ # is not used for this class as it provides too many methods that might
7
+ # conflict with this class' {#method_missing}.
8
+ #
9
+ # ASTs can be built by creating a node and then chaining various method
10
+ # calls together. For example, the following could be used to build an "if"
11
+ # statement:
12
+ #
13
+ # number1 = Node.new(:lit, %w{10})
14
+ # number2 = Node.new(:lit, %w{20})
15
+ #
16
+ # (number2 > number1).if_true do
17
+ # Node.new(:lit, %w{30})
18
+ # end
19
+ #
20
+ # When serialized to Ruby this would roughly lead to the following code:
21
+ #
22
+ # if 20 > 10
23
+ # 30
24
+ # end
25
+ #
26
+ # @private
27
+ class Node < BasicObject
28
+ undef_method :!, :!=
29
+
30
+ # @return [Symbol]
31
+ attr_reader :type
32
+
33
+ # @param [Symbol] type
34
+ # @param [Array] children
35
+ def initialize(type, children = [])
36
+ @type = type.to_sym
37
+ @children = children
38
+ end
39
+
40
+ # @return [Array]
41
+ def to_a
42
+ @children
43
+ end
44
+
45
+ alias_method :to_ary, :to_a
46
+
47
+ # Returns a "to_a" call node.
48
+ #
49
+ # @return [Oga::Ruby::Node]
50
+ def to_array
51
+ Node.new(:send, [self, :to_a])
52
+ end
53
+
54
+ # Returns an assignment node.
55
+ #
56
+ # This method wraps assigned values in a begin/end block to ensure that
57
+ # multiple lines of code result in the proper value being assigned.
58
+ #
59
+ # @param [Oga::Ruby::Node] other
60
+ # @return [Oga::Ruby::Node]
61
+ def assign(other)
62
+ if other.type == :followed_by
63
+ other = other.wrap
64
+ end
65
+
66
+ Node.new(:assign, [self, other])
67
+ end
68
+
69
+ # Returns an equality expression node.
70
+ #
71
+ # @param [Oga::Ruby::Node] other
72
+ # @return [Oga::Ruby::Node]
73
+ def eq(other)
74
+ Node.new(:eq, [self, other])
75
+ end
76
+
77
+ # Returns a boolean "and" node.
78
+ #
79
+ # @param [Oga::Ruby::Node] other
80
+ # @return [Oga::Ruby::Node]
81
+ def and(other)
82
+ Node.new(:and, [self, other])
83
+ end
84
+
85
+ # Returns a boolean "or" node.
86
+ #
87
+ # @param [Oga::Ruby::Node] other
88
+ # @return [Oga::Ruby::Node]
89
+ def or(other)
90
+ Node.new(:or, [self, other])
91
+ end
92
+
93
+ # Returns a node that evaluates to its inverse.
94
+ #
95
+ # For example, a variable `foo` would be turned into `!foo`.
96
+ #
97
+ # @return [Oga::Ruby::Node]
98
+ def not
99
+ !self
100
+ end
101
+
102
+ # Returns a node for Ruby's "is_a?" method.
103
+ #
104
+ # @param [Class] klass
105
+ # @return [Oga::Ruby::Node]
106
+ def is_a?(klass)
107
+ Node.new(:send, [self, 'is_a?', Node.new(:lit, [klass.to_s])])
108
+ end
109
+
110
+ # Wraps the current node in a block.
111
+ #
112
+ # @param [Array] args Arguments (as Node instances) to pass to the block.
113
+ # @return [Oga::Ruby::Node]
114
+ def add_block(*args)
115
+ Node.new(:block, [self, args, yield])
116
+ end
117
+
118
+ # Wraps the current node in a `begin` node.
119
+ #
120
+ # @return [Oga::Ruby::Node]
121
+ def wrap
122
+ Node.new(:begin, [self])
123
+ end
124
+
125
+ # Wraps the current node in an if statement node.
126
+ #
127
+ # The body of this statement is set to the return value of the supplied
128
+ # block.
129
+ #
130
+ # @return [Oga::Ruby::Node]
131
+ def if_true
132
+ Node.new(:if, [self, yield])
133
+ end
134
+
135
+ # Wraps the current node in an `if !...` statement.
136
+ #
137
+ # @see [#if_true]
138
+ def if_false
139
+ self.not.if_true { yield }
140
+ end
141
+
142
+ # Wraps the current node in a `while` statement.
143
+ #
144
+ # The body of this statement is set to the return value of the supplied
145
+ # block.
146
+ #
147
+ # @return [Oga::Ruby::Node]
148
+ def while_true
149
+ Node.new(:while, [self, yield])
150
+ end
151
+
152
+ # Adds an "else" statement to the current node.
153
+ #
154
+ # This method assumes it's being called only on "if" nodes.
155
+ #
156
+ # @return [Oga::Ruby::Node]
157
+ def else
158
+ Node.new(:if, @children + [yield])
159
+ end
160
+
161
+ # Chains two nodes together.
162
+ #
163
+ # @param [Oga::Ruby::Node] other
164
+ # @return [Oga::Ruby::Node]
165
+ def followed_by(other = nil)
166
+ other = yield if ::Kernel.block_given?
167
+
168
+ Node.new(:followed_by, [self, other])
169
+ end
170
+
171
+ # Returns a node for a method call.
172
+ #
173
+ # @param [Symbol] name The name of the method to call.
174
+ #
175
+ # @param [Array] args Any arguments (as Node instances) to pass to the
176
+ # method.
177
+ #
178
+ # @return [Oga::Ruby::Node]
179
+ def method_missing(name, *args)
180
+ Node.new(:send, [self, name.to_s, *args])
181
+ end
182
+
183
+ # @return [String]
184
+ def inspect
185
+ "(#{type} #{@children.map(&:inspect).join(' ')})"
186
+ end
187
+ end # Node
188
+ end # Ruby
189
+ end # Oga
@@ -1,3 +1,3 @@
1
1
  module Oga
2
- VERSION = '1.2.3'
2
+ VERSION = '1.3.0'
3
3
  end # Oga
@@ -1,18 +1,12 @@
1
1
  module Oga
2
- ##
3
2
  # @api private
4
- #
5
3
  class Whitelist < Blacklist
6
- ##
7
4
  # @return [TrueClass|FalseClass]
8
- #
9
5
  def allow?(name)
10
6
  names.include?(name)
11
7
  end
12
8
 
13
- ##
14
9
  # @return [Oga::Blacklist]
15
- #
16
10
  def to_blacklist
17
11
  Blacklist.new(names)
18
12
  end
@@ -1,9 +1,9 @@
1
1
  module Oga
2
2
  module XML
3
- ##
4
3
  # Class for storing information about a single XML attribute.
5
- #
6
4
  class Attribute
5
+ include ExpandedName
6
+
7
7
  # The name of the attribute.
8
8
  # @return [String]
9
9
  attr_accessor :name
@@ -15,25 +15,23 @@ module Oga
15
15
  # @return [Oga::XML::Element]
16
16
  attr_accessor :element
17
17
 
18
- ##
18
+ alias_method :parent, :element
19
+
19
20
  # The default namespace available to all attributes. This namespace can
20
21
  # not be modified.
21
22
  #
22
23
  # @return [Oga::XML::Namespace]
23
- #
24
24
  DEFAULT_NAMESPACE = Namespace.new(
25
25
  :name => 'xml',
26
26
  :uri => XML::DEFAULT_NAMESPACE.uri
27
27
  ).freeze
28
28
 
29
- ##
30
29
  # @param [Hash] options
31
30
  #
32
31
  # @option options [String] :name
33
32
  # @option options [String] :namespace_name
34
33
  # @option options [String] :value
35
34
  # @option options [Oga::XML::Element] :element
36
- #
37
35
  def initialize(options = {})
38
36
  @name = options[:name]
39
37
  @value = options[:value]
@@ -42,12 +40,10 @@ module Oga
42
40
  @namespace_name = options[:namespace_name]
43
41
  end
44
42
 
45
- ##
46
43
  # Returns the {Oga::XML::Namespace} instance for the current namespace
47
44
  # name.
48
45
  #
49
46
  # @return [Oga::XML::Namespace]
50
- #
51
47
  def namespace
52
48
  unless @namespace
53
49
  if namespace_name == DEFAULT_NAMESPACE.name
@@ -60,19 +56,15 @@ module Oga
60
56
  @namespace
61
57
  end
62
58
 
63
- ##
64
59
  # @param [String] value
65
- #
66
60
  def value=(value)
67
61
  @value = value
68
62
  @decoded = false
69
63
  end
70
64
 
71
- ##
72
65
  # Returns the value of the attribute or nil if no explicit value was set.
73
66
  #
74
67
  # @return [String|NilClass]
75
- #
76
68
  def value
77
69
  if !@decoded and @value
78
70
  @value = EntityDecoder.try_decode(@value, html?)
@@ -82,18 +74,14 @@ module Oga
82
74
  @value
83
75
  end
84
76
 
85
- ##
86
77
  # @return [String]
87
- #
88
78
  def text
89
79
  value.to_s
90
80
  end
91
81
 
92
82
  alias_method :to_s, :text
93
83
 
94
- ##
95
84
  # @return [String]
96
- #
97
85
  def to_xml
98
86
  if namespace_name
99
87
  full_name = "#{namespace_name}:#{name}"
@@ -106,9 +94,7 @@ module Oga
106
94
  %Q(#{full_name}="#{enc_value}")
107
95
  end
108
96
 
109
- ##
110
97
  # @return [String]
111
- #
112
98
  def inspect
113
99
  segments = []
114
100
 
@@ -123,11 +109,18 @@ module Oga
123
109
  "Attribute(#{segments.join(' ')})"
124
110
  end
125
111
 
112
+ # @see [Oga::XML::Node#each_ancestor]
113
+ def each_ancestor(&block)
114
+ return unless element
115
+
116
+ yield element
117
+
118
+ element.each_ancestor(&block)
119
+ end
120
+
126
121
  private
127
122
 
128
- ##
129
123
  # @return [TrueClass|FalseClass]
130
- #
131
124
  def html?
132
125
  !!@element && @element.html?
133
126
  end
@@ -1,14 +1,10 @@
1
1
  module Oga
2
2
  module XML
3
- ##
4
3
  # Class used for storing information about CDATA tags.
5
- #
6
4
  class Cdata < CharacterNode
7
- ##
8
5
  # Converts the node back to XML.
9
6
  #
10
7
  # @return [String]
11
- #
12
8
  def to_xml
13
9
  "<![CDATA[#{text}]]>"
14
10
  end
@@ -1,34 +1,26 @@
1
1
  module Oga
2
2
  module XML
3
- ##
4
3
  # Base class for nodes that represent a text-like value such as Text and
5
4
  # Comment nodes.
6
- #
7
5
  class CharacterNode < Node
8
6
  # @return [String]
9
7
  attr_accessor :text
10
8
 
11
- ##
12
9
  # @param [Hash] options
13
10
  #
14
11
  # @option options [String] :text The text of the node.
15
- #
16
12
  def initialize(options = {})
17
13
  super
18
14
 
19
15
  @text = options[:text]
20
16
  end
21
17
 
22
- ##
23
18
  # @return [String]
24
- #
25
19
  def to_xml
26
20
  text.to_s
27
21
  end
28
22
 
29
- ##
30
23
  # @return [String]
31
- #
32
24
  def inspect
33
25
  "#{self.class.to_s.split('::').last}(#{text.inspect})"
34
26
  end
@@ -1,14 +1,10 @@
1
1
  module Oga
2
2
  module XML
3
- ##
4
3
  # Class used for storing information about XML comments.
5
- #
6
4
  class Comment < CharacterNode
7
- ##
8
5
  # Converts the node back to XML.
9
6
  #
10
7
  # @return [String]
11
- #
12
8
  def to_xml
13
9
  "<!--#{text}-->"
14
10
  end
@@ -1,10 +1,8 @@
1
1
  module Oga
2
2
  module XML
3
- ##
4
3
  # The default XML namespace.
5
4
  #
6
5
  # @return [Oga::XML::Namespace]
7
- #
8
6
  DEFAULT_NAMESPACE = Namespace.new(
9
7
  :name => 'xmlns',
10
8
  :uri => 'http://www.w3.org/XML/1998/namespace'
@@ -1,8 +1,6 @@
1
1
  module Oga
2
2
  module XML
3
- ##
4
3
  # Class used for storing information about Doctypes.
5
- #
6
4
  class Doctype
7
5
  # The name of the doctype (e.g. "HTML").
8
6
  # @return [String]
@@ -24,7 +22,6 @@ module Oga
24
22
  # @return [String]
25
23
  attr_accessor :inline_rules
26
24
 
27
- ##
28
25
  # @example
29
26
  # dtd = Doctype.new(:name => 'html', :type => 'PUBLIC')
30
27
  #
@@ -34,7 +31,6 @@ module Oga
34
31
  # @option options [String] :type
35
32
  # @option options [String] :public_id
36
33
  # @option options [String] :system_id
37
- #
38
34
  def initialize(options = {})
39
35
  @name = options[:name]
40
36
  @type = options[:type]
@@ -43,11 +39,9 @@ module Oga
43
39
  @inline_rules = options[:inline_rules]
44
40
  end
45
41
 
46
- ##
47
42
  # Converts the doctype back to XML.
48
43
  #
49
44
  # @return [String]
50
- #
51
45
  def to_xml
52
46
  segments = "<!DOCTYPE #{name}"
53
47
 
@@ -59,11 +53,9 @@ module Oga
59
53
  segments + '>'
60
54
  end
61
55
 
62
- ##
63
56
  # Inspects the doctype.
64
57
  #
65
58
  # @return [String]
66
- #
67
59
  def inspect
68
60
  segments = []
69
61