-
Notifications
You must be signed in to change notification settings - Fork 74
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 #118: Improve isVisible
for correctness and performance
#116
Changes from 2 commits
68ce4cd
335657e
cfd7a50
c912f13
23f86cf
c0a81db
b96c4e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -460,11 +460,9 @@ export function sigmoid(x) { | |
/** | ||
* Return whether an element is practically visible, considering things like 0 | ||
* size or opacity, ``visibility: hidden`` and ``overflow: hidden``. | ||
* | ||
* This could be 5x more efficient if https://github.com/w3c/csswg-drafts/issues/4122 | ||
* happens. | ||
*/ | ||
export function isVisible(fnodeOrElement) { | ||
// This could be 5x more efficient if https://github.com/w3c/csswg-drafts/issues/4122 happens. | ||
const element = toDomElement(fnodeOrElement); | ||
// Avoid reading ``display: none`` due to Bug 1381071 | ||
const elementRect = element.getBoundingClientRect(); | ||
|
@@ -475,7 +473,14 @@ export function isVisible(fnodeOrElement) { | |
if (elementStyle.visibility === 'hidden') { | ||
return false; | ||
} | ||
// Check if the element is off-screen | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was saying to Daniel the other day that I like to punctuate comments like sentences if they are, in fact, sentences. I think it's influence from https://www.python.org/dev/peps/pep-0008/#comments. But on comments like this that take up the whole line and refer to code below, I often stick a colon afterward. That way, even if you don't skip a line before, it's clear you're talking about code only below it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can get behind the complete sentence thing. Not sure if I will remember your preference around colons, but I'll try to keep that in mind...I added a colon. |
||
const frame = element.ownerDocument.defaultView; | ||
if (elementRect.x + elementRect.width < 0 | ||
|| (elementRect.y + elementRect.height) < 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why parens here but not in the sum above? Also, if you put the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to add an eslint rule on parens? e.g. It sounds like you do have an opinion about operators in conditional statements, so I added an eslint rule for that in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see a benefit in making everybody memorize the precedence of every little operator, which would seem to be a requirement for no-extra-parens. I'm just looking for consistency. When I see inconsistency, I think "Aha, the author is trying to show me that something is different about this other case," and I waste time looking for it. That's all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As for the operator placement, I was just trying to make the very similar lines line up for faster scanning. |
||
|| (elementRect.x > frame.innerWidth || elementRect.y > frame.innerHeight) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems like the parens around this pair of conditions shouldn't be here, since you didn't put them around the top pair (and they don't affect the logic). |
||
) { | ||
return false; | ||
} | ||
for (const ancestor of ancestors(element)) { | ||
const isElement = ancestor === element; | ||
const style = isElement ? elementStyle : getComputedStyle(ancestor); | ||
|
@@ -493,10 +498,6 @@ export function isVisible(fnodeOrElement) { | |
// has overflow: hidden | ||
return false; | ||
} | ||
// Check if the element is off-screen | ||
if (isElement && ((rect.right + frame.scrollX < 0) || (rect.bottom + frame.scrollY < 0))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
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.
Great place for knowledge like this.