Fix colorpicker (#745)

* process CM5.48+ new token for color functions

* restore scientific number notation support in colors

rgba(1.5E2 0 0 / .5e2%)
This commit is contained in:
tophf 2019-07-15 03:16:53 +03:00 committed by narcolepticinsomniac
parent c2e83fb3c4
commit 7206f4cd9e
2 changed files with 14 additions and 6 deletions

View File

@ -54,12 +54,14 @@ const colorConverter = (() => {
} }
function validatePercentage(s) { function validatePercentage(s) {
const match = s.match(/^(\d+|\d*\.\d+)%$/); if (!s.endsWith('%')) return false;
return match && Number(match[1]) >= 0 && Number(match[1]) <= 100; const n = Number(s.slice(0, -1));
return n >= 0 && n <= 100;
} }
function validateNum(s) { function validateNum(s) {
return /^\d+$/.test(s) && Number(s) >= 0 && Number(s) <= 255; const n = Number(s);
return n >= 0 && n <= 255;
} }
function validateHSL(nums) { function validateHSL(nums) {
@ -74,7 +76,8 @@ const colorConverter = (() => {
if (alpha.endsWith('%')) { if (alpha.endsWith('%')) {
return validatePercentage(alpha); return validatePercentage(alpha);
} }
return Number(alpha) >= 0 && Number(alpha) <= 1; const n = Number(alpha);
return n >= 0 && n <= 1;
} }
function parse(str) { function parse(str) {

View File

@ -363,7 +363,12 @@
for (let i = styleIndex; i + 1 < styles.length; i += 2) { for (let i = styleIndex; i + 1 < styles.length; i += 2) {
style = styles[i + 1]; style = styles[i + 1];
const styleSupported = style && (style.includes('atom') || style.includes('keyword')); const styleSupported = style && (
// old CodeMirror
style.includes('atom') || style.includes('keyword') ||
// new CodeMirror since 5.48
style.includes('variable callee')
);
if (!styleSupported) continue; if (!styleSupported) continue;
start = i > 2 ? styles[i - 2] : 0; start = i > 2 ? styles[i - 2] : 0;
@ -574,7 +579,7 @@
function findNearestColor({styles, text}, pos) { function findNearestColor({styles, text}, pos) {
const ALLOWED_STYLES = ['atom', 'keyword', 'comment', 'string']; const ALLOWED_STYLES = ['atom', 'keyword', 'callee', 'comment', 'string'];
let start, color, prevStart, prevColor, m; let start, color, prevStart, prevColor, m;
RX_DETECT.lastIndex = Math.max(0, pos - 1000); RX_DETECT.lastIndex = Math.max(0, pos - 1000);