[json] Protect from incompete colors (for exception in #32298)

This commit is contained in:
Martin Aeschlimann
2017-08-12 16:20:38 +02:00
parent d3a80dc181
commit 5fce6df8a2

View File

@@ -108,9 +108,7 @@ export function activateColorDecorations(decoratorProvider: (uri: string) => The
let decorations = [];
for (let i = 0; i < ranges.length && decorations.length < MAX_DECORATORS; i++) {
let range = ranges[i];
let text = document.getText(range);
let value = <string>JSON.parse(text);
let c = Color.fromHex(value);
let c = parseColorFromRange(document, range);
if (c) {
decorations.push(<DecorationOptions>{
range: range,
@@ -144,9 +142,7 @@ export class ColorProvider implements DocumentColorProvider {
const ranges = await this.decoratorProvider(document.uri.toString());
const result = [];
for (let range of ranges) {
let text = document.getText(range);
let value = <string>JSON.parse(text);
let color = Color.fromHex(value);
let color = parseColorFromRange(document, range);
if (color) {
let r = new Range(range.start.line, range.start.character + 1, range.end.line, range.end.character - 1);
result.push(new ColorRange(r, color, [ColorFormat_HEX]));
@@ -155,3 +151,16 @@ export class ColorProvider implements DocumentColorProvider {
return result;
}
}
function parseColorFromRange(document: TextDocument, range: Range) {
let text = document.getText(range);
try {
let value = <string>JSON.parse(text);
if (typeof value === 'string') {
return Color.fromHex(value);
}
} catch (e) {
// ignore JSON parse error
}
return null;
}