From 7206f4cd9e73f090347f491d82d345a339c9983b Mon Sep 17 00:00:00 2001 From: tophf Date: Mon, 15 Jul 2019 03:16:53 +0300 Subject: [PATCH] Fix colorpicker (#745) * process CM5.48+ new token for color functions * restore scientific number notation support in colors rgba(1.5E2 0 0 / .5e2%) --- vendor-overwrites/colorpicker/colorconverter.js | 11 +++++++---- vendor-overwrites/colorpicker/colorview.js | 9 +++++++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/vendor-overwrites/colorpicker/colorconverter.js b/vendor-overwrites/colorpicker/colorconverter.js index 3c175976..17eea98a 100644 --- a/vendor-overwrites/colorpicker/colorconverter.js +++ b/vendor-overwrites/colorpicker/colorconverter.js @@ -54,12 +54,14 @@ const colorConverter = (() => { } function validatePercentage(s) { - const match = s.match(/^(\d+|\d*\.\d+)%$/); - return match && Number(match[1]) >= 0 && Number(match[1]) <= 100; + if (!s.endsWith('%')) return false; + const n = Number(s.slice(0, -1)); + return n >= 0 && n <= 100; } 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) { @@ -74,7 +76,8 @@ const colorConverter = (() => { if (alpha.endsWith('%')) { return validatePercentage(alpha); } - return Number(alpha) >= 0 && Number(alpha) <= 1; + const n = Number(alpha); + return n >= 0 && n <= 1; } function parse(str) { diff --git a/vendor-overwrites/colorpicker/colorview.js b/vendor-overwrites/colorpicker/colorview.js index 4892876e..8614b524 100644 --- a/vendor-overwrites/colorpicker/colorview.js +++ b/vendor-overwrites/colorpicker/colorview.js @@ -363,7 +363,12 @@ for (let i = styleIndex; i + 1 < styles.length; i += 2) { 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; start = i > 2 ? styles[i - 2] : 0; @@ -574,7 +579,7 @@ 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; RX_DETECT.lastIndex = Math.max(0, pos - 1000);