|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +var process = require('node:process'); |
3 | 4 | var promises = require('node:fs/promises'); |
4 | 5 |
|
5 | 6 | exports.EnumToken = void 0; |
@@ -182,6 +183,10 @@ const colorFuncColorSpace = ['srgb', 'srgb-linear', 'display-p3', 'prophoto-rgb' |
182 | 183 | const D50 = [0.3457 / 0.3585, 1.00000, (1.0 - 0.3457 - 0.3585) / 0.3585]; |
183 | 184 | const k = Math.pow(29, 3) / Math.pow(3, 3); |
184 | 185 | const e = Math.pow(6, 3) / Math.pow(29, 3); |
| 186 | +// color module v4 |
| 187 | +const systemColors = new Set(['ActiveText', 'ButtonBorder', 'ButtonFace', 'ButtonText', 'Canvas', 'CanvasText', 'Field', 'FieldText', 'GrayText', 'Highlight', 'HighlightText', 'LinkText', 'Mark', 'MarkText', 'VisitedText'].map(m => m.toLowerCase())); |
| 188 | +// deprecated |
| 189 | +const deprecatedSystemColors = new Set(['ActiveBorder', 'ActiveCaption', 'AppWorkspace', 'Background', 'ButtonFace', 'ButtonHighlight', 'ButtonShadow', 'ButtonText', 'CaptionText', 'GrayText', 'Highlight', 'HighlightText', 'InactiveBorder', 'InactiveCaption', 'InactiveCaptionText', 'InfoBackground', 'InfoText', 'Menu', 'MenuText', 'Scrollbar', 'ThreeDDarkShadow', 'ThreeDFace', 'ThreeDHighlight', 'ThreeDLightShadow', 'ThreeDShadow', 'Window', 'WindowFrame', 'WindowText'].map(t => t.toLowerCase())); |
185 | 190 | // name to color |
186 | 191 | const COLORS_NAMES = Object.seal({ |
187 | 192 | 'aliceblue': '#f0f8ff', |
@@ -2956,7 +2961,7 @@ class SourceMap { |
2956 | 2961 | } |
2957 | 2962 | } |
2958 | 2963 |
|
2959 | | -const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk', 'color-mix', 'color', 'oklab', 'lab', 'oklch', 'lch']; |
| 2964 | +const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk', 'color-mix', 'color', 'oklab', 'lab', 'oklch', 'lch', 'light-dark']; |
2960 | 2965 | function reduceNumber(val) { |
2961 | 2966 | val = String(+val); |
2962 | 2967 | if (val === '0') { |
@@ -3224,6 +3229,9 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer, |
3224 | 3229 | case exports.EnumToken.Div: |
3225 | 3230 | return '/'; |
3226 | 3231 | case exports.EnumToken.ColorTokenType: |
| 3232 | + if (token.kin == 'light-dark') { |
| 3233 | + return token.val + '(' + token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), '') + ')'; |
| 3234 | + } |
3227 | 3235 | if (options.convertColor) { |
3228 | 3236 | if (token.cal == 'mix' && token.val == 'color-mix') { |
3229 | 3237 | const children = token.chi.reduce((acc, t) => { |
@@ -3317,7 +3325,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer, |
3317 | 3325 | return reduceHexValue(value); |
3318 | 3326 | } |
3319 | 3327 | } |
3320 | | - if (token.kin == 'hex' || token.kin == 'lit') { |
| 3328 | + if (['hex', 'lit', 'sys', 'dpsys'].includes(token.kin)) { |
3321 | 3329 | return token.val; |
3322 | 3330 | } |
3323 | 3331 | if (Array.isArray(token.chi)) { |
@@ -3583,6 +3591,15 @@ function isColor(token) { |
3583 | 3591 | } |
3584 | 3592 | let isLegacySyntax = false; |
3585 | 3593 | if (token.typ == exports.EnumToken.FunctionTokenType && token.chi.length > 0 && colorsFunc.includes(token.val)) { |
| 3594 | + if (token.val == 'light-dark') { |
| 3595 | + const children = token.chi.filter((t) => [exports.EnumToken.IdenTokenType, exports.EnumToken.NumberTokenType, exports.EnumToken.LiteralTokenType, exports.EnumToken.ColorTokenType, exports.EnumToken.FunctionTokenType, exports.EnumToken.PercentageTokenType].includes(t.typ)); |
| 3596 | + if (children.length != 2) { |
| 3597 | + return false; |
| 3598 | + } |
| 3599 | + if (isColor(children[0]) && isColor(children[1])) { |
| 3600 | + return true; |
| 3601 | + } |
| 3602 | + } |
3586 | 3603 | if (token.val == 'color') { |
3587 | 3604 | const children = token.chi.filter((t) => [exports.EnumToken.IdenTokenType, exports.EnumToken.NumberTokenType, exports.EnumToken.LiteralTokenType, exports.EnumToken.ColorTokenType, exports.EnumToken.FunctionTokenType, exports.EnumToken.PercentageTokenType].includes(t.typ)); |
3588 | 3605 | const isRelative = children[0].typ == exports.EnumToken.IdenTokenType && children[0].val == 'from'; |
@@ -6572,9 +6589,9 @@ function parseString(src, options = { location: false }) { |
6572 | 6589 | })); |
6573 | 6590 | } |
6574 | 6591 | function getTokenType(val, hint) { |
6575 | | - if (val === '' && hint == null) { |
6576 | | - throw new Error('empty string?'); |
6577 | | - } |
| 6592 | + // if (val === '' && hint == null) { |
| 6593 | + // throw new Error('empty string?'); |
| 6594 | + // } |
6578 | 6595 | if (hint != null) { |
6579 | 6596 | return enumTokenHints.has(hint) ? { typ: hint } : { typ: hint, val }; |
6580 | 6597 | } |
@@ -6703,6 +6720,20 @@ function getTokenType(val, hint) { |
6703 | 6720 | }; |
6704 | 6721 | } |
6705 | 6722 | if (isIdent(val)) { |
| 6723 | + if (systemColors.has(val.toLowerCase())) { |
| 6724 | + return { |
| 6725 | + typ: exports.EnumToken.ColorTokenType, |
| 6726 | + val, |
| 6727 | + kin: 'sys' |
| 6728 | + }; |
| 6729 | + } |
| 6730 | + if (deprecatedSystemColors.has(val.toLowerCase())) { |
| 6731 | + return { |
| 6732 | + typ: exports.EnumToken.ColorTokenType, |
| 6733 | + val, |
| 6734 | + kin: 'dpsys' |
| 6735 | + }; |
| 6736 | + } |
6706 | 6737 | return { |
6707 | 6738 | typ: val.startsWith('--') ? exports.EnumToken.DashedIdenTokenType : exports.EnumToken.IdenTokenType, |
6708 | 6739 | val |
@@ -6984,7 +7015,11 @@ function parseTokens(tokens, options = {}) { |
6984 | 7015 | // t.chi = t.chi.filter((t: Token) => [EnumToken.IdenTokenType, EnumToken.NumberTokenType, EnumToken.PercentageTokenType].includes(t.typ)); |
6985 | 7016 | } |
6986 | 7017 | } |
6987 | | - t.chi = t.chi.filter((t) => ![exports.EnumToken.WhitespaceTokenType, exports.EnumToken.CommaTokenType, exports.EnumToken.CommentTokenType].includes(t.typ)); |
| 7018 | + const filter = [exports.EnumToken.WhitespaceTokenType, exports.EnumToken.CommentTokenType]; |
| 7019 | + if (t.val != 'light-dark') { |
| 7020 | + filter.push(exports.EnumToken.CommaTokenType); |
| 7021 | + } |
| 7022 | + t.chi = t.chi.filter((t) => !filter.includes(t.typ)); |
6988 | 7023 | continue; |
6989 | 7024 | } |
6990 | 7025 | if (t.typ == exports.EnumToken.UrlFunctionTokenType) { |
|
0 commit comments