8
0

writer.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. it( 'should transfer markers from given node to the root element of target position', () => {
  46. root.appendChildren( [ new Element( 'div' ) ] );
  47. const docFrag = new DocumentFragment( [
  48. new Element( 'paragraph', null, [
  49. new Text( 'foo bar' ),
  50. ] )
  51. ] );
  52. const range = new Range( new Position( docFrag, [ 1, 2 ] ), new Position( docFrag, [ 1, 4 ] ) );
  53. docFrag.markers.set( 'foo', range );
  54. writer.insert( new Position( root, [ 10, 0 ] ), docFrag );
  55. const marker = doc.markers.get( 'foo' );
  56. const expectedRange = new Range( new Position( root, [ 10, 1, 2 ] ), new Position( root, [ 10, 1, 4 ] ) );
  57. expectData( 'foo<$text bold="true">bar</$text><image src="img.jpg"></image>xyz<div><paragraph>foo bar</paragraph></div>' );
  58. expect( marker.getRange().isEqual( expectedRange ) ).to.true;
  59. } );
  60. } );
  61. describe( 'remove', () => {
  62. it( 'should remove nodes in given range', () => {
  63. const range = Range.createFromParentsAndOffsets( root, 3, root, 6 );
  64. writer.remove( range );
  65. expectData( 'foo<image src="img.jpg"></image>xyz' );
  66. } );
  67. it( 'should split text node if range starts or ends inside text node', () => {
  68. const range = Range.createFromParentsAndOffsets( root, 1, root, 5 );
  69. writer.remove( range );
  70. expectData( 'f<$text bold="true">r</$text><image src="img.jpg"></image>xyz' );
  71. } );
  72. it( 'should merge text nodes if possible', () => {
  73. const range = Range.createFromParentsAndOffsets( root, 3, root, 7 );
  74. writer.remove( range );
  75. expectData( 'fooxyz' );
  76. expect( root.childCount ).to.equal( 1 );
  77. } );
  78. it( 'should throw if given range is not flat', () => {
  79. expect( () => {
  80. writer.remove( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1, 2 ] ) ) );
  81. } ).to.throw( CKEditorError, /model-writer-remove-range-not-flat/ );
  82. } );
  83. } );
  84. describe( 'move', () => {
  85. it( 'should move a range of nodes', () => {
  86. const range = Range.createFromParentsAndOffsets( root, 3, root, 6 );
  87. writer.move( range, Position.createAt( root, 0 ) );
  88. expectData( '<$text bold="true">bar</$text>foo<image src="img.jpg"></image>xyz' );
  89. } );
  90. it( 'should use remove and insert methods', () => {
  91. sinon.spy( writer, 'remove' );
  92. sinon.spy( writer, 'insert' );
  93. const range = Range.createFromParentsAndOffsets( root, 3, root, 6 );
  94. const position = Position.createAt( root, 0 );
  95. writer.move( range, position );
  96. expect( writer.remove.calledWithExactly( range ) ).to.be.true;
  97. expect( writer.insert.calledWith( position ) ).to.be.true;
  98. } );
  99. it( 'should correctly move if target position is in same element as moved range, but after range', () => {
  100. const range = Range.createFromParentsAndOffsets( root, 3, root, 6 );
  101. writer.move( range, Position.createAt( root, 10 ) );
  102. expectData( 'foo<image src="img.jpg"></image>xyz<$text bold="true">bar</$text>' );
  103. } );
  104. it( 'should throw if given range is not flat', () => {
  105. expect( () => {
  106. writer.move( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1, 2 ] ) ), null );
  107. } ).to.throw( CKEditorError, /model-writer-move-range-not-flat/ );
  108. } );
  109. } );
  110. describe( 'setAttribute', () => {
  111. it( 'should set attribute on given range of nodes', () => {
  112. const range = Range.createFromParentsAndOffsets( root, 6, root, 8 );
  113. writer.setAttribute( range, 'newAttr', true );
  114. expectData( 'foo<$text bold="true">bar</$text><image newAttr="true" src="img.jpg"></image><$text newAttr="true">x</$text>yz' );
  115. } );
  116. it( 'should remove attribute if null was passed as a value', () => {
  117. const range = Range.createFromParentsAndOffsets( root, 6, root, 7 );
  118. writer.setAttribute( range, 'src', null );
  119. expectData( 'foo<$text bold="true">bar</$text><image></image>xyz' );
  120. } );
  121. it( 'should merge nodes if possible', () => {
  122. const range = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  123. writer.setAttribute( range, 'bold', true );
  124. expectData( '<$text bold="true">foobar</$text><image src="img.jpg"></image>xyz' );
  125. } );
  126. } );
  127. describe( 'removeAttribute', () => {
  128. it( 'should use setAttribute', () => {
  129. sinon.spy( writer, 'setAttribute' );
  130. const range = Range.createFromParentsAndOffsets( root, 6, root, 7 );
  131. writer.removeAttribute( range, 'src' );
  132. expect( writer.setAttribute.calledWithExactly( range, 'src', null ) ).to.be.true;
  133. } );
  134. } );
  135. } );
  136. describe( 'normalizeNodes', () => {
  137. it( 'should change single object into an array', () => {
  138. const p = new Element( 'p' );
  139. expect( writer.normalizeNodes( p ) ).to.deep.equal( [ p ] );
  140. } );
  141. it( 'should change strings to text nodes', () => {
  142. const text = writer.normalizeNodes( 'abc' )[ 0 ];
  143. expect( text ).to.be.instanceof( Text );
  144. expect( text.data ).to.equal( 'abc' );
  145. } );
  146. it( 'should change text proxies to text nodes', () => {
  147. const textNode = new Text( 'abc' );
  148. const textProxy = new TextProxy( textNode, 1, 1 );
  149. const text = writer.normalizeNodes( textProxy )[ 0 ];
  150. expect( text ).to.be.instanceof( Text );
  151. expect( text.data ).to.equal( 'b' );
  152. } );
  153. it( 'should not change elements', () => {
  154. const p = new Element( 'p' );
  155. expect( writer.normalizeNodes( p )[ 0 ] ).to.equal( p );
  156. } );
  157. it( 'should omit unrecognized objects', () => {
  158. expect( writer.normalizeNodes( 1 ) ).to.deep.equal( [] );
  159. } );
  160. it( 'should accept arrays', () => {
  161. const text = new Text( 'foo', { bold: true } );
  162. const image = new Element( 'image' );
  163. const nodes = [ 'abc', text, image, 1, 'xyz' ];
  164. const normalized = writer.normalizeNodes( nodes );
  165. expect( normalized[ 0 ] ).to.be.instanceof( Text );
  166. expect( normalized[ 1 ] ).to.equal( text );
  167. expect( normalized[ 2 ] ).to.equal( image );
  168. expect( normalized[ 3 ] ).to.be.instanceof( Text );
  169. } );
  170. it( 'should merge text nodes if mergeTextNodes flag is set to true', () => {
  171. const normalized = writer.normalizeNodes( [ 'foo', 'bar' ], true );
  172. expect( normalized.length ).to.equal( 1 );
  173. expect( normalized[ 0 ].data ).to.equal( 'foobar' );
  174. } );
  175. it( 'should replace document fragment by the list of it\'s children', () => {
  176. const nodes = [
  177. new Text( 'foo', { bold: true } ),
  178. new DocumentFragment( [ new Text( 'bar', { bold: true } ), new Element( 'image' ) ] ),
  179. 'xyz'
  180. ];
  181. const normalized = writer.normalizeNodes( nodes, true );
  182. expect( normalized[ 0 ] ).to.be.instanceof( Text );
  183. expect( normalized[ 0 ].getAttribute( 'bold' ) ).to.be.true;
  184. expect( normalized[ 0 ].data ).to.equal( 'foobar' );
  185. expect( normalized[ 1 ].name ).to.equal( 'image' );
  186. expect( normalized[ 2 ].data ).to.equal( 'xyz' );
  187. } );
  188. } );
  189. function expectData( html ) {
  190. expect( getData( doc, { withoutSelection: true } ) ).to.equal( html );
  191. }