diff --git a/lib/string_tools.rb b/lib/string_tools.rb index bba54a8..925f3c9 100644 --- a/lib/string_tools.rb +++ b/lib/string_tools.rb @@ -159,21 +159,19 @@ class Base 'a' => %w(href target name style), 'table' => %w(cellpadding cellspacing width border align style), 'img' => %w(src width height style) - } + }.freeze - TAGS_WITHOUT_ATTRIBUTES = %w(b strong i em sup sub ul ol li blockquote br tr u caption thead) + TAGS_WITHOUT_ATTRIBUTES = %w(b strong i em sup sub ul ol li blockquote br tr u caption thead).freeze - def sanitize(str, attr = {}) + def sanitize(str, attrs = {}) # для корректного обрезания utf строчек режем через mb_chars # для защиты от перегрузки парсера пропускаем максимум 1 мегабайт текста # длина русского символа в utf-8 - 2 байта, 1Мб/2б = 524288 = 2**19 символов # длина по символам с перестраховкой, т.к. латинские символы(теги, например) занимают 1 байт str = str.mb_chars.slice(0..(2**19)).to_s - attributes = TAGS_WITH_ATTRIBUTES - # Мерджим добавочные теги и атрибуты - attributes.merge!(attr) + attributes = TAGS_WITH_ATTRIBUTES.merge(attrs) elements = attributes.keys | TAGS_WITHOUT_ATTRIBUTES transformers = [LINK_NORMALIZER] diff --git a/spec/string_tools_spec.rb b/spec/string_tools_spec.rb index 052374b..7605f1a 100644 --- a/spec/string_tools_spec.rb +++ b/spec/string_tools_spec.rb @@ -54,6 +54,18 @@ expect(sanitized_string). to eq('') end + + context 'multiple invocations of the method' do + it 'does not mess up default config' do + origin_str = '

' + + with_custom_config = described_class.sanitize(origin_str, 'p' => %w(style title)) + with_default_config = described_class.sanitize(origin_str) + + expect(with_custom_config).to eq('

') + expect(with_default_config).to eq('

') + end + end end describe '#clear_unicode_separator_characters' do