nokogiri icon indicating copy to clipboard operation
nokogiri copied to clipboard

[Enhancement] Allow builder to continue serializing content after comment

Open wbrisett opened this issue 4 years ago • 3 comments

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.

wbrisett avatar Feb 03 '21 21:02 wbrisett

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?

flavorjones avatar Feb 03 '21 22:02 flavorjones

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.

wbrisett avatar Feb 03 '21 22:02 wbrisett

Yeah ;hunter

jsx150 avatar Feb 28 '21 08:02 jsx150