Subversion Repositories oidplus

Rev

Rev 637 | Rev 759 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
597 daniel-mar 1
/**
2
 * Copyright (c) Tiny Technologies, Inc. All rights reserved.
3
 * Licensed under the LGPL or a commercial license.
4
 * For LGPL see License.txt in the project root for license information.
5
 * For commercial licenses see https://www.tiny.cloud/
6
 *
679 daniel-mar 7
 * Version: 5.10.2 (2021-11-17)
597 daniel-mar 8
 */
9
(function () {
10
    'use strict';
11
 
12
    var Cell = function (initial) {
13
      var value = initial;
14
      var get = function () {
15
        return value;
16
      };
17
      var set = function (v) {
18
        value = v;
19
      };
20
      return {
21
        get: get,
22
        set: set
23
      };
24
    };
25
 
637 daniel-mar 26
    var global$4 = tinymce.util.Tools.resolve('tinymce.PluginManager');
597 daniel-mar 27
 
28
    var __assign = function () {
29
      __assign = Object.assign || function __assign(t) {
30
        for (var s, i = 1, n = arguments.length; i < n; i++) {
31
          s = arguments[i];
32
          for (var p in s)
33
            if (Object.prototype.hasOwnProperty.call(s, p))
34
              t[p] = s[p];
35
        }
36
        return t;
37
      };
38
      return __assign.apply(this, arguments);
39
    };
40
 
637 daniel-mar 41
    var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');
597 daniel-mar 42
 
43
    var global$2 = tinymce.util.Tools.resolve('tinymce.html.DomParser');
44
 
637 daniel-mar 45
    var global$1 = tinymce.util.Tools.resolve('tinymce.html.Node');
597 daniel-mar 46
 
637 daniel-mar 47
    var global = tinymce.util.Tools.resolve('tinymce.html.Serializer');
597 daniel-mar 48
 
49
    var shouldHideInSourceView = function (editor) {
50
      return editor.getParam('fullpage_hide_in_source_view');
51
    };
52
    var getDefaultXmlPi = function (editor) {
53
      return editor.getParam('fullpage_default_xml_pi');
54
    };
55
    var getDefaultEncoding = function (editor) {
56
      return editor.getParam('fullpage_default_encoding');
57
    };
58
    var getDefaultFontFamily = function (editor) {
59
      return editor.getParam('fullpage_default_font_family');
60
    };
61
    var getDefaultFontSize = function (editor) {
62
      return editor.getParam('fullpage_default_font_size');
63
    };
64
    var getDefaultTextColor = function (editor) {
65
      return editor.getParam('fullpage_default_text_color');
66
    };
67
    var getDefaultTitle = function (editor) {
68
      return editor.getParam('fullpage_default_title');
69
    };
70
    var getDefaultDocType = function (editor) {
71
      return editor.getParam('fullpage_default_doctype', '<!DOCTYPE html>');
72
    };
73
    var getProtect = function (editor) {
74
      return editor.getParam('protect');
75
    };
76
 
637 daniel-mar 77
    var parseHeader = function (editor, head) {
597 daniel-mar 78
      return global$2({
79
        validate: false,
80
        root_name: '#document'
637 daniel-mar 81
      }, editor.schema).parse(head, { format: 'xhtml' });
597 daniel-mar 82
    };
83
    var htmlToData = function (editor, head) {
637 daniel-mar 84
      var headerFragment = parseHeader(editor, head);
597 daniel-mar 85
      var data = {};
86
      var elm, matches;
87
      var getAttr = function (elm, name) {
88
        var value = elm.attr(name);
89
        return value || '';
90
      };
91
      data.fontface = getDefaultFontFamily(editor);
92
      data.fontsize = getDefaultFontSize(editor);
93
      elm = headerFragment.firstChild;
94
      if (elm.type === 7) {
95
        data.xml_pi = true;
96
        matches = /encoding="([^"]+)"/.exec(elm.value);
97
        if (matches) {
98
          data.docencoding = matches[1];
99
        }
100
      }
101
      elm = headerFragment.getAll('#doctype')[0];
102
      if (elm) {
103
        data.doctype = '<!DOCTYPE' + elm.value + '>';
104
      }
105
      elm = headerFragment.getAll('title')[0];
106
      if (elm && elm.firstChild) {
107
        data.title = elm.firstChild.value;
108
      }
637 daniel-mar 109
      global$3.each(headerFragment.getAll('meta'), function (meta) {
597 daniel-mar 110
        var name = meta.attr('name');
111
        var httpEquiv = meta.attr('http-equiv');
112
        var matches;
113
        if (name) {
114
          data[name.toLowerCase()] = meta.attr('content');
115
        } else if (httpEquiv === 'Content-Type') {
116
          matches = /charset\s*=\s*(.*)\s*/gi.exec(meta.attr('content'));
117
          if (matches) {
118
            data.docencoding = matches[1];
119
          }
120
        }
121
      });
122
      elm = headerFragment.getAll('html')[0];
123
      if (elm) {
124
        data.langcode = getAttr(elm, 'lang') || getAttr(elm, 'xml:lang');
125
      }
126
      data.stylesheets = [];
637 daniel-mar 127
      global$3.each(headerFragment.getAll('link'), function (link) {
597 daniel-mar 128
        if (link.attr('rel') === 'stylesheet') {
129
          data.stylesheets.push(link.attr('href'));
130
        }
131
      });
132
      elm = headerFragment.getAll('body')[0];
133
      if (elm) {
134
        data.langdir = getAttr(elm, 'dir');
135
        data.style = getAttr(elm, 'style');
136
        data.visited_color = getAttr(elm, 'vlink');
137
        data.link_color = getAttr(elm, 'link');
138
        data.active_color = getAttr(elm, 'alink');
139
      }
140
      return data;
141
    };
142
    var dataToHtml = function (editor, data, head) {
637 daniel-mar 143
      var headElement, elm;
597 daniel-mar 144
      var dom = editor.dom;
145
      var setAttr = function (elm, name, value) {
146
        elm.attr(name, value ? value : undefined);
147
      };
148
      var addHeadNode = function (node) {
149
        if (headElement.firstChild) {
150
          headElement.insert(node, headElement.firstChild);
151
        } else {
152
          headElement.append(node);
153
        }
154
      };
637 daniel-mar 155
      var headerFragment = parseHeader(editor, head);
597 daniel-mar 156
      headElement = headerFragment.getAll('head')[0];
157
      if (!headElement) {
158
        elm = headerFragment.getAll('html')[0];
637 daniel-mar 159
        headElement = new global$1('head', 1);
597 daniel-mar 160
        if (elm.firstChild) {
161
          elm.insert(headElement, elm.firstChild, true);
162
        } else {
163
          elm.append(headElement);
164
        }
165
      }
166
      elm = headerFragment.firstChild;
167
      if (data.xml_pi) {
637 daniel-mar 168
        var value = 'version="1.0"';
597 daniel-mar 169
        if (data.docencoding) {
170
          value += ' encoding="' + data.docencoding + '"';
171
        }
172
        if (elm.type !== 7) {
637 daniel-mar 173
          elm = new global$1('xml', 7);
597 daniel-mar 174
          headerFragment.insert(elm, headerFragment.firstChild, true);
175
        }
176
        elm.value = value;
177
      } else if (elm && elm.type === 7) {
178
        elm.remove();
179
      }
180
      elm = headerFragment.getAll('#doctype')[0];
181
      if (data.doctype) {
182
        if (!elm) {
637 daniel-mar 183
          elm = new global$1('#doctype', 10);
597 daniel-mar 184
          if (data.xml_pi) {
185
            headerFragment.insert(elm, headerFragment.firstChild);
186
          } else {
187
            addHeadNode(elm);
188
          }
189
        }
190
        elm.value = data.doctype.substring(9, data.doctype.length - 1);
191
      } else if (elm) {
192
        elm.remove();
193
      }
194
      elm = null;
637 daniel-mar 195
      global$3.each(headerFragment.getAll('meta'), function (meta) {
597 daniel-mar 196
        if (meta.attr('http-equiv') === 'Content-Type') {
197
          elm = meta;
198
        }
199
      });
200
      if (data.docencoding) {
201
        if (!elm) {
637 daniel-mar 202
          elm = new global$1('meta', 1);
597 daniel-mar 203
          elm.attr('http-equiv', 'Content-Type');
204
          elm.shortEnded = true;
205
          addHeadNode(elm);
206
        }
207
        elm.attr('content', 'text/html; charset=' + data.docencoding);
208
      } else if (elm) {
209
        elm.remove();
210
      }
211
      elm = headerFragment.getAll('title')[0];
212
      if (data.title) {
213
        if (!elm) {
637 daniel-mar 214
          elm = new global$1('title', 1);
597 daniel-mar 215
          addHeadNode(elm);
216
        } else {
217
          elm.empty();
218
        }
637 daniel-mar 219
        elm.append(new global$1('#text', 3)).value = data.title;
597 daniel-mar 220
      } else if (elm) {
221
        elm.remove();
222
      }
637 daniel-mar 223
      global$3.each('keywords,description,author,copyright,robots'.split(','), function (name) {
597 daniel-mar 224
        var nodes = headerFragment.getAll('meta');
225
        var i, meta;
226
        var value = data[name];
227
        for (i = 0; i < nodes.length; i++) {
228
          meta = nodes[i];
229
          if (meta.attr('name') === name) {
230
            if (value) {
231
              meta.attr('content', value);
232
            } else {
233
              meta.remove();
234
            }
235
            return;
236
          }
237
        }
238
        if (value) {
637 daniel-mar 239
          elm = new global$1('meta', 1);
597 daniel-mar 240
          elm.attr('name', name);
241
          elm.attr('content', value);
242
          elm.shortEnded = true;
243
          addHeadNode(elm);
244
        }
245
      });
246
      var currentStyleSheetsMap = {};
637 daniel-mar 247
      global$3.each(headerFragment.getAll('link'), function (stylesheet) {
597 daniel-mar 248
        if (stylesheet.attr('rel') === 'stylesheet') {
249
          currentStyleSheetsMap[stylesheet.attr('href')] = stylesheet;
250
        }
251
      });
637 daniel-mar 252
      global$3.each(data.stylesheets, function (stylesheet) {
597 daniel-mar 253
        if (!currentStyleSheetsMap[stylesheet]) {
637 daniel-mar 254
          elm = new global$1('link', 1);
597 daniel-mar 255
          elm.attr({
256
            rel: 'stylesheet',
257
            text: 'text/css',
258
            href: stylesheet
259
          });
260
          elm.shortEnded = true;
261
          addHeadNode(elm);
262
        }
263
        delete currentStyleSheetsMap[stylesheet];
264
      });
637 daniel-mar 265
      global$3.each(currentStyleSheetsMap, function (stylesheet) {
597 daniel-mar 266
        stylesheet.remove();
267
      });
268
      elm = headerFragment.getAll('body')[0];
269
      if (elm) {
270
        setAttr(elm, 'dir', data.langdir);
271
        setAttr(elm, 'style', data.style);
272
        setAttr(elm, 'vlink', data.visited_color);
273
        setAttr(elm, 'link', data.link_color);
274
        setAttr(elm, 'alink', data.active_color);
275
        dom.setAttribs(editor.getBody(), {
276
          style: data.style,
277
          dir: data.dir,
278
          vLink: data.visited_color,
279
          link: data.link_color,
280
          aLink: data.active_color
281
        });
282
      }
283
      elm = headerFragment.getAll('html')[0];
284
      if (elm) {
285
        setAttr(elm, 'lang', data.langcode);
286
        setAttr(elm, 'xml:lang', data.langcode);
287
      }
288
      if (!headElement.firstChild) {
289
        headElement.remove();
290
      }
637 daniel-mar 291
      var html = global({
597 daniel-mar 292
        validate: false,
293
        indent: true,
294
        indent_before: 'head,html,body,meta,title,script,link,style',
295
        indent_after: 'head,html,body,meta,title,script,link,style'
296
      }).serialize(headerFragment);
297
      return html.substring(0, html.indexOf('</body>'));
298
    };
299
 
300
    var open = function (editor, headState) {
301
      var data = htmlToData(editor, headState.get());
302
      var defaultData = {
303
        title: '',
304
        keywords: '',
305
        description: '',
306
        robots: '',
307
        author: '',
308
        docencoding: ''
309
      };
310
      var initialData = __assign(__assign({}, defaultData), data);
311
      editor.windowManager.open({
312
        title: 'Metadata and Document Properties',
313
        size: 'normal',
314
        body: {
315
          type: 'panel',
316
          items: [
317
            {
318
              name: 'title',
319
              type: 'input',
320
              label: 'Title'
321
            },
322
            {
323
              name: 'keywords',
324
              type: 'input',
325
              label: 'Keywords'
326
            },
327
            {
328
              name: 'description',
329
              type: 'input',
330
              label: 'Description'
331
            },
332
            {
333
              name: 'robots',
334
              type: 'input',
335
              label: 'Robots'
336
            },
337
            {
338
              name: 'author',
339
              type: 'input',
340
              label: 'Author'
341
            },
342
            {
343
              name: 'docencoding',
344
              type: 'input',
345
              label: 'Encoding'
346
            }
347
          ]
348
        },
349
        buttons: [
350
          {
351
            type: 'cancel',
352
            name: 'cancel',
353
            text: 'Cancel'
354
          },
355
          {
356
            type: 'submit',
357
            name: 'save',
358
            text: 'Save',
359
            primary: true
360
          }
361
        ],
362
        initialData: initialData,
363
        onSubmit: function (api) {
364
          var nuData = api.getData();
637 daniel-mar 365
          var headHtml = dataToHtml(editor, global$3.extend(data, nuData), headState.get());
597 daniel-mar 366
          headState.set(headHtml);
367
          api.close();
368
        }
369
      });
370
    };
371
 
637 daniel-mar 372
    var register$1 = function (editor, headState) {
597 daniel-mar 373
      editor.addCommand('mceFullPageProperties', function () {
374
        open(editor, headState);
375
      });
376
    };
377
 
378
    var protectHtml = function (protect, html) {
637 daniel-mar 379
      global$3.each(protect, function (pattern) {
597 daniel-mar 380
        html = html.replace(pattern, function (str) {
381
          return '<!--mce:protected ' + escape(str) + '-->';
382
        });
383
      });
384
      return html;
385
    };
386
    var unprotectHtml = function (html) {
387
      return html.replace(/<!--mce:protected ([\s\S]*?)-->/g, function (a, m) {
388
        return unescape(m);
389
      });
390
    };
391
 
637 daniel-mar 392
    var each = global$3.each;
597 daniel-mar 393
    var low = function (s) {
394
      return s.replace(/<\/?[A-Z]+/g, function (a) {
395
        return a.toLowerCase();
396
      });
397
    };
398
    var handleSetContent = function (editor, headState, footState, evt) {
399
      var startPos, endPos, content, styles = '';
400
      var dom = editor.dom;
401
      if (evt.selection) {
402
        return;
403
      }
404
      content = protectHtml(getProtect(editor), evt.content);
405
      if (evt.format === 'raw' && headState.get()) {
406
        return;
407
      }
408
      if (evt.source_view && shouldHideInSourceView(editor)) {
409
        return;
410
      }
411
      if (content.length === 0 && !evt.source_view) {
637 daniel-mar 412
        content = global$3.trim(headState.get()) + '\n' + global$3.trim(content) + '\n' + global$3.trim(footState.get());
597 daniel-mar 413
      }
414
      content = content.replace(/<(\/?)BODY/gi, '<$1body');
415
      startPos = content.indexOf('<body');
416
      if (startPos !== -1) {
417
        startPos = content.indexOf('>', startPos);
418
        headState.set(low(content.substring(0, startPos + 1)));
419
        endPos = content.indexOf('</body', startPos);
420
        if (endPos === -1) {
421
          endPos = content.length;
422
        }
637 daniel-mar 423
        evt.content = global$3.trim(content.substring(startPos + 1, endPos));
597 daniel-mar 424
        footState.set(low(content.substring(endPos)));
425
      } else {
426
        headState.set(getDefaultHeader(editor));
427
        footState.set('\n</body>\n</html>');
428
      }
637 daniel-mar 429
      var headerFragment = parseHeader(editor, headState.get());
597 daniel-mar 430
      each(headerFragment.getAll('style'), function (node) {
431
        if (node.firstChild) {
432
          styles += node.firstChild.value;
433
        }
434
      });
435
      var bodyElm = headerFragment.getAll('body')[0];
436
      if (bodyElm) {
437
        dom.setAttribs(editor.getBody(), {
438
          style: bodyElm.attr('style') || '',
439
          dir: bodyElm.attr('dir') || '',
440
          vLink: bodyElm.attr('vlink') || '',
441
          link: bodyElm.attr('link') || '',
442
          aLink: bodyElm.attr('alink') || ''
443
        });
444
      }
445
      dom.remove('fullpage_styles');
446
      var headElm = editor.getDoc().getElementsByTagName('head')[0];
447
      if (styles) {
448
        var styleElm = dom.add(headElm, 'style', { id: 'fullpage_styles' });
449
        styleElm.appendChild(document.createTextNode(styles));
450
      }
451
      var currentStyleSheetsMap = {};
637 daniel-mar 452
      global$3.each(headElm.getElementsByTagName('link'), function (stylesheet) {
597 daniel-mar 453
        if (stylesheet.rel === 'stylesheet' && stylesheet.getAttribute('data-mce-fullpage')) {
454
          currentStyleSheetsMap[stylesheet.href] = stylesheet;
455
        }
456
      });
637 daniel-mar 457
      global$3.each(headerFragment.getAll('link'), function (stylesheet) {
597 daniel-mar 458
        var href = stylesheet.attr('href');
459
        if (!href) {
460
          return true;
461
        }
462
        if (!currentStyleSheetsMap[href] && stylesheet.attr('rel') === 'stylesheet') {
463
          dom.add(headElm, 'link', {
464
            'rel': 'stylesheet',
465
            'text': 'text/css',
466
            href: href,
467
            'data-mce-fullpage': '1'
468
          });
469
        }
470
        delete currentStyleSheetsMap[href];
471
      });
637 daniel-mar 472
      global$3.each(currentStyleSheetsMap, function (stylesheet) {
597 daniel-mar 473
        stylesheet.parentNode.removeChild(stylesheet);
474
      });
475
    };
476
    var getDefaultHeader = function (editor) {
477
      var header = '', value, styles = '';
478
      if (getDefaultXmlPi(editor)) {
479
        var piEncoding = getDefaultEncoding(editor);
480
        header += '<?xml version="1.0" encoding="' + (piEncoding ? piEncoding : 'ISO-8859-1') + '" ?>\n';
481
      }
482
      header += getDefaultDocType(editor);
483
      header += '\n<html>\n<head>\n';
484
      if (value = getDefaultTitle(editor)) {
485
        header += '<title>' + value + '</title>\n';
486
      }
487
      if (value = getDefaultEncoding(editor)) {
488
        header += '<meta http-equiv="Content-Type" content="text/html; charset=' + value + '" />\n';
489
      }
490
      if (value = getDefaultFontFamily(editor)) {
491
        styles += 'font-family: ' + value + ';';
492
      }
493
      if (value = getDefaultFontSize(editor)) {
494
        styles += 'font-size: ' + value + ';';
495
      }
496
      if (value = getDefaultTextColor(editor)) {
497
        styles += 'color: ' + value + ';';
498
      }
499
      header += '</head>\n<body' + (styles ? ' style="' + styles + '"' : '') + '>\n';
500
      return header;
501
    };
502
    var handleGetContent = function (editor, head, foot, evt) {
503
      if (evt.format === 'html' && !evt.selection && (!evt.source_view || !shouldHideInSourceView(editor))) {
637 daniel-mar 504
        evt.content = unprotectHtml(global$3.trim(head) + '\n' + global$3.trim(evt.content) + '\n' + global$3.trim(foot));
597 daniel-mar 505
      }
506
    };
507
    var setup = function (editor, headState, footState) {
508
      editor.on('BeforeSetContent', function (evt) {
509
        handleSetContent(editor, headState, footState, evt);
510
      });
511
      editor.on('GetContent', function (evt) {
512
        handleGetContent(editor, headState.get(), footState.get(), evt);
513
      });
514
    };
515
 
637 daniel-mar 516
    var register = function (editor) {
597 daniel-mar 517
      editor.ui.registry.addButton('fullpage', {
518
        tooltip: 'Metadata and document properties',
519
        icon: 'document-properties',
520
        onAction: function () {
521
          editor.execCommand('mceFullPageProperties');
522
        }
523
      });
524
      editor.ui.registry.addMenuItem('fullpage', {
525
        text: 'Metadata and document properties',
526
        icon: 'document-properties',
527
        onAction: function () {
528
          editor.execCommand('mceFullPageProperties');
529
        }
530
      });
531
    };
532
 
533
    function Plugin () {
637 daniel-mar 534
      global$4.add('fullpage', function (editor) {
597 daniel-mar 535
        var headState = Cell(''), footState = Cell('');
637 daniel-mar 536
        register$1(editor, headState);
537
        register(editor);
597 daniel-mar 538
        setup(editor, headState, footState);
539
      });
540
    }
541
 
542
    Plugin();
543
 
544
}());