|
| 1 | +# frozen_string_literal: true |
1 | 2 | require "active_model"
|
2 | 3 |
|
3 | 4 | module ActiveModel::Validations::HelperMethods
|
@@ -27,8 +28,11 @@ module StripAttributes
|
27 | 28 | # U+FEFF ZERO WIDTH NO-BREAK SPACE
|
28 | 29 | MULTIBYTE_WHITE = "\u180E\u200B\u200C\u200D\u2060\uFEFF".freeze
|
29 | 30 | MULTIBYTE_SPACE = /[[:space:]#{MULTIBYTE_WHITE}]/.freeze
|
| 31 | + MULTIBYTE_SPACE_AT_ENDS = /\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/.freeze |
30 | 32 | MULTIBYTE_BLANK = /[[:blank:]#{MULTIBYTE_WHITE}]/.freeze
|
| 33 | + MULTIBYTE_BLANK_REPEATED = /#{MULTIBYTE_BLANK}+/.freeze |
31 | 34 | MULTIBYTE_SUPPORTED = "\u0020" == " "
|
| 35 | + NEWLINES = /[\r\n]+/.freeze |
32 | 36 |
|
33 | 37 | def self.strip(record_or_string, options = {})
|
34 | 38 | if record_or_string.respond_to?(:attributes)
|
@@ -62,16 +66,16 @@ def self.strip_string(value, options = {})
|
62 | 66 | value.gsub!(regex, "") if regex
|
63 | 67 |
|
64 | 68 | if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_SPACE)
|
65 |
| - value.gsub!(/\A#{MULTIBYTE_SPACE}+|#{MULTIBYTE_SPACE}+\z/, "") |
| 69 | + value.gsub!(MULTIBYTE_SPACE_AT_ENDS, "") |
66 | 70 | else
|
67 | 71 | value.strip!
|
68 | 72 | end
|
69 | 73 |
|
70 |
| - value.gsub!(/[\r\n]+/, " ") if replace_newlines |
| 74 | + value.gsub!(NEWLINES, " ") if replace_newlines |
71 | 75 |
|
72 | 76 | if collapse_spaces
|
73 | 77 | if MULTIBYTE_SUPPORTED && Encoding.compatible?(value, MULTIBYTE_BLANK)
|
74 |
| - value.gsub!(/#{MULTIBYTE_BLANK}+/, " ") |
| 78 | + value.gsub!(MULTIBYTE_BLANK_REPEATED, " ") |
75 | 79 | else
|
76 | 80 | value.squeeze!(" ")
|
77 | 81 | end
|
|
0 commit comments