Skip to content

Commit

Permalink
Check for invalid color default (microsoft#176033)
Browse files Browse the repository at this point in the history
* Check for invalid color default

* simplify
  • Loading branch information
aeschli authored Mar 3, 2023
1 parent 6f26936 commit 9cde57d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
31 changes: 16 additions & 15 deletions src/vs/platform/theme/common/colorRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import * as nls from 'vs/nls';
import { Extensions as JSONExtensions, IJSONContributionRegistry } from 'vs/platform/jsonschemas/common/jsonContributionRegistry';
import * as platform from 'vs/platform/registry/common/platform';
import { IColorTheme } from 'vs/platform/theme/common/themeService';
import { isString } from 'vs/base/common/types';
import { CharCode } from 'vs/base/common/charCode';

// ------ API types

Expand Down Expand Up @@ -69,6 +71,7 @@ export interface ColorDefaults {
hcLight: ColorValue | null;
}


/**
* A Color Value is either a color literal, a reference to an other color or a derived color
*/
Expand Down Expand Up @@ -138,6 +141,17 @@ class ColorRegistry implements IColorRegistry {
if (deprecationMessage) {
propertySchema.deprecationMessage = deprecationMessage;
}
if (defaults) {
const validateColorValue = (c: ColorValue | null) => {
if (isString(c) && c.charCodeAt(0) !== CharCode.Hash && !Color.Format.CSS.parseHex(c)) {
console.log(`Invalid color value: ${c} for color ${id}`);
}
};
validateColorValue(defaults.dark);
validateColorValue(defaults.light);
validateColorValue(defaults.hcDark);
validateColorValue(defaults.hcLight);
}
this.colorSchema.properties[id] = propertySchema;
this.colorReferenceSchema.enum.push(id);
this.colorReferenceSchema.enumDescriptions.push(description);
Expand Down Expand Up @@ -197,22 +211,9 @@ class ColorRegistry implements IColorRegistry {
const colorRegistry = new ColorRegistry();
platform.Registry.add(Extensions.ColorContribution, colorRegistry);

function migrateColorDefaults(o: any): null | ColorDefaults {
if (o === null) {
return o;
}
if (typeof o.hcLight === 'undefined') {
if (o.hcDark === null || typeof o.hcDark === 'string') {
o.hcLight = o.hcDark;
} else {
o.hcLight = o.light;
}
}
return o as ColorDefaults;
}

export function registerColor(id: string, defaults: ColorDefaults | null, description: string, needsTransparency?: boolean, deprecationMessage?: string): ColorIdentifier {
return colorRegistry.registerColor(id, migrateColorDefaults(defaults), description, needsTransparency, deprecationMessage);
return colorRegistry.registerColor(id, defaults, description, needsTransparency, deprecationMessage);
}

export function getColorRegistry(): IColorRegistry {
Expand Down Expand Up @@ -298,7 +299,7 @@ export const editorErrorBorder = registerColor('editorError.border', { dark: nul

export const editorWarningBackground = registerColor('editorWarning.background', { dark: null, light: null, hcDark: null, hcLight: null }, nls.localize('editorWarning.background', 'Background color of warning text in the editor. The color must not be opaque so as not to hide underlying decorations.'), true);
export const editorWarningForeground = registerColor('editorWarning.foreground', { dark: '#CCA700', light: '#BF8803', hcDark: '#FFD370', hcLight: '#895503' }, nls.localize('editorWarning.foreground', 'Foreground color of warning squigglies in the editor.'));
export const editorWarningBorder = registerColor('editorWarning.border', { dark: null, light: null, hcDark: Color.fromHex('#FFCC00').transparent(0.8), hcLight: '#' }, nls.localize('warningBorder', 'Border color of warning boxes in the editor.'));
export const editorWarningBorder = registerColor('editorWarning.border', { dark: null, light: null, hcDark: Color.fromHex('#FFCC00').transparent(0.8), hcLight: Color.fromHex('#FFCC00').transparent(0.8) }, nls.localize('warningBorder', 'Border color of warning boxes in the editor.'));

export const editorInfoBackground = registerColor('editorInfo.background', { dark: null, light: null, hcDark: null, hcLight: null }, nls.localize('editorInfo.background', 'Background color of info text in the editor. The color must not be opaque so as not to hide underlying decorations.'), true);
export const editorInfoForeground = registerColor('editorInfo.foreground', { dark: '#3794FF', light: '#1a85ff', hcDark: '#3794FF', hcLight: '#1a85ff' }, nls.localize('editorInfo.foreground', 'Foreground color of info squigglies in the editor.'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const ansiColorMap: { [key: string]: { index: number; defaults: ColorDefa
light: '#0598bc',
dark: '#11a8cd',
hcDark: '#00cdcd',
hcLight: '#0598b'
hcLight: '#0598bc'
}
},
'terminal.ansiWhite': {
Expand Down

0 comments on commit 9cde57d

Please sign in to comment.