Fix #2894: Use [[:space:]] instead of \s in regexes.

This commit is contained in:
evazion
2018-09-20 19:13:31 -05:00
parent 29cdaddd86
commit 03abbd0683
24 changed files with 56 additions and 44 deletions

View File

@@ -24,11 +24,9 @@ class AliasAndImplicationImporter
end end
def self.tokenize(text) def self.tokenize(text)
text = text.dup
text.gsub!(/^\s+/, "")
text.gsub!(/\s+$/, "")
text.gsub!(/ {2,}/, " ")
text.split(/\r\n|\r|\n/).map do |line| text.split(/\r\n|\r|\n/).map do |line|
line = line.gsub(/[[:space:]]+/, " ").strip
if line =~ /^(?:create alias|aliasing|alias) (\S+) -> (\S+)$/i if line =~ /^(?:create alias|aliasing|alias) (\S+) -> (\S+)$/i
[:create_alias, $1, $2] [:create_alias, $1, $2]

View File

@@ -33,13 +33,13 @@ module GoogleBigQuery
constraints << "updater_id = #{user_id.to_i}" constraints << "updater_id = #{user_id.to_i}"
if added_tags if added_tags
added_tags.scan(/\S+/).each do |tag| added_tags.split.each do |tag|
constraints << add_tag_condition(tag) constraints << add_tag_condition(tag)
end end
end end
if removed_tags if removed_tags
removed_tags.scan(/\S+/).each do |tag| removed_tags.split.each do |tag|
constraints << remove_tag_condition(tag) constraints << remove_tag_condition(tag)
end end
end end

View File

@@ -22,7 +22,7 @@ module PostSets
end end
def unordered_tag_array def unordered_tag_array
tag_array.reject{|tag| tag =~ /\Aorder:\S+/} tag_array.reject {|tag| tag =~ /\Aorder:/i }
end end
def has_wiki? def has_wiki?
@@ -55,7 +55,7 @@ module PostSets
end end
def pool_name def pool_name
tag_string.match(/^(?:ord)?pool:(\S+)$/i).try(:[], 1) @pool_name ||= Tag.has_metatag?(tag_array, :ordpool, :pool)
end end
def has_pool? def has_pool?
@@ -67,7 +67,7 @@ module PostSets
end end
def favgroup_name def favgroup_name
tag_string.match(/^favgroup:(\S+)$/i).try(:[], 1) @favgroup_name ||= Tag.has_metatag?(tag_array, :favgroup)
end end
def has_favgroup? def has_favgroup?

View File

@@ -27,7 +27,7 @@ class RelatedTagCalculator
CurrentUser.without_safe_mode do CurrentUser.without_safe_mode do
Post.with_timeout(5_000, [], {:tags => tag}) do Post.with_timeout(5_000, [], {:tags => tag}) do
Post.tag_match(tag).limit(400).reorder("posts.md5").pluck(:tag_string).each do |tag_string| Post.tag_match(tag).limit(400).reorder("posts.md5").pluck(:tag_string).each do |tag_string|
tag_string.scan(/\S+/).each do |tag| tag_string.split.each do |tag|
counts[tag] += 1 counts[tag] += 1
end end
end end

View File

@@ -96,7 +96,7 @@ private
if (cookies[:favorite_tags].blank? || cookies[:favorite_tags_with_categories].blank?) && CurrentUser.user.favorite_tags.present? if (cookies[:favorite_tags].blank? || cookies[:favorite_tags_with_categories].blank?) && CurrentUser.user.favorite_tags.present?
favorite_tags = CurrentUser.user.favorite_tags.slice(0, 1024) favorite_tags = CurrentUser.user.favorite_tags.slice(0, 1024)
cookies[:favorite_tags] = favorite_tags cookies[:favorite_tags] = favorite_tags
cookies[:favorite_tags_with_categories] = Tag.categories_for(favorite_tags.scan(/\S+/)).to_a.flatten.join(" ") cookies[:favorite_tags_with_categories] = Tag.categories_for(favorite_tags.split(/[[:space:]]+/)).to_a.flatten.join(" ")
end end
end end

View File

@@ -233,7 +233,7 @@ class Artist < ApplicationRecord
end end
def other_names_array def other_names_array
other_names.try(:split, /\s/) other_names.try(:split, /[[:space:]]+/)
end end
def other_names_comma def other_names_comma

View File

@@ -48,11 +48,11 @@ class ArtistVersion < ApplicationRecord
extend SearchMethods extend SearchMethods
def url_array def url_array
url_string.to_s.scan(/\S+/) url_string.to_s.split(/[[:space:]]+/)
end end
def other_names_array def other_names_array
other_names.to_s.scan(/\S+/) other_names.to_s.split(/[[:space:]]+/)
end end
def urls_diff(version) def urls_diff(version)

View File

@@ -3,7 +3,7 @@ class Comment < ApplicationRecord
validate :validate_post_exists, :on => :create validate :validate_post_exists, :on => :create
validate :validate_creator_is_not_limited, :on => :create validate :validate_creator_is_not_limited, :on => :create
validates_format_of :body, :with => /\S/, :message => 'has no content' validates_presence_of :body, :message => "has no content"
belongs_to :post belongs_to :post
belongs_to_creator belongs_to_creator
belongs_to_updater belongs_to_updater

View File

@@ -8,13 +8,8 @@ class Dmail < ApplicationRecord
include Rakismet::Model include Rakismet::Model
with_options on: :create do validates_presence_of :title, :body, on: :create
validates_presence_of :to_id validate :validate_sender_is_not_banned, on: :create
validates_presence_of :from_id
validates_format_of :title, :with => /\S/
validates_format_of :body, :with => /\S/
validate :validate_sender_is_not_banned
end
belongs_to :owner, :class_name => "User" belongs_to :owner, :class_name => "User"
belongs_to :to, :class_name => "User" belongs_to :to, :class_name => "User"
@@ -217,7 +212,7 @@ class Dmail < ApplicationRecord
extend SearchMethods extend SearchMethods
def validate_sender_is_not_banned def validate_sender_is_not_banned
if from.is_banned? if from.try(:is_banned?)
errors[:base] << "Sender is banned and cannot send messages" errors[:base] << "Sender is banned and cannot send messages"
return false return false
else else

View File

@@ -18,6 +18,6 @@ class DmailFilter < ApplicationRecord
end end
def regexp def regexp
@regexp ||= Regexp.compile('\b(?:' + words.scan(/\S+/).map {|x| Regexp.escape(x)}.join("|") + ')\b') @regexp ||= /\b#{Regexp.union(words.split(/[[:space:]]+/))}\b/
end end
end end

View File

@@ -108,7 +108,7 @@ class FavoriteGroup < ApplicationRecord
end end
def self.normalize_name(name) def self.normalize_name(name)
name.gsub(/\s+/, "_") name.gsub(/[[:space:]]+/, "_")
end end
def normalize_name def normalize_name

View File

@@ -970,7 +970,7 @@ class Post < ApplicationRecord
end end
def clean_fav_string! def clean_fav_string!
array = fav_string.scan(/\S+/).uniq array = fav_string.split.uniq
self.fav_string = array.join(" ") self.fav_string = array.join(" ")
self.fav_count = array.size self.fav_count = array.size
update_column(:fav_string, fav_string) update_column(:fav_string, fav_string)
@@ -1113,7 +1113,7 @@ class Post < ApplicationRecord
end end
def set_pool_category_pseudo_tags def set_pool_category_pseudo_tags
self.pool_string = (pool_string.scan(/\S+/) - ["pool:series", "pool:collection"]).join(" ") self.pool_string = (pool_string.split - ["pool:series", "pool:collection"]).join(" ")
pool_categories = pools.undeleted.pluck(:category) pool_categories = pools.undeleted.pluck(:category)
if pool_categories.include?("series") if pool_categories.include?("series")
@@ -1157,7 +1157,7 @@ class Post < ApplicationRecord
def fast_count(tags = "", options = {}) def fast_count(tags = "", options = {})
tags = tags.to_s tags = tags.to_s
tags += " rating:s" if CurrentUser.safe_mode? tags += " rating:s" if CurrentUser.safe_mode?
tags += " -status:deleted" if CurrentUser.hide_deleted_posts? && tags !~ /(?:^|\s)(?:-)?status:.+/ tags += " -status:deleted" if CurrentUser.hide_deleted_posts? && !Tag.has_metatag?(tags, "status", "-status")
tags = Tag.normalize_query(tags) tags = Tag.normalize_query(tags)
# optimize some cases. these are just estimates but at these # optimize some cases. these are just estimates but at these

View File

@@ -88,7 +88,7 @@ class PostArchive < ApplicationRecord
include ArchiveServiceMethods include ArchiveServiceMethods
def tag_array def tag_array
tags.scan(/\S+/) tags.split
end end
def presenter def presenter

View File

@@ -51,7 +51,7 @@ class PostVersion < ApplicationRecord
end end
def tag_array def tag_array
@tag_array ||= tags.scan(/\S+/) @tag_array ||= tags.split
end end
def reload def reload

View File

@@ -179,7 +179,7 @@ class Tag < ApplicationRecord
while counts.empty? && n < 1000 while counts.empty? && n < 1000
tag_strings = Post.select_values_sql("select tag_string from posts where created_at >= ?", n.hours.ago) tag_strings = Post.select_values_sql("select tag_string from posts where created_at >= ?", n.hours.ago)
tag_strings.each do |tag_string| tag_strings.each do |tag_string|
tag_string.scan(/\S+/).each do |tag| tag_string.split.each do |tag|
counts[tag] ||= 0 counts[tag] ||= 0
counts[tag] += 1 counts[tag] += 1
end end

View File

@@ -239,8 +239,8 @@ class Upload < ApplicationRecord
end end
def assign_rating_from_tags def assign_rating_from_tags
if tag_string =~ /(?:\s|^)rating:([qse])/i if rating = Tag.has_metatag?(tag_string, :rating)
self.rating = $1.downcase self.rating = rating.downcase
end end
end end

View File

@@ -234,6 +234,6 @@ class WikiPage < ApplicationRecord
end end
def other_names_array def other_names_array
other_names.to_s.scan(/\S+/) other_names.to_s.split(/[[:space:]]+/)
end end
end end

View File

@@ -47,6 +47,6 @@ class WikiPageVersion < ApplicationRecord
end end
def other_names_array def other_names_array
other_names.to_s.scan(/\S+/) other_names.to_s.split(/[[:space:]]+/)
end end
end end

View File

@@ -199,7 +199,7 @@ class PostPresenter < Presenter
end end
def has_sequential_navigation?(params) def has_sequential_navigation?(params)
return false if params[:tags] =~ /(?:^|\s)(?:order|ordfav|ordpool):/i return false if Tag.has_metatag?(params[:tags], :order, :ordfav, :ordpool)
return false if params[:pool_id].present? || params[:favgroup_id].present? return false if params[:pool_id].present? || params[:favgroup_id].present?
return CurrentUser.user.enable_sequential_post_navigation return CurrentUser.user.enable_sequential_post_navigation
end end

View File

@@ -180,4 +180,14 @@ class UserPresenter
def previous_names(template) def previous_names(template)
user.user_name_change_requests.map { |req| template.link_to req.original_name, req }.join(", ").html_safe user.user_name_change_requests.map { |req| template.link_to req.original_name, req }.join(", ").html_safe
end end
def custom_css
user.custom_style.to_s.split(/\r\n|\r|\n/).map do |line|
if line =~ /\A@import/
line
else
line.gsub(/([^[:space:]])[[:space:]]*(?:!important)?[[:space:]]*(;|})/, "\\1 !important\\2")
end
end.join("\n")
end
end end

View File

@@ -1,7 +1 @@
<% CurrentUser.user.custom_style.to_s.split(/\r\n|\r|\n/).each do |line| %> <%= raw CurrentUser.user.presenter.custom_css %>
<% if line =~ /^@import/ %>
<%= raw(line) %>
<% else %>
<%= raw(line.gsub(/(\S)\s*(?:!important)?\s*(;|})/, "\\1 !important\\2")) %>
<% end %>
<% end %>

View File

@@ -298,5 +298,10 @@ class CommentTest < ActiveSupport::TestCase
end end
end end
end end
context "during validation" do
subject { FactoryBot.build(:comment) }
should_not allow_value(" ").for(:body)
end
end end
end end

View File

@@ -210,5 +210,15 @@ class DmailTest < ActiveSupport::TestCase
end end
end end
end end
context "during validation" do
subject { FactoryBot.build(:dmail) }
should_not allow_value(" ").for(:title)
should_not allow_value(" ").for(:body)
should_not allow_value(nil).for(:to)
should_not allow_value(nil).for(:from)
should_not allow_value(nil).for(:owner)
end
end end
end end

View File

@@ -74,7 +74,7 @@ class TagAliasTest < ActiveSupport::TestCase
ss = FactoryBot.create(:saved_search, :query => "123 ... 456", :user => CurrentUser.user) ss = FactoryBot.create(:saved_search, :query => "123 ... 456", :user => CurrentUser.user)
ta = FactoryBot.create(:tag_alias, :antecedent_name => "...", :consequent_name => "bbb") ta = FactoryBot.create(:tag_alias, :antecedent_name => "...", :consequent_name => "bbb")
ss.reload ss.reload
assert_equal(%w(123 456 bbb), ss.query.scan(/\S+/).sort) assert_equal(%w(123 456 bbb), ss.query.split.sort)
end end
should "update any affected posts when saved" do should "update any affected posts when saved" do