Skip to content

Commit

Permalink
Merge pull request #116 from mozilla/is-visible
Browse files Browse the repository at this point in the history
Merge `isVisible` improvement. Closes #116 .
  • Loading branch information
biancadanforth authored Aug 13, 2019
2 parents 8ad0376 + b96c4e3 commit 871728f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 21 deletions.
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') {
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

0 comments on commit 871728f

Please sign in to comment.