writer.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  6. import DocumentFragment from '../../src/model/documentfragment';
  7. import Element from '../../src/model/element';
  8. import Text from '../../src/model/text';
  9. import TextProxy from '../../src/model/textproxy';
  10. import Position from '../../src/model/position';
  11. import Range from '../../src/model/range';
  12. import writer from '../../src/model/writer';
  13. import { getData } from '../../src/dev-utils/model';
  14. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  15. let doc, root;
  16. describe( 'writer', () => {
  17. beforeEach( () => {
  18. doc = new Document();
  19. doc.schema.allow( { name: '$text', inside: '$root' } );
  20. root = doc.createRoot();
  21. // index: 0001112333
  22. // offset: 0123456789
  23. // data: foobarIxyz
  24. // bold: ___BBBB___
  25. root.appendChildren( [
  26. new Text( 'foo' ),
  27. new Text( 'bar', { bold: true } ),
  28. new Element( 'image', { src: 'img.jpg' } ),
  29. new Text( 'xyz' )
  30. ] );
  31. } );
  32. describe( 'insert', () => {
  33. it( 'should insert nodes between nodes', () => {
  34. writer.insert( Position.createAt( root, 3 ), [ 'xxx', new Element( 'p' ) ] );
  35. expectData( 'fooxxx<p></p><$text bold="true">bar</$text><image src="img.jpg"></image>xyz' );
  36. } );
  37. it( 'should split text node if nodes at inserted at offset inside text node', () => {
  38. writer.insert( Position.createAt( root, 5 ), new Element( 'p' ) );
  39. expectData( 'foo<$text bold="true">ba</$text><p></p><$text bold="true">r</$text><image src="img.jpg"></image>xyz' );
  40. } );
  41. it( 'should merge text nodes if possible', () => {
  42. writer.insert( Position.createAt( root, 3 ), new Text( 'xxx', { bold: true } ) );
  43. expectData( 'foo<$text bold="true">xxxbar</$text><image src="img.jpg"></image>xyz' );
  44. } );
  45. } );
  46. describe( 'remove', () => {
  47. it( 'should remove nodes in given range', () => {
  48. const range = Range.createFromParentsAndOffsets( root, 3, root, 6 );
  49. writer.remove( range );
  50. expectData( 'foo<image src="img.jpg"></image>xyz' );
  51. } );
  52. it( 'should split text node if range starts or ends inside text node', () => {
  53. const range = Range.createFromParentsAndOffsets( root, 1, root, 5 );
  54. writer.remove( range );
  55. expectData( 'f<$text bold="true">r</$text><image src="img.jpg"></image>xyz' );
  56. } );
  57. it( 'should merge text nodes if possible', () => {
  58. const range = Range.createFromParentsAndOffsets( root, 3, root, 7 );
  59. writer.remove( range );
  60. expectData( 'fooxyz' );
  61. expect( root.childCount ).to.equal( 1 );
  62. } );
  63. it( 'should throw if given range is not flat', () => {
  64. expect( () => {
  65. writer.remove( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1, 2 ] ) ) );
  66. } ).to.throw( CKEditorError, /model-writer-remove-range-not-flat/ );
  67. } );
  68. } );
  69. describe( 'move', () => {
  70. it( 'should move a range of nodes', () => {
  71. const range = Range.createFromParentsAndOffsets( root, 3, root, 6 );
  72. writer.move( range, Position.createAt( root, 0 ) );
  73. expectData( '<$text bold="true">bar</$text>foo<image src="img.jpg"></image>xyz' );
  74. } );
  75. it( 'should use remove and insert methods', () => {
  76. sinon.spy( writer, 'remove' );
  77. sinon.spy( writer, 'insert' );
  78. const range = Range.createFromParentsAndOffsets( root, 3, root, 6 );
  79. const position = Position.createAt( root, 0 );
  80. writer.move( range, position );
  81. expect( writer.remove.calledWithExactly( range ) ).to.be.true;
  82. expect( writer.insert.calledWith( position ) ).to.be.true;
  83. } );
  84. it( 'should correctly move if target position is in same element as moved range, but after range', () => {
  85. const range = Range.createFromParentsAndOffsets( root, 3, root, 6 );
  86. writer.move( range, Position.createAt( root, 10 ) );
  87. expectData( 'foo<image src="img.jpg"></image>xyz<$text bold="true">bar</$text>' );
  88. } );
  89. it( 'should throw if given range is not flat', () => {
  90. expect( () => {
  91. writer.move( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1, 2 ] ) ), null );
  92. } ).to.throw( CKEditorError, /model-writer-move-range-not-flat/ );
  93. } );
  94. } );
  95. describe( 'setAttribute', () => {
  96. it( 'should set attribute on given range of nodes', () => {
  97. const range = Range.createFromParentsAndOffsets( root, 6, root, 8 );
  98. writer.setAttribute( range, 'newAttr', true );
  99. expectData( 'foo<$text bold="true">bar</$text><image newAttr="true" src="img.jpg"></image><$text newAttr="true">x</$text>yz' );
  100. } );
  101. it( 'should remove attribute if null was passed as a value', () => {
  102. const range = Range.createFromParentsAndOffsets( root, 6, root, 7 );
  103. writer.setAttribute( range, 'src', null );
  104. expectData( 'foo<$text bold="true">bar</$text><image></image>xyz' );
  105. } );
  106. it( 'should merge nodes if possible', () => {
  107. const range = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  108. writer.setAttribute( range, 'bold', true );
  109. expectData( '<$text bold="true">foobar</$text><image src="img.jpg"></image>xyz' );
  110. } );
  111. } );
  112. describe( 'removeAttribute', () => {
  113. it( 'should use setAttribute', () => {
  114. sinon.spy( writer, 'setAttribute' );
  115. const range = Range.createFromParentsAndOffsets( root, 6, root, 7 );
  116. writer.removeAttribute( range, 'src' );
  117. expect( writer.setAttribute.calledWithExactly( range, 'src', null ) ).to.be.true;
  118. } );
  119. } );
  120. } );
  121. describe( 'normalizeNodes', () => {
  122. it( 'should change single object into an array', () => {
  123. const p = new Element( 'p' );
  124. expect( writer.normalizeNodes( p ) ).to.deep.equal( [ p ] );
  125. } );
  126. it( 'should change strings to text nodes', () => {
  127. const text = writer.normalizeNodes( 'abc' )[ 0 ];
  128. expect( text ).to.be.instanceof( Text );
  129. expect( text.data ).to.equal( 'abc' );
  130. } );
  131. it( 'should change text proxies to text nodes', () => {
  132. const textNode = new Text( 'abc' );
  133. const textProxy = new TextProxy( textNode, 1, 1 );
  134. const text = writer.normalizeNodes( textProxy )[ 0 ];
  135. expect( text ).to.be.instanceof( Text );
  136. expect( text.data ).to.equal( 'b' );
  137. } );
  138. it( 'should not change elements', () => {
  139. const p = new Element( 'p' );
  140. expect( writer.normalizeNodes( p )[ 0 ] ).to.equal( p );
  141. } );
  142. it( 'should omit unrecognized objects', () => {
  143. expect( writer.normalizeNodes( 1 ) ).to.deep.equal( [] );
  144. } );
  145. it( 'should accept arrays', () => {
  146. const text = new Text( 'foo', { bold: true } );
  147. const image = new Element( 'image' );
  148. const nodes = [ 'abc', text, image, 1, 'xyz' ];
  149. const normalized = writer.normalizeNodes( nodes );
  150. expect( normalized[ 0 ] ).to.be.instanceof( Text );
  151. expect( normalized[ 1 ] ).to.equal( text );
  152. expect( normalized[ 2 ] ).to.equal( image );
  153. expect( normalized[ 3 ] ).to.be.instanceof( Text );
  154. } );
  155. it( 'should merge text nodes if mergeTextNodes flag is set to true', () => {
  156. const normalized = writer.normalizeNodes( [ 'foo', 'bar' ], true );
  157. expect( normalized.length ).to.equal( 1 );
  158. expect( normalized[ 0 ].data ).to.equal( 'foobar' );
  159. } );
  160. it( 'should replace document fragment by the list of it\'s children', () => {
  161. const nodes = [
  162. new Text( 'foo', { bold: true } ),
  163. new DocumentFragment( [ new Text( 'bar', { bold: true } ), new Element( 'image' ) ] ),
  164. 'xyz'
  165. ];
  166. const normalized = writer.normalizeNodes( nodes, true );
  167. expect( normalized[ 0 ] ).to.be.instanceof( Text );
  168. expect( normalized[ 0 ].getAttribute( 'bold' ) ).to.be.true;
  169. expect( normalized[ 0 ].data ).to.equal( 'foobar' );
  170. expect( normalized[ 1 ].name ).to.equal( 'image' );
  171. expect( normalized[ 2 ].data ).to.equal( 'xyz' );
  172. } );
  173. } );
  174. function expectData( html ) {
  175. expect( getData( doc, { withoutSelection: true } ) ).to.equal( html );
  176. }