From 20fefdb7145b48449e9d6a824a9846dadfd6ac2f Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 10 Jul 2018 03:01:50 +0200 Subject: [PATCH] Make whole-word filter regex consistent between Ruby and JS (#7987) --- app/javascript/mastodon/selectors/index.js | 13 ++++++++++++- app/lib/feed_manager.rb | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/javascript/mastodon/selectors/index.js b/app/javascript/mastodon/selectors/index.js index d0212c379d..106198f744 100644 --- a/app/javascript/mastodon/selectors/index.js +++ b/app/javascript/mastodon/selectors/index.js @@ -47,7 +47,18 @@ export const regexFromFilters = filters => { return new RegExp(filters.map(filter => { let expr = escapeRegExp(filter.get('phrase')); - return filter.get('whole_word') ? `\\b${expr}\\b` : expr; + + if (filter.get('whole_word')) { + if (/^[\w]/.test(expr)) { + expr = `\\b${expr}`; + } + + if (/[\w]$/.test(expr)) { + expr = `${expr}\\b`; + } + } + + return expr; }).join('|'), 'i'); }; diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb index c247ab21db..14cba70dcf 100644 --- a/app/lib/feed_manager.rb +++ b/app/lib/feed_manager.rb @@ -200,10 +200,11 @@ class FeedManager active_filters = Rails.cache.fetch("filters:#{receiver_id}") { CustomFilter.where(account_id: receiver_id).active_irreversible.to_a }.to_a active_filters.select! { |filter| filter.context.include?(context.to_s) && !filter.expired? } + active_filters.map! do |filter| if filter.whole_word sb = filter.phrase =~ /\A[[:word:]]/ ? '\b' : '' - eb = filter.phrase =~ /[[:word:]]\Z/ ? '\b' : '' + eb = filter.phrase =~ /[[:word:]]\z/ ? '\b' : '' /(?mix:#{sb}#{Regexp.escape(filter.phrase)}#{eb})/ else