nokogiri 1.3.2-x86-mingw32 → 1.3.3-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of nokogiri might be problematic. Click here for more details.

Files changed (68) hide show
  1. data/CHANGELOG.ja.rdoc +25 -4
  2. data/CHANGELOG.rdoc +20 -0
  3. data/Manifest.txt +2 -0
  4. data/Rakefile +67 -24
  5. data/ext/nokogiri/extconf.rb +16 -9
  6. data/ext/nokogiri/html_document.c +0 -2
  7. data/ext/nokogiri/nokogiri.c +2 -0
  8. data/ext/nokogiri/nokogiri.h +3 -4
  9. data/ext/nokogiri/xml_document.c +30 -23
  10. data/ext/nokogiri/xml_document.h +3 -2
  11. data/ext/nokogiri/xml_dtd.c +4 -0
  12. data/ext/nokogiri/xml_dtd.h +2 -0
  13. data/ext/nokogiri/xml_node.c +28 -9
  14. data/ext/nokogiri/xml_reader.c +0 -7
  15. data/ext/nokogiri/xml_relax_ng.c +7 -1
  16. data/ext/nokogiri/xml_sax_parser.c +2 -0
  17. data/lib/action-nokogiri.rb +2 -0
  18. data/lib/nokogiri.rb +9 -3
  19. data/lib/nokogiri/1.8/nokogiri.so +0 -0
  20. data/lib/nokogiri/1.9/nokogiri.so +0 -0
  21. data/lib/nokogiri/css/generated_tokenizer.rb +80 -82
  22. data/lib/nokogiri/css/tokenizer.rb +1 -5
  23. data/lib/nokogiri/decorators/hpricot/node_set.rb +1 -1
  24. data/lib/nokogiri/ffi/structs/common_node.rb +1 -1
  25. data/lib/nokogiri/ffi/structs/xml_document.rb +1 -1
  26. data/lib/nokogiri/ffi/xml/document.rb +15 -4
  27. data/lib/nokogiri/ffi/xml/node.rb +85 -63
  28. data/lib/nokogiri/ffi/xml/reader.rb +4 -15
  29. data/lib/nokogiri/ffi/xml/relax_ng.rb +3 -1
  30. data/lib/nokogiri/hpricot.rb +30 -0
  31. data/lib/nokogiri/html/document.rb +3 -1
  32. data/lib/nokogiri/html/document_fragment.rb +1 -1
  33. data/lib/nokogiri/html/sax/parser.rb +2 -1
  34. data/lib/nokogiri/version.rb +1 -1
  35. data/lib/nokogiri/xml/builder.rb +44 -1
  36. data/lib/nokogiri/xml/document.rb +8 -1
  37. data/lib/nokogiri/xml/document_fragment.rb +1 -1
  38. data/lib/nokogiri/xml/fragment_handler.rb +4 -7
  39. data/lib/nokogiri/xml/node.rb +9 -6
  40. data/lib/nokogiri/xml/node_set.rb +7 -0
  41. data/lib/nokogiri/xml/parse_options.rb +1 -1
  42. data/test/css/test_nthiness.rb +2 -3
  43. data/test/ffi/test_document.rb +6 -6
  44. data/test/files/2ch.html +108 -0
  45. data/test/files/shift_jis.xml +5 -0
  46. data/test/helper.rb +3 -0
  47. data/test/hpricot/test_alter.rb +9 -9
  48. data/test/hpricot/test_builder.rb +2 -2
  49. data/test/hpricot/test_parser.rb +70 -146
  50. data/test/hpricot/test_paths.rb +2 -2
  51. data/test/hpricot/test_preserved.rb +2 -2
  52. data/test/hpricot/test_xml.rb +3 -3
  53. data/test/html/sax/test_parser.rb +12 -0
  54. data/test/html/test_builder.rb +6 -4
  55. data/test/html/test_document.rb +7 -0
  56. data/test/html/test_document_encoding.rb +17 -0
  57. data/test/html/test_document_fragment.rb +12 -0
  58. data/test/html/test_node.rb +5 -2
  59. data/test/test_convert_xpath.rb +1 -50
  60. data/test/test_css_cache.rb +1 -12
  61. data/test/test_nokogiri.rb +7 -0
  62. data/test/test_reader.rb +14 -0
  63. data/test/xml/test_document.rb +44 -0
  64. data/test/xml/test_document_fragment.rb +12 -0
  65. data/test/xml/test_node.rb +10 -2
  66. data/test/xml/test_node_encoding.rb +23 -0
  67. data/test/xml/test_node_set.rb +10 -0
  68. metadata +48 -46
@@ -18,7 +18,7 @@ module Nokogiri
18
18
  def keep_reference_from_document! # equivalent to NOKOGIRI_ROOT_NODE
19
19
  doc = self.document
20
20
  raise "no document to add reference to" unless doc
21
- LibXML.xmlXPathNodeSetAdd(doc.node_set, self)
21
+ LibXML.xmlXPathNodeSetAdd(doc.unlinked_nodes, self)
22
22
  end
23
23
  end
24
24
  end
@@ -46,7 +46,7 @@ module Nokogiri
46
46
  self[:_private].put_long(0, object.object_id)
47
47
  end
48
48
 
49
- def node_set
49
+ def unlinked_nodes
50
50
  LibXML::XmlNodeSetCast.new(self[:_private].get_pointer(FFI.type_size(:pointer)))
51
51
  end
52
52
 
@@ -8,9 +8,19 @@ module Nokogiri
8
8
  cstruct[:URL]
9
9
  end
10
10
 
11
- def root=(node) # :nodoc:
12
- LibXML.xmlDocSetRootElement(cstruct, node.cstruct)
13
- node
11
+ def root=(new_root) # :nodoc:
12
+ old_root = nil
13
+ if new_root.cstruct[:doc] != cstruct[:doc]
14
+ old_root_ptr = LibXML.xmlDocGetRootElement(cstruct)
15
+ new_root_ptr = LibXML.xmlDocCopyNode(new_root.cstruct, cstruct, 1)
16
+ raise RuntimeError "Could not reparent node (xmlDocCopyNode)" if new_root_ptr.null?
17
+ new_root = Node.wrap(new_root_ptr)
18
+ end
19
+ LibXML.xmlDocSetRootElement(cstruct, new_root.cstruct)
20
+ if old_root_ptr && ! old_root_ptr.null?
21
+ LibXML::XmlNode.new(old_root_ptr).keep_reference_from_document!
22
+ end
23
+ new_root
14
24
  end
15
25
 
16
26
  def root # :nodoc:
@@ -24,7 +34,8 @@ module Nokogiri
24
34
  end
25
35
 
26
36
  def encoding # :nodoc:
27
- cstruct[:encoding].read_string
37
+ ptr = cstruct[:encoding]
38
+ ptr.null? ? nil : ptr.read_string
28
39
  end
29
40
 
30
41
  def self.read_io(io, url, encoding, options) # :nodoc:
@@ -1,14 +1,15 @@
1
1
  module Nokogiri
2
2
  module XML
3
3
  class Node
4
+ # :stopdoc:
4
5
 
5
- attr_accessor :cstruct # :nodoc:
6
+ attr_accessor :cstruct
6
7
 
7
- def pointer_id # :nodoc:
8
+ def pointer_id
8
9
  cstruct.pointer
9
10
  end
10
11
 
11
- def encode_special_chars(string) # :nodoc:
12
+ def encode_special_chars(string)
12
13
  char_ptr = LibXML.xmlEncodeSpecialChars(self[:doc], string)
13
14
  encoded = char_ptr.read_string
14
15
  # TODO: encoding?
@@ -16,7 +17,7 @@ module Nokogiri
16
17
  encoded
17
18
  end
18
19
 
19
- def internal_subset # :nodoc:
20
+ def internal_subset
20
21
  return nil if cstruct[:doc].null?
21
22
  doc = cstruct.document
22
23
  dtd = LibXML.xmlGetIntSubset(doc)
@@ -24,37 +25,37 @@ module Nokogiri
24
25
  Node.wrap(dtd)
25
26
  end
26
27
 
27
- def dup(deep = 1) # :nodoc:
28
+ def dup(deep = 1)
28
29
  dup_ptr = LibXML.xmlDocCopyNode(cstruct, cstruct.document, deep)
29
30
  return nil if dup_ptr.null?
30
31
  Node.wrap(dup_ptr, self.class)
31
32
  end
32
33
 
33
- def unlink # :nodoc:
34
+ def unlink
34
35
  LibXML.xmlUnlinkNode(cstruct)
35
36
  cstruct.keep_reference_from_document!
36
37
  self
37
38
  end
38
39
 
39
- def blank? # :nodoc:
40
+ def blank?
40
41
  LibXML.xmlIsBlankNode(cstruct) == 1
41
42
  end
42
43
 
43
- def next_sibling # :nodoc:
44
+ def next_sibling
44
45
  cstruct_node_from :next
45
46
  end
46
47
 
47
- def previous_sibling # :nodoc:
48
+ def previous_sibling
48
49
  cstruct_node_from :prev
49
50
  end
50
51
 
51
- def replace_with_node(new_node) # :nodoc:
52
+ def replace_with_node(new_node)
52
53
  LibXML.xmlReplaceNode(cstruct, new_node.cstruct)
53
54
  Node.send(:relink_namespace, new_node.cstruct)
54
55
  self
55
56
  end
56
57
 
57
- def children # :nodoc:
58
+ def children
58
59
  return NodeSet.new(nil) if cstruct[:children].null?
59
60
  child = Node.wrap(cstruct[:children])
60
61
 
@@ -74,26 +75,26 @@ module Nokogiri
74
75
  return set
75
76
  end
76
77
 
77
- def child # :nodoc:
78
+ def child
78
79
  (val = cstruct[:children]).null? ? nil : Node.wrap(val)
79
80
  end
80
81
 
81
- def key?(attribute) # :nodoc:
82
+ def key?(attribute)
82
83
  ! (prop = LibXML.xmlHasProp(cstruct, attribute.to_s)).null?
83
84
  end
84
85
 
85
- def namespaced_key?(attribute, namespace) # :nodoc:
86
+ def namespaced_key?(attribute, namespace)
86
87
  prop = LibXML.xmlHasNsProp(cstruct, attribute.to_s,
87
88
  namespace.nil? ? nil : namespace.to_s)
88
89
  prop.null? ? false : true
89
90
  end
90
91
 
91
- def []=(property, value) # :nodoc:
92
+ def []=(property, value)
92
93
  LibXML.xmlSetProp(cstruct, property, value)
93
94
  value
94
95
  end
95
96
 
96
- def get(attribute) # :nodoc:
97
+ def get(attribute)
97
98
  return nil unless attribute
98
99
  propstr = LibXML.xmlGetProp(cstruct, attribute.to_s)
99
100
  return nil if propstr.null?
@@ -107,33 +108,26 @@ module Nokogiri
107
108
  self
108
109
  end
109
110
 
110
- def attribute(name) # :nodoc:
111
+ def attribute(name)
111
112
  raise "Node#attribute not implemented yet"
112
113
  end
113
114
 
114
- def attribute_with_ns(name, namespace) # :nodoc:
115
+ def attribute_with_ns(name, namespace)
115
116
  prop = LibXML.xmlHasNsProp(cstruct, name.to_s,
116
117
  namespace.nil? ? NULL : namespace.to_s)
117
118
  return prop if prop.null?
118
119
  Node.wrap(prop)
119
120
  end
120
121
 
121
- def attribute_nodes # :nodoc:
122
- attr = []
123
- prop_cstruct = cstruct[:properties]
124
- while ! prop_cstruct.null?
125
- prop = Node.wrap(prop_cstruct)
126
- attr << prop
127
- prop_cstruct = prop.cstruct[:next]
128
- end
129
- attr
122
+ def attribute_nodes
123
+ Node.node_properties cstruct
130
124
  end
131
125
 
132
- def namespace # :nodoc:
126
+ def namespace
133
127
  cstruct[:ns].null? ? nil : Namespace.wrap(cstruct.document, cstruct[:ns])
134
128
  end
135
129
 
136
- def namespace_definitions # :nodoc:
130
+ def namespace_definitions
137
131
  list = []
138
132
  ns_ptr = cstruct[:nsDef]
139
133
  return list if ns_ptr.null?
@@ -145,16 +139,16 @@ module Nokogiri
145
139
  list
146
140
  end
147
141
 
148
- def node_type # :nodoc:
142
+ def node_type
149
143
  cstruct[:type]
150
144
  end
151
145
 
152
- def native_content=(content) # :nodoc:
146
+ def native_content=(content)
153
147
  LibXML.xmlNodeSetContent(cstruct, content)
154
148
  content
155
149
  end
156
150
 
157
- def content # :nodoc:
151
+ def content
158
152
  content_ptr = LibXML.xmlNodeGetContent(cstruct)
159
153
  return nil if content_ptr.null?
160
154
  content = content_ptr.read_string # TODO: encoding?
@@ -162,45 +156,45 @@ module Nokogiri
162
156
  content
163
157
  end
164
158
 
165
- def add_child(child) # :nodoc:
159
+ def add_child(child)
166
160
  Node.reparent_node_with(child, self) do |child_cstruct, my_cstruct|
167
161
  LibXML.xmlAddChild(my_cstruct, child_cstruct)
168
162
  end
169
163
  end
170
164
 
171
- def parent # :nodoc:
165
+ def parent
172
166
  cstruct_node_from :parent
173
167
  end
174
-
175
- def node_name=(string) # :nodoc:
168
+
169
+ def node_name=(string)
176
170
  LibXML.xmlNodeSetName(cstruct, string)
177
171
  string
178
172
  end
179
173
 
180
- def node_name # :nodoc:
174
+ def node_name
181
175
  cstruct[:name] # TODO: encoding?
182
176
  end
183
177
 
184
- def path # :nodoc:
178
+ def path
185
179
  path_ptr = LibXML.xmlGetNodePath(cstruct)
186
180
  val = path_ptr.null? ? nil : path_ptr.read_string # TODO: encoding?
187
181
  LibXML.xmlFree(path_ptr)
188
182
  val
189
183
  end
190
184
 
191
- def add_next_sibling(next_sibling) # :nodoc:
185
+ def add_next_sibling(next_sibling)
192
186
  Node.reparent_node_with(next_sibling, self) do |sibling_cstruct, my_cstruct|
193
187
  LibXML.xmlAddNextSibling(my_cstruct, sibling_cstruct)
194
188
  end
195
189
  end
196
190
 
197
- def add_previous_sibling(prev_sibling) # :nodoc:
191
+ def add_previous_sibling(prev_sibling)
198
192
  Node.reparent_node_with(prev_sibling, self) do |sibling_cstruct, my_cstruct|
199
193
  LibXML.xmlAddPrevSibling(my_cstruct, sibling_cstruct)
200
194
  end
201
195
  end
202
196
 
203
- def native_write_to(io, encoding, indent_string, options) # :nodoc:
197
+ def native_write_to(io, encoding, indent_string, options)
204
198
  set_xml_indent_tree_output 1
205
199
  set_xml_tree_indent_string indent_string
206
200
  savectx = LibXML.xmlSaveToIO(IoCallbacks.writer(io), nil, nil, encoding, options)
@@ -209,11 +203,11 @@ module Nokogiri
209
203
  io
210
204
  end
211
205
 
212
- def line # :nodoc:
206
+ def line
213
207
  cstruct[:line]
214
208
  end
215
209
 
216
- def add_namespace_definition(prefix, href) # :nodoc:
210
+ def add_namespace_definition(prefix, href)
217
211
  ns = LibXML.xmlNewNs(cstruct, href, prefix)
218
212
  if ns.null?
219
213
  ns = LibXML.xmlSearchNs(cstruct.document, cstruct,
@@ -223,43 +217,46 @@ module Nokogiri
223
217
  Namespace.wrap(cstruct.document, ns)
224
218
  end
225
219
 
226
- def self.new(name, document, *rest) # :nodoc:
220
+ def self.new(name, doc, *rest)
227
221
  ptr = LibXML.xmlNewNode(nil, name.to_s)
228
222
 
229
223
  node_cstruct = LibXML::XmlNode.new(ptr)
230
- node_cstruct[:doc] = document.cstruct[:doc]
224
+ node_cstruct[:doc] = doc.cstruct[:doc]
231
225
  node_cstruct.keep_reference_from_document!
232
226
 
233
- node = Node.wrap(node_cstruct, self)
234
- node.send :initialize, name, document, *rest
227
+ node = Node.wrap(
228
+ node_cstruct,
229
+ Node == self ? nil : self
230
+ )
231
+ node.send :initialize, name, doc, *rest
235
232
  yield node if block_given?
236
233
  node
237
234
  end
238
235
 
239
- def dump_html # :nodoc:
236
+ def dump_html
240
237
  return to_xml if type == DOCUMENT_NODE
241
238
  buffer = LibXML::XmlBuffer.new(LibXML.xmlBufferCreate())
242
239
  LibXML.htmlNodeDump(buffer, cstruct[:doc], cstruct)
243
240
  buffer[:content] # TODO: encoding?
244
241
  end
245
242
 
246
- def compare(other) # :nodoc:
243
+ def compare(other)
247
244
  LibXML.xmlXPathCmpNodes(other.cstruct, self.cstruct)
248
245
  end
249
246
 
250
- def self.wrap(node_struct, klass=nil) # :nodoc:
247
+ def self.wrap(node_struct, klass=nil)
251
248
  if node_struct.is_a?(FFI::Pointer)
252
249
  # cast native pointers up into a node cstruct
253
250
  return nil if node_struct.null?
254
- node_struct = LibXML::XmlNode.new(node_struct)
251
+ node_struct = LibXML::XmlNode.new(node_struct)
255
252
  end
256
253
 
257
254
  raise "wrapping a node without a document" unless node_struct.document
258
255
 
259
256
  document_struct = node_struct.document
260
- document = document_struct.nil? ? nil : document_struct.ruby_doc
257
+ document_obj = document_struct.nil? ? nil : document_struct.ruby_doc
261
258
  if node_struct[:type] == DOCUMENT_NODE || node_struct[:type] == HTML_DOCUMENT_NODE
262
- return document
259
+ return document_obj
263
260
  end
264
261
 
265
262
  ruby_node = node_struct.ruby_node
@@ -288,11 +285,13 @@ module Nokogiri
288
285
 
289
286
  node.cstruct.ruby_node = node
290
287
 
291
- cache = document.instance_variable_get(:@node_cache)
292
- cache << node
288
+ if document_obj
289
+ node.instance_variable_set(:@document, document_obj)
290
+ cache = document_obj.instance_variable_get(:@node_cache)
291
+ cache << node
292
+ document_obj.decorate(node)
293
+ end
293
294
 
294
- node.instance_variable_set(:@document, document)
295
- document.decorate(node)
296
295
  node
297
296
  end
298
297
 
@@ -300,11 +299,33 @@ module Nokogiri
300
299
  cstruct.document.ruby_doc
301
300
  end
302
301
 
302
+ class << self
303
+ def node_properties(cstruct)
304
+ attr = []
305
+ prop_cstruct = cstruct[:properties]
306
+ while ! prop_cstruct.null?
307
+ prop = Node.wrap(prop_cstruct)
308
+ attr << prop
309
+ prop_cstruct = prop.cstruct[:next]
310
+ end
311
+ attr
312
+ end
313
+ end
314
+
303
315
  private
304
316
 
305
- def self.reparent_node_with(node, other, &block) # :nodoc:
317
+ def self.reparent_node_with(node, other, &block)
306
318
  raise(ArgumentError, "node must be a Nokogiri::XML::Node") unless node.is_a?(Nokogiri::XML::Node)
307
319
 
320
+ # If a document fragment is added, we need to reparent all of it's
321
+ # children
322
+ if node.type == DOCUMENT_FRAG_NODE
323
+ node.children.each do |child|
324
+ reparent_node_with(child, other, &block)
325
+ end
326
+ return node
327
+ end
328
+
308
329
  if node.cstruct[:doc] == other.cstruct[:doc]
309
330
  LibXML.xmlUnlinkNode(node.cstruct)
310
331
  if node.type == TEXT_NODE && other.type == TEXT_NODE && Nokogiri.is_2_6_16?
@@ -320,7 +341,7 @@ module Nokogiri
320
341
  LibXML.xmlUnlinkNode(node.cstruct)
321
342
  node.cstruct.keep_reference_from_document!
322
343
  end
323
-
344
+
324
345
  reparented_struct = LibXML::XmlNode.new(reparented_struct)
325
346
 
326
347
  # the child was a text node that was coalesced. we need to have the object
@@ -336,7 +357,7 @@ module Nokogiri
336
357
  reparented
337
358
  end
338
359
 
339
- def self.relink_namespace(reparented_struct) # :nodoc:
360
+ def self.relink_namespace(reparented_struct)
340
361
  # Make sure that our reparented node has the correct namespaces
341
362
  if reparented_struct[:doc] != reparented_struct[:parent]
342
363
  LibXML.xmlSetNs(reparented_struct, LibXML::XmlNode.new(reparented_struct[:parent])[:ns])
@@ -365,18 +386,19 @@ module Nokogiri
365
386
  end
366
387
  end
367
388
 
368
- def cstruct_node_from(sym) # :nodoc:
389
+ def cstruct_node_from(sym)
369
390
  (val = cstruct[sym]).null? ? nil : Node.wrap(val)
370
391
  end
371
392
 
372
- def set_xml_indent_tree_output(value) # :nodoc:
393
+ def set_xml_indent_tree_output(value)
373
394
  LibXML.__xmlIndentTreeOutput.write_int(value)
374
395
  end
375
396
 
376
- def set_xml_tree_indent_string(value) # :nodoc:
397
+ def set_xml_tree_indent_string(value)
377
398
  LibXML.__xmlTreeIndentString.write_pointer(LibXML.xmlStrdup(value.to_s))
378
399
  end
379
400
 
401
+ # :startdoc:
380
402
  end
381
403
  end
382
404
  end
@@ -29,8 +29,7 @@ module Nokogiri
29
29
  ptr = LibXML.xmlTextReaderExpand(cstruct)
30
30
  return nil if ptr.null?
31
31
 
32
- node = Node.wrap(ptr)
33
- Reader.node_namespaces(node)
32
+ Reader.node_namespaces(ptr)
34
33
  end
35
34
 
36
35
  def attribute_nodes # :nodoc:
@@ -40,17 +39,7 @@ module Nokogiri
40
39
  return nil if ptr.null?
41
40
  node_struct = LibXML::XmlNode.new(ptr)
42
41
 
43
- # FIXME I'm not sure if this is correct..... I don't really like pointing
44
- # at this document, but I have to because of the assertions in
45
- # the node wrapping code.
46
- unless node_struct.document.ruby_doc
47
- doc_struct = LibXML::XmlDocumentCast.new(node_struct[:doc])
48
- doc_struct.alloc_tuple
49
- doc = Document.wrap(doc_struct)
50
- end
51
-
52
- node = Node.wrap(node_struct)
53
- node.attribute_nodes
42
+ Node.node_properties node_struct
54
43
  end
55
44
 
56
45
  def attribute_at(index) # :nodoc:
@@ -192,8 +181,8 @@ module Nokogiri
192
181
  private
193
182
 
194
183
  class << self
195
- def node_namespaces(node) # :nodoc:
196
- cstruct = node.cstruct
184
+ def node_namespaces(ptr) # :nodoc:
185
+ cstruct = LibXML::XmlNode.new(ptr)
197
186
  ahash = {}
198
187
  return ahash unless cstruct[:type] == Node::ELEMENT_NODE
199
188
  ns = cstruct[:nsDef]