1 jasmine.TrivialReporter = function(doc) {
  2   this.document = doc || document;
  3   this.suiteDivs = {};
  4 };
  5 
  6 jasmine.TrivialReporter.prototype.createDom = function(type, attrs, childrenVarArgs) {
  7   var el = document.createElement(type);
  8 
  9   for (var i = 2; i < arguments.length; i++) {
 10     var child = arguments[i];
 11 
 12     if (typeof child === 'string') {
 13       el.appendChild(document.createTextNode(child));
 14     } else {
 15       if (child) { el.appendChild(child); }
 16     }
 17   }
 18 
 19   for (var attr in attrs) {
 20     if (attr == 'className') {
 21       el.setAttribute('class', attrs[attr]);
 22     } else {
 23       el[attr] = attrs[attr];
 24     }
 25   }
 26 
 27   return el;
 28 };
 29 
 30 jasmine.TrivialReporter.prototype.reportRunnerStarting = function(runner) {
 31   var suites = runner.suites();
 32 
 33   this.runnerDiv = this.createDom('div', { className: 'runner running' },
 34       this.createDom('a', { className: 'runSpec', href: '?' }, "run all"),
 35       this.runnerMessageSpan = this.createDom('span', {}, "Running..."));
 36   this.document.body.appendChild(this.runnerDiv);
 37 
 38   for (var i = 0; i < suites.length; i++) {
 39     var suite = suites[i];
 40     var suiteDiv = this.createDom('div', { className: 'suite' },
 41         this.createDom('a', { className: 'runSpec', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, "run"),
 42         this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(suite.getFullName()) }, suite.description));
 43     this.suiteDivs[suite.getFullName()] = suiteDiv;
 44     var parentDiv = this.document.body;
 45     if (suite.parentSuite) {
 46       parentDiv = this.suiteDivs[suite.parentSuite.getFullName()];
 47     }
 48     parentDiv.appendChild(suiteDiv);
 49   }
 50 
 51   this.startedAt = new Date();
 52 };
 53 
 54 jasmine.TrivialReporter.prototype.reportRunnerResults = function(runner) {
 55   var results = runner.results();
 56   var className = (results.failedCount > 0) ? "runner failed" : "runner passed";
 57   this.runnerDiv.setAttribute("class", className);
 58   var specs = runner.specs();
 59   var specCount = 0;
 60   for (var i = 0; i < specs.length; i++) {
 61     if (this.specFilter(specs[i])) {
 62       specCount++;
 63     }
 64   }
 65   var message = "" + specCount + " spec" + (specCount == 1 ? "" : "s" ) + ", " + results.failedCount + " failure" + ((results.failedCount == 1) ? "" : "s");
 66   message += " in " + ((new Date().getTime() - this.startedAt.getTime()) / 1000) + "s";
 67   this.runnerMessageSpan.replaceChild(this.createDom('a', { className: 'description', href: '?'}, message), this.runnerMessageSpan.firstChild);
 68 };
 69 
 70 jasmine.TrivialReporter.prototype.reportSuiteResults = function(suite) {
 71   var results = suite.results();
 72   var status = results.passed() ? 'passed' : 'failed';
 73   if (results.totalCount == 0) { // todo: change this to check results.skipped
 74     status = 'skipped';
 75   }
 76   this.suiteDivs[suite.getFullName()].className += " " + status;
 77 };
 78 
 79 jasmine.TrivialReporter.prototype.reportSpecResults = function(spec) {
 80   var results = spec.results();
 81   var status = results.passed() ? 'passed' : 'failed';
 82   if (results.skipped) {
 83     status = 'skipped';
 84   }
 85   var specDiv = this.createDom('div', { className: 'spec '  + status },
 86       this.createDom('a', { className: 'runSpec', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, "run"),
 87       this.createDom('a', { className: 'description', href: '?spec=' + encodeURIComponent(spec.getFullName()) }, spec.getFullName()));
 88 
 89 
 90   var resultItems = results.getItems();
 91   for (var i = 0; i < resultItems.length; i++) {
 92     var result = resultItems[i];
 93     if (result.passed && !result.passed()) {
 94       var resultMessageDiv = this.createDom('div', {className: 'resultMessage fail'});
 95       resultMessageDiv.innerHTML = result.message; // todo: lame; mend
 96       specDiv.appendChild(resultMessageDiv);
 97       specDiv.appendChild(this.createDom('div', {className: 'stackTrace'}, result.trace.stack));
 98     }
 99   }
100 
101   this.suiteDivs[spec.suite.getFullName()].appendChild(specDiv);
102 };
103 
104 jasmine.TrivialReporter.prototype.log = function() {
105   console.log.apply(console, arguments);
106 };
107 
108 jasmine.TrivialReporter.prototype.getLocation = function() {
109   return this.document.location;
110 };
111 
112 jasmine.TrivialReporter.prototype.specFilter = function(spec) {
113   var paramMap = {};
114   var params = this.getLocation().search.substring(1).split('&');
115   for (var i = 0; i < params.length; i++) {
116     var p = params[i].split('=');
117     paramMap[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
118   }
119 
120   if (!paramMap["spec"]) return true;
121   return spec.getFullName().indexOf(paramMap["spec"]) == 0;
122 };
123 
124 //protect against console.log incidents
125 if (!("console" in window) || !("firebug" in console)) {
126   var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
127   window.console = {};
128   for (var i = 0, len = names.length; i < len; ++i) {
129     window.console[names[i]] = function() {
130     };
131   }
132 }
133