Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check for non-hex values #315

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/languageFacts/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ export function isColorConstructor(node: nodes.Function): boolean {
return /^(rgb|rgba|hsl|hsla|hwb)$/gi.test(name);
}

export function isColorString(s: string) {
// From https://stackoverflow.com/questions/8027423/how-to-check-if-a-string-is-a-valid-hex-color-representation/8027444
return (s.toLowerCase() in colors) || /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(s) || /^(rgb|rgba|hsl|hsla|hwb)/gi.test(s.toLowerCase());
}


/**
* Returns true if the node is a color value - either
* defined a hex number, as rgb or rgba function, or
Expand Down Expand Up @@ -366,7 +372,7 @@ export function hslFromColor(rgba: Color): HSLA {
export function colorFromHWB(hue: number, white: number, black: number, alpha: number = 1.0): Color {
if (white + black >= 1) {
const gray = white / (white + black);
return {red: gray, green: gray, blue: gray, alpha};
return { red: gray, green: gray, blue: gray, alpha };
}

const rgb = colorFromHSL(hue, 1, 0.5, alpha);
Expand Down Expand Up @@ -422,11 +428,11 @@ export function getColorValue(node: nodes.Node): Color | null {
if (lastValue instanceof nodes.BinaryExpression) {
const left = lastValue.getLeft(), right = lastValue.getRight(), operator = lastValue.getOperator();
if (left && right && operator && operator.matches('/')) {
colorValues = [ colorValues[0], colorValues[1], left, right ];
colorValues = [colorValues[0], colorValues[1], left, right];
}
}
}
}
}
}
if (!name || colorValues.length < 3 || colorValues.length > 4) {
return null;
Expand Down
9 changes: 2 additions & 7 deletions src/services/cssCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export class CSSCompletion {
sortText: SortTexts.Variable
};

if (typeof completionItem.documentation === 'string' && isColorString(completionItem.documentation)) {
if (typeof completionItem.documentation === 'string' && languageFacts.isColorString(completionItem.documentation)) {
completionItem.kind = CompletionItemKind.Color;
}

Expand Down Expand Up @@ -466,7 +466,7 @@ export class CSSCompletion {
textEdit: TextEdit.replace(this.getCompletionRange(null), symbol.name),
kind: CompletionItemKind.Variable
};
if (typeof completionItem.documentation === 'string' && isColorString(completionItem.documentation)) {
if (typeof completionItem.documentation === 'string' && languageFacts.isColorString(completionItem.documentation)) {
completionItem.kind = CompletionItemKind.Color;
}

Expand Down Expand Up @@ -1150,8 +1150,3 @@ function getCurrentWord(document: TextDocument, offset: number): string {
}
return text.substring(i + 1, offset);
}

function isColorString(s: string) {
// From https://stackoverflow.com/questions/8027423/how-to-check-if-a-string-is-a-valid-hex-color-representation/8027444
return (s.toLowerCase() in languageFacts.colors) || /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(s);
}