Skip to content

Commit

Permalink
fix: не мутировать дефолтный конфиг при многократном вызове sanitize
Browse files Browse the repository at this point in the history
  • Loading branch information
Le6ow5k1 committed Jan 23, 2018
1 parent b7682ae commit 629b9fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/string_tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions spec/string_tools_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@
expect(sanitized_string).
to eq('<iframe width="123" height="456" src="https://www.youtube.com/embed/abc" frameborder="0"></iframe>')
end

context 'multiple invocations of the method' do
it 'does not mess up default config' do
origin_str = '<p style="text-align: center;" title="foobar"></p>'

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('<p style="text-align: center;" title="foobar"></p>')
expect(with_default_config).to eq('<p style="text-align: center;"></p>')
end
end
end

describe '#clear_unicode_separator_characters' do
Expand Down

0 comments on commit 629b9fd

Please sign in to comment.