<commit_before>/**
 * Quink, Copyright (c) 2013-2014 IMD - International Institute for Management Development, Switzerland.
 *
 * This file is part of Quink.
 * 
 * Quink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Quink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Quink.  If not, see <http://www.gnu.org/licenses/>.
 */

define([
    'jquery'
], function ($) {
    'use strict';

    var PluginContext = function (def) {
        if (def.def) {
            this.container = def.el;
            this.definition = def.def;
        } else {
            this.container = def.container;
            this.definition = def;
        }
    };

    PluginContext.prototype.getContainer = function () {
        return this.container;
    };

    PluginContext.prototype.isEdit = function () {
        return this.getContainer() instanceof $;
    };

    /**
     * Returns the data that the containing element current holds. The containing element can be
     * created by the plugin or by Quink. In the former case the element and its content are returned.
     * In the latter case only the content is returned.
     */
    PluginContext.prototype.getContainerData = function () {
        var data = null,
            cont;
        if (this.isEdit()) {
            cont = this.getContainer();
            if (this.getDefinition().container.pluginCreated) {
                // Use outerHTML if possible as it's more efficient
                if (cont[0].outerHTML !== undefined) {
                    data = cont[0].outerHTML;
                } else {
                    data = $('<div>').append(cont.clone()).html();
                }
            } else {
                data = cont.html();
            }
        }
        return data;
    };

    /**
     * The plugin data replaces anything in the existing range.
     */
    PluginContext.prototype.commit = function (data, range) {
        var cfg, el;
        if (this.isEdit()) {
            if (!this.getDefinition().container.pluginCreated) {
                this.getContainer().html(data);
            } else {
                this.getContainer().replaceWith(data);
            }
        } else {
            cfg = this.getContainer();
            if (!cfg.pluginCreated) {
                el = document.createElement(cfg.element);
                el.setAttribute('class', cfg['class']);
                el.innerHTML = data;
            } else {
                el = $(data)[0];
            }
            if (!range.collapsed) {
                range.deleteContents();
            }
            range.insertNode(el);
            range.setStartAfter(el);
            range.collapse(false);
        }
    };

    PluginContext.prototype.getDefinition = function () {
        return this.definition;
    };
<commit_msg>Allow a class spec when using a node in the plugin config.<commit_after>/**
 * Quink, Copyright (c) 2013-2014 IMD - International Institute for Management Development, Switzerland.
 *
 * This file is part of Quink.
 * 
 * Quink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Quink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Quink.  If not, see <http://www.gnu.org/licenses/>.
 */

define([
    'jquery'
], function ($) {
    'use strict';

    var PluginContext = function (def) {
        if (def.def) {
            this.container = def.el;
            this.definition = def.def;
        } else {
            this.container = def.container;
            this.definition = def;
        }
    };

    PluginContext.prototype.getContainer = function () {
        return this.container;
    };

    PluginContext.prototype.isEdit = function () {
        return this.getContainer() instanceof $;
    };

    /**
     * Returns the data that the containing element current holds. The containing element can be
     * created by the plugin or by Quink. In the former case the element and its content are returned.
     * In the latter case only the content is returned.
     */
    PluginContext.prototype.getContainerData = function () {
        var data = null,
            cont;
        if (this.isEdit()) {
            cont = this.getContainer();
            if (this.getDefinition().container.pluginCreated) {
                // Use outerHTML if possible as it's more efficient
                if (cont[0].outerHTML !== undefined) {
                    data = cont[0].outerHTML;
                } else {
                    data = $('<div>').append(cont.clone()).html();
                }
            } else {
                data = cont.html();
            }
        }
        return data;
    };

    /**
     * The plugin data replaces anything in the existing range.
     */
    PluginContext.prototype.commit = function (data, range) {
        var cfg, el, cls;
        if (this.isEdit()) {
            if (!this.getDefinition().container.pluginCreated) {
                this.getContainer().html(data);
            } else {
                this.getContainer().replaceWith(data);
            }
        } else {
            cfg = this.getContainer();
            cls = cfg['class'];
            if (!cfg.pluginCreated) {
                el = document.createElement(cfg.element);
                el.innerHTML = data;
            } else {
                el = $(data)[0];
            }
            if (cls) {
                el.setAttribute('class', cls);
            }
            if (!range.collapsed) {
                range.deleteContents();
            }
            range.insertNode(el);
            range.setStartAfter(el);
            range.collapse(false);
        }
    };

    PluginContext.prototype.getDefinition = function () {
        return this.definition;
    };

{'fraction_non_alphanumeric': 0.1019915509957755, 'fraction_numerical': 0.003621001810500905, 'mean_word_length': 1.791157894736842, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1980561', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1629, 'n_words': 550}