Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PATCH] adding Builder#<< for appending raw strings #141

Closed
dudleyf opened this issue Oct 2, 2009 · 4 comments
Closed

[PATCH] adding Builder#<< for appending raw strings #141

dudleyf opened this issue Oct 2, 2009 · 4 comments

Comments

@dudleyf
Copy link

dudleyf commented Oct 2, 2009

Here's a tiny patch implementing the functionality talked about here[0].

[0] http://rubyforge.org/pipermail/nokogiri-talk/2009-March/000224.html

diff --git a/lib/nokogiri/xml/builder.rb b/lib/nokogiri/xml/builder.rb
index 89cd63a..5cdcafd 100644
--- a/lib/nokogiri/xml/builder.rb
+++ b/lib/nokogiri/xml/builder.rb
@@ -277,6 +277,12 @@ module Nokogiri
         @doc.to_xml
       end
 
+      ###
+      # Append the given raw XML +string+ to the document
+      def << string
+        @doc.fragment(string).children.each { |x| insert(x) }
+      end
+
       def method_missing method, *args, &block # :nodoc:
         if @context && @context.respond_to?(method)
           @context.send(method, *args, &block)
diff --git a/test/xml/test_builder.rb b/test/xml/test_builder.rb
index d4a6e26..12b1f86 100644
--- a/test/xml/test_builder.rb
+++ b/test/xml/test_builder.rb
@@ -117,6 +117,26 @@ module Nokogiri
         assert_equal 'hello', builder.doc.at('baz').content
       end
 
+      def test_raw_append
+        builder = Nokogiri::XML::Builder.new do |xml|
+          xml.root do
+            xml << 'hello'
+          end
+        end
+
+        assert_equal 'hello', builder.doc.at('//root/foo').content
+      end
+
+      def test_raw_append_with_instance_eval
+        builder = Nokogiri::XML::Builder.new do
+          root do
+            self << 'hello'
+          end
+        end
+
+        assert_equal 'hello', builder.doc.at('//root/foo').content
+      end
+
       def test_cdata
         builder = Nokogiri::XML::Builder.new do
           root {
-- 
1.6.4.3
@tenderlove
Copy link
Member

I've applied the patch, but next time please make sure the tests pass. After applying the patch, I got these errors:

  1) Error:
test_raw_append(Nokogiri::XML::TestBuilder):
NoMethodError: undefined method `content' for nil:NilClass
    test/xml/test_builder.rb:127:in `test_raw_append'

  2) Error:
test_raw_append_with_instance_eval(Nokogiri::XML::TestBuilder):
NoMethodError: undefined method `content' for nil:NilClass
    test/xml/test_builder.rb:137:in `test_raw_append_with_instance_eval'

@tenderlove
Copy link
Member

XML Builder can append raw strings. closed by 98b10d2

@dudleyf
Copy link
Author

dudleyf commented Oct 5, 2009

Yeah, I pasted the wrong patch. Sorry about that.

@tenderlove
Copy link
Member

No problem! :-)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants