<commit_before>// Copyright (c) 2011-2012 UX Productivity Pty Ltd. All rights reserved.

function prettyPrintDocument()
{
    var clone = DOM.cloneNode(document.documentElement,true);
    removeRedundantWhitespace(clone,"");
    addIndentation(clone,"");
    return clone.outerHTML;

    function isWhitespaceChar(c)
    {
        return ((c == " ") || (c == "\t") || (c == "\r") || (c == "\n"));
    }

    // Note: This could potentially remove some whitespace that is important, e.g.
    // in the case of <b>one </b>two... it should only be used for debugging purposes
    function removeRedundantWhitespace(node,indent)
    {
        if (node.nodeType == Node.ELEMENT_NODE) {
            var next;
            for (var child = node.firstChild; child != null; child = next) {
                next = child.nextSibling;
                if (isWhitespaceTextNode(child)) {
                    DOM.deleteNode(child);
                }
                else {
                    removeRedundantWhitespace(child,indent+"    ");
                }
            }
        }
        else if (node.nodeType == Node.TEXT_NODE) {
            var str = node.nodeValue;
            var start = 0;
            var end = str.length;
            while ((start < end) && isWhitespaceChar(str.charAt(start)))
                start++;
            while ((end > start) && isWhitespaceChar(str.charAt(end-1)))
                end--;
            DOM.deleteCharacters(node,end);
            DOM.deleteCharacters(node,0,start);
        }
    }

    function addIndentation(node,indent)
    {
        if (node.nodeType == Node.ELEMENT_NODE) {
            var next;
            for (var child = node.firstChild; child != null; child = next) {
                next = child.nextSibling;
                var text = DOM.createTextNode(document,"\n"+indent+"    ");
                DOM.insertBefore(node,text,child);
                addIndentation(child,indent+"    ");
            }
            var text = DOM.createTextNode(document,"\n"+indent);
            DOM.appendChild(node,text);
        }
    }
}

function getDOMTree()
{
    var root = convertToObjects(document.documentElement);
    return JSON.stringify(root);

    function convertToObjects(node)
    {
        var obj = new Object();
        obj.nodeType = node.nodeType;
        if (node.nodeType == Node.ELEMENT_NODE) {
            obj.nodeName = DOM.upperName(node);
            obj.attributes = new Object();
            for (var i = 0; i < node.attributes.length; i++) {
                var attr = node.attributes[i];
                obj.attributes[attr.nodeName] = attr.nodeValue;
            }
            obj.childNodes = new Array();
            for (var child = node.firstChild; child != null; child = child.nextSibling)
                obj.childNodes.push(convertToObjects(child));
        }
        else if (node.nodeType == Node.TEXT_NODE) {
            obj.nodeValue = node.nodeValue;
        }
        else {
            throw new Error("Unexpected node type: "+node.nodeType);
        }
        return obj;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                //
//                                          General                                               //
//                                                                                                //
////////////////////////////////////////////////////////////////////////////////////////////////////

function getStyles()
{
    var list = new Array();
    for (var i = 0; i < document.styleSheets.length; i++) {
        var sheet = document.styleSheets[i];
        var str = "";
        for (name in sheet)
            str += name+"\n";
        for (var j = 0; j < sheet.cssRules.length; j++) {
            var rule = sheet.cssRules[j];
            if (rule.type == CSSRule.STYLE_RULE) {
                var properties = new Object();
                for (k = 0; k < rule.style.length; k++)
                    properties[rule.style[k]] = rule.style.getPropertyValue(rule.style[k]);
                
                list.push({"selector": rule.selectorText,
                           "properties": properties });
            }
        }
    }
    editor.setStyles(list);
}

function getHTML()
{
    var clone = DOM.cloneNode(document.documentElement,true);
    removeSpecial(clone);

    return clone.outerHTML;
}

function getErrorReportingInfo()
{
    try {
        var html = htmlWithSelection();
        cleanse(html);
        return html.outerHTML;
    }
    catch (e) {
        try {
            var html = DOM.cloneNode(document.documentElement,true);
            cleanse(html);
            return html.outerHTML+"\n[Error getting selection: "+e+"]";
        }
        catch (e2) {
            return "[Error getting HTML: "+e2+"]";
        }
    }

    function cleanse(node)
    {
        if ((node.nodeType == Node.TEXT_NODE) || (node.nodeType == Node.COMMENT_NODE)) {
            node.nodeValue = node.nodeValue.replace(/[^\s\.\@\^]/g,"X");
        }
        else if (node.nodeType == Node.ELEMENT_NODE) {
            for (var child = node.firstChild; child != null; child = child.nextSibling)
                cleanse(child);
        }
    }

    function htmlWithSelection()
    {
        var selectionRange = Selection.getSelectionRange();
        if (selectionRange != null) {
            selectionRange = selectionRange.forwards();
            var startSave = new Object();
            var endSave = new Object();

            var html = null;

            selectionRange.trackWhileExecuting(function() {
                // We use the strings @@^^ and ^^@@ to represent the selection
                // start and end, respectively. The reason for this is that after we have
                // cloned the tree, all text will be removed. We keeping the @ and ^ characters
                // so we have some way to identifiy the selection markers; leaving these in
                // is not going to reveal any confidential information.

                addPositionMarker(selectionRange.end,"^^@@",endSave);
                addPositionMarker(selectionRange.start,"@@^^",startSave);

                html = DOM.cloneNode(document.documentElement,true);

                removePositionMarker(selectionRange.start,startSave);
                removePositionMarker(selectionRange.end,endSave);
            });

            return html;
        }
        else {
            return DOM.cloneNode(document.documentElement,true);
        }
    }

    function addPositionMarker(pos,name,save)
    {
        var node = pos.node;
        var offset = pos.offset;
        if (node.nodeType == Node.ELEMENT_NODE) {
            save.tempNode = DOM.createTextNode(document,name);
            DOM.insertBefore(node,save.tempNode,node.childNodes[offset]);
        }
        else if (node.nodeType == Node.TEXT_NODE) {
            save.originalNodeValue = node.nodeValue;
            node.nodeValue = node.nodeValue.slice(0,offset) + name + node.nodeValue.slice(offset);
        }
    }

    function removePositionMarker(pos,save)
    {
        var node = pos.node;
        var offset = pos.offset;
        if (pos.node.nodeType == Node.ELEMENT_NODE) {
            DOM.deleteNode(save.tempNode);
        }
        else if (pos.node.nodeType == Node.TEXT_NODE) {
            node.nodeValue = save.originalNodeValue;
        }
    }
}

function removeSpecial(node)
{
    if ((DOM.upperName(node) == "SPAN") &&
        ((node.getAttribute("class") == Keys.HEADING_NUMBER) ||
         (node.getAttribute("class") == Keys.FIGURE_NUMBER) ||
         (node.getAttribute("class") == Keys.TABLE_NUMBER))) {
        DOM.removeNodeButKeepChildren(node);
    }
    else if ((DOM.upperName(node) == "DIV") &&
             (node.getAttribute("class") == Keys.SELECTION_HIGHLIGHT)) {
        DOM.removeNodeButKeepChildren(node);
    }
    else if ((DOM.upperName(node) == "META") &&
             node.hasAttribute("name") &&
             (node.getAttribute("name").toLowerCase() == "viewport")) {
        DOM.deleteNode(node);
    }
    else {
        var next;
        for (var child = node.firstChild; child != null; child = next) {
            next = child.nextSibling;
            removeSpecial(child);
        }
    }
}

function isEmptyDocument()
{
    return !nodeHasContent(document.body);
}

function execute(fun)
{
    try {
        var res = fun();
        PostponedActions.perform();
        return res;
    }
    catch (e) {
        editor.reportJSError(e);
    }
}

function addContentType()
{
    var head = DOM.documentHead(document);
    var haveContentType = false;
    for (var child = head.firstChild; child != null; child = child.nextSibling) {
        if (DOM.upperName(child) == "META") {
            var httpEquiv = child.getAttribute("http-equiv");
            if ((httpEquiv != null) && (httpEquiv.toLowerCase() == "content-type")) {
                haveContentType = true;
                break;
            }
        }
    }
    if (!haveContentType) {
        var meta = DOM.createElement(document,"META");
        meta.setAttribute("http-equiv","Content-Type");
        meta.setAttribute("content","text/html; charset=utf-8");
        DOM.insertBefore(head,meta,head.firstChild);
    }
}

function init()
{
    try {
        DOM.assignNodeIds(document);
        addContentType();
        getStyles();
        Outline.init();
        return true;
    }
    catch (e) {
        return e.toString();
    }
}
<commit_msg>Removed the debug button and the DOMView, and associated javascript functions<commit_after>// Copyright (c) 2011-2012 UX Productivity Pty Ltd. All rights reserved.

function getStyles()
{
    var list = new Array();
    for (var i = 0; i < document.styleSheets.length; i++) {
        var sheet = document.styleSheets[i];
        var str = "";
        for (name in sheet)
            str += name+"\n";
        for (var j = 0; j < sheet.cssRules.length; j++) {
            var rule = sheet.cssRules[j];
            if (rule.type == CSSRule.STYLE_RULE) {
                var properties = new Object();
                for (k = 0; k < rule.style.length; k++)
                    properties[rule.style[k]] = rule.style.getPropertyValue(rule.style[k]);
                
                list.push({"selector": rule.selectorText,
                           "properties": properties });
            }
        }
    }
    editor.setStyles(list);
}

function getHTML()
{
    var clone = DOM.cloneNode(document.documentElement,true);
    removeSpecial(clone);

    return clone.outerHTML;
}

function getErrorReportingInfo()
{
    try {
        var html = htmlWithSelection();
        cleanse(html);
        return html.outerHTML;
    }
    catch (e) {
        try {
            var html = DOM.cloneNode(document.documentElement,true);
            cleanse(html);
            return html.outerHTML+"\n[Error getting selection: "+e+"]";
        }
        catch (e2) {
            return "[Error getting HTML: "+e2+"]";
        }
    }

    function cleanse(node)
    {
        if ((node.nodeType == Node.TEXT_NODE) || (node.nodeType == Node.COMMENT_NODE)) {
            node.nodeValue = node.nodeValue.replace(/[^\s\.\@\^]/g,"X");
        }
        else if (node.nodeType == Node.ELEMENT_NODE) {
            for (var child = node.firstChild; child != null; child = child.nextSibling)
                cleanse(child);
        }
    }

    function htmlWithSelection()
    {
        var selectionRange = Selection.getSelectionRange();
        if (selectionRange != null) {
            selectionRange = selectionRange.forwards();
            var startSave = new Object();
            var endSave = new Object();

            var html = null;

            selectionRange.trackWhileExecuting(function() {
                // We use the strings @@^^ and ^^@@ to represent the selection
                // start and end, respectively. The reason for this is that after we have
                // cloned the tree, all text will be removed. We keeping the @ and ^ characters
                // so we have some way to identifiy the selection markers; leaving these in
                // is not going to reveal any confidential information.

                addPositionMarker(selectionRange.end,"^^@@",endSave);
                addPositionMarker(selectionRange.start,"@@^^",startSave);

                html = DOM.cloneNode(document.documentElement,true);

                removePositionMarker(selectionRange.start,startSave);
                removePositionMarker(selectionRange.end,endSave);
            });

            return html;
        }
        else {
            return DOM.cloneNode(document.documentElement,true);
        }
    }

    function addPositionMarker(pos,name,save)
    {
        var node = pos.node;
        var offset = pos.offset;
        if (node.nodeType == Node.ELEMENT_NODE) {
            save.tempNode = DOM.createTextNode(document,name);
            DOM.insertBefore(node,save.tempNode,node.childNodes[offset]);
        }
        else if (node.nodeType == Node.TEXT_NODE) {
            save.originalNodeValue = node.nodeValue;
            node.nodeValue = node.nodeValue.slice(0,offset) + name + node.nodeValue.slice(offset);
        }
    }

    function removePositionMarker(pos,save)
    {
        var node = pos.node;
        var offset = pos.offset;
        if (pos.node.nodeType == Node.ELEMENT_NODE) {
            DOM.deleteNode(save.tempNode);
        }
        else if (pos.node.nodeType == Node.TEXT_NODE) {
            node.nodeValue = save.originalNodeValue;
        }
    }
}

function removeSpecial(node)
{
    if ((DOM.upperName(node) == "SPAN") &&
        ((node.getAttribute("class") == Keys.HEADING_NUMBER) ||
         (node.getAttribute("class") == Keys.FIGURE_NUMBER) ||
         (node.getAttribute("class") == Keys.TABLE_NUMBER))) {
        DOM.removeNodeButKeepChildren(node);
    }
    else if ((DOM.upperName(node) == "DIV") &&
             (node.getAttribute("class") == Keys.SELECTION_HIGHLIGHT)) {
        DOM.removeNodeButKeepChildren(node);
    }
    else if ((DOM.upperName(node) == "META") &&
             node.hasAttribute("name") &&
             (node.getAttribute("name").toLowerCase() == "viewport")) {
        DOM.deleteNode(node);
    }
    else {
        var next;
        for (var child = node.firstChild; child != null; child = next) {
            next = child.nextSibling;
            removeSpecial(child);
        }
    }
}

function isEmptyDocument()
{
    return !nodeHasContent(document.body);
}

function execute(fun)
{
    try {
        var res = fun();
        PostponedActions.perform();
        return res;
    }
    catch (e) {
        editor.reportJSError(e);
    }
}

function addContentType()
{
    var head = DOM.documentHead(document);
    var haveContentType = false;
    for (var child = head.firstChild; child != null; child = child.nextSibling) {
        if (DOM.upperName(child) == "META") {
            var httpEquiv = child.getAttribute("http-equiv");
            if ((httpEquiv != null) && (httpEquiv.toLowerCase() == "content-type")) {
                haveContentType = true;
                break;
            }
        }
    }
    if (!haveContentType) {
        var meta = DOM.createElement(document,"META");
        meta.setAttribute("http-equiv","Content-Type");
        meta.setAttribute("content","text/html; charset=utf-8");
        DOM.insertBefore(head,meta,head.firstChild);
    }
}

function init()
{
    try {
        DOM.assignNodeIds(document);
        addContentType();
        getStyles();
        Outline.init();
        return true;
    }
    catch (e) {
        return e.toString();
    }
}

{'fraction_non_alphanumeric': 0.1370809303504658, 'fraction_numerical': 0.002154762659230623, 'mean_word_length': 1.7626050420168067, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4684845', 'n_tokens_mistral': 4395, 'n_tokens_neox': 4310, 'n_words': 916}