# all files / test/base/module/ Editor.spec.js

99.61% Statements 258/259
63.64% Branches 7/11
100% Functions 58/58
99.61% Lines 257/258

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    3871×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    2×
    37×

    2×
    9×
    9×
    9×
    9×
    9×

    1×
    32×
    32×
    32×
    32×
    32×
    32×

    32×

    1×
    1×
    1×

    8×
    8×

    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1000×

    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×
    1×

    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×

    1×

    1×

    1×

    1×
    1×

    1×
    1×

    1×
    1×

    1×
    1×
    1×
    1×

    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
    1×

    1×

    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×
    1×

    1×

     (function(global){global.wrappers['/Users/lqez/Dev/summernote/test/base/module/Editor.spec.js']=[function(require,module,exports,__dirname,__filename){ "use strict";
    /**
     * Editor.spec.js
     * (c) 2015~ Summernote Team
     * summernote may be freely distributed under the MIT license./
     */
    exports.__esModule = true;
    var chai_1 = require("chai");
    var chai_spies_1 = require("chai-spies");
    var chaidom_1 = require("../../chaidom");
    var jquery_1 = require("jquery");
    var env_1 = require("../../../src/js/base/core/env");
    var range_1 = require("../../../src/js/base/core/range");
    var Context_1 = require("../../../src/js/base/Context");
    require("../../../src/js/bs4/settings");
    describe('Editor', function () {
        var expect = chai_1["default"].expect;
        chai_1["default"].use(chai_spies_1["default"]);
        chai_1["default"].use(chaidom_1["default"]);
        var editor, context, $editable;
        function expectContents(context, markup) {
            expect(context.layoutInfo.editable.html()).to.equalsIgnoreCase(markup);
        }
        function expectToHaveBeenCalled(context, customEvent, handler) {
            var $note = context.layoutInfo.note;
            var spy = chai_1["default"].spy();
            $note.on(customEvent, spy);
            handler();
            expect(spy).to.have.been.called();
        }
        beforeEach(function () {
            jquery_1["default"]('body').empty(); // important !
            var $note = jquery_1["default"]('<div><p>hello</p></div>');
            var options = jquery_1["default"].extend({}, jquery_1["default"].summernote.options);
            context = new Context_1["default"]($note, options);
            editor = context.modules.editor;
            $editable = context.layoutInfo.editable;
            // [workaround]
            //  - Firefox need setTimeout for applying contents
            //  - IE8-11 can't create range in headless mode
            Iif (env_1["default"].isFF || env_1["default"].isMSIE || env_1["default"].isEdge) {
                this.skip();
            }
        });
        describe('initialize', function () {
            it('should bind custom events', function () {
                [
                    'keydown', 'keyup', 'blur', 'mousedown', 'mouseup',
                    'scroll', 'focusin', 'focusout',
                ].forEach(function (eventName) {
                    expectToHaveBeenCalled(context, 'summernote.' + eventName, function () {
                        $editable.trigger(eventName);
                    });
                });
                expectToHaveBeenCalled(context, 'summernote.change', function () {
                    editor.insertText('hello');
                });
            });
        });
        Eif (env_1["default"].isWebkit) {
            describe('undo and redo', function () {
                it('should control history', function () {
                    editor.insertText(' world');
                    expectContents(context, '<p>hello world</p>');
                    editor.undo();
                    expectContents(context, '<p>hello</p>');
                    editor.redo();
                    expectContents(context, '<p>hello world</p>');
                });
            });
        }
        describe('tab', function () {
            it('should insert tab', function () {
                editor.tab();
                expectContents(context, '<p>hello&nbsp;&nbsp;&nbsp;&nbsp;</p>');
            });
        });
        describe('insertParagraph', function () {
            it('should insert paragraph', function () {
                editor.insertParagraph();
                expectContents(context, '<p>hello</p><p><br></p>');
                editor.insertParagraph();
                expectContents(context, '<p>hello</p><p><br></p><p><br></p>');
            });
        });
        Eif (env_1["default"].isWebkit) {
            describe('insertImage', function () {
                it('should insert image', function () {
                    var source = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAAF0lEQVQYGWP8////fwYsgAmLGFiIHhIAT+oECGHuN2UAAAAASUVORK5CYII=';
                    return editor.insertImage(source, 'image').then(function () {
                        expectContents(context, '<p>hello<img src="' + source + '" data-filename="image" style="width: 0px;"></p>');
                    });
                });
            });
        }
        describe('insertOrderedList and insertUnorderedList', function () {
            it('should toggle paragraph to list', function () {
                editor.insertOrderedList();
                expectContents(context, '<ol><li>hello</li></ol>');
                editor.insertUnorderedList();
                expectContents(context, '<ul><li>hello</li></ul>');
                editor.insertUnorderedList();
                expectContents(context, '<p>hello</p>');
            });
        });
        describe('indent and outdent', function () {
            // [workaround] style is different by browser
            Eif (env_1["default"].isPhantom) {
                it('should indent and outdent paragraph', function () {
                    editor.indent();
                    expectContents(context, '<p style="margin-left: 25px;">hello</p>');
                    editor.outdent();
                    expectContents(context, '<p style="">hello</p>');
                });
            }
            it('should indent and outdent list', function () {
                editor.insertOrderedList();
                expectContents(context, '<ol><li>hello</li></ol>');
                editor.indent();
                expectContents(context, '<ol><li><ol><li>hello</li></ol></li></ol>');
                editor.indent();
                expectContents(context, '<ol><li><ol><li><ol><li>hello</li></ol></li></ol></li></ol>');
                editor.outdent();
                expectContents(context, '<ol><li><ol><li>hello</li></ol></li></ol>');
                editor.outdent();
                expectContents(context, '<ol><li>hello</li></ol>');
            });
        });
        describe('insertNode', function () {
            it('should insert node', function () {
                editor.insertNode(jquery_1["default"]('<span> world</span>')[0]);
                expectContents(context, '<p>hello<span> world</span></p>');
            });
            it('should be limited', function () {
                var options = jquery_1["default"].extend({}, jquery_1["default"].summernote.options);
                options.maxTextLength = 5;
                context = new Context_1["default"](jquery_1["default"]('<div><p>hello</p></div>'), options);
                editor = context.modules.editor;
                editor.insertNode(jquery_1["default"]('<span> world</span>')[0]);
                expectContents(context, '<p>hello</p>');
            });
            it('should insert node in last focus', function () {
                $editable.appendTo('body');
                context.invoke('editor.focus');
                editor.insertNode(jquery_1["default"]('<span> world</span>')[0]);
                jquery_1["default"]('body').focus();
                editor.insertNode(jquery_1["default"]('<span> hello</span>')[0]);
                expectContents(context, '<p><span> world</span><span> hello</span>hello</p>');
                $editable.remove();
            });
        });
        describe('insertText', function () {
            it('should insert text', function () {
                editor.insertText(' world');
                expectContents(context, '<p>hello world</p>');
            });
            it('should be limited', function () {
                var options = jquery_1["default"].extend({}, jquery_1["default"].summernote.options);
                options.maxTextLength = 5;
                context = new Context_1["default"](jquery_1["default"]('<div><p>hello</p></div>'), options);
                editor = context.modules.editor;
                editor.insertText(' world');
                expectContents(context, '<p>hello</p>');
            });
            it('should insert text in last focus', function () {
                $editable.appendTo('body');
                context.invoke('editor.focus');
                editor.insertText(' world');
                jquery_1["default"]('body').focus();
                editor.insertText(' summernote ');
                expectContents(context, '<p> world summernote hello</p>');
                $editable.remove();
            });
        });
        describe('pasteHTML', function () {
            it('should paste html', function () {
                editor.pasteHTML('<span> world</span>');
                expectContents(context, '<p>hello<span> world</span></p>');
            });
            it('should not call change change event more than once per paste event', function () {
                var generateLargeHtml = function () {
                    var html = '<div>';
                    for (var i = 0; i < 1000; i++) {
                        html += '<p>HTML element #' + i + '</p>';
                    }
                    html += '</div>';
                    return html;
                };
                var $note = context.layoutInfo.note;
                var spy = chai_1["default"].spy();
                $note.on('summernote.change', spy);
                var html = generateLargeHtml();
                editor.pasteHTML(html);
                expect(spy).to.have.been.called.once;
            });
            it('should be limited', function () {
                var options = jquery_1["default"].extend({}, jquery_1["default"].summernote.options);
                options.maxTextLength = 5;
                context = new Context_1["default"](jquery_1["default"]('<div><p>hello</p></div>'), options);
                editor = context.modules.editor;
                editor.pasteHTML('<span> world</span>');
                expectContents(context, '<p>hello</p>');
            });
        });
        describe('insertHorizontalRule', function () {
            it('should insert horizontal rule', function () {
                editor.insertHorizontalRule();
                expectContents(context, '<p>hello</p><hr><p><br></p>');
            });
        });
        describe('insertTable', function () {
            it('should insert table', function () {
                var markup = [
                    '<p>hello</p>',
                    '<table class="table table-bordered"><tbody>',
                    '<tr><td><br></td><td><br></td></tr>',
                    '<tr><td><br></td><td><br></td></tr>',
                    '</tbody></table>',
                    '<p><br></p>',
                ].join('');
                editor.insertTable('2x2');
                expectContents(context, markup);
            });
        });
        describe('empty', function () {
            it('should make contents empty', function () {
                editor.empty();
                expect(editor.isEmpty()).to.be["true"];
            });
        });
        describe('formatBlock', function () {
            it('should apply formatBlock', function () {
                $editable.appendTo('body');
                editor.formatBlock('blockquote');
                // start <p>hello</p> => <blockquote>hello</blockquote>
                expectContents(context, '<blockquote>hello</blockquote>');
            });
            it('should apply multi formatBlock', function () {
                // set multi block html
                var codes = [
                    '<p><a href="http://summernote.org">hello world</a></p>',
                    '<p><a href="http://summernote.org">hello world</a></p>',
                    '<p><a href="http://summernote.org">hello world</a></p>',
                ];
                context.invoke('code', codes.join(''));
                // run formatBlock
                $editable.appendTo('body');
                editor.formatBlock('blockquote');
                // check current range position in blockquote element
                var nodeName = $editable.children()[0].nodeName;
                expect(nodeName).to.equalsIgnoreCase('blockquote');
            });
            it('should apply multi test 2 - formatBlock', function () {
                var codes = [
                    '<p><a href="http://summernote.org">hello world</a></p>',
                    '<p><a href="http://summernote.org">hello world</a></p>',
                    '<p><a href="http://summernote.org">hello world</a></p>',
                ];
                context.invoke('code', codes.join(''));
                $editable.appendTo('body');
                var startNode = $editable.find('p').first()[0];
                var endNode = $editable.find('p').last()[0];
                // all p tags is wrapped
                range_1["default"].create(startNode, 1, endNode, 1).normalize().select();
                editor.formatBlock('blockquote');
                var nodeName = $editable.children()[0].nodeName;
                expect(nodeName).to.equalsIgnoreCase('blockquote');
                // p -> blockquote, p is none
                expect($editable.find('p').length).to.equals(0);
            });
            it('should apply custom className in formatBlock', function () {
                var $target = jquery_1["default"]('<blockquote class="blockquote" />');
                $editable.appendTo('body');
                editor.formatBlock('blockquote', $target);
                // start <p>hello</p> => <blockquote class="blockquote">hello</blockquote>
                expectContents(context, '<blockquote class="blockquote">hello</blockquote>');
            });
            it('should find exact target in formatBlock', function () {
                var $target = jquery_1["default"]('<a class="dropdown-item" href="#" data-value="blockquote" role="listitem" aria-label="blockquote"><blockquote class="blockquote">Blockquote</blockquote></a>');
                $editable.appendTo('body');
                editor.formatBlock('blockquote', $target);
                // start <p>hello</p> => <blockquote class="blockquote">hello</blockquote>
                expectContents(context, '<blockquote class="blockquote">hello</blockquote>');
            });
        });
        describe('createLink', function () {
            it('should create normal link', function () {
                var text = 'hello';
                var pNode = $editable.find('p')[0];
                var textNode = pNode.childNodes[0];
                var startIndex = textNode.wholeText.indexOf(text);
                var endIndex = startIndex + text.length;
                range_1["default"].create(textNode, startIndex, textNode, endIndex).normalize().select();
                // check creation normal link
                editor.createLink({
                    url: 'http://summernote.org',
                    text: 'summernote'
                });
                expectContents(context, '<p>hello<a href="http://summernote.org">summernote</a></p>');
            });
            it('should create a link with range', function () {
                var text = 'hello';
                var pNode = $editable.find('p')[0];
                var textNode = pNode.childNodes[0];
                var startIndex = textNode.wholeText.indexOf(text);
                var endIndex = startIndex + text.length;
                var rng = range_1["default"].create(textNode, startIndex, textNode, endIndex);
                editor.createLink({
                    url: 'http://summernote.org',
                    text: 'summernote',
                    range: rng
                });
                expectContents(context, '<p><a href="http://summernote.org">summernote</a></p>');
            });
            it('should create a link with isNewWindow', function () {
                var text = 'hello';
                var pNode = $editable.find('p')[0];
                var textNode = pNode.childNodes[0];
                var startIndex = textNode.wholeText.indexOf(text);
                var endIndex = startIndex + text.length;
                var rng = range_1["default"].create(textNode, startIndex, textNode, endIndex);
                editor.createLink({
                    url: 'http://summernote.org',
                    text: 'summernote',
                    range: rng,
                    isNewWindow: true
                });
                expectContents(context, '<p><a href="http://summernote.org" target="_blank">summernote</a></p>');
            });
            it('should create a relative link without scheme', function () {
                var text = 'hello';
                var pNode = $editable.find('p')[0];
                var textNode = pNode.childNodes[0];
                var startIndex = textNode.wholeText.indexOf(text);
                var endIndex = startIndex + text.length;
                var rng = range_1["default"].create(textNode, startIndex, textNode, endIndex);
                editor.createLink({
                    url: '/relative/url',
                    text: 'summernote',
                    range: rng,
                    isNewWindow: true
                });
                expectContents(context, '<p><a href="/relative/url" target="_blank">summernote</a></p>');
            });
            it('should modify a link', function () {
                context.invoke('code', '<p><a href="http://summernote.org">hello world</a></p>');
                var anchorNode = $editable.find('a')[0];
                var rng = range_1["default"].createFromNode(anchorNode);
                editor.createLink({
                    url: 'http://wow.summernote.org',
                    text: 'summernote wow',
                    range: rng
                });
                expectContents(context, '<p><a href="http://wow.summernote.org">summernote wow</a></p>');
            });
            it('should be limited when creating a link', function () {
                var options = jquery_1["default"].extend({}, jquery_1["default"].summernote.options);
                options.maxTextLength = 5;
                context = new Context_1["default"](jquery_1["default"]('<div><p>hello</p></div>'), options);
                editor = context.modules.editor;
                editor.createLink({
                    url: 'http://summernote.org',
                    text: 'summernote'
                });
                expectContents(context, '<p>hello</p>');
            });
            it('should be limited when modifying a link', function () {
                var options = jquery_1["default"].extend({}, jquery_1["default"].summernote.options);
                options.maxTextLength = 5;
                context = new Context_1["default"](jquery_1["default"]('<p><a href="http://summernote.org">hello</a></p>'), options);
                var editable = context.layoutInfo.editable;
                var anchorNode = editable.find('a')[0];
                var rng = range_1["default"].createFromNode(anchorNode);
                editor = context.modules.editor;
                editor.createLink({
                    url: 'http://summernote.org',
                    text: 'hello world',
                    range: rng
                });
                expectContents(context, '<a href="http://summernote.org">hello</a>');
            });
        });
    });

    },'test/base/module/Editor.spec.js',{"chai":"/Users/lqez/Dev/summernote/node_modules/chai/index.js","chai-spies":"/Users/lqez/Dev/summernote/node_modules/chai-spies/chai-spies.js","../../chaidom":"/Users/lqez/Dev/summernote/test/chaidom.js","jquery":"/Users/lqez/Dev/summernote/node_modules/jquery/dist/jquery.js","../../../src/js/base/core/env":"/Users/lqez/Dev/summernote/src/js/base/core/env.js","../../../src/js/base/core/range":"/Users/lqez/Dev/summernote/src/js/base/core/range.js","../../../src/js/base/Context":"/Users/lqez/Dev/summernote/src/js/base/Context.js","../../../src/js/bs4/settings":"/Users/lqez/Dev/summernote/src/js/bs4/settings.js"}];})(this);
    //# sourceMappingURL=data:application/json;charset=utf-8;base64,