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): improve speed and accuracy of code blocks with syntax highlighting #2003

Merged
merged 6 commits into from
Jan 31, 2020

Conversation

straker
Copy link
Contributor

@straker straker commented Jan 28, 2020

Code blocks with syntax highlighting could have large amounts of client rects, which would then cause it to take a few seconds each to process. Code blocks could also cause us lots of incomplete results with "partially obscured by another element."

The code changes fix the speed by focusing only on the client rects of the text nodes of the element rather than all client rects. Since each of the child nodes that have text nodes will be checked individually, there is no need for the main parent to also check those nodes as well. This reduced the large code block from 2000+ client rects to just <20, taking color contrast from running in 10+ seconds to <1 seconds.

It also adjusts the collision check to account for elements which lie exactly on the middle of the element being checked. This solved all the incomplete issues and they now return as proper pass/fail.

Closes issue: #1985

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

@straker straker requested a review from a team as a code owner January 28, 2020 23:01
@straker straker changed the title fix(color-contrast): improve speed and accuracy of code block with syntax highlighting fix(color-contrast): improve speed and accuracy of code blocks with syntax highlighting Jan 28, 2020
@@ -14,8 +14,8 @@ describe('color.getForegroundColor', function() {

it('should return the blended color if it has alpha set', function() {
fixture.innerHTML =
'<div style="height: 40px; width: 30px; background-color: #800000;">' +
'<div id="target" style="height: 20px; width: 15px; color: rgba(0, 0, 128, 0.5);' +
'<div style="height: 40px; background-color: #800000;">' +
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 had to update these tests as my code changes made them more accurate than before. Before my changes, we would test only the first line of this fixture. However, if we look at the result of the fixture, we can see that there should be 3 background colors and 3 foreground colors, but we only calculate 1.

image

So I updated the tests to just focus on the single color with alpha/opacity.

Copy link
Contributor

@jeeyyy jeeyyy left a comment

Choose a reason for hiding this comment

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

👍

@straker walked me through the changes, when debugging FF & IE.
This looks good.

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