<commit_before>require 'test_helper'
require 'lotus/utils'
require 'lotus/utils/escape'

describe Lotus::Utils::Escape do
  let(:mod) { Lotus::Utils::Escape }

  describe '.html' do
    TEST_ENCODINGS.each do |encoding|
      describe "#{ encoding }" do
        it "doesn't escape safe string" do
          input  = Lotus::Utils::Escape::SafeString.new('&')
          result = mod.html(input.encode(encoding))
          result.must_equal '&'
        end

        it "escapes nil" do
          result = mod.html(nil)
          result.must_equal ''
        end

        it "escapes 'test'" do
          result = mod.html('test'.encode(encoding))
          result.must_equal 'test'
        end

        it "escapes '&'" do
          result = mod.html('&'.encode(encoding))
          result.must_equal '&amp;'
        end

        it "escapes '<'" do
          result = mod.html('<'.encode(encoding))
          result.must_equal '&lt;'
        end

        it "escapes '>'" do
          result = mod.html('>'.encode(encoding))
          result.must_equal '&gt;'
        end

        it %(escapes '"') do
          result = mod.html('"'.encode(encoding))
          result.must_equal '&quot;'
        end

        it %(escapes "'") do
          result = mod.html("'".encode(encoding))
          result.must_equal '&apos;'
        end

        it "escapes '/'" do
          result = mod.html('/'.encode(encoding))
          result.must_equal '&#x2F;'
        end

        it "escapes '<script>'" do
          result = mod.html('<script>'.encode(encoding))
          result.must_equal '&lt;script&gt;'
        end

        it "escapes '<scr<script>ipt>'" do
          result = mod.html('<scr<script>ipt>'.encode(encoding))
          result.must_equal '&lt;scr&lt;script&gt;ipt&gt;'
        end

        it "escapes '&lt;script&gt;'" do
          result = mod.html('&lt;script&gt;'.encode(encoding))
          result.must_equal '&amp;lt;script&amp;gt;'
        end

        it %(escapes '""><script>xss(5)</script>') do
          result = mod.html('""><script>xss(5)</script>'.encode(encoding))
          result.must_equal '&quot;&quot;&gt;&lt;script&gt;xss(5)&lt;&#x2F;script&gt;'
        end

        it %(escapes '><script>xss(6)</script>') do
          result = mod.html('><script>xss(6)</script>'.encode(encoding))
          result.must_equal '&gt;&lt;script&gt;xss(6)&lt;&#x2F;script&gt;'
        end

        it %(escapes '# onmouseover="xss(7)" ') do
          result = mod.html('# onmouseover="xss(7)" '.encode(encoding))
          result.must_equal '# onmouseover=&quot;xss(7)&quot; '
        end

        it %(escapes '/" onerror="xss(9)">') do
          result = mod.html('/" onerror="xss(9)">'.encode(encoding))
          result.must_equal '&#x2F;&quot; onerror=&quot;xss(9)&quot;&gt;'
        end

        it %(escapes '/ onerror="xss(10)"') do
          result = mod.html('/ onerror="xss(10)"'.encode(encoding))
          result.must_equal '&#x2F; onerror=&quot;xss(10)&quot;'
        end

        it %(escapes '<<script>xss(14);//<</script>') do
          result = mod.html('<<script>xss(14);//<</script>'.encode(encoding))
          result.must_equal '&lt;&lt;script&gt;xss(14);&#x2F;&#x2F;&lt;&lt;&#x2F;script&gt;'
        end
      end
    end

    # 'тест' means test in russian
    it "escapes 'тест'" do
      skip "There is no ASCII-8BIT encoding" unless Encoding.name_list.include?('ASCII-8BIT')

      string   = 'тест'.force_encoding('ASCII-8BIT')
      encoding = string.encoding

      result = mod.html(string)
      result.must_equal 'тест'
      result.encoding.must_equal Encoding::UTF_8

      string.encoding.must_equal encoding
    end
  end

  describe '.html_attribute' do
    TEST_ENCODINGS.each do |encoding|
      describe "#{ encoding }" do
        it "doesn't escape safe string" do
          input  = Lotus::Utils::Escape::SafeString.new('&')
          result = mod.html_attribute(input.encode(encoding))<commit_msg>Remove ascii 8bit string from test name, for JRuby compat<commit_after>require 'test_helper'
require 'lotus/utils'
require 'lotus/utils/escape'

describe Lotus::Utils::Escape do
  let(:mod) { Lotus::Utils::Escape }

  describe '.html' do
    TEST_ENCODINGS.each do |encoding|
      describe "#{ encoding }" do
        it "doesn't escape safe string" do
          input  = Lotus::Utils::Escape::SafeString.new('&')
          result = mod.html(input.encode(encoding))
          result.must_equal '&'
        end

        it "escapes nil" do
          result = mod.html(nil)
          result.must_equal ''
        end

        it "escapes 'test'" do
          result = mod.html('test'.encode(encoding))
          result.must_equal 'test'
        end

        it "escapes '&'" do
          result = mod.html('&'.encode(encoding))
          result.must_equal '&amp;'
        end

        it "escapes '<'" do
          result = mod.html('<'.encode(encoding))
          result.must_equal '&lt;'
        end

        it "escapes '>'" do
          result = mod.html('>'.encode(encoding))
          result.must_equal '&gt;'
        end

        it %(escapes '"') do
          result = mod.html('"'.encode(encoding))
          result.must_equal '&quot;'
        end

        it %(escapes "'") do
          result = mod.html("'".encode(encoding))
          result.must_equal '&apos;'
        end

        it "escapes '/'" do
          result = mod.html('/'.encode(encoding))
          result.must_equal '&#x2F;'
        end

        it "escapes '<script>'" do
          result = mod.html('<script>'.encode(encoding))
          result.must_equal '&lt;script&gt;'
        end

        it "escapes '<scr<script>ipt>'" do
          result = mod.html('<scr<script>ipt>'.encode(encoding))
          result.must_equal '&lt;scr&lt;script&gt;ipt&gt;'
        end

        it "escapes '&lt;script&gt;'" do
          result = mod.html('&lt;script&gt;'.encode(encoding))
          result.must_equal '&amp;lt;script&amp;gt;'
        end

        it %(escapes '""><script>xss(5)</script>') do
          result = mod.html('""><script>xss(5)</script>'.encode(encoding))
          result.must_equal '&quot;&quot;&gt;&lt;script&gt;xss(5)&lt;&#x2F;script&gt;'
        end

        it %(escapes '><script>xss(6)</script>') do
          result = mod.html('><script>xss(6)</script>'.encode(encoding))
          result.must_equal '&gt;&lt;script&gt;xss(6)&lt;&#x2F;script&gt;'
        end

        it %(escapes '# onmouseover="xss(7)" ') do
          result = mod.html('# onmouseover="xss(7)" '.encode(encoding))
          result.must_equal '# onmouseover=&quot;xss(7)&quot; '
        end

        it %(escapes '/" onerror="xss(9)">') do
          result = mod.html('/" onerror="xss(9)">'.encode(encoding))
          result.must_equal '&#x2F;&quot; onerror=&quot;xss(9)&quot;&gt;'
        end

        it %(escapes '/ onerror="xss(10)"') do
          result = mod.html('/ onerror="xss(10)"'.encode(encoding))
          result.must_equal '&#x2F; onerror=&quot;xss(10)&quot;'
        end

        it %(escapes '<<script>xss(14);//<</script>') do
          result = mod.html('<<script>xss(14);//<</script>'.encode(encoding))
          result.must_equal '&lt;&lt;script&gt;xss(14);&#x2F;&#x2F;&lt;&lt;&#x2F;script&gt;'
        end
      end
    end

    it "escapes word with different encoding" do
      skip "There is no ASCII-8BIT encoding" unless Encoding.name_list.include?('ASCII-8BIT')

      # 'тест' means test in russian
      string   = 'тест'.force_encoding('ASCII-8BIT')
      encoding = string.encoding

      result = mod.html(string)
      result.must_equal 'тест'
      result.encoding.must_equal Encoding::UTF_8

      string.encoding.must_equal encoding
    end
  end

  describe '.html_attribute' do
    TEST_ENCODINGS.each do |encoding|
      describe "#{ encoding }" do
        it "doesn't escape safe string" do
          input  = Lotus::Utils::Escape::SafeString.new('&')
          result = mod.html_attribute(input.encode(encoding))
{'fraction_non_alphanumeric': 0.1667921686746988, 'fraction_numerical': 0.009161646586345381, 'mean_word_length': 2.358196375895491, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2535389', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2600, 'n_words': 483}