-
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 1 commit
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 |
---|---|---|
|
@@ -160,8 +160,8 @@ export function *inlineTexts(element, shouldTraverse = element => true) { | |
for (let child of walk(element, | ||
element => !(isBlock(element) || | ||
element.tagName === 'SCRIPT' && | ||
element.tagName === 'STYLE') | ||
&& shouldTraverse(element))) { | ||
element.tagName === 'STYLE') && | ||
shouldTraverse(element))) { | ||
if (child.nodeType === child.TEXT_NODE) { | ||
// wholeText() is not implemented by jsdom, so we use | ||
// textContent(). The result should be the same, since | ||
|
@@ -464,20 +464,20 @@ export function sigmoid(x) { | |
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(); | ||
if (elementRect.width === 0 && elementRect.height === 0) { | ||
const elementStyle = getComputedStyle(element); | ||
// Alternative to reading ``display: none`` due to Bug 1381071. | ||
if (elementRect.width === 0 && elementRect.height === 0 && elementStyle.overflow !== 'hidden') { | ||
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. Shouldn't this be 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. The check you're thinking of is done inside 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. Ah, so this is a check strictly for the visibility of the element passed in, not any of its descendents. A child, for example, could still be visible. Callers will have to be aware of this. That'd be worth documenting in the doclet, as I wasn't front-end-savvy enough to realize 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. Hm... I'm not sure I follow what you want me to document. In general, this method is to check if the element passed in is visible. It doesn't make any claims about that element's descendants. 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. We discussed this synchronously, and we're just going to leave off any additional comments for now. |
||
return false; | ||
} | ||
const elementStyle = getComputedStyle(element); | ||
if (elementStyle.visibility === 'hidden') { | ||
return false; | ||
} | ||
// Check if the element is off-screen | ||
// Check if the element is off-screen: | ||
const frame = element.ownerDocument.defaultView; | ||
if (elementRect.x + elementRect.width < 0 | ||
|| (elementRect.y + elementRect.height) < 0 | ||
|| (elementRect.x > frame.innerWidth || elementRect.y > frame.innerHeight) | ||
if (elementRect.x + elementRect.width < 0 || | ||
elementRect.y + elementRect.height < 0 || | ||
elementRect.x > frame.innerWidth || elementRect.y > frame.innerHeight | ||
) { | ||
return false; | ||
} | ||
|
@@ -488,14 +488,14 @@ export function isVisible(fnodeOrElement) { | |
return false; | ||
} | ||
if (style.display === 'contents') { | ||
// display: contents elements have no box themselves, but children are | ||
// ``display: contents`` elements have no box themselves, but children are | ||
// still rendered. | ||
continue; | ||
} | ||
const rect = isElement ? elementRect : ancestor.getBoundingClientRect(); | ||
if ((rect.width === 0 || rect.height === 0) && elementStyle.overflow === 'hidden') { | ||
// Zero-sized ancestors don’t make descendants hidden unless the descendant | ||
// has overflow: hidden | ||
// has ``overflow: hidden``. | ||
return false; | ||
} | ||
} | ||
|
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.
I think this actually make it harder to understand. I don't remember writing this (though I probably did), but here's how I read the original:
The ! lines up with the &&, suggesting that 2 clauses are ANDed together. That's what's actually going on.
When you move the 2nd &&, the 2 &&s line up exactly, and it starts looking like shouldTraverse is just one more clause in the earlier &&, unless you take the time to painstakingly balance the parens.
So I think the original indentation visually conveys the structure more clearly. Does that make sense?
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.
I see what you mean, though that prevents us from having an eslint rule to cover it, as leaving this as-is is not consistent (from eslint's perspective) with changing the other one.
I'm happy to change it back as you prefer, however.