From 36e70c91c373077c70ac6b9961b0ce2e5917239e Mon Sep 17 00:00:00 2001 From: tophf Date: Sat, 13 Mar 2021 09:47:27 +0300 Subject: [PATCH] fixup: group named colors, preserve letter case from spec --- edit/editor-worker.js | 20 ++++---- js/csslint/parserlib.js | 100 ++++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/edit/editor-worker.js b/edit/editor-worker.js index 837dbbcc..ad150da1 100644 --- a/edit/editor-worker.js +++ b/edit/editor-worker.js @@ -19,23 +19,25 @@ getCssPropsValues() { require(['/js/csslint/parserlib']); /* global parserlib */ const {css: {Colors, Properties}, util: {describeProp}} = parserlib; - const namedColors = Object.keys(Colors).join(' '); + const namedColors = Object.keys(Colors); const rxNonWord = /(?:<.+?>|[^-\w<(]+\d*)+/g; const res = {}; // moving vendor-prefixed props to the end - const comparator = (a, b) => a[0] === '-' && b[0] !== '-' ? 1 : a < b ? -1 : a > b; + const cmp = (a, b) => a[0] === '-' && b[0] !== '-' ? 1 : a < b ? -1 : a > b; for (const [k, v] of Object.entries(Properties)) { if (typeof v === 'string') { let last = ''; const uniq = []; - const words = describeProp(v) - .replace(/\(.*?\)/g, '(') - .replace('', namedColors) - .split(rxNonWord) - .sort(comparator); - for (const word of words) { - if (word !== last) uniq.push(last = word); + // strip definitions of function arguments + const desc = describeProp(v).replace(/([-\w]+)\(.*?\)/g, 'z-$1'); + const descNoColors = desc.replace(//g, ''); + // add a prefix to functions to group them at the end + const words = descNoColors.split(rxNonWord).sort(cmp); + for (let w of words) { + if (w.startsWith('z-')) w = w.slice(2) + '('; + if (w !== last) uniq.push(last = w); } + if (desc !== descNoColors) uniq.push(...namedColors); if (uniq.length) res[k] = uniq; } } diff --git a/js/csslint/parserlib.js b/js/csslint/parserlib.js index 3ff892fd..6f437be4 100644 --- a/js/csslint/parserlib.js +++ b/js/csslint/parserlib.js @@ -778,7 +778,7 @@ self.parserlib = (() => { '': p => p.value >= 0 && (p.type === 'number' || p.type === 'percentage') || p.isCalc, //eslint-disable-next-line no-use-before-define - '': p => p.text in Colors || lower(p.text) in Colors, + '': p => p.text in Colors || ColorsLC.has(lower(p.text)), '': p => p.type === 'number' || p.isCalc, '': p => p.type === 'number' || p.type === 'percentage' || p.isCalc, '': p => p.type === 'number' && p.value >= 0 && p.value <= 1 || p.isCalc, @@ -979,7 +979,12 @@ self.parserlib = (() => { //#endregion //#region Colors - const Colors = { + const Colors = Object.assign(Object.create(null), { + // 'currentColor' color keyword + // https://www.w3.org/TR/css3-color/#currentcolor + currentColor: '', + transparent: '#0000', + aliceblue: '#f0f8ff', antiquewhite: '#faebd7', aqua: '#00ffff', @@ -1128,56 +1133,49 @@ self.parserlib = (() => { whitesmoke: '#f5f5f5', yellow: '#ffff00', yellowgreen: '#9acd32', - // 'currentColor' color keyword - // https://www.w3.org/TR/css3-color/#currentcolor - currentcolor: '', - transparent: '#0000', - // CSS2 system colors - // https://www.w3.org/TR/css3-color/#css2-system - activeborder: '', - activecaption: '', - appworkspace: '', - background: '', - buttonface: '', - buttonhighlight: '', - buttonshadow: '', - buttontext: '', - captiontext: '', - graytext: '', - greytext: '', - highlight: '', - highlighttext: '', - inactiveborder: '', - inactivecaption: '', - inactivecaptiontext: '', - infobackground: '', - infotext: '', - menu: '', - menutext: '', - scrollbar: '', - threeddarkshadow: '', - threedface: '', - threedhighlight: '', - threedlightshadow: '', - threedshadow: '', - window: '', - windowframe: '', - windowtext: '', - - // CSS4 system colors, only additions to the above - // https://drafts.csswg.org/css-color-4/#css-system-colors - activetext: '', - buttonborder: '', - canvas: '', - canvastext: '', - field: '', - fieldtext: '', - linktext: '', - mark: '', - marktext: '', - visitedtext: '', - }; + // old = CSS2 system colors: https://www.w3.org/TR/css3-color/#css2-system + // new = CSS4 system colors: https://drafts.csswg.org/css-color-4/#css-system-colors + ActiveBorder: '', + ActiveCaption: '', + ActiveText: '', // new + AppWorkspace: '', + Background: '', + ButtonBorder: '', // new + ButtonFace: '', // old+new + ButtonHighlight: '', + ButtonShadow: '', + ButtonText: '', // old+new + Canvas: '', // new + CanvasText: '', // new + CaptionText: '', + Field: '', // new + FieldText: '', // new + GrayText: '', // old+new + Highlight: '', // old+new + HighlightText: '', // old+new + InactiveBorder: '', + InactiveCaption: '', + InactiveCaptionText: '', + InfoBackground: '', + InfoText: '', + LinkText: '', // new + Mark: '', // new + MarkText: '', // new + Menu: '', + MenuText: '', + Scrollbar: '', + ThreeDDarkShadow: '', + ThreeDFace: '', + ThreeDHighlight: '', + ThreeDLightShadow: '', + ThreeDShadow: '', + VisitedText: '', // new + Window: '', + WindowFrame: '', + WindowText: '', + }); + const ColorsLC = new Set(Object.keys(Colors).map(lower)); //#endregion //#region Tokens