kangax / cft

Common Feature Tests

This URL has Read+Write access

cft / master.js
100644 156 lines (143 sloc) 4.65 kb
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
(function(){
  
  if (!('getElementsByClassName' in document)) {
    document.getElementsByClassName = function(className) {
      var all = document.getElementsByTagName('*');
      if (all.length === 0 && 'all' in document) {
        // IE5.5
        all = document.all;
      }
      if (!className) return all;
      var re = new RegExp('(^|\\s)' + className + '(\\s|$)');
      var result = [];
      for (var i=0, len=all.length; i<len; i++) {
        if (re.test(all[i].className)) {
          result.push(all[i]);
        }
      }
      return result;
    }
  }
  
  function toggleCodeSection(trigger, action) {
    var parent = trigger.parentNode, firstCell, testName;
    if (parent) {
      parent = parent.parentNode;
      if (parent) {
        firstCell = parent.childNodes[1];
        if (firstCell) {
          testName = firstCell.firstChild.nodeValue;
        }
      }
    }
    if (testName && parent) {
      var next = parent.nextSibling;
      while (next && next.nodeType !== 1) {
        next = next.nextSibling;
      }
      if (!next) return;
      
      if (typeof action !== 'undefined') {
        if (action === 'hide') {
          next.style.display = 'none';
          trigger.firstChild.nodeValue = 'show';
          parent.style.backgroundColor = '';
        }
        else if (action === 'show') {
          next.style.display = '';
          trigger.firstChild.nodeValue = 'hide';
          parent.style.backgroundColor = '#ffc';
        }
        return;
      }
      
      if (next.style.display === 'none') {
        next.style.display = '';
        trigger.firstChild.nodeValue = 'hide';
        parent.style.backgroundColor = '#ffc';
      }
      else if (next.style.display !== 'none') {
        next.style.display = 'none';
        trigger.firstChild.nodeValue = 'show';
        parent.style.backgroundColor = '';
      }
    }
  }
  
  
  function populateTable(tbl) {
    var table = document.getElementById(tbl);
    var rows = table.tBodies[0].rows;
    for (var i=0, len=rows.length; i<len; i++) {
      var name = rows[i].cells[0].innerHTML;
      var testResult = name in __features ? __features[name] : __bugs[name];
      var value = String(testResult === null ? 'N/A' : testResult)
      if (rows[i].cells[1]) {
        rows[i].cells[1].appendChild(document.createTextNode((value + '').toUpperCase()));
        rows[i].cells[1].className = (value === 'N/A' ? 'na' : value);
      }
      var next = rows[i].nextSibling;
      while (next && next.nodeType !== 1) {
        next = next.nextSibling;
      }
      if (!next) return;
      var cell = next.cells[0];
      if (!cell) return;
      var placeholder = cell.getElementsByTagName('pre')[0];
      if (!placeholder) return;
      name = ('__' + name);
      var value = name in __features ? __features[name] : __bugs[name];
      // transform leading 4 spaces to 2
      value = (value + '').replace(/\n /g, '\n');
      if (__bugs.PRE_ELEMENTS_IGNORE_NEWLINES) {
        value = value.replace(/\n/g, '\n\r');
      }
 
      placeholder.appendChild(document.createTextNode(value));
      i++;
    }
  };
 
  populateTable('cft-data');
  populateTable('cft-data-bugs');
  
  document.documentElement.onclick = function(e) {
    if (!e) e = window.event;
    if (!e) return;
    var target = ('target' in e) ? e.target : e.srcElement;
    
    if (target && /^a$/i.test(target.nodeName)) {
      if (target.className === 'show-test-code') {
        toggleCodeSection(target);
      }
      else if (target.className === 'show-all') {
        var all = document.getElementsByClassName('show-test-code');
        for (var i=0, len=all.length; i<len; i++) {
          toggleCodeSection(all[i], 'show');
        }
      }
      else if (target.className === 'hide-all') {
        var all = document.getElementsByClassName('show-test-code');
        for (var i=0, len=all.length; i<len; i++) {
          toggleCodeSection(all[i], 'hide');
        }
      }
      else {
        return true;
      }
      if (e.preventDefault) {
        e.preventDefault();
      }
      if (e.stopPropagation) {
        e.stopPropagation();
      }
      return false;
    }
    return true;
  }
  
  if (document.body) {
    var p = document.createElement('p');
    p.className = 'total-time';
    p.innerHTML = ('Finished all tests in: <strong>' + __totalTime + '</strong>ms');
    document.body.appendChild(p);
  }
  
  var hash = document.location.hash.slice(1);
  var links = document.anchors, i = links.length;
  while (i--) {
    if (links[i].name === hash) {
      toggleCodeSection(links[i]);
      break;
    }
  }
  
  window.scrollTo(0,0);
})();