writer.js 7.7 KB

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