nokogiri 1.4.1-x86-mingw32 → 1.4.2.1-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 (112) hide show
  1. data/CHANGELOG.ja.rdoc +45 -0
  2. data/CHANGELOG.rdoc +53 -1
  3. data/Manifest.txt +3 -3
  4. data/README.ja.rdoc +1 -1
  5. data/README.rdoc +11 -5
  6. data/Rakefile +13 -79
  7. data/ext/nokogiri/extconf.rb +25 -74
  8. data/ext/nokogiri/html_document.c +17 -8
  9. data/ext/nokogiri/html_element_description.c +20 -16
  10. data/ext/nokogiri/html_entity_lookup.c +2 -2
  11. data/ext/nokogiri/html_sax_parser_context.c +10 -8
  12. data/ext/nokogiri/nokogiri.c +0 -1
  13. data/ext/nokogiri/nokogiri.h +33 -28
  14. data/ext/nokogiri/xml_attr.c +7 -5
  15. data/ext/nokogiri/xml_attribute_decl.c +5 -2
  16. data/ext/nokogiri/xml_cdata.c +4 -2
  17. data/ext/nokogiri/xml_comment.c +4 -2
  18. data/ext/nokogiri/xml_document.c +93 -15
  19. data/ext/nokogiri/xml_document.h +0 -1
  20. data/ext/nokogiri/xml_document_fragment.c +4 -2
  21. data/ext/nokogiri/xml_dtd.c +18 -8
  22. data/ext/nokogiri/xml_element_content.c +2 -2
  23. data/ext/nokogiri/xml_entity_decl.c +15 -2
  24. data/ext/nokogiri/xml_entity_reference.c +4 -2
  25. data/ext/nokogiri/xml_io.c +1 -1
  26. data/ext/nokogiri/xml_namespace.c +5 -3
  27. data/ext/nokogiri/xml_node.c +353 -114
  28. data/ext/nokogiri/xml_node_set.c +35 -22
  29. data/ext/nokogiri/xml_node_set.h +1 -1
  30. data/ext/nokogiri/xml_processing_instruction.c +4 -2
  31. data/ext/nokogiri/xml_reader.c +119 -47
  32. data/ext/nokogiri/xml_relax_ng.c +21 -12
  33. data/ext/nokogiri/xml_sax_parser.c +6 -3
  34. data/ext/nokogiri/xml_sax_parser.h +13 -17
  35. data/ext/nokogiri/xml_sax_parser_context.c +8 -6
  36. data/ext/nokogiri/xml_sax_push_parser.c +7 -6
  37. data/ext/nokogiri/xml_schema.c +62 -13
  38. data/ext/nokogiri/xml_syntax_error.c +18 -12
  39. data/ext/nokogiri/xml_syntax_error.h +1 -1
  40. data/ext/nokogiri/xml_text.c +4 -2
  41. data/ext/nokogiri/xml_xpath_context.c +60 -23
  42. data/ext/nokogiri/xslt_stylesheet.c +14 -3
  43. data/lib/nokogiri.rb +17 -0
  44. data/lib/nokogiri/1.8/nokogiri.so +0 -0
  45. data/lib/nokogiri/1.9/nokogiri.so +0 -0
  46. data/lib/nokogiri/css/generated_parser.rb +72 -62
  47. data/lib/nokogiri/css/generated_tokenizer.rb +23 -23
  48. data/lib/nokogiri/css/parser.y +3 -1
  49. data/lib/nokogiri/css/tokenizer.rex +3 -3
  50. data/lib/nokogiri/css/xpath_visitor.rb +8 -3
  51. data/lib/nokogiri/ffi/html/sax/parser_context.rb +3 -3
  52. data/lib/nokogiri/ffi/libxml.rb +16 -2
  53. data/lib/nokogiri/ffi/structs/common_node.rb +15 -3
  54. data/lib/nokogiri/ffi/structs/xml_document.rb +13 -4
  55. data/lib/nokogiri/ffi/structs/xml_xpath_context.rb +3 -2
  56. data/lib/nokogiri/ffi/weak_bucket.rb +40 -0
  57. data/lib/nokogiri/ffi/xml/document.rb +27 -0
  58. data/lib/nokogiri/ffi/xml/entity_decl.rb +9 -0
  59. data/lib/nokogiri/ffi/xml/node.rb +142 -61
  60. data/lib/nokogiri/ffi/xml/node_set.rb +15 -12
  61. data/lib/nokogiri/ffi/xml/reader.rb +5 -0
  62. data/lib/nokogiri/ffi/xml/schema.rb +17 -0
  63. data/lib/nokogiri/ffi/xml/syntax_error.rb +4 -4
  64. data/lib/nokogiri/ffi/xml/xpath.rb +0 -10
  65. data/lib/nokogiri/ffi/xml/xpath_context.rb +22 -9
  66. data/lib/nokogiri/ffi/xslt/stylesheet.rb +3 -0
  67. data/lib/nokogiri/html/document.rb +5 -3
  68. data/lib/nokogiri/html/document_fragment.rb +28 -7
  69. data/lib/nokogiri/version.rb +6 -2
  70. data/lib/nokogiri/version_warning.rb +6 -3
  71. data/lib/nokogiri/xml.rb +1 -1
  72. data/lib/nokogiri/xml/builder.rb +35 -22
  73. data/lib/nokogiri/xml/document.rb +44 -12
  74. data/lib/nokogiri/xml/document_fragment.rb +16 -12
  75. data/lib/nokogiri/xml/entity_decl.rb +4 -0
  76. data/lib/nokogiri/xml/node.rb +152 -95
  77. data/lib/nokogiri/xml/node_set.rb +2 -1
  78. data/lib/nokogiri/xml/sax/push_parser.rb +1 -1
  79. data/lib/nokogiri/xml/schema.rb +1 -5
  80. data/lib/nokogiri/xml/syntax_error.rb +4 -0
  81. data/lib/nokogiri/xml/text.rb +9 -0
  82. data/lib/nokogiri/xml/xpath/syntax_error.rb +3 -0
  83. data/tasks/cross_compile.rb +158 -0
  84. data/tasks/test.rb +0 -6
  85. data/test/css/test_xpath_visitor.rb +9 -0
  86. data/test/helper.rb +49 -11
  87. data/test/html/sax/test_parser.rb +11 -1
  88. data/test/html/test_document.rb +8 -0
  89. data/test/html/test_document_fragment.rb +14 -2
  90. data/test/html/test_element_description.rb +5 -1
  91. data/test/html/test_node.rb +5 -66
  92. data/test/test_reader.rb +28 -0
  93. data/test/test_xslt_transforms.rb +14 -0
  94. data/test/xml/test_builder.rb +43 -0
  95. data/test/xml/test_cdata.rb +12 -0
  96. data/test/xml/test_document.rb +74 -39
  97. data/test/xml/test_document_fragment.rb +36 -0
  98. data/test/xml/test_entity_decl.rb +37 -0
  99. data/test/xml/test_node.rb +192 -65
  100. data/test/xml/test_node_reparenting.rb +253 -236
  101. data/test/xml/test_node_set.rb +67 -0
  102. data/test/xml/test_text.rb +8 -0
  103. data/test/xml/test_xpath.rb +32 -0
  104. metadata +151 -79
  105. data/ext/nokogiri/iconv.dll +0 -0
  106. data/ext/nokogiri/libexslt.dll +0 -0
  107. data/ext/nokogiri/libxml2.dll +0 -0
  108. data/ext/nokogiri/libxslt.dll +0 -0
  109. data/ext/nokogiri/xml_xpath.c +0 -53
  110. data/ext/nokogiri/xml_xpath.h +0 -11
  111. data/ext/nokogiri/zlib1.dll +0 -0
  112. data/lib/nokogiri/xml/fragment_handler.rb +0 -79
@@ -4,16 +4,6 @@ module Nokogiri
4
4
 
5
5
  attr_accessor :cstruct # :nodoc:
6
6
 
7
- def node_set # :nodoc:
8
- ptr = cstruct[:nodesetval] if cstruct[:nodesetval]
9
- ptr = LibXML.xmlXPathNodeSetCreate(nil) if ptr.null?
10
-
11
- set = XML::NodeSet.new(@document)
12
- set.cstruct = LibXML::XmlNodeSet.new(ptr)
13
- set.document = @document
14
- set
15
- end
16
-
17
7
  end
18
8
  end
19
9
  end
@@ -43,8 +43,24 @@ module Nokogiri
43
43
 
44
44
  xpath = XML::XPath.new
45
45
  xpath.cstruct = LibXML::XmlXpathObject.new(xpath_ptr)
46
- xpath.document = cstruct[:doc]
47
- xpath
46
+ xpath.document = cstruct.document.ruby_doc
47
+
48
+ case xpath.cstruct[:type]
49
+ when LibXML::XmlXpathObject::XPATH_NODESET
50
+ if xpath.cstruct[:nodesetval].null?
51
+ NodeSet.new(xpath.document)
52
+ else
53
+ NodeSet.wrap(xpath.cstruct[:nodesetval], xpath.document)
54
+ end
55
+ when LibXML::XmlXpathObject::XPATH_STRING
56
+ xpath.cstruct[:stringval]
57
+ when LibXML::XmlXpathObject::XPATH_NUMBER
58
+ xpath.cstruct[:floatval]
59
+ when LibXML::XmlXpathObject::XPATH_BOOLEAN
60
+ 0 != xpath.cstruct[:boolval]
61
+ else
62
+ NodeSet.new(xpath.document)
63
+ end
48
64
  end
49
65
 
50
66
  def self.new(node) # :nodoc:
@@ -66,8 +82,8 @@ module Nokogiri
66
82
  def ruby_funcall(name, xpath_handler) # :nodoc:
67
83
  lambda do |ctx, nargs|
68
84
  parser_context = LibXML::XmlXpathParserContext.new(ctx)
69
- context = parser_context.context
70
- doc = context.doc.ruby_doc
85
+ context_cstruct = parser_context.context
86
+ document = context_cstruct.document.ruby_doc
71
87
 
72
88
  params = []
73
89
 
@@ -81,10 +97,7 @@ module Nokogiri
81
97
  when LibXML::XmlXpathObject::XPATH_NUMBER
82
98
  params.unshift obj[:floatval]
83
99
  when LibXML::XmlXpathObject::XPATH_NODESET
84
- ns_ptr = LibXML::XmlNodeSet.new(obj[:nodesetval])
85
- set = NodeSet.allocate
86
- set.cstruct = ns_ptr
87
- params.unshift set
100
+ params.unshift NodeSet.wrap(obj[:nodesetval], document)
88
101
  else
89
102
  char_ptr = params.unshift LibXML.xmlXPathCastToString(obj)
90
103
  string = char_ptr.read_string
@@ -110,7 +123,7 @@ module Nokogiri
110
123
  when NilClass.to_s
111
124
  ;
112
125
  when Array.to_s
113
- node_set = XML::NodeSet.new(doc, result)
126
+ node_set = XML::NodeSet.new(document, result)
114
127
  LibXML.xmlXPathReturnNodeSet(
115
128
  ctx,
116
129
  LibXML.xmlXPathNodeSetMerge(nil, node_set.cstruct)
@@ -30,6 +30,9 @@ module Nokogiri
30
30
  end
31
31
 
32
32
  def transform(document, params=[]) # :nodoc:
33
+ params = params.to_a.flatten if params.is_a?(Hash)
34
+ raise(TypeError) unless params.is_a?(Array)
35
+
33
36
  param_arr = FFI::MemoryPointer.new(:pointer, params.length + 1, false)
34
37
 
35
38
  # Keep the MemoryPointer instances alive until after the call
@@ -9,7 +9,7 @@ module Nokogiri
9
9
  node['http-equiv'] =~ /Content-Type/i
10
10
  }
11
11
 
12
- /charset\s*=\s*([\w\d-]+)/i.match(meta['content'])[1]
12
+ /charset\s*=\s*([\w-]+)/i.match(meta['content'])[1]
13
13
  end
14
14
 
15
15
  ###
@@ -49,7 +49,7 @@ module Nokogiri
49
49
  ####
50
50
  # Create a Nokogiri::XML::DocumentFragment from +tags+
51
51
  def fragment tags = nil
52
- DocumentFragment.new(self, tags)
52
+ DocumentFragment.new(self, tags, self.root)
53
53
  end
54
54
 
55
55
  class << self
@@ -68,7 +68,9 @@ module Nokogiri
68
68
  yield options if block_given?
69
69
 
70
70
  if string_or_io.respond_to?(:encoding)
71
- encoding ||= string_or_io.encoding.name
71
+ unless string_or_io.encoding.name == "ASCII-8BIT"
72
+ encoding ||= string_or_io.encoding.name
73
+ end
72
74
  end
73
75
 
74
76
  if string_or_io.respond_to?(:read)
@@ -1,15 +1,36 @@
1
1
  module Nokogiri
2
2
  module HTML
3
3
  class DocumentFragment < Nokogiri::XML::DocumentFragment
4
-
5
- class << self
6
- ####
7
- # Create a Nokogiri::XML::DocumentFragment from +tags+
8
- def parse tags
9
- self.new(HTML::Document.new, tags)
10
- end
4
+ ####
5
+ # Create a Nokogiri::XML::DocumentFragment from +tags+
6
+ def self.parse tags
7
+ doc = HTML::Document.new
8
+ doc.encoding = 'UTF-8'
9
+ self.new(doc, tags)
11
10
  end
12
11
 
12
+ def initialize document, tags = nil, ctx = nil
13
+ return self unless tags
14
+
15
+ children = if ctx
16
+ ctx.parse("<div>#{tags.strip}</div>").first.children
17
+ else
18
+ ###
19
+ # This is a horrible hack, but I don't care
20
+ if tags.strip =~ /^<body/i
21
+ path = "/html/body"
22
+ else
23
+ path = "/html/body/node()"
24
+ end
25
+
26
+ HTML::Document.parse(
27
+ "<html><body>#{tags.strip}</body></html>",
28
+ nil,
29
+ document.encoding
30
+ ).xpath(path)
31
+ end
32
+ children.each { |child| child.parent = self }
33
+ end
13
34
  end
14
35
  end
15
36
  end
@@ -1,11 +1,15 @@
1
1
  module Nokogiri
2
2
  # The version of Nokogiri you are using
3
- VERSION = '1.4.1'
3
+ VERSION = '1.4.2.1'
4
4
 
5
5
  # More complete version information about libxml
6
6
  VERSION_INFO = {}
7
7
  VERSION_INFO['warnings'] = []
8
- VERSION_INFO['nokogiri'] = VERSION
8
+ VERSION_INFO['nokogiri'] = Nokogiri::VERSION
9
+ VERSION_INFO['ruby'] = {}
10
+ VERSION_INFO['ruby']['version'] = ::RUBY_VERSION
11
+ VERSION_INFO['ruby']['platform'] = ::RUBY_PLATFORM
12
+ VERSION_INFO['ruby']['jruby'] = ::JRUBY_VERSION if RUBY_PLATFORM == "java"
9
13
  if defined?(LIBXML_VERSION)
10
14
  VERSION_INFO['libxml'] = {}
11
15
  VERSION_INFO['libxml']['binding'] = 'extension'
@@ -1,11 +1,14 @@
1
1
  module Nokogiri
2
- if self.is_2_6_16? && !defined?(I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2)
3
- warn <<-eom
2
+ if self.is_2_6_16?
3
+ VERSION_INFO['warnings'] << "libxml 2.6.16 is old and buggy."
4
+ if !defined?(I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2)
5
+ warn <<-eom
4
6
  HI. You're using libxml2 version 2.6.16 which is over 4 years old and has
5
7
  plenty of bugs. We suggest that for maximum HTML/XML parsing pleasure, you
6
8
  upgrade your version of libxml2 and re-install nokogiri. If you like using
7
9
  libxml2 version 2.6.16, but don't like this warning, please define the constant
8
10
  I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 before requring nokogiri.
9
- eom
11
+ eom
12
+ end
10
13
  end
11
14
  end
@@ -1,7 +1,6 @@
1
1
  require 'nokogiri/xml/pp'
2
2
  require 'nokogiri/xml/parse_options'
3
3
  require 'nokogiri/xml/sax'
4
- require 'nokogiri/xml/fragment_handler'
5
4
  require 'nokogiri/xml/node'
6
5
  require 'nokogiri/xml/attribute_decl'
7
6
  require 'nokogiri/xml/element_decl'
@@ -11,6 +10,7 @@ require 'nokogiri/xml/namespace'
11
10
  require 'nokogiri/xml/attr'
12
11
  require 'nokogiri/xml/dtd'
13
12
  require 'nokogiri/xml/cdata'
13
+ require 'nokogiri/xml/text'
14
14
  require 'nokogiri/xml/document'
15
15
  require 'nokogiri/xml/document_fragment'
16
16
  require 'nokogiri/xml/processing_instruction'
@@ -194,6 +194,36 @@ module Nokogiri
194
194
  # </root>
195
195
  #
196
196
  # Note the "foo:object" tag.
197
+ #
198
+ # == Document Types
199
+ #
200
+ # To create a document type (DTD), access use the Builder#doc method to get
201
+ # the current context document. Then call Node#create_internal_subset to
202
+ # create the DTD node.
203
+ #
204
+ # For example, this Ruby:
205
+ #
206
+ # builder = Nokogiri::XML::Builder.new do |xml|
207
+ # xml.doc.create_internal_subset(
208
+ # 'html',
209
+ # "-//W3C//DTD HTML 4.01 Transitional//EN",
210
+ # "http://www.w3.org/TR/html4/loose.dtd"
211
+ # )
212
+ # xml.root do
213
+ # xml.foo
214
+ # end
215
+ # end
216
+ #
217
+ # puts builder.to_xml
218
+ #
219
+ # Will output this xml:
220
+ #
221
+ # <?xml version="1.0"?>
222
+ # <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
223
+ # <root>
224
+ # <foo/>
225
+ # </root>
226
+ #
197
227
  class Builder
198
228
  # The current Document object being built
199
229
  attr_accessor :doc
@@ -275,8 +305,7 @@ module Nokogiri
275
305
  ###
276
306
  # Create a CDATA Node with content of +string+
277
307
  def cdata string
278
- node = Nokogiri::XML::CDATA.new(@doc, string.to_s)
279
- insert(node)
308
+ insert(doc.create_cdata(string))
280
309
  end
281
310
 
282
311
  ###
@@ -311,29 +340,12 @@ module Nokogiri
311
340
  if @context && @context.respond_to?(method)
312
341
  @context.send(method, *args, &block)
313
342
  else
314
- node = @doc.create_element(method.to_s.sub(/[_!]$/, '')) { |n|
343
+ node = @doc.create_element(method.to_s.sub(/[_!]$/, ''),*args) { |n|
315
344
  # Set up the namespace
316
345
  if @ns
317
346
  n.namespace = @ns
318
347
  @ns = nil
319
348
  end
320
-
321
- args.each do |arg|
322
- case arg
323
- when Hash
324
- arg.each { |k,v|
325
- key = k.to_s
326
- if key =~ /^xmlns(:\w+)?$/
327
- ns_name = key.split(":", 2)[1]
328
- n.add_namespace_definition ns_name, v
329
- next
330
- end
331
- n[k.to_s] = v.to_s
332
- }
333
- else
334
- n.content = arg
335
- end
336
- end
337
349
  }
338
350
  insert(node, &block)
339
351
  end
@@ -345,14 +357,15 @@ module Nokogiri
345
357
  def insert(node, &block)
346
358
  node.parent = @parent
347
359
  if block_given?
348
- @parent = node
360
+ old_parent = @parent
361
+ @parent = node
349
362
  @arity ||= block.arity
350
363
  if @arity <= 0
351
364
  instance_eval(&block)
352
365
  else
353
366
  block.call(self)
354
367
  end
355
- @parent = node.parent
368
+ @parent = old_parent
356
369
  end
357
370
  NodeBuilder.new(node, self)
358
371
  end
@@ -1,6 +1,6 @@
1
1
  module Nokogiri
2
2
  module XML
3
- ####
3
+ ##
4
4
  # Nokogiri::XML::Document is the main entry point for dealing with
5
5
  # XML documents. The Document is created by parsing an XML document.
6
6
  # See Nokogiri.XML()
@@ -8,7 +8,7 @@ module Nokogiri
8
8
  # For searching a Document, see Nokogiri::XML::Node#css and
9
9
  # Nokogiri::XML::Node#xpath
10
10
  class Document < Node
11
- ###
11
+ ##
12
12
  # Parse an XML file. +thing+ may be a String, or any object that
13
13
  # responds to _read_ and _close_ such as an IO, or StringIO.
14
14
  # +url+ is resource where this document is located. +encoding+ is the
@@ -36,13 +36,39 @@ module Nokogiri
36
36
  # A list of Nokogiri::XML::SyntaxError found when parsing a document
37
37
  attr_accessor :errors
38
38
 
39
- def initialize *args
39
+ def initialize *args # :nodoc:
40
+ @errors = []
40
41
  @decorators = nil
41
42
  end
42
43
 
43
- # Create an element with +name+
44
- def create_element name, &block
45
- Nokogiri::XML::Element.new(name, self, &block)
44
+ ##
45
+ # Create an element with +name+, and optionally setting the content and attributes.
46
+ #
47
+ # doc.create_element "div" # <div></div>
48
+ # doc.create_element "div", :class => "container" # <div class='container'></div>
49
+ # doc.create_element "div", "contents" # <div>contents</div>
50
+ # doc.create_element "div", "contents", :class => "container" # <div class='container'>contents</div>
51
+ # doc.create_element "div" { |node| node['class'] = "container" } # <div class='container'></div>
52
+ #
53
+ def create_element name, *args, &block
54
+ elm = Nokogiri::XML::Element.new(name, self, &block)
55
+ args.each do |arg|
56
+ case arg
57
+ when Hash
58
+ arg.each { |k,v|
59
+ key = k.to_s
60
+ if key =~ /^xmlns(:\w+)?$/
61
+ ns_name = key.split(":", 2)[1]
62
+ elm.add_namespace_definition ns_name, v
63
+ next
64
+ end
65
+ elm[k.to_s] = v.to_s
66
+ }
67
+ else
68
+ elm.content = arg
69
+ end
70
+ end
71
+ elm
46
72
  end
47
73
 
48
74
  # Create a text node with +text+
@@ -50,6 +76,11 @@ module Nokogiri
50
76
  Nokogiri::XML::Text.new(text.to_s, self, &block)
51
77
  end
52
78
 
79
+ # Create a CDATA element containing +text+
80
+ def create_cdata text
81
+ Nokogiri::XML::CDATA.new(self, text.to_s)
82
+ end
83
+
53
84
  # The name of this document. Always returns "document"
54
85
  def name
55
86
  'document'
@@ -60,7 +91,7 @@ module Nokogiri
60
91
  self
61
92
  end
62
93
 
63
- ###
94
+ ##
64
95
  # Recursively get all namespaces from this node and its subtree and
65
96
  # return them as a hash.
66
97
  #
@@ -94,7 +125,7 @@ module Nokogiri
94
125
  @decorators[key] ||= []
95
126
  end
96
127
 
97
- ###
128
+ ##
98
129
  # Validate this Document against it's DTD. Returns a list of errors on
99
130
  # the document or +nil+ when there is no DTD.
100
131
  def validate
@@ -102,7 +133,7 @@ module Nokogiri
102
133
  internal_subset.validate self
103
134
  end
104
135
 
105
- ###
136
+ ##
106
137
  # Explore a document with shortcut methods.
107
138
  def slop!
108
139
  unless decorators(XML::Node).include? Nokogiri::Decorators::Slop
@@ -113,7 +144,7 @@ module Nokogiri
113
144
  self
114
145
  end
115
146
 
116
- ###
147
+ ##
117
148
  # Apply any decorators to +node+
118
149
  def decorate node
119
150
  return unless @decorators
@@ -131,16 +162,17 @@ module Nokogiri
131
162
  root ? root.namespaces : {}
132
163
  end
133
164
 
134
- ####
165
+ ##
135
166
  # Create a Nokogiri::XML::DocumentFragment from +tags+
136
167
  # Returns an empty fragment if +tags+ is nil.
137
168
  def fragment tags = nil
138
- DocumentFragment.new(self, tags)
169
+ DocumentFragment.new(self, tags, self.root)
139
170
  end
140
171
 
141
172
  undef_method :swap, :parent, :namespace, :default_namespace=
142
173
  undef_method :add_namespace_definition, :attributes
143
174
  undef_method :namespace_definitions, :line, :add_namespace
175
+ undef_method :parse, :in_context
144
176
 
145
177
  def add_child child
146
178
  raise "Document already has a root node" if root
@@ -1,18 +1,22 @@
1
1
  module Nokogiri
2
2
  module XML
3
3
  class DocumentFragment < Nokogiri::XML::Node
4
- def initialize document, tags=nil
5
- if tags
6
- if self.kind_of?(Nokogiri::HTML::DocumentFragment)
7
- HTML::SAX::Parser.new(FragmentHandler.new(self, tags)).parse(tags)
8
- else
9
- wrapped = "<div>#{tags.strip}</div>"
10
- XML::SAX::Parser.new(FragmentHandler.new(self, wrapped)).parse(wrapped)
11
- div = self.child
12
- div.children.each { |child| child.parent = self }
13
- div.unlink
14
- end
15
- end
4
+ ##
5
+ # Create a new DocumentFragment from +tags+.
6
+ #
7
+ # If +ctx+ is present, it is used as a context node for the
8
+ # subtree created, e.g., namespaces will be resolved relative
9
+ # to +ctx+.
10
+ def initialize document, tags = nil, ctx = nil
11
+ return self unless tags
12
+
13
+ children = if ctx
14
+ ctx.parse(tags.strip)
15
+ else
16
+ XML::Document.parse("<root>#{tags.strip}</root>") \
17
+ .xpath("/root/node()")
18
+ end
19
+ children.each { |child| child.parent = self }
16
20
  end
17
21
 
18
22
  ###