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 #118: Improve isVisible for correctness and performance #116

Merged
merged 7 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ rules:
no-var: warn
no-warning-comments: [warn, {terms: [xxx, fixme, hack], location: start}]
object-shorthand: [error, properties]
operator-linebreak: [error, after]
prefer-const: off
quotes: [error, single, {avoidEscape: true, allowTemplateLiterals: true}]
semi: [error, always]
Expand Down
22 changes: 11 additions & 11 deletions utilsForFrontend.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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') &&
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

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
Expand Down Expand Up @@ -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') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be elementStyle.overflow === 'hidden'? As you say below, "Zero-sized ancestors don’t make descendants hidden unless the descendant has overflow: hidden".

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The check you're thinking of is done inside the for loop below. This first check is a proxy for checking display: none, for which just checking width and height === 0 is not sufficient as I learned while working on #122 .

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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;
}
Expand All @@ -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;
}
}
Expand Down