Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / editor / js / editor.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7
8 (function ($, Drupal, drupalSettings) {
9   function findFieldForFormatSelector($formatSelector) {
10     var field_id = $formatSelector.attr('data-editor-for');
11
12     return $('#' + field_id).get(0);
13   }
14
15   function changeTextEditor(field, newFormatID) {
16     var previousFormatID = field.getAttribute('data-editor-active-text-format');
17
18     if (drupalSettings.editor.formats[previousFormatID]) {
19       Drupal.editorDetach(field, drupalSettings.editor.formats[previousFormatID]);
20     } else {
21         $(field).off('.editor');
22       }
23
24     if (drupalSettings.editor.formats[newFormatID]) {
25       var format = drupalSettings.editor.formats[newFormatID];
26       filterXssWhenSwitching(field, format, previousFormatID, Drupal.editorAttach);
27     }
28
29     field.setAttribute('data-editor-active-text-format', newFormatID);
30   }
31
32   function onTextFormatChange(event) {
33     var $select = $(event.target);
34     var field = event.data.field;
35     var activeFormatID = field.getAttribute('data-editor-active-text-format');
36     var newFormatID = $select.val();
37
38     if (newFormatID === activeFormatID) {
39       return;
40     }
41
42     var supportContentFiltering = drupalSettings.editor.formats[newFormatID] && drupalSettings.editor.formats[newFormatID].editorSupportsContentFiltering;
43
44     var hasContent = field.value !== '';
45     if (hasContent && supportContentFiltering) {
46       var message = Drupal.t('Changing the text format to %text_format will permanently remove content that is not allowed in that text format.<br><br>Save your changes before switching the text format to avoid losing data.', {
47         '%text_format': $select.find('option:selected').text()
48       });
49       var confirmationDialog = Drupal.dialog('<div>' + message + '</div>', {
50         title: Drupal.t('Change text format?'),
51         dialogClass: 'editor-change-text-format-modal',
52         resizable: false,
53         buttons: [{
54           text: Drupal.t('Continue'),
55           class: 'button button--primary',
56           click: function click() {
57             changeTextEditor(field, newFormatID);
58             confirmationDialog.close();
59           }
60         }, {
61           text: Drupal.t('Cancel'),
62           class: 'button',
63           click: function click() {
64             $select.val(activeFormatID);
65             confirmationDialog.close();
66           }
67         }],
68
69         closeOnEscape: false,
70         create: function create() {
71           $(this).parent().find('.ui-dialog-titlebar-close').remove();
72         },
73
74         beforeClose: false,
75         close: function close(event) {
76           $(event.target).remove();
77         }
78       });
79
80       confirmationDialog.showModal();
81     } else {
82       changeTextEditor(field, newFormatID);
83     }
84   }
85
86   Drupal.editors = {};
87
88   Drupal.behaviors.editor = {
89     attach: function attach(context, settings) {
90       if (!settings.editor) {
91         return;
92       }
93
94       $(context).find('[data-editor-for]').once('editor').each(function () {
95         var $this = $(this);
96         var field = findFieldForFormatSelector($this);
97
98         if (!field) {
99           return;
100         }
101
102         var activeFormatID = $this.val();
103         field.setAttribute('data-editor-active-text-format', activeFormatID);
104
105         if (settings.editor.formats[activeFormatID]) {
106           Drupal.editorAttach(field, settings.editor.formats[activeFormatID]);
107         }
108
109         $(field).on('change.editor keypress.editor', function () {
110           field.setAttribute('data-editor-value-is-changed', 'true');
111
112           $(field).off('.editor');
113         });
114
115         if ($this.is('select')) {
116           $this.on('change.editorAttach', { field: field }, onTextFormatChange);
117         }
118
119         $this.parents('form').on('submit', function (event) {
120           if (event.isDefaultPrevented()) {
121             return;
122           }
123
124           if (settings.editor.formats[activeFormatID]) {
125             Drupal.editorDetach(field, settings.editor.formats[activeFormatID], 'serialize');
126           }
127         });
128       });
129     },
130     detach: function detach(context, settings, trigger) {
131       var editors = void 0;
132
133       if (trigger === 'serialize') {
134         editors = $(context).find('[data-editor-for]').findOnce('editor');
135       } else {
136         editors = $(context).find('[data-editor-for]').removeOnce('editor');
137       }
138
139       editors.each(function () {
140         var $this = $(this);
141         var activeFormatID = $this.val();
142         var field = findFieldForFormatSelector($this);
143         if (field && activeFormatID in settings.editor.formats) {
144           Drupal.editorDetach(field, settings.editor.formats[activeFormatID], trigger);
145         }
146       });
147     }
148   };
149
150   Drupal.editorAttach = function (field, format) {
151     if (format.editor) {
152       Drupal.editors[format.editor].attach(field, format);
153
154       Drupal.editors[format.editor].onChange(field, function () {
155         $(field).trigger('formUpdated');
156
157         field.setAttribute('data-editor-value-is-changed', 'true');
158       });
159     }
160   };
161
162   Drupal.editorDetach = function (field, format, trigger) {
163     if (format.editor) {
164       Drupal.editors[format.editor].detach(field, format, trigger);
165
166       if (field.getAttribute('data-editor-value-is-changed') === 'false') {
167         field.value = field.getAttribute('data-editor-value-original');
168       }
169     }
170   };
171
172   function filterXssWhenSwitching(field, format, originalFormatID, callback) {
173     if (format.editor.isXssSafe) {
174       callback(field, format);
175     } else {
176         $.ajax({
177           url: Drupal.url('editor/filter_xss/' + format.format),
178           type: 'POST',
179           data: {
180             value: field.value,
181             original_format_id: originalFormatID
182           },
183           dataType: 'json',
184           success: function success(xssFilteredValue) {
185             if (xssFilteredValue !== false) {
186               field.value = xssFilteredValue;
187             }
188             callback(field, format);
189           }
190         });
191       }
192   }
193 })(jQuery, Drupal, drupalSettings);