utils.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 * as utils from '../../../src/model/operation/utils';
  13. import { getData } from '../../../src/dev-utils/model';
  14. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  15. let doc, root;
  16. describe( 'writer utils', () => {
  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. utils._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. utils._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. utils._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. utils._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. utils._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. utils._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. utils._remove( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1, 2 ] ) ) );
  66. } ).to.throw( CKEditorError, /operation-utils-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. utils._move( range, Position.createAt( root, 0 ) );
  73. expectData( '<$text bold="true">bar</$text>foo<image src="img.jpg"></image>xyz' );
  74. } );
  75. it( 'should correctly move if target position is in same element as moved range, but after range', () => {
  76. const range = Range.createFromParentsAndOffsets( root, 3, root, 6 );
  77. utils._move( range, Position.createAt( root, 10 ) );
  78. expectData( 'foo<image src="img.jpg"></image>xyz<$text bold="true">bar</$text>' );
  79. } );
  80. it( 'should throw if given range is not flat', () => {
  81. expect( () => {
  82. utils._move( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1, 2 ] ) ), null );
  83. } ).to.throw( CKEditorError, /operation-utils-move-range-not-flat/ );
  84. } );
  85. } );
  86. describe( 'setAttribute', () => {
  87. it( 'should set attribute on given range of nodes', () => {
  88. const range = Range.createFromParentsAndOffsets( root, 6, root, 8 );
  89. utils._setAttribute( range, 'newAttr', true );
  90. expectData( 'foo<$text bold="true">bar</$text><image newAttr="true" src="img.jpg"></image><$text newAttr="true">x</$text>yz' );
  91. } );
  92. it( 'should remove attribute if null was passed as a value', () => {
  93. const range = Range.createFromParentsAndOffsets( root, 6, root, 7 );
  94. utils._setAttribute( range, 'src', null );
  95. expectData( 'foo<$text bold="true">bar</$text><image></image>xyz' );
  96. } );
  97. it( 'should merge nodes if possible', () => {
  98. const range = Range.createFromParentsAndOffsets( root, 0, root, 3 );
  99. utils._setAttribute( range, 'bold', true );
  100. expectData( '<$text bold="true">foobar</$text><image src="img.jpg"></image>xyz' );
  101. } );
  102. } );
  103. } );
  104. describe( 'normalizeNodes', () => {
  105. it( 'should change single object into an array', () => {
  106. const p = new Element( 'p' );
  107. expect( utils._normalizeNodes( p ) ).to.deep.equal( [ p ] );
  108. } );
  109. it( 'should change strings to text nodes', () => {
  110. const text = utils._normalizeNodes( 'abc' )[ 0 ];
  111. expect( text ).to.be.instanceof( Text );
  112. expect( text.data ).to.equal( 'abc' );
  113. } );
  114. it( 'should change text proxies to text nodes', () => {
  115. const textNode = new Text( 'abc' );
  116. const textProxy = new TextProxy( textNode, 1, 1 );
  117. const text = utils._normalizeNodes( textProxy )[ 0 ];
  118. expect( text ).to.be.instanceof( Text );
  119. expect( text.data ).to.equal( 'b' );
  120. } );
  121. it( 'should not change elements', () => {
  122. const p = new Element( 'p' );
  123. expect( utils._normalizeNodes( p )[ 0 ] ).to.equal( p );
  124. } );
  125. it( 'should omit unrecognized objects', () => {
  126. expect( utils._normalizeNodes( 1 ) ).to.deep.equal( [] );
  127. } );
  128. it( 'should accept arrays', () => {
  129. const text = new Text( 'foo', { bold: true } );
  130. const image = new Element( 'image' );
  131. const nodes = [ 'abc', text, image, 1, 'xyz' ];
  132. const normalized = utils._normalizeNodes( nodes );
  133. expect( normalized[ 0 ] ).to.be.instanceof( Text );
  134. expect( normalized[ 1 ] ).to.equal( text );
  135. expect( normalized[ 2 ] ).to.equal( image );
  136. expect( normalized[ 3 ] ).to.be.instanceof( Text );
  137. } );
  138. it( 'should merge text nodes if mergeTextNodes flag is set to true', () => {
  139. const normalized = utils._normalizeNodes( [ 'foo', 'bar' ], true );
  140. expect( normalized.length ).to.equal( 1 );
  141. expect( normalized[ 0 ].data ).to.equal( 'foobar' );
  142. } );
  143. it( 'should replace document fragment by the list of it\'s children', () => {
  144. const nodes = [
  145. new Text( 'foo', { bold: true } ),
  146. new DocumentFragment( [ new Text( 'bar', { bold: true } ), new Element( 'image' ) ] ),
  147. 'xyz'
  148. ];
  149. const normalized = utils._normalizeNodes( nodes, true );
  150. expect( normalized[ 0 ] ).to.be.instanceof( Text );
  151. expect( normalized[ 0 ].getAttribute( 'bold' ) ).to.be.true;
  152. expect( normalized[ 0 ].data ).to.equal( 'foobar' );
  153. expect( normalized[ 1 ].name ).to.equal( 'image' );
  154. expect( normalized[ 2 ].data ).to.equal( 'xyz' );
  155. } );
  156. } );
  157. function expectData( html ) {
  158. expect( getData( doc, { withoutSelection: true } ) ).to.equal( html );
  159. }