aboutsummaryrefslogtreecommitdiffstats
path: root/test_harnesses/visibility_test.html
blob: 5fa4d728d2d565264ac8bbee77c26b92aae5274a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!--
  This demonstrates the effectiveness a method for testing for visibility elements on various conditions.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Visibility test</title>
  <style type="text/css" media="screen">
    span {
      display:block;
      width:30px;
      height:20px;
      background-color:blue;
    }
  </style>

  <script type="text/javascript" charset="utf-8">
    window.addEventListener("load", checkDivsForVisibility, false);
    
    function checkDivsForVisibility() {
      var divs = document.getElementsByTagName("span");
      for (var i = 0; i < divs.length; i++) {
        console.log(divs[i].id, isVisible(divs[i]));
      }
    }
    /*
     * Determine whether elements are visible.
     * Inspired by Vimperator.
     */
    function isVisible(element) {
      // eliminate offscreen elements (case 4)
      var rect = element.getBoundingClientRect();
      if (!rect || rect.top > window.innerHeight || rect.bottom < 0 || rect.left > window.innerWidth || rect.right < 0)
          return false;

      // this catches cases 2, 3, & 5
      var computedStyle = window.getComputedStyle(element, null);
      if (computedStyle.getPropertyValue('visibility') != 'visible' || 
          computedStyle.getPropertyValue('display') == 'none')
          return false;

      // this catches cases 3 & 6
      var clientRect = element.getClientRects()[0];
      if (!clientRect)
        return false;

      return true;
    }
  </script>
</head>

<body>
  <span id="div1true"></span>
  
  <span id="div2false" style="visibility:hidden"></span>

  <span id="div3false" style="display:none"></span>

  <span id="div4false" style="position:absolute;top:2000px"></span>

  <div style="visibility:hidden">
    <span id="div5false"></span>
  </div>
  <div style="display:none">
    <span id="div6false"></span>
  </div>
  <div style="opacity:0">
    <span id="div7true" style="opacity:0"></span>
  </div>
</html>