From d263e3bc2de720f4e6749cf4a8f2074427b65d07 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sun, 3 Dec 2017 21:40:28 -0600 Subject: [PATCH] Fill out some examples for Glitch::FilterHelper. #234. Also add HTML entity decoding to Glitch::FilterHelper, which is needed to e.g. match "<" to the tag-stripped version of "

<3

" or "

<3

". --- app/models/glitch/filter_helper.rb | 16 ++++++--- spec/models/glitch/filter_helper_spec.rb | 43 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 spec/models/glitch/filter_helper_spec.rb diff --git a/app/models/glitch/filter_helper.rb b/app/models/glitch/filter_helper.rb index 0dfd69526..11be877c1 100644 --- a/app/models/glitch/filter_helper.rb +++ b/app/models/glitch/filter_helper.rb @@ -1,12 +1,16 @@ +require 'htmlentities' + class Glitch::FilterHelper include ActionView::Helpers::SanitizeHelper attr_reader :text_matcher attr_reader :tag_matcher + attr_reader :entity_decoder def initialize(receiver_id) - @text_matcher = Glitch::KeywordMute.text_matcher_for(receiver_id) - @tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id) + @text_matcher = Glitch::KeywordMute.text_matcher_for(receiver_id) + @tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id) + @entity_decoder = HTMLEntities.new end def matches?(status) @@ -16,8 +20,12 @@ class Glitch::FilterHelper private def matchers_match?(status) - text_matcher.matches?(strip_tags(status.text)) || - text_matcher.matches?(strip_tags(status.spoiler_text)) || + text_matcher.matches?(prepare_text(status.text)) || + text_matcher.matches?(prepare_text(status.spoiler_text)) || tag_matcher.matches?(status.tags) end + + def prepare_text(text) + entity_decoder.decode(strip_tags(text)) + end end diff --git a/spec/models/glitch/filter_helper_spec.rb b/spec/models/glitch/filter_helper_spec.rb new file mode 100644 index 000000000..9a808667d --- /dev/null +++ b/spec/models/glitch/filter_helper_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +RSpec.describe Glitch::FilterHelper do + describe '#matches?' do + let(:alice) { Fabricate(:account, username: 'alice').tap(&:save!) } + let(:helper) { Glitch::FilterHelper.new(alice) } + + it 'ignores names of HTML tags in status text' do + status = Fabricate(:status, text: 'uh example') + Glitch::KeywordMute.create!(account: alice, keyword: 'addr') + + expect(helper.matches?(status)).to be false + end + + it 'ignores properties of HTML tags in status text' do + status = Fabricate(:status, text: 'uh example') + Glitch::KeywordMute.create!(account: alice, keyword: 'href') + + expect(helper.matches?(status)).to be false + end + + it 'matches text inside HTML tags' do + status = Fabricate(:status, text: '

HEY THIS IS SOMETHING ANNOYING

') + Glitch::KeywordMute.create!(account: alice, keyword: 'annoying') + + expect(helper.matches?(status)).to be true + end + + it 'matches < in HTML-stripped text' do + status = Fabricate(:status, text: '

I <3 oats

') + Glitch::KeywordMute.create!(account: alice, keyword: '<3') + + expect(helper.matches?(status)).to be true + end + + it 'matches < in HTML text' do + status = Fabricate(:status, text: '

I <3 oats

') + Glitch::KeywordMute.create!(account: alice, keyword: '<3') + + expect(helper.matches?(status)).to be true + end + end +end