sparklemotion / nokogiri

Nokogiri (鋸) makes it easy and painless to work with XML and HTML from Ruby.
https://nokogiri.org/
MIT License
6.14k stars 899 forks source link

[Enhancement] Allow builder to continue serializing content after comment #2188

Open wbrisett opened 3 years ago

wbrisett commented 3 years ago

In trying to enhance an existing script, I asked on the Nokogiri list about the possibility of enabling Builder to open a comment block, continue serializing data, then closing the comment. It was mentioned that's not currently possible. I am requesting that folks consider this enhancement to builder.

flavorjones commented 3 years ago

Hey, @wbrisett! :wave:

I was imagining something like this:

#! /usr/bin/env ruby

require 'nokogiri'
require 'minitest/autorun'

class Test < MiniTest::Spec
  describe "Builder support for commenting out subtrees" do
    it "works like this currently" do
      builder = Nokogiri::HTML::Builder.new do |doc|
        doc.html do
          doc.body do
            doc.span do
              doc.text "Hello world"
            end
          end
        end
      end

      assert_includes(builder.to_html, <<~EOF)
        <html><body><span>Hello world</span></body></html>
      EOF
    end

    it "might be extended to do this" do
      builder = Nokogiri::HTML::Builder.new do |doc|
        doc.html do
          doc.body do
            doc.comment do # added
              doc.span do
                doc.text "Hello world"
              end
            end
          end
        end
      end

      assert_includes(builder.to_html, <<~EOF)
        <html><body><!-- <span>Hello world</span> --></body></html>
      EOF
    end
  end
end

WDYT?

wbrisett commented 3 years ago

I really think the "extended to do this" version is cleaner and it honestly follows what you have been doing in builder, that is, opens up a block, then you build inside that block until it's time to close.

However, this is exactly what I was hoping for, and if you said, I could only have the first one, I'd find ways to make that work.

jsx150 commented 3 years ago

Yeah ;hunter