<commit_before>"use strict";
/*globals Sass, setTimeout, clearTimeout, document, require, console */

var ace = require("brace");
require("brace/mode/javascript");
require("brace/mode/css");
require("brace/mode/scss");
require("brace/mode/html");
require("brace/theme/monokai");
var babel = require("babel");
var xhr = require("xhr");
var autoprefixer = require("autoprefixer-core");

var chapters = require("./chapters");

var es5Editor, es6Editor, scssEditor, cssEditor, htmlEditor;

//var oldConsole = console;
var consoleOutput = document.getElementById("consoleOutput");

var chapterSelect = document.getElementById("chapters"),
    exampleSelect = document.getElementById("examples");

function Console() {
    var console = this;

    function _print(level, data) {
        var div = document.createElement("div");
        div.textContent = "[" + level + "] " + JSON.stringify(data, null, 2);
        consoleOutput.appendChild(div);
    }
    ["log", "info", "debug", "warn", "error" ].forEach(function(level) {
        console[level] = _print.bind(console, level);
    });
    return console;
}
function clearConsole() {
    consoleOutput.innerHTML = "";
}

function populateEditor(err, resp, data) {
    if (!err) {
        this.setValue(data);
        this.clearSelection();
        this.scrollToLine(0);
    }
}

function renderResults() {
    var selectedChapter = chapterSelect.selectedIndex,
        selectedExample = exampleSelect.selectedIndex,
        example = chapters[selectedChapter].examples[selectedExample];

    var el = document.createElement("iframe");
    var resultsFrame = document.getElementById("results");
    var resultsParent = resultsFrame.parentElement;
    el.setAttribute("id", "results");
    resultsParent.removeChild(resultsFrame);
    resultsParent.appendChild(el);
    setTimeout( function() {
        var doc = el.contentDocument,
            docStyle, docScript, docHead, docBody;

        var docBase = example && example.base;

        clearConsole();

        doc.open();

        doc.addEventListener("DOMContentLoaded", function installStylesAndScripts() {
            doc.removeEventListener("DOMContentLoaded", installStylesAndScripts);
            docHead = doc.getElementsByTagName("head")[0];
            docBody = doc.getElementsByTagName("body")[0];

            docStyle = doc.createElement("style");
            docScript = doc.createElement("script");

            docStyle.textContent = cssEditor.getValue();
            docScript.textContent = ";(function () { try { " + es5Editor.getValue() + "\n } catch(err) { console.log(err.message); } })();";
            docScript.setAttribute("type", "text/javascript");

            docHead.appendChild(docStyle);

            // before we insert our script, we need to process any extant script tags in the HTML
            var docScripts = [].slice.call(doc.getElementsByTagName("script"), 0),
                sN, pN, nSN;
            for (var i = 0, l = docScripts.length; i < l; i++) {
                sN = docScripts[i];
                pN = sN.parentNode;
                pN.removeChild(sN);
                nSN = doc.createElement("script");
                nSN.setAttribute("src", sN.getAttribute("src"));
                pN.appendChild(nSN);
            }
            docBody.appendChild(docScript);

            setTimeout(function() {
                var deviceReady = doc.createEvent("Event");
                deviceReady.initEvent("deviceready", true, true);
                doc.dispatchEvent(deviceReady);
            }, 500);
        }, false);

        doc.write(htmlEditor.getValue().replace("<!--base-->", "<base href='" + docBase + "/' />"));
        doc.close();

        setTimeout(function() {
            document.getElementById("results").contentWindow.console = new Console();
        }, 0);
    }, 0);
}

function configureSelectElements() {

    function addOptions(selectEl, arr) {
        arr.map(function(item) {
            var optionEl = document.createElement("option");
            optionEl.textContent = item.name;
            return optionEl;
        }).forEach(function(optionEl) {
            selectEl.appendChild(optionEl);
        });
    }

    function loadOrClear(load, uri, editor) {
        if (load) {
            xhr({
                uri: uri
            }, populateEditor.bind(editor));
        } else {
            editor.setValue("");<commit_msg>Fix docbase; only send deviceready if cordova isn't loaded; ignore script tags (they seem to load)<commit_after>"use strict";
/*globals Sass, setTimeout, clearTimeout, document, require, console */

var ace = require("brace");
require("brace/mode/javascript");
require("brace/mode/css");
require("brace/mode/scss");
require("brace/mode/html");
require("brace/theme/monokai");
var babel = require("babel");
var xhr = require("xhr");
var autoprefixer = require("autoprefixer-core");

var chapters = require("./chapters");

var es5Editor, es6Editor, scssEditor, cssEditor, htmlEditor;

//var oldConsole = console;
var consoleOutput = document.getElementById("consoleOutput");

var chapterSelect = document.getElementById("chapters"),
    exampleSelect = document.getElementById("examples");

function Console() {
    var console = this;

    function _print(level, data) {
        var div = document.createElement("div");
        div.textContent = "[" + level + "] " + JSON.stringify(data, null, 2);
        consoleOutput.appendChild(div);
    }
    ["log", "info", "debug", "warn", "error" ].forEach(function(level) {
        console[level] = _print.bind(console, level);
    });
    return console;
}
function clearConsole() {
    consoleOutput.innerHTML = "";
}

function populateEditor(err, resp, data) {
    if (!err) {
        this.setValue(data);
        this.clearSelection();
        this.scrollToLine(0);
    }
}

function _renderResults() {

    console.log("[INFO] Rendering code changes");

    var selectedChapter = chapterSelect.selectedIndex,
        selectedExample = exampleSelect.selectedIndex,
        example = chapters[selectedChapter].examples[selectedExample];

    var el = document.createElement("iframe");
    var resultsFrame = document.getElementById("results");
    var resultsParent = resultsFrame.parentElement;
    el.setAttribute("id", "results");
    resultsParent.removeChild(resultsFrame);
    resultsParent.appendChild(el);
    setTimeout( function() {
        var doc = el.contentDocument,
            win = el.contentWindow,
            docStyle, docScript, docHead, docBody;

        var docBase = example && example.base;
        if (docBase) {
            if (docBase.substr(docBase.length-1) !== "/") {
                docBase += "/";
            }
        }

        clearConsole();

        doc.open();

        doc.addEventListener("DOMContentLoaded", function installStylesAndScripts() {
            doc.removeEventListener("DOMContentLoaded", installStylesAndScripts);
            docHead = doc.getElementsByTagName("head")[0];
            docBody = doc.getElementsByTagName("body")[0];

            docStyle = doc.createElement("style");
            docScript = doc.createElement("script");

            docStyle.textContent = cssEditor.getValue();
            docScript.textContent = ";(function () { try { " + es5Editor.getValue() + "\n } catch(err) { console.log(err.message); } })();";
            docScript.setAttribute("type", "text/javascript");

            docHead.appendChild(docStyle);
/*
            // before we insert our script, we need to process any extant script tags in the HTML
            var docScripts = [].slice.call(doc.getElementsByTagName("script"), 0),
                sN, pN, nSN;
            for (var i = 0, l = docScripts.length; i < l; i++) {
                sN = docScripts[i];
                pN = sN.parentNode;
                pN.removeChild(sN);
                nSN = doc.createElement("script");
                nSN.setAttribute("src", sN.getAttribute("src"));
                pN.appendChild(nSN);

                console.log("Inserted script tag with " + sN.getAttribute("src"));
            } */
            docBody.appendChild(docScript);

            setTimeout(function() {
                if (!win.cordova) {
                    // simulate Cordova deviceReady event, but only if cordova isn't defined
                    var deviceReady = doc.createEvent("Event");
                    deviceReady.initEvent("deviceready", true, true);
                    doc.dispatchEvent(deviceReady);
                }
            }, 500);
        }, false);

        doc.write(htmlEditor.getValue().replace("<!--base-->", "<base href='" + docBase + "' />"));
        doc.close();

        setTimeout(function() {
            document.getElementById("results").contentWindow.console = new Console();
        }, 0);
    }, 0);
}

var renderTimer;
function renderResults() {
    if (renderTimer) {
        console.log("[INFO] Delaying render");
        clearTimeout(renderTimer);
        renderTimer = null;
    }
    renderTimer = setTimeout(function() {
        renderTimer = null;
        _renderResults();
    }, 1000);
}

function configureSelectElements() {

    function addOptions(selectEl, arr) {
        arr.map(function(item) {
            var optionEl = document.createElement("option");
            optionEl.textContent = item.name;
            return optionEl;
        }).forEach(function(optionEl) {
            selectEl.appendChild(optionEl);
        });
    }

    function loadOrClear(load, uri, editor) {
        if (load) {
            xhr({
                uri: uri
            }, populateEditor.bind(editor));
        } else {
            editor.setValue("");
{'fraction_non_alphanumeric': 0.13871635610766045, 'fraction_numerical': 0.0034161490683229812, 'mean_word_length': 2.533650329188003, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7415627', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2565, 'n_words': 538}