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(color-contrast): account for text-shadow #2334

Merged
merged 13 commits into from
Jul 20, 2020
Merged

fix(color-contrast): account for text-shadow #2334

merged 13 commits into from
Jul 20, 2020

Conversation

WilcoFiers
Copy link
Contributor

@WilcoFiers WilcoFiers commented Jun 30, 2020

Take text-shadow into account when computing the background color of a text node.

Related #246

Reviewer checks

Required fields, to be filled out by PR reviewer(s)

  • Follows the commit message policy, appropriate for next version
  • Code is reviewed for security

@WilcoFiers WilcoFiers requested a review from a team as a code owner June 30, 2020 11:45
Copy link
Member

@stephenmathieson stephenmathieson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't look like we're accounting for HSL colors here. Are we not concerned with them?

@@ -83,6 +107,31 @@ function Color(red, green, blue, alpha) {
}
};

this.parseHexString = function(colorString) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NBD either way, but now that we can import things, I'm curious why you didn't grab something from npm rather than building this yourself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security / package size. I know this does exactly what we need, and nothing else.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our bundler should be tree-shaking any unused dependency code out for us, and we would "bundle" it at build-time so it'd be easier to analyze the sources. I understand the security concern, but am just worried about adding more to the list of complex things we're responsible for maintaining. Not worth blocking this PR, but I thought once we'd migrated to modules, we'd stop "reinventing the wheel".

@@ -0,0 +1,147 @@
// Source: https://www.w3.org/TR/css-color-3/#svg-color
const cssColors = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few packages available from npm that have these listed in them already. Do we want to be responsible for keeping this list up-to-date, or should we offload that responsibility to the maintainers of one of the existing packages?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this is out-of-date. For example "rebeccapurple" has been added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yer right, we should use CSS 4 stuff here. As for why not NPM. Partially because if we do this ourselves we get exactly what we want, and it is more secure.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we going to remember to update this when changes are made to the spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm cued into W3C publications, I do keep an eye out for things like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be convinced either way. On the one had, theres https://github.com/bahamas10/css-color-names which auto-generates a single JSON list of colors (which is nicer than hand maintaining it). On the other hand, a list like this allows users to add their own so we aren't waiting on new names to work in all the browsers before we add it.

Personally, if we don't care about users being able to customize this list then we should probably just grab it from that npm package so we don't have to worry about it.

Copy link
Member

@stephenmathieson stephenmathieson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced that we're any more secure by not using npm packages for this. We (or someone else?) could vet any packages we rely on and we could use strict versioning to ensure the vetted version is always installed. We'd have "dependabot" (or alike) warn us if a security concern was found, and the dependency would be "bundled" into axe itself. The way our build works would prevent any additional trips to the npm registry for the end-user.

Can you please clarify the security concern?

If you'd rather not discuss this on your PR, I'm happy to open an issue to hold this conversation.

straker
straker previously requested changes Jul 7, 2020
@@ -0,0 +1,147 @@
// Source: https://www.w3.org/TR/css-color-3/#svg-color
const cssColors = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be convinced either way. On the one had, theres https://github.com/bahamas10/css-color-names which auto-generates a single JSON list of colors (which is nicer than hand maintaining it). On the other hand, a list like this allows users to add their own so we aren't waiting on new names to work in all the browsers before we add it.

Personally, if we don't care about users being able to customize this list then we should probably just grab it from that npm package so we don't have to worry about it.

@@ -38,8 +42,30 @@ function Color(red, green, blue, alpha) {
);
};

var rgbRegex = /^rgb\((\d+), (\d+), (\d+)\)$/;
var rgbaRegex = /^rgba\((\d+), (\d+), (\d+), (\d*(\.\d+)?)\)/;
const rgbRegex = /^rgb\(([0-9.]+),\s*([0-9.]+),\s*([0-9.]+)\)$/i;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can rgb values be decimals?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean floating point number? Yeah, they can. I didn't know that either until I looked into this.

Comment on lines 45 to 46
const rgbRegex = /^rgb\(([0-9.]+),\s*([0-9.]+),\s*([0-9.]+)\)$/i;
const rgbaRegex = /^rgba\(([0-9.]+),\s*([0-9.]+),\s*([0-9.]+),\s*([0-9.]*(\.[0-9.]+)?)\)$/i;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal but we could combine the two different regexs into one. This will match rgb values as well as rgba values and capture all 4 groups.

Suggested change
const rgbRegex = /^rgb\(([0-9.]+),\s*([0-9.]+),\s*([0-9.]+)\)$/i;
const rgbaRegex = /^rgba\(([0-9.]+),\s*([0-9.]+),\s*([0-9.]+),\s*([0-9.]*(\.[0-9.]+)?)\)$/i;
const rgbRegex = /^rgba?\(([\d.]+)\,\s*([\d.]+),\s*([\d.]+),?(?:\)|\s*([\d.]+)\))/
'rgb(12, 14, 1)'.match(rgbRegex); // [rgb(12, 14, 1), 12, 14, 1, undefined]
'rgba(255, 1, 24, 0.8)'.match(rgbRegex); // [rgba(255, 1, 24, 0.8), 255, 1, 24, 0.8]

lib/commons/color/get-text-shadow-colors.js Outdated Show resolved Hide resolved
lib/commons/color/get-text-shadow-colors.js Show resolved Hide resolved
lib/commons/color/get-text-shadow-colors.js Show resolved Hide resolved
lib/commons/color/get-text-shadow-colors.js Outdated Show resolved Hide resolved
lib/commons/color/get-text-shadow-colors.js Show resolved Hide resolved
function blurRadiusToAlpha(blurRadius) {
// This formula is an estimate based on various tests.
// Different people test this differently, so opinions may vary.
return 3.7 / (blurRadius + 8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you leave a comment in the code explaining the formula and magic numbers? If we needed to adjust this formula based on user feedback, what would we need to adjust to get the desired results?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this resolved? I wouldn't be able to change this formula if we had to adjust it based on user feedback. Where does 3.7 and blurRadius + 8 come from? I know it was based on testing, if they are truly magic then maybe include the results of those tests so we can better gauge how to adjust this in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Afraid I don't have the data anymore, sorry. These estimates are pretty rough.

test/commons/color/get-text-shadow-colors.js Show resolved Hide resolved
Copy link
Member

@stephenmathieson stephenmathieson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't want this to get lost. Are we not worried about hsl() and hsla() colors?

@WilcoFiers WilcoFiers changed the title fix(color-contrast): account for text-shadow [WIP] fix(color-contrast): account for text-shadow Jul 15, 2020
@WilcoFiers WilcoFiers changed the title [WIP] fix(color-contrast): account for text-shadow fix(color-contrast): account for text-shadow Jul 17, 2020
@WilcoFiers WilcoFiers requested a review from straker July 17, 2020 14:37
@WilcoFiers WilcoFiers dismissed straker’s stale review July 17, 2020 14:37

Ready for another review, me thinks

function blurRadiusToAlpha(blurRadius) {
// This formula is an estimate based on various tests.
// Different people test this differently, so opinions may vary.
return 3.7 / (blurRadius + 8);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this resolved? I wouldn't be able to change this formula if we had to adjust it based on user feedback. Where does 3.7 and blurRadius + 8 come from? I know it was based on testing, if they are truly magic then maybe include the results of those tests so we can better gauge how to adjust this in the future.

@@ -1,5 +1,18 @@
describe('color-contrast code highlighting test', function() {
'use strict';
var results;

before(function(done) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will undo the changes I made to fix the flaky test. The before that waits for the window load event needs to happen first before we run axe on the file.

@WilcoFiers WilcoFiers merged commit 3eb6d2c into develop Jul 20, 2020
@WilcoFiers WilcoFiers deleted the text-shadow branch July 20, 2020 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants