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 all commits
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
2 changes: 1 addition & 1 deletion test/demos.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {assert} from 'chai';

import {dom, out, rule, ruleset, type} from '../index';
import {dom, rule, ruleset, type} from '../index';
import {sigmoid, staticDom} from '../utils';


Expand Down
2 changes: 1 addition & 1 deletion test/lhs_tests.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {assert} from 'chai';

import {dom, rule, ruleset, out, type} from '../index';
import {dom, rule, ruleset, type} from '../index';
import {staticDom} from '../utils';


Expand Down
2 changes: 1 addition & 1 deletion test/ruleset_tests.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {assert} from 'chai';

import {distance} from '../clusters';
import {and, dom, nearest, out, props, rule, ruleset, score, type} from '../index';
import {and, dom, nearest, props, rule, ruleset, score, type} from '../index';
import {domSort, sigmoid, staticDom} from '../utils';


Expand Down
2 changes: 1 addition & 1 deletion test/utils_tests.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {assert} from 'chai';
import {dom, out, rule, ruleset, score, type} from '../index';
import {dom, rule, ruleset, score, type} from '../index';
import {NiceSet, toposort, staticDom, attributesMatch} from '../utils';


Expand Down
51 changes: 34 additions & 17 deletions utilsForFrontend.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -458,28 +458,45 @@ export function sigmoid(x) {
}

/**
* Return whether an element is practically visible, considing things like 0
* size or opacity, ``display: none``, and ``visibility: hidden``.
* Return whether an element is practically visible, considering things like 0
* size or opacity, ``visibility: hidden`` and ``overflow: hidden``.
*/
export function isVisible(fnodeOrElement) {
// This could be 5x more efficient if https://github.com/w3c/csswg-drafts/issues/4122 happens.
const element = toDomElement(fnodeOrElement);
const elementRect = element.getBoundingClientRect();
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;
}
if (elementStyle.visibility === 'hidden') {
return false;
}
// 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
) {
return false;
}
for (const ancestor of ancestors(element)) {
const style = getComputedStyle(ancestor);
if (style.visibility === 'hidden' ||
style.display === 'none' ||
style.opacity === '0' ||
style.width === '0' ||
style.height === '0') {
const isElement = ancestor === element;
const style = isElement ? elementStyle : getComputedStyle(ancestor);
if (style.opacity === '0') {
return false;
}
if (style.display === 'contents') {
// ``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``.
return false;
} else {
// It wasn't hidden based on a computed style. See if it's
// offscreen:
const rect = element.getBoundingClientRect();
const frame = element.ownerDocument.defaultView; // window or iframe
if ((rect.right + frame.scrollX < 0) ||
(rect.bottom + frame.scrollY < 0)) {
return false;
}
}
}
return true;
Expand Down