PageRenderTime 142ms CodeModel.GetById 40ms app.highlight 74ms RepoModel.GetById 17ms app.codeStats 1ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/jscoverage-overlay.js

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
JavaScript | 211 lines | 166 code | 18 blank | 27 comment | 22 complexity | 2e43bb66a482b49e20eab1ce61b7edb6 MD5 | raw file
  1/*
  2    jscoverage-overlay.js - script for XUL overlay
  3    Copyright (C) 2008 siliconforks.com
  4
  5    This program is free software; you can redistribute it and/or modify
  6    it under the terms of the GNU General Public License as published by
  7    the Free Software Foundation; either version 2 of the License, or
  8    (at your option) any later version.
  9
 10    This program is distributed in the hope that it will be useful,
 11    but WITHOUT ANY WARRANTY; without even the implied warranty of
 12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13    GNU General Public License for more details.
 14
 15    You should have received a copy of the GNU General Public License along
 16    with this program; if not, write to the Free Software Foundation, Inc.,
 17    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 18*/
 19
 20Components.utils.import('resource://gre/modules/jscoverage.jsm');
 21
 22// https://developer.mozilla.org/en/Code_snippets/Tabbed_browser
 23function openAndReuseOneTabPerURL(url) {
 24  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
 25                     .getService(Components.interfaces.nsIWindowMediator);
 26  var browserEnumerator = wm.getEnumerator("navigator:browser");
 27
 28  // Check each browser instance for our URL
 29  var found = false;
 30  while (!found && browserEnumerator.hasMoreElements()) {
 31    var browserWin = browserEnumerator.getNext();
 32    var tabbrowser = browserWin.getBrowser();
 33
 34    // Check each tab of this browser instance
 35    var numTabs = tabbrowser.browsers.length;
 36    for(var index=0; index<numTabs; index++) {
 37      var currentBrowser = tabbrowser.getBrowserAtIndex(index);
 38      if (url == currentBrowser.currentURI.spec) {
 39
 40        // The URL is already opened. Select this tab.
 41        tabbrowser.selectedTab = tabbrowser.mTabs[index];
 42
 43        // Focus *this* browser-window
 44        browserWin.focus();
 45
 46        found = true;
 47        break;
 48      }
 49    }
 50  }
 51
 52  // Our URL isn't open. Open it now.
 53  if (!found) {
 54    var recentWindow = wm.getMostRecentWindow("navigator:browser");
 55    if (recentWindow) {
 56      // Use an existing browser window
 57      recentWindow.delayedOpenTab(url, null, null, null, null);
 58    }
 59    else {
 60      // No browser windows are open, so open a new one.
 61      window.open(url);
 62    }
 63  }
 64}
 65
 66function jscoverage_view() {
 67  openAndReuseOneTabPerURL('chrome://jscoverage/content/jscoverage.html');
 68}
 69
 70function jscoverage_pad(s) {
 71  return '0000'.substr(s.length) + s;
 72}
 73
 74function jscoverage_quote(s) {
 75  return '"' + s.replace(/[\u0000-\u001f"\\\u007f-\uffff]/g, function (c) {
 76    switch (c) {
 77    case '\b':
 78      return '\\b';
 79    case '\f':
 80      return '\\f';
 81    case '\n':
 82      return '\\n';
 83    case '\r':
 84      return '\\r';
 85    case '\t':
 86      return '\\t';
 87    case '\v':
 88      return '\\v';
 89    case '"':
 90      return '\\"';
 91    case '\\':
 92      return '\\\\';
 93    default:
 94      return '\\u' + jscoverage_pad(c.charCodeAt(0).toString(16));
 95    }
 96  }) + '"';
 97}
 98
 99function jscoverage_store() {
100  try {
101    const Cc = Components.classes;
102    const Ci = Components.interfaces;
103
104    var directoryService = Cc['@mozilla.org/file/directory_service;1'].getService(Ci.nsIProperties);
105    var reportDirectory = directoryService.get('CurWorkD', Ci.nsILocalFile);
106    reportDirectory.appendRelativePath('jscoverage-report');
107    if (! reportDirectory.exists()) {
108      reportDirectory.create(Ci.nsIFile.DIRECTORY_TYPE, 0755);
109    }
110
111    var ioService = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
112    var copyChrome = function(filename) {
113      var channel = ioService.newChannel('chrome://jscoverage/content/' + filename, null, null);
114      var binaryInputStream = Cc['@mozilla.org/binaryinputstream;1'].createInstance(Ci.nsIBinaryInputStream);
115      try {
116        binaryInputStream.setInputStream(channel.open());
117
118        var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
119        file.initWithFile(reportDirectory);
120        file.appendRelativePath(filename);
121        var fileOutputStream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
122        fileOutputStream.init(file, 0x02 | 0x08 | 0x20, 0644, 0);
123        var binaryOutputStream = Cc['@mozilla.org/binaryoutputstream;1'].createInstance(Ci.nsIBinaryOutputStream);
124        try {
125          binaryOutputStream.setOutputStream(fileOutputStream);
126
127          for (;;) {
128            var available = binaryInputStream.available();
129            if (available === 0) {
130              break;
131            }
132            var bytes = binaryInputStream.readBytes(available);
133            binaryOutputStream.writeBytes(bytes, bytes.length);
134          }
135
136          if (filename === 'jscoverage.js') {
137            var s = 'jscoverage_isReport = true;\n';
138            binaryOutputStream.write(s, s.length);
139          }
140        }
141        finally {
142          binaryOutputStream.close();
143        }
144      }
145      finally {
146        binaryInputStream.close();
147      }
148    };
149    copyChrome('jscoverage.html');
150    copyChrome('jscoverage.js');
151    copyChrome('jscoverage.css');
152    copyChrome('jscoverage-throbber.gif');
153    copyChrome('jscoverage-highlight.css');
154
155    // write the coverage data
156    var file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
157    file.initWithFile(reportDirectory);
158    file.appendRelativePath('jscoverage.json');
159    var fileOutputStream = Cc['@mozilla.org/network/file-output-stream;1'].createInstance(Ci.nsIFileOutputStream);
160    try {
161      fileOutputStream.init(file, 0x02 | 0x08 | 0x20, 0644, 0);
162      function write(s) {
163        fileOutputStream.write(s, s.length);
164      }
165      write('{');
166      var first = true;
167      for (var file in _$jscoverage) {
168        if (first) {
169          first = false;
170        }
171        else {
172          write(',');
173        }
174        write(jscoverage_quote(file));
175        write(':{"coverage":[');
176        var coverage = _$jscoverage[file];
177        var length = coverage.length;
178        for (var line = 0; line < length; line++) {
179          if (line > 0) {
180            write(',');
181          }
182          var value = coverage[line];
183          if (value === undefined || value === null) {
184            value = 'null';
185          }
186          write(value.toString());
187        }
188        write('],"source":[');
189        var source = coverage.source;
190        length = source.length;
191        for (line = 0; line < length; line++) {
192          if (line > 0) {
193            write(',');
194          }
195          write(jscoverage_quote(source[line]));
196        }
197        write(']}');
198      }
199      write('}');
200      alert('Coverage data stored.');
201    }
202    finally {
203      fileOutputStream.close();
204    }
205  }
206  catch (e) {
207    alert(e);
208    dump(e);
209    dump('\n');
210  }
211}