123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- import Delta from 'quill-delta';
- import Editor from '../../../core/editor';
- describe('Code', function() {
- it('format newline', function() {
- const editor = this.initialize(Editor, '<p><br></p>');
- editor.formatLine(0, 1, { 'code-block': true });
- expect(editor.scroll.domNode).toEqualHTML(
- `<div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block"><br></div>
- </div>`,
- );
- });
- it('format lines', function() {
- const editor = this.initialize(Editor, '<p><em>0123</em></p><p>5678</p>');
- editor.formatLine(2, 5, { 'code-block': true });
- expect(editor.getDelta()).toEqual(
- new Delta()
- .insert('0123')
- .insert('\n', { 'code-block': true })
- .insert('5678')
- .insert('\n', { 'code-block': true }),
- );
- expect(editor.scroll.domNode).toEqualHTML(
- `<div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">0123</div>
- <div class="ql-code-block">5678</div>
- </div>`,
- );
- });
- it('remove format', function() {
- const editor = this.initialize(
- Editor,
- '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div>',
- );
- editor.formatText(4, 1, { 'code-block': false });
- expect(editor.getDelta()).toEqual(new Delta().insert('0123\n'));
- expect(editor.scroll.domNode).toEqualHTML('<p>0123</p>');
- });
- it('delete last', function() {
- const editor = this.initialize(
- Editor,
- '<p>0123</p><div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block"><br></div></div><p>5678</p>',
- );
- editor.deleteText(4, 1);
- expect(editor.getDelta()).toEqual(
- new Delta()
- .insert('0123')
- .insert('\n', { 'code-block': true })
- .insert('5678\n'),
- );
- expect(editor.scroll.domNode).toEqualHTML(
- '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div><p>5678</p>',
- );
- });
- it('delete merge before', function() {
- const editor = this.initialize(
- Editor,
- '<h1>0123</h1><div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">4567</div></div>',
- );
- editor.deleteText(4, 1);
- expect(editor.getDelta()).toEqual(
- new Delta().insert('01234567').insert('\n', { 'code-block': true }),
- );
- expect(editor.scroll.domNode).toEqualHTML(
- '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">01234567</div></div>',
- );
- });
- it('delete merge after', function() {
- const editor = this.initialize(
- Editor,
- '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div><h1>4567</h1>',
- );
- editor.deleteText(4, 1);
- expect(editor.getDelta()).toEqual(
- new Delta().insert('01234567').insert('\n', { header: 1 }),
- );
- expect(editor.scroll.domNode).toEqualHTML('<h1>01234567</h1>');
- });
- it('delete across before partial merge', function() {
- const editor = this.initialize(
- Editor,
- `<div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">01</div>
- <div class="ql-code-block">34</div>
- <div class="ql-code-block">67</div>
- </div>
- <h1>90</h1>`,
- );
- editor.deleteText(7, 3);
- expect(editor.getDelta()).toEqual(
- new Delta()
- .insert('01')
- .insert('\n', { 'code-block': true })
- .insert('34')
- .insert('\n', { 'code-block': true })
- .insert('60')
- .insert('\n', { header: 1 }),
- );
- expect(editor.scroll.domNode.innerHTML).toEqualHTML(
- `<div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">01</div>
- <div class="ql-code-block">34</div>
- </div>
- <h1>60</h1>`,
- );
- });
- it('delete across before no merge', function() {
- const editor = this.initialize(
- Editor,
- `<div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">01</div>
- <div class="ql-code-block">34</div>
- </div>
- <h1>6789</h1>`,
- );
- editor.deleteText(3, 5);
- expect(editor.getDelta()).toEqual(
- new Delta()
- .insert('01')
- .insert('\n', { 'code-block': true })
- .insert('89')
- .insert('\n', { header: 1 }),
- );
- expect(editor.scroll.domNode.innerHTML).toEqualHTML(
- '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">01</div></div><h1>89</h1>',
- );
- });
- it('delete across after', function() {
- const editor = this.initialize(
- Editor,
- `<h1>0123</h1>
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">56</div>
- <div class="ql-code-block">89</div>
- </div>`,
- );
- editor.deleteText(2, 4);
- expect(editor.getDelta()).toEqual(
- new Delta()
- .insert('016')
- .insert('\n', { 'code-block': true })
- .insert('89')
- .insert('\n', { 'code-block': true }),
- );
- expect(editor.scroll.domNode.innerHTML).toEqualHTML(`
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">016</div>
- <div class="ql-code-block">89</div>
- </div>
- `);
- });
- it('replace', function() {
- const editor = this.initialize(
- Editor,
- '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div>',
- );
- editor.formatText(4, 1, { header: 1 });
- expect(editor.getDelta()).toEqual(
- new Delta().insert('0123').insert('\n', { header: 1 }),
- );
- expect(editor.scroll.domNode).toEqualHTML('<h1>0123</h1>');
- });
- it('replace multiple', function() {
- const editor = this.initialize(
- Editor,
- `
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">01</div>
- <div class="ql-code-block">23</div>
- </div>
- `,
- );
- editor.formatText(0, 6, { header: 1 });
- expect(editor.getDelta()).toEqual(
- new Delta()
- .insert('01')
- .insert('\n', { header: 1 })
- .insert('23')
- .insert('\n', { header: 1 }),
- );
- expect(editor.scroll.domNode).toEqualHTML('<h1>01</h1><h1>23</h1>');
- });
- it('format imprecise bounds', function() {
- const editor = this.initialize(
- Editor,
- `
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">01</div>
- <div class="ql-code-block">23</div>
- <div class="ql-code-block">45</div>
- </div>
- `,
- );
- editor.formatText(1, 6, { header: 1 });
- expect(editor.getDelta()).toEqual(
- new Delta()
- .insert('01')
- .insert('\n', { header: 1 })
- .insert('23')
- .insert('\n', { header: 1 })
- .insert('45')
- .insert('\n', { 'code-block': true }),
- );
- expect(editor.scroll.domNode.innerHTML).toEqualHTML(
- '<h1>01</h1><h1>23</h1><div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">45</div></div>',
- );
- });
- it('format without newline', function() {
- const editor = this.initialize(
- Editor,
- `
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">01</div>
- <div class="ql-code-block">23</div>
- <div class="ql-code-block">45</div>
- </div>
- `,
- );
- editor.formatText(3, 1, { header: 1 });
- expect(editor.getDelta()).toEqual(
- new Delta()
- .insert('01')
- .insert('\n', { 'code-block': true })
- .insert('23')
- .insert('\n', { 'code-block': true })
- .insert('45')
- .insert('\n', { 'code-block': true }),
- );
- expect(editor.scroll.domNode.innerHTML).toEqualHTML(`
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">01</div>
- <div class="ql-code-block">23</div>
- <div class="ql-code-block">45</div>
- </div>
- `);
- });
- it('format line', function() {
- const editor = this.initialize(
- Editor,
- `
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">01</div>
- <div class="ql-code-block">23</div>
- <div class="ql-code-block">45</div>
- </div>
- `,
- );
- editor.formatLine(3, 1, { header: 1 });
- expect(editor.getDelta()).toEqual(
- new Delta()
- .insert('01')
- .insert('\n', { 'code-block': true })
- .insert('23')
- .insert('\n', { header: 1 })
- .insert('45')
- .insert('\n', { 'code-block': true }),
- );
- expect(editor.scroll.domNode.innerHTML).toEqualHTML(`
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">01</div>
- </div>
- <h1>23</h1>
- <div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">45</div>
- </div>
- `);
- });
- it('ignore formatAt', function() {
- const editor = this.initialize(
- Editor,
- '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div>',
- );
- editor.formatText(1, 1, { bold: true });
- expect(editor.getDelta()).toEqual(
- new Delta().insert('0123').insert('\n', { 'code-block': true }),
- );
- expect(editor.scroll.domNode).toEqualHTML(
- '<div class="ql-code-block-container" spellcheck="false"><div class="ql-code-block">0123</div></div>',
- );
- });
- it('partial block modification applyDelta', function() {
- const editor = this.initialize(
- Editor,
- `<div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">a</div>
- <div class="ql-code-block">b</div>
- <div class="ql-code-block"><br></div>
- </div>`,
- );
- const delta = new Delta()
- .retain(3)
- .insert('\n', { 'code-block': true })
- .delete(1)
- .retain(1, { 'code-block': null });
- editor.applyDelta(delta);
- expect(editor.scroll.domNode.innerHTML).toEqualHTML(
- `<div class="ql-code-block-container" spellcheck="false">
- <div class="ql-code-block">a</div>
- <div class="ql-code-block">b</div>
- </div>
- <p><br></p>`,
- );
- });
- });
|