-
Notifications
You must be signed in to change notification settings - Fork 33.2k
Fix hex color detection in strings by adding lookbehind patterns #251589
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
Conversation
Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com>
@@ -101,7 +101,7 @@ function _findMatches(model: IDocumentColorComputerTarget | string, regex: RegEx | |||
function computeColors(model: IDocumentColorComputerTarget): IColorInformation[] { | |||
const result: IColorInformation[] = []; | |||
// Early validation for RGB and HSL | |||
const initialValidationRegex = /\b(rgb|rgba|hsl|hsla)(\([0-9\s,.\%]*\))|\s+(#)([A-Fa-f0-9]{6})\b|\s+(#)([A-Fa-f0-9]{8})\b|^(#)([A-Fa-f0-9]{6})\b|^(#)([A-Fa-f0-9]{8})\b/gm; | |||
const initialValidationRegex = /\b(rgb|rgba|hsl|hsla)(\([0-9\s,.\%]*\))|\s+(#)([A-Fa-f0-9]{6})\b|\s+(#)([A-Fa-f0-9]{8})\b|^(#)([A-Fa-f0-9]{6})\b|^(#)([A-Fa-f0-9]{8})\b|(?<=['":=\[({])(#)([A-Fa-f0-9]{6})\b|(?<=['":=\[({])(#)([A-Fa-f0-9]{8})\b/gm; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi thanks for the change. I want the hex regex to be changed so that is simply adds the case when the length 6 or 8 color is inside of a string (or after a single or double quote). You don't need to cover the case where it follows other delimiters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! I've simplified the regex to only support hex colors after single and double quotes. Removed the lookbehind patterns for other delimiters (=, [, (, {, :) and updated the tests accordingly. The regex now only includes (?<=['"])(#)([A-Fa-f0-9]{6})\b|(?<=['"])(#)([A-Fa-f0-9]{8})\b
for the quote cases. Commit: 55e10f1
…imiters Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com>
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
…rosoft#251589) * Initial plan for issue * Fix hex color detection in strings by adding lookbehind patterns Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com> * Simplify hex color detection to only support quotes, remove other delimiters Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com> * polish --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: aiday-mar <61460952+aiday-mar@users.noreply.github.com> Co-authored-by: Aiday Marlen Kyzy <amarlenkyzy@microsoft.com>
The color picker was not detecting hex colors when they appeared directly after quote characters or other common delimiters in code. For example:
The issue was in the regex pattern in
defaultDocumentColorsComputer.ts
which only matched hex colors that were preceded by whitespace (\s+
) or at the start of a line (^
).Solution:
Added positive lookbehind patterns to detect hex colors after common programming delimiters:
'#ff0000'
"#ff0000"
background-color=#ff0000
colors = [#ff0000]
rgb(#ff0000)
style: {color: #ff0000}
Changes:
defaultDocumentColorsComputer.ts
to include(?<=['":=\[({])(#)([A-Fa-f0-9]{6})\b
and(?<=['":=\[({])(#)([A-Fa-f0-9]{8})\b
patternsTesting:
'#ff0000'
,"#ff0000"
['#ff0000', '#00ff00', '#0000ff']
=
,[
,(
,:
This is a minimal change (1 line modified) that significantly improves the color picker's usability in common coding scenarios.
Fixes #251582.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
electronjs.org
node-gyp
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.