title: Using Nokogiri's swap on text nodes inside a DocumentFragment
body: <p>With Hpricot, you can do this:</p>
  
  <pre><code>&gt; doc = Hpricot("a")
  =&gt; #&lt;Hpricot::Doc "a"&gt;
  &gt; doc.children.first.swap('b')
  =&gt; ["b"]
  &gt; doc.to_s
  =&gt; "b"
  </code></pre>
  
  <p>But if you try the same thing with Nokogiri, you get an error:</p>
  
  <pre><code>&gt; doc = Nokogiri::HTML::DocumentFragment.parse('a')
  =&gt; #&lt;Nokogiri::HTML::DocumentFragment:0x825bb88c name="#document-fragment" children=[#&lt;Nokogiri::XML::Text:0x825bb580 "a"&gt;]&gt;
  &gt; doc.children.first.swap('b')
  RuntimeError: error parsing fragment (1)
   from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.4/lib/nokogiri/xml/node.rb:509:in `in_context'
   from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.4/lib/nokogiri/xml/node.rb:509:in `parse'
   from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.4/lib/nokogiri/html/document_fragment.rb:22:in `initialize'
   from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.4/lib/nokogiri/xml/node.rb:485:in `new'
   from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.4/lib/nokogiri/xml/node.rb:485:in `fragment'
   from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.4/lib/nokogiri/xml/node.rb:885:in `coerce'
   from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.4/lib/nokogiri/xml/node.rb:382:in `replace'
   from /Library/Ruby/Gems/1.8/gems/nokogiri-1.4.4/lib/nokogiri/xml/node.rb:407:in `swap'
   from (irb):63
  </code></pre>
  
  <p>How can I use <code>swap</code> on text nodes in Nokogiri?</p>
  
  <p><strong>Edit: Note that this isn't a problem with the ARGUMENT to <code>swap</code>, it's a problem with the receiver</strong></p>
  
  <p>For example:</p>
  
  <pre><code>&gt; doc = Nokogiri::HTML::DocumentFragment.parse('&lt;a&gt;b&lt;/a&gt;')
  =&gt; #&lt;Nokogiri::HTML::DocumentFragment:0x825bb508 name="#document-fragment" children=[#&lt;Nokogiri::XML::Element:0x825bb1fc name="a" children=[#&lt;Nokogiri::XML::Text:0x825bab6c "b"&gt;]&gt;]&gt;
  &gt; doc.at("a").swap('x')
  =&gt; #&lt;Nokogiri::XML::Element:0x825bb1fc name="a" children=[#&lt;Nokogiri::XML::Text:0x825bab6c "b"&gt;]&gt;
  &gt; doc.to_s
  =&gt; "x"
  </code></pre>