<commit_before>/*jslint newcap: true */
/*global XMLHttpRequest: false, inlineAttach: false, FormData: false */
/*
 * Inline Text Attachment
 *
 * Author: <NAME>
 * Contact: <EMAIL>
 */
(function(document, window) {
    "use strict";

    /**
     * Simple function to merge the given objects
     *
     * @param {Object[]} object Multiple object parameters
     * @returns {Object}
     */
    function merge() {
        var result = {};
        for (var i = arguments.length - 1; i >= 0; i--) {
            var obj = arguments[i];
            for (var k in obj) {
                result[k] = obj[k];
            }
        }
        return result;
    }

    /**
     * @param {Object} options
     */
    window.inlineAttach = function(options, instance) {

        var settings = merge(options, inlineAttach.defaults),
            editor = instance,
            filenameTag = '{filename}',
            lastValue,
            me = this;

        /**
         * Upload a given file blob
         *
         * @param {Blob} file
         */
        this.uploadFile = function(file) {
            var formData = new FormData(),
                xhr = new XMLHttpRequest(),
                extension = 'png';

            // Attach the file. If coming from clipboard, add a default filename (only works in Chrome for now)
            // http://stackoverflow.com/questions/6664967/how-to-give-a-blob-uploaded-as-formdata-a-file-name
            if (file.name) {
                var fileNameMatches = file.name.match(/\.(.+)$/);
                if (fileNameMatches) {
                    extension = fileNameMatches[1];
                }
            }
            formData.append(settings.uploadFieldName, file, "image-" + Date.now() + "." + extension);

            // Add any available extra parameters
            if (typeof settings.extraParams === "object") {
                for (var key in settings.extraParams) {
                    if (settings.extraParams.hasOwnProperty(key)) {
                        formData.append(key, settings.extraParams[key]);
                    }
                }
            }

            xhr.open('POST', settings.uploadUrl);

            // Add any available extra headers
            if (typeof settings.extraHeaders === "object") {
                for (var header in settings.extraHeaders) {
                    if (settings.extraHeaders.hasOwnProperty(header)) {
                        xhr.setRequestHeader(header, settings.extraHeaders[header]);
                    }
                }
            }

            xhr.onload = function() {
                // If HTTP status is OK or Created
                if (xhr.status === 200 || xhr.status === 201) {
                    var data = JSON.parse(xhr.responseText);
                    me.onUploadedFile(data);
                } else {
                    me.onErrorUploading();
                }
            };
            xhr.send(formData);
        };

        /**
         * Check if the given file is allowed
         *
         * @param {File} file
         */
        this.isAllowedFile = function(file) {
            return settings.allowedTypes.indexOf(file.type) >= 0;
        };

        /**
         * When a file has finished uploading
         *
         * @param {Object} data
         */
        this.onUploadedFile = function(data) {
            var result = settings.onUploadedFile(data),
                filename;

            if (settings.dataProcessor) {
                data = settings.dataProcessor(data);
            }

            filename = data[settings.downloadFieldName];

            if (result !== false && filename) {
                var text = editor.getValue().replace(lastValue, settings.urlText.replace(filenameTag, filename));
                editor.setValue(text);
            }
        };

        /**
         * Custom upload handler
         *
         * @param {Blob} file
         * @return {Boolean} when false is returned it will prevent default upload behavior
         */
        this.customUploadHandler = function(file) {
            return settings.customUploadHandler(file);
        };

        /**
         * When a file didn't upload properly.
         * Override by passing your own onErrorUploading function with settings.
         *
         * @param {Object} data
         */
        this.onErrorUploading = function() {
            var text = editor.getValue().replace(lastValue, "");
            editor.setValue(text);
            if (settings.customErrorHandler()) {
                window.alert(settings.errorText);
            }
        };

        /**
         * Append a line of text at the bottom, ensuring there aren't unnecessary newlines
         *
         * @param {String} appended Current content
         * @param {String} previous Value which should be appended after the current content
         */
        function appendInItsOwnLine(previous, appended) {
            return (previous + "\n\n[[D]]" + appended)
                .replace(/(\n{2,})\[\[D\]\]/, "\n\n")
                .replace(/^(\n*)/, "");
        }

        /**
         * When a file has been received by a drop or paste event
         * @param {Blob} file
         */
        this.onReceivedFile = function(file) {
            var result = settings.onReceivedFile(file);
            if (result !== false) {
                lastValue = settings.progressText;<commit_msg>Proxied invocation of overridable functions through call to apply `this` context<commit_after>/*jslint newcap: true */
/*global XMLHttpRequest: false, inlineAttach: false, FormData: false */
/*
 * Inline Text Attachment
 *
 * Author: <NAME>
 * Contact: <EMAIL>
 */
(function(document, window) {
    "use strict";

    /**
     * Simple function to merge the given objects
     *
     * @param {Object[]} object Multiple object parameters
     * @returns {Object}
     */
    function merge() {
        var result = {};
        for (var i = arguments.length - 1; i >= 0; i--) {
            var obj = arguments[i];
            for (var k in obj) {
                result[k] = obj[k];
            }
        }
        return result;
    }

    /**
     * @param {Object} options
     */
    window.inlineAttach = function(options, instance) {

        var settings = merge(options, inlineAttach.defaults),
            editor = instance,
            filenameTag = '{filename}',
            lastValue,
            me = this;

        /**
         * Upload a given file blob
         *
         * @param {Blob} file
         */
        this.uploadFile = function(file) {
            var formData = new FormData(),
                xhr = new XMLHttpRequest(),
                extension = 'png';

            // Attach the file. If coming from clipboard, add a default filename (only works in Chrome for now)
            // http://stackoverflow.com/questions/6664967/how-to-give-a-blob-uploaded-as-formdata-a-file-name
            if (file.name) {
                var fileNameMatches = file.name.match(/\.(.+)$/);
                if (fileNameMatches) {
                    extension = fileNameMatches[1];
                }
            }
            formData.append(settings.uploadFieldName, file, "image-" + Date.now() + "." + extension);

            // Add any available extra parameters
            if (typeof settings.extraParams === "object") {
                for (var key in settings.extraParams) {
                    if (settings.extraParams.hasOwnProperty(key)) {
                        formData.append(key, settings.extraParams[key]);
                    }
                }
            }

            xhr.open('POST', settings.uploadUrl);

            // Add any available extra headers
            if (typeof settings.extraHeaders === "object") {
                for (var header in settings.extraHeaders) {
                    if (settings.extraHeaders.hasOwnProperty(header)) {
                        xhr.setRequestHeader(header, settings.extraHeaders[header]);
                    }
                }
            }

            xhr.onload = function() {
                // If HTTP status is OK or Created
                if (xhr.status === 200 || xhr.status === 201) {
                    var data = JSON.parse(xhr.responseText);
                    me.onUploadedFile(data);
                } else {
                    me.onErrorUploading();
                }
            };
            xhr.send(formData);
        };

        /**
         * Check if the given file is allowed
         *
         * @param {File} file
         */
        this.isAllowedFile = function(file) {
            return settings.allowedTypes.indexOf(file.type) >= 0;
        };

        /**
         * When a file has finished uploading
         *
         * @param {Object} data
         */
        this.onUploadedFile = function(data) {
            var result = settings.onUploadedFile.call(this, data),
                filename;

            if (settings.dataProcessor) {
                data = settings.dataProcessor.call(this, data);
            }

            filename = data[settings.downloadFieldName];

            if (result !== false && filename) {
                var text = editor.getValue().replace(lastValue, settings.urlText.replace(filenameTag, filename));
                editor.setValue(text);
            }
        };

        /**
         * Custom upload handler
         *
         * @param {Blob} file
         * @return {Boolean} when false is returned it will prevent default upload behavior
         */
        this.customUploadHandler = function(file) {
            return settings.customUploadHandler.call(this, file);
        };

        /**
         * When a file didn't upload properly.
         * Override by passing your own onErrorUploading function with settings.
         *
         * @param {Object} data
         */
        this.onErrorUploading = function() {
            var text = editor.getValue().replace(lastValue, "");
            editor.setValue(text);
            if (settings.customErrorHandler.call(this)) {
                window.alert(settings.errorText);
            }
        };

        /**
         * Append a line of text at the bottom, ensuring there aren't unnecessary newlines
         *
         * @param {String} appended Current content
         * @param {String} previous Value which should be appended after the current content
         */
        function appendInItsOwnLine(previous, appended) {
            return (previous + "\n\n[[D]]" + appended)
                .replace(/(\n{2,})\[\[D\]\]/, "\n\n")
                .replace(/^(\n*)/, "");
        }

        /**
         * When a file has been received by a drop or paste event
         * @param {Blob} file
         */
        this.onReceivedFile = function(file) {
            var result = settings.onReceivedFile.call(this, file);
            if (result !== false) {
                lastValue = settings.progressText;
{'fraction_non_alphanumeric': 0.11280878316559927, 'fraction_numerical': 0.003293687099725526, 'mean_word_length': 1.4630464173050923, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '879026', 'n_tokens_mistral': 2848, 'n_tokens_neox': 2717, 'n_words': 707}