/*!
 * Nodeunit
 * Copyright (c) 2010 Caolan McMahon
 * MIT Licensed
 *
 * THIS FILE SHOULD BE BROWSER-COMPATIBLE JS!
 * You can use @REMOVE_LINE_FOR_BROWSER to remove code from the browser build.
 * Only code on that line will be removed, its mostly to avoid requiring code
 * that is node specific
 */


/**
 * NOTE: this test runner is not listed in index.js because it cannot be
 * used with the command-line tool, only inside the browser.
 */


/**
 * Reporter info string
 */

exports.info = "Browser-based test reporter";


exports.addStyles = function () {
    document.body.innerHTML += '<style type="text/css">' +
        'body { font: 12px Helvetica Neue }' +
        'h2 { margin:0 ; padding:0 }' +
        'pre {' +
            'font: 11px Andale Mono;' +
            'margin-left: 1em;' +
            'padding-left: 1em;' +
            'margin-top: 0;' +
            'font-size:smaller;' +
        '}' +
        '.assertion_message { margin-left: 1em; }' +
        '  ol {' +
            'list-style: none;' +
            'margin-left: 1em;' +
            'padding-left: 1em;' +
            'text-indent: -1em;' +
        '}' +
        '  ol li.pass:before { content: "\\2714 \\0020"; }' +
        '  ol li.fail:before { content: "\\2716 \\0020"; }' +
    '</style>';
};


/**
 * Run all tests within each module, reporting the results
 *
 * @param {Array} files
 * @api public
 */

exports.run = function (modules, options) {
    var start = new Date().getTime();
    exports.addStyles();

    var html = '';
    nodeunit.runModules(modules, {
        moduleStart: function (name) {
            html += '<h2>' + name + '</h2>';
            html += '<ol>';
        },
        testDone: function (name, assertions) {
            if (!assertions.failures()) {
                html += '<li class="pass">' + name + '</li>';
            }
            else {
                html += '<li class="fail">' + name;
                for (var i=0; i<assertions.length; i++) {
                    var a = assertions[i];
                    if (a.failed()) {
                        if (a.error instanceof assert.AssertionError && a.message) {
                            html += '<div class="assertion_message">' +
                                'Assertion Message: ' + a.message +
                            '</div>';
                        }
                        html += '<pre>';
                        html += a.error.stack || a.error;
                        html += '</pre>';
                    }
                };
                html += '</li>';
            }
        },
        moduleDone: function () {
            html += '</ol>';
        },
        done: function (assertions) {
            var end = new Date().getTime();
            var duration = end - start;
            if (assertions.failures()) {
                html += '<h3>FAILURES: '  + assertions.failures() +
                    '/' + assertions.length + ' assertions failed (' +
                    assertions.duration + 'ms)</h3>';
            }
            else {
                html += '<h3>OK: ' + assertions.length +
                    ' assertions (' + assertions.duration + 'ms)</h3>';
            }
            document.body.innerHTML += html;
        }
    });
};

@@ -1510,164 +1510,443 @@
 var 
-html = '';%0A    nodeunit.runModules(modules, %7B%0A        moduleStart: function (name) %7B%0A            html += '%3Ch2%3E' + name + '%3C/h2%3E';%0A            html += '%3Col%3E'
+results, module;%0A%0A    results = document.createElement('div');%0A    results.id = 'results';%0A    document.body.appendChild(results);%0A%0A    nodeunit.runModules(modules, %7B%0A        moduleStart: function (name) %7B%0A            var mheading = document.createElement('h2');%0A            mheading.innerText = name;%0A            results.appendChild(mheading);%0A            module = document.createElement('ol');%0A            results.appendChild(module)
 ;%0A  
@@ -1994,32 +1994,85 @@
 , assertions) %7B%0A
+            var test = document.createElement('li');%0A
             if (
@@ -2117,132 +2117,172 @@
     
-html += '%3Cli class=%22pass%22%3E' + name + '%3C/li%3E';%0A            %7D%0A            else %7B%0A                html += '%3Cli class=%22fail%22%3E' +
+test.className = 'pass';%0A                test.innerText = name;%0A            %7D%0A            else %7B%0A                test.className = 'fail';%0A                var html =
  nam
@@ -2914,23 +2914,29 @@
     
-html += '%3C/li%3E'
+test.innerHTML = html
 ;%0A  
@@ -2947,31 +2947,24 @@
       %7D%0A    
-    %7D,%0A
         modu
@@ -2969,55 +2969,26 @@
 dule
-Done: function () %7B%0A            html += '%3C/ol%3E'
+.appendChild(test)
 ;%0A  
@@ -3117,16 +3117,73 @@
 - start;
+%0A%0A            var summary = document.createElement('h3');
 %0A       
@@ -3232,29 +3232,37 @@
         
-html += '%3Ch3%3E
+summary.innerText = '
 FAILURES
@@ -3401,37 +3401,32 @@
 .duration + 'ms)
-%3C/h3%3E
 ';%0A            %7D
@@ -3465,21 +3465,29 @@
     
-html += '%3Ch3%3E
+summary.innerText = '
 OK: 
@@ -3578,13 +3578,8 @@
 'ms)
-%3C/h3%3E
 ';%0A 
@@ -3621,25 +3621,28 @@
 ody.
-innerHTML += html
+appendChild(summary)
 ;%0A  

update browser test reporter to add test results to the page as they complete