|
77 | 77 | }
|
78 | 78 |
|
79 | 79 | var removedSpace = val.replace(/ /g, '');
|
80 |
| - var regex = /\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)/i; |
| 80 | + var regex = /rgb\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\)/i; |
81 | 81 |
|
82 | 82 | if (removedSpace.match(regex)) {
|
83 |
| - var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, ''); |
| 83 | + var removeRgbCall = removedSpace.replace(/rgb/g, ''); |
| 84 | + var removeBrackets = removeRgbCall.replace(/\(/g, '').replace(/\)/g, ''); |
84 | 85 | var valueSliced = removeBrackets.split(',');
|
85 | 86 | var isValid = true;
|
86 | 87 |
|
87 | 88 | valueSliced.forEach(function(i) {
|
88 |
| - var parsedInt = parseInt(i); |
| 89 | + var parsedInt = parseInt(i, 10); |
89 | 90 | if ((Number.isInteger(parsedInt) && 0 <= parsedInt && parsedInt <= 255) === false) {
|
90 | 91 | isValid = false;
|
91 | 92 | }
|
|
110 | 111 | }
|
111 | 112 |
|
112 | 113 | var removedSpace = val.replace(/ /g, '');
|
113 |
| - var regex = /\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0,1]{1}.?[0-9]*\)/i; |
| 114 | + var regex = /rgba\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3},[0,1]?.?[0-9]*\)/i; |
114 | 115 |
|
115 | 116 | if (removedSpace.match(regex)) {
|
116 |
| - var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, ''); |
| 117 | + var removeRgbaCall = removedSpace.replace(/rgba/g, ''); |
| 118 | + var removeBrackets = removeRgbaCall.replace(/\(/g, '').replace(/\)/g, ''); |
117 | 119 | var valueSliced = removeBrackets.split(',');
|
118 | 120 | var isValid = true;
|
119 | 121 |
|
120 |
| - valueSliced.forEach(function(i) { |
| 122 | + valueSliced.forEach(function(i, index) { |
121 | 123 | var value = filterFloat(i);
|
122 | 124 | if (Number.isInteger(value)) {
|
123 | 125 | var isInRange = value >= 0 && value <= 255;
|
124 | 126 | if (!isInRange) {
|
125 | 127 | isValid = false;
|
126 | 128 | }
|
127 |
| - } else { |
128 |
| - if (!isBetween0and1(value)) { |
129 |
| - isValid = false; |
| 129 | + |
| 130 | + if (isValid && index === 3) { |
| 131 | + isValid = value >= 0 && value < 2; |
130 | 132 | }
|
| 133 | + } else if (!isBetween0and1(i)) { |
| 134 | + isValid = false; |
131 | 135 | }
|
132 | 136 | });
|
133 | 137 | return isValid;
|
|
149 | 153 | return true;
|
150 | 154 | }
|
151 | 155 |
|
152 |
| - var isInRange; |
153 | 156 | var removedSpace = val.replace(/ /g, '');
|
154 |
| - var regex = /\([0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%\)/i; |
| 157 | + var regex = /hsl\(-?[0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%\)/i; |
155 | 158 |
|
156 | 159 | if (removedSpace.match(regex)) {
|
157 |
| - var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, ''); |
| 160 | + var removeHslCall = removedSpace.replace(/hsl/g, ''); |
| 161 | + var removeBrackets = removeHslCall.replace(/\(/g, '').replace(/\)/g, ''); |
158 | 162 | var valueSliced = removeBrackets.split(',');
|
159 | 163 | var isValid = true;
|
160 | 164 |
|
161 | 165 | valueSliced.forEach(function(i, index) {
|
162 |
| - var parsedInt = parseInt(i); |
| 166 | + var parsedInt = parseInt(i, 10); |
163 | 167 |
|
164 | 168 | if (Number.isInteger(parsedInt)) {
|
165 |
| - if (index === 0) { |
166 |
| - isInRange = 0 <= parsedInt && parsedInt <= 360; |
167 |
| - if (!isInRange) { |
168 |
| - isValid = false; |
169 |
| - } |
170 |
| - } else { |
171 |
| - isInRange = 0 <= parsedInt && parsedInt <= 100; |
| 169 | + if (index !== 0) { |
| 170 | + var isInRange = parsedInt >= 0 && parsedInt <= 100; |
172 | 171 | if (!isInRange) {
|
173 | 172 | isValid = false;
|
174 | 173 | }
|
|
198 | 197 |
|
199 | 198 | var isInRange;
|
200 | 199 | var removedSpace = val.replace(/ /g, '');
|
201 |
| - var regex = /\([0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%,[0,1]{1}.?[0-9]*\)/i; |
| 200 | + var regex = /hsla\(-?[0-9]{1,3},[0-9]{1,3}%,[0-9]{1,3}%,[0,1]?.?[0-9]*\)/i; |
202 | 201 |
|
203 | 202 | if (removedSpace.match(regex)) {
|
204 |
| - var removeBrackets = removedSpace.replace(/\(/g, '').replace(/\)/g, ''); |
| 203 | + var removeHslaCall = removedSpace.replace(/hsla/g, ''); |
| 204 | + var removeBrackets = removeHslaCall.replace(/\(/g, '').replace(/\)/g, ''); |
205 | 205 | var valueSliced = removeBrackets.split(',');
|
206 | 206 | var isValid = true;
|
207 | 207 |
|
208 | 208 | valueSliced.forEach(function(i, index) {
|
209 | 209 | var value = filterFloat(i);
|
| 210 | + var parsedInt = parseInt(i, 10); |
210 | 211 |
|
211 | 212 | if (Number.isInteger(value)) {
|
212 |
| - |
213 |
| - if (index === 0) { |
214 |
| - isInRange = 0 <= value && value <= 360; |
| 213 | + if (index !== 0 && index !== 3) { |
| 214 | + isInRange = value >= 0 && value <= 100; |
215 | 215 | if (!isInRange) {
|
216 | 216 | isValid = false;
|
217 | 217 | }
|
218 |
| - } else { |
| 218 | + } |
219 | 219 |
|
220 |
| - isInRange = 0 <= value && value <= 100; |
221 |
| - if (!isInRange) { |
222 |
| - isValid = false; |
223 |
| - } |
| 220 | + if (isValid && index === 3) { |
| 221 | + isValid = value >= 0 && value < 2; |
| 222 | + } |
| 223 | + } else if (isNaN(value) && Number.isInteger(parsedInt)) { |
| 224 | + isInRange = parsedInt >= 0 && parsedInt <= 100; |
| 225 | + if (!isInRange) { |
| 226 | + isValid = false; |
224 | 227 | }
|
225 | 228 | } else {
|
226 |
| - if (isNaN(value)) { |
227 |
| - // percent value |
228 |
| - value = parseInt(i); |
229 |
| - isInRange = 0 <= value && value <= 100; |
230 |
| - if (!isInRange) { |
231 |
| - isValid = false; |
232 |
| - } |
233 |
| - } else { |
234 |
| - isInRange = 0 <= value && value <= 1; |
235 |
| - if (!isInRange) { |
236 |
| - isValid = false; |
237 |
| - } |
| 229 | + value = filterFloat(Number(i).toFixed(20)); |
| 230 | + |
| 231 | + isInRange = value >= 0 && value <= 1; |
| 232 | + if (!isInRange) { |
| 233 | + isValid = false; |
238 | 234 | }
|
239 | 235 | }
|
240 | 236 | });
|
| 237 | + |
241 | 238 | return isValid;
|
242 | 239 | }
|
243 | 240 |
|
|
0 commit comments