Sha256: 60787dc23748326f63a48273db7c40b80abd0299cf12178ad21ed588a3fdf7a8

Contents?: true

Size: 1.97 KB

Versions: 8

Compression:

Stored size: 1.97 KB

Contents

# encoding: utf-8
require_relative 'common'

describe 'Sanitize::Transformers::CleanComment' do
  make_my_diffs_pretty!
  parallelize_me!

  describe 'when :allow_comments is false' do
    before do
      @s = Sanitize.new(:allow_comments => false, :elements => ['div'])
    end

    it 'should remove comments' do
      _(@s.fragment('foo <!-- comment --> bar')).must_equal 'foo  bar'
      _(@s.fragment('foo <!-- ')).must_equal 'foo '
      _(@s.fragment('foo <!-- - -> bar')).must_equal 'foo '
      _(@s.fragment("foo <!--\n\n\n\n-->bar")).must_equal 'foo bar'
      _(@s.fragment("foo <!-- <!-- <!-- --> --> -->bar")).must_equal 'foo  --&gt; --&gt;bar'
      _(@s.fragment("foo <div <!-- comment -->>bar</div>")).must_equal 'foo <div>&gt;bar</div>'

      # Special case: the comment markup is inside a <script>, which makes it
      # text content and not an actual HTML comment.
      _(@s.fragment("<script><!-- comment --></script>")).must_equal ''

      _(Sanitize.fragment("<script><!-- comment --></script>", :allow_comments => false, :elements => ['script']))
        .must_equal '<script><!-- comment --></script>'
    end
  end

  describe 'when :allow_comments is true' do
    before do
      @s = Sanitize.new(:allow_comments => true, :elements => ['div'])
    end

    it 'should allow comments' do
      _(@s.fragment('foo <!-- comment --> bar')).must_equal 'foo <!-- comment --> bar'
      _(@s.fragment('foo <!-- ')).must_equal 'foo <!-- -->'
      _(@s.fragment('foo <!-- - -> bar')).must_equal 'foo <!-- - -> bar-->'
      _(@s.fragment("foo <!--\n\n\n\n-->bar")).must_equal "foo <!--\n\n\n\n-->bar"
      _(@s.fragment("foo <!-- <!-- <!-- --> --> -->bar")).must_equal 'foo <!-- <!-- <!-- --> --&gt; --&gt;bar'
      _(@s.fragment("foo <div <!-- comment -->>bar</div>")).must_equal 'foo <div>&gt;bar</div>'

      _(Sanitize.fragment("<script><!-- comment --></script>", :allow_comments => true, :elements => ['script']))
        .must_equal '<script><!-- comment --></script>'
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
sanitize-6.1.3 test/test_clean_comment.rb
sanitize-6.1.2 test/test_clean_comment.rb
sanitize-6.1.1 test/test_clean_comment.rb
sanitize-6.1.0 test/test_clean_comment.rb
sanitize-6.0.2 test/test_clean_comment.rb
mumukit-content-type-1.12.1 vendor/bundle/ruby/2.7.0/gems/sanitize-6.0.1/test/test_clean_comment.rb
mumukit-content-type-1.12.0 vendor/bundle/ruby/2.7.0/gems/sanitize-6.0.1/test/test_clean_comment.rb
sanitize-6.0.1 test/test_clean_comment.rb