<commit_before>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This 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 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
(function() {
  'use strict';
  CKEDITOR.plugins.add('xwiki-source', {
    requires: 'sourcearea,notification',

    init: function(editor) {
      editor.on('beforeSetMode', jQuery.proxy(this.onBeforeSetMode, this));
      editor.on('beforeModeUnload', jQuery.proxy(this.onBeforeModeUnload, this));
      editor.on('mode', jQuery.proxy(this.onMode, this));
    },

    onBeforeSetMode: function(event) {
      if (this.isModeSupported(event.data)) {
        this.setLoading(event.editor, true);
      }
    },

    isModeSupported: function(mode) {
      return mode === 'wysiwyg' || mode === 'source';
    },

    onBeforeModeUnload: function(event) {
      var editor = event.editor;
      if (!this.isModeSupported(editor.mode)) {
        return;
      }
      var mode = editor._.modes[editor.mode];
      if (mode.failed) {
        mode.dirty = mode.failed = false;
        // Make sure we retry the conversion on the next mode switch.
        delete mode.data;
      } else {
        var oldData = mode.data;
        var newData = this.getFullData(editor);
        mode.dirty = oldData !== newData;
        mode.data = newData;
      }
    },

    getFullData: function(editor) {
      var isFullData = editor.config.fullData;
      editor.config.fullData = true;
      var fullData = editor.getData();
      editor.config.fullData = isFullData;
      return fullData;
    },

    onMode: function(event) {
      var editor = event.editor;
      if (editor.mode === 'wysiwyg' && editor._.previousMode === 'source') {
        // Convert from wiki syntax to HTML.
        this.maybeConvertHTML(editor, true);
      } else if (editor.mode === 'source' && editor._.previousMode === 'wysiwyg') {
        // Convert from HTML to wiki syntax.
        this.maybeConvertHTML(editor, false);
      } else if (this.isModeSupported(editor.mode)) {
        this.setLoading(editor, false);
      }
    },

    maybeConvertHTML: function(editor, toHTML) {
      var oldMode = editor._.modes[editor._.previousMode];
      var newMode = editor._.modes[editor.mode];
      if (oldMode.dirty || typeof newMode.data !== 'string') {
        this.convertHTML(editor, toHTML);
      } else {
        editor.setData(newMode.data, {
          callback: jQuery.proxy(this.setLoading, this, editor, false)
        });
      }
    },

    convertHTML: function(editor, toHTML) {
      var thisPlugin = this;
      var config = editor.config['xwiki-source'] || {};
      jQuery.post(config.htmlConverter, {
        fromHTML: !toHTML,
        toHTML: toHTML,
        text: editor._.previousModeData
      }).done(function(data) {
        editor.setData(data, {
          callback: function() {
            // Take a snapshot after the data has been set, in order to be able to detect changes.
            editor._.modes[editor.mode].data = thisPlugin.getFullData(editor);
            thisPlugin.setLoading(editor, false);
          }
        });
      }).fail(function() {
        // Switch back to the previous edit mode without performing a conversion.
        editor._.modes[editor.mode].failed = true;
        editor.setMode(editor._.previousMode, function() {
          thisPlugin.setLoading(editor, false);
          editor.showNotification('Failed to perform the conversion.', 'warning');
        });
      });
    },

    setLoading: function(editor, loading) {
      var sourceButton, editable = editor.editable();
      if (editable) {
        editor.setReadOnly(loading);
        sourceButton = editor.container.findOne('.cke_button__source_icon');
      }
      if (loading) {
        editor.ui.space('contents').setStyle('visibility', 'hidden');
        if (sourceButton) {
          sourceButton.addClass('loading');
        }
        if (editor.mode === 'source') {<commit_msg>CKEDITOR-102: Unable to edit a page in Source mode on IE11<commit_after>/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This 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 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
(function() {
  'use strict';
  CKEDITOR.plugins.add('xwiki-source', {
    requires: 'sourcearea,notification',

    init: function(editor) {
      editor.on('beforeSetMode', jQuery.proxy(this.onBeforeSetMode, this));
      editor.on('beforeModeUnload', jQuery.proxy(this.onBeforeModeUnload, this));
      editor.on('mode', jQuery.proxy(this.onMode, this));
    },

    onBeforeSetMode: function(event) {
      if (this.isModeSupported(event.data)) {
        this.setLoading(event.editor, true);
      }
    },

    isModeSupported: function(mode) {
      return mode === 'wysiwyg' || mode === 'source';
    },

    onBeforeModeUnload: function(event) {
      var editor = event.editor;
      if (!this.isModeSupported(editor.mode)) {
        return;
      }
      var mode = editor._.modes[editor.mode];
      if (mode.failed) {
        mode.dirty = mode.failed = false;
        // Make sure we retry the conversion on the next mode switch.
        delete mode.data;
      } else {
        var oldData = mode.data;
        var newData = this.getFullData(editor);
        mode.dirty = oldData !== newData;
        mode.data = newData;
      }
    },

    getFullData: function(editor) {
      var isFullData = editor.config.fullData;
      editor.config.fullData = true;
      var fullData = editor.getData();
      editor.config.fullData = isFullData;
      return fullData;
    },

    onMode: function(event) {
      var editor = event.editor;
      if (editor.mode === 'wysiwyg' && editor._.previousMode === 'source') {
        // Convert from wiki syntax to HTML.
        this.maybeConvertHTML(editor, true);
      } else if (editor.mode === 'source' && editor._.previousMode === 'wysiwyg') {
        // Convert from HTML to wiki syntax.
        this.maybeConvertHTML(editor, false);
      } else if (this.isModeSupported(editor.mode)) {
        this.setLoading(editor, false);
      }
    },

    maybeConvertHTML: function(editor, toHTML) {
      var oldMode = editor._.modes[editor._.previousMode];
      var newMode = editor._.modes[editor.mode];
      if (oldMode.dirty || typeof newMode.data !== 'string') {
        this.convertHTML(editor, toHTML);
      } else {
        editor.setData(newMode.data, {
          callback: jQuery.proxy(this.setLoading, this, editor, false)
        });
      }
    },

    convertHTML: function(editor, toHTML) {
      var thisPlugin = this;
      var config = editor.config['xwiki-source'] || {};
      jQuery.post(config.htmlConverter, {
        fromHTML: !toHTML,
        toHTML: toHTML,
        text: editor._.previousModeData
      }).done(function(data) {
        editor.setData(data, {
          callback: function() {
            // Take a snapshot after the data has been set, in order to be able to detect changes.
            editor._.modes[editor.mode].data = thisPlugin.getFullData(editor);
            thisPlugin.setLoading(editor, false);
          }
        });
      }).fail(function() {
        // Switch back to the previous edit mode without performing a conversion.
        editor._.modes[editor.mode].failed = true;
        editor.setMode(editor._.previousMode, function() {
          thisPlugin.setLoading(editor, false);
          editor.showNotification('Failed to perform the conversion.', 'warning');
        });
      });
    },

    setLoading: function(editor, loading) {
      var sourceButton;
      if (editor.editable()) {
        editor.setReadOnly(loading);
        sourceButton = editor.container.findOne('.cke_button__source_icon');
      }
      if (loading) {
        // A bug in Internet Explorer 11 prevents the user from typing into the Source text area if the WYSIWYG text
        // area is focused and the selection is collapsed before switching to Source mode. In order to avoid this
        // problem we have to either remove the focus from the WYSIWYG text area or to make sure the selection is not
        // collapsed before the switch. We didn't manage to remove the focus because we don't know what other focusable
        // elements are available on the page. Thus the solution we applied was to select all the content before the
        // switch so that the selection is not collapsed.
        // CKEDITOR-102: Unable to edit a page in Source mode on IE11
        // https://connect.microsoft.com/IE/feedback/details/1613994/ie-10-11-iframe-removal-causes-loss-of-the-ability-to-focus-input-elements
        // https://dev.ckeditor.com/ticket/7386
        if (editor.document && editor.document != CKEDITOR.document && CKEDITOR.env.ie && !CKEDITOR.env.edge) {
          // We apply the fix only if the WYSIWYG text area is using an iframe and if the browser is Internet Explorer
          // except Edge (that doesn't have the problem).
          editor.document.$.execCommand('SelectAll', false, null);
        }
        editor.ui.space('contents').setStyle('visibility', 'hidden');
        if (sourceButton) {
          sourceButton.addClass('loading');
        }
        if (editor.mode === 'source') {
{'fraction_non_alphanumeric': 0.11368757040931281, 'fraction_numerical': 0.004975591438227563, 'mean_word_length': 2.6136363636363638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2552789', 'n_tokens_mistral': 2992, 'n_tokens_neox': 2908, 'n_words': 898}