Make whole-word filter regex consistent between Ruby and JS (#7987)

rebase/4.0.0rc2
Eugen Rochko 2018-07-10 03:01:50 +02:00 committed by GitHub
parent 1ca4e51eb3
commit 20fefdb714
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 2 deletions

View File

@ -47,7 +47,18 @@ export const regexFromFilters = filters => {
return new RegExp(filters.map(filter => { return new RegExp(filters.map(filter => {
let expr = escapeRegExp(filter.get('phrase')); 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'); }).join('|'), 'i');
}; };

View File

@ -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 = 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.select! { |filter| filter.context.include?(context.to_s) && !filter.expired? }
active_filters.map! do |filter| active_filters.map! do |filter|
if filter.whole_word if filter.whole_word
sb = filter.phrase =~ /\A[[:word:]]/ ? '\b' : '' 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})/ /(?mix:#{sb}#{Regexp.escape(filter.phrase)}#{eb})/
else else