writer.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import Writer from '/ckeditor5/core/treeview/writer.js';
  8. import Element from '/ckeditor5/core/treeview/element.js';
  9. import Text from '/ckeditor5/core/treeview/text.js';
  10. import Position from '/ckeditor5/core/treeview/position.js';
  11. describe( 'Writer', () => {
  12. describe( 'isContainer', () => {
  13. it( 'should return true for container elements', () => {
  14. const containerElement = new Element( 'p' );
  15. const attributeElement = new Element( 'b' );
  16. const writer = new Writer();
  17. writer._priorities.set( attributeElement, 1 );
  18. expect( writer.isContainer( containerElement ) ).to.be.true;
  19. expect( writer.isContainer( attributeElement ) ).to.be.false;
  20. } );
  21. } );
  22. describe( 'isAttribute', () => {
  23. it( 'should return true for container elements', () => {
  24. const containerElement = new Element( 'p' );
  25. const attributeElement = new Element( 'b' );
  26. const writer = new Writer();
  27. writer._priorities.set( attributeElement, 1 );
  28. expect( writer.isAttribute( containerElement ) ).to.be.false;
  29. expect( writer.isAttribute( attributeElement ) ).to.be.true;
  30. } );
  31. } );
  32. describe( 'setPriority', () => {
  33. it( 'sets node priority', () => {
  34. const writer = new Writer();
  35. const nodeMock = {};
  36. writer.setPriority( nodeMock, 10 );
  37. expect( writer._priorities.get( nodeMock ) ).to.equal( 10 );
  38. } );
  39. } );
  40. describe( 'getPriority', () => {
  41. it( 'gets node priority', () => {
  42. const writer = new Writer();
  43. const nodeMock = {};
  44. writer._priorities.set( nodeMock, 12 );
  45. expect( writer.getPriority( nodeMock ) ).to.equal( 12 );
  46. } );
  47. } );
  48. describe( 'breakAttributes', () => {
  49. // Start position is at the begin of the text node.
  50. // After breaking it should be at the begin of the 'p' element.
  51. it( '<p>|foobar</p>', () => {
  52. const writer = new Writer();
  53. const text = new Text( 'foobar' );
  54. const element = new Element( 'p', {}, [ text ] );
  55. const position = new Position( text, 0 );
  56. const newPosition = writer.breakAttributes( position );
  57. const newParent = newPosition.parent;
  58. expect( newPosition.offset ).to.equal( 0 );
  59. expect( newParent ).to.equal( element );
  60. expect( newParent.getChildCount() ).to.equal( 1 );
  61. expect( newParent.getChild( 0 ) ).to.equal( text );
  62. } );
  63. // Start position is located in the middle of text node.
  64. // After breaking it should be located inside 'p' element, between two text nodes 'foo' and 'bar'.
  65. it( '<p>foo|bar</p>', () => {
  66. const writer = new Writer();
  67. const text = new Text( 'foobar' );
  68. const element = new Element( 'p', {}, [ text ] );
  69. const position = new Position( text, 3 );
  70. const newPosition = writer.breakAttributes( position );
  71. const newParent = newPosition.parent;
  72. expect( newPosition.offset ).to.equal( 1 );
  73. expect( newParent ).to.equal( element );
  74. expect( newParent.getChildCount() ).to.equal( 2 );
  75. expect( newParent.getChild( 0 ) ).to.be.instanceOf( Text );
  76. expect( newParent.getChild( 1 ) ).to.be.instanceOf( Text );
  77. expect( newParent.getChild( 0 ).data ).to.equal( 'foo' );
  78. expect( newParent.getChild( 1 ).data ).to.equal( 'bar' );
  79. } );
  80. // Start position is at the end of the text node.
  81. // After breaking it should be at the end of the 'p' element.
  82. it( '<p>foobar|</p>', () => {
  83. const writer = new Writer();
  84. const text = new Text( 'foobar' );
  85. const element = new Element( 'p', {}, [ text ] );
  86. const position = new Position( text, 6 );
  87. const newPosition = writer.breakAttributes( position );
  88. const newParent = newPosition.parent;
  89. expect( newPosition.offset ).to.equal( 1 );
  90. expect( newParent ).to.equal( element );
  91. expect( newParent.getChildCount() ).to.equal( 1 );
  92. expect( newParent.getChild( 0 ) ).to.equal( text );
  93. } );
  94. // <p><b>foo|bar</b></p> -> <p><b>foo</b>|<b>bar</b></p>
  95. it( '<p><b>foo|bar</b></p>', () => {
  96. const writer = new Writer();
  97. const text = new Text( 'foobar' );
  98. const b = new Element( 'b', {}, [ text ] );
  99. const p = new Element( 'p', {}, [ b ] );
  100. const position = new Position( text, 3 );
  101. writer.setPriority( b, 1 );
  102. const newPosition = writer.breakAttributes( position );
  103. const parent = newPosition.parent;
  104. expect( parent ).to.equal( p );
  105. expect( newPosition.offset ).to.equal( 1 );
  106. expect( p.getChildCount() ).to.equal( 2 );
  107. const child1 = p.getChild( 0 );
  108. const child2 = p.getChild( 1 );
  109. expect( child1 ).to.be.instanceOf( Element );
  110. expect( child1.name ).to.equal( 'b' );
  111. expect( child1.getChildCount() ).to.equal( 1 );
  112. expect( child1.getChild( 0 ).data ).to.equal( 'foo' );
  113. expect( child2 ).to.be.instanceOf( Element );
  114. expect( child2.name ).to.equal( 'b' );
  115. expect( child2.getChildCount() ).to.equal( 1 );
  116. expect( child2.getChild( 0 ).data ).to.equal( 'bar' );
  117. } );
  118. // <p><b><u>|foobar</u></b></p> -> <p>|<b><u>foobar</u></b></p>
  119. it( '<p><b><u>|foobar</u></b></p>', () => {
  120. const writer = new Writer();
  121. const text = new Text( 'foobar' );
  122. const u = new Element( 'u', {}, [ text ] );
  123. const b = new Element( 'b', {}, [ u ] );
  124. const p = new Element( 'p', {}, [ b ] );
  125. const position = new Position( text, 0 );
  126. writer.setPriority( u, 1 );
  127. writer.setPriority( b, 1 );
  128. const newPosition = writer.breakAttributes( position );
  129. const parent = newPosition.parent;
  130. expect( parent ).to.equal( p );
  131. expect( newPosition.offset ).to.equal( 0 );
  132. expect( p.getChildCount() ).to.equal( 1 );
  133. const b1 = p.getChild( 0 );
  134. expect( b1 ).to.equal( b );
  135. expect( b1.getChildCount() ).to.equal( 1 );
  136. const u1 = b.getChild( 0 );
  137. expect( u1 ).to.equal( u );
  138. expect( u1.getChildCount() ).to.equal( 1 );
  139. const text1 = u1.getChild( 0 );
  140. expect( text1 ).to.equal( text );
  141. expect( text1.data ).to.equal( 'foobar' );
  142. } );
  143. // <p><b><u>foo|bar</u></b></p> -> <p><b><u>foo</u></b>|<b></u>bar</u></b></p>
  144. it( '<p><b><u>foo|bar</u></b></p>', () => {
  145. const writer = new Writer();
  146. const text = new Text( 'foobar' );
  147. const u = new Element( 'u', {}, [ text ] );
  148. const b = new Element( 'b', {}, [ u ] );
  149. const p = new Element( 'p', {}, [ b ] );
  150. const position = new Position( text, 3 );
  151. writer.setPriority( u, 1 );
  152. writer.setPriority( b, 1 );
  153. const newPosition = writer.breakAttributes( position );
  154. const parent = newPosition.parent;
  155. expect( parent ).to.equal( p );
  156. expect( newPosition.offset ).to.equal( 1 );
  157. expect( parent.getChildCount() ).to.equal( 2 );
  158. const child1 = parent.getChild( 0 );
  159. const child2 = parent.getChild( 1 );
  160. expect( child1.name ).to.equal( 'b' );
  161. expect( child1.name ).to.equal( 'b' );
  162. expect( child1.getChildCount() ).to.equal( 1 );
  163. expect( child2.getChildCount() ).to.equal( 1 );
  164. const u1 = child1.getChild( 0 );
  165. const u2 = child2.getChild( 0 );
  166. expect( u1.name ).to.equal( 'u' );
  167. expect( u2.name ).to.equal( 'u' );
  168. expect( u1.getChildCount() ).to.equal( 1 );
  169. expect( u2.getChildCount() ).to.equal( 1 );
  170. expect( u1.getChild( 0 ).data ).to.equal( 'foo' );
  171. expect( u2.getChild( 0 ).data ).to.equal( 'bar' );
  172. } );
  173. // <p><b><u>foobar|</u></b></p> -> <p><b><u>foobar</u></b>|</p>
  174. it( '<p><b><u>|foobar</u></b></p>', () => {
  175. const writer = new Writer();
  176. const text = new Text( 'foobar' );
  177. const u = new Element( 'u', {}, [ text ] );
  178. const b = new Element( 'b', {}, [ u ] );
  179. const p = new Element( 'p', {}, [ b ] );
  180. const position = new Position( text, 6 );
  181. writer.setPriority( u, 1 );
  182. writer.setPriority( b, 1 );
  183. const newPosition = writer.breakAttributes( position );
  184. const parent = newPosition.parent;
  185. expect( parent ).to.equal( p );
  186. expect( newPosition.offset ).to.equal( 1 );
  187. expect( p.getChildCount() ).to.equal( 1 );
  188. const b1 = p.getChild( 0 );
  189. expect( b1 ).to.equal( b );
  190. expect( b1.getChildCount() ).to.equal( 1 );
  191. const u1 = b.getChild( 0 );
  192. expect( u1 ).to.equal( u );
  193. expect( u1.getChildCount() ).to.equal( 1 );
  194. const text1 = u1.getChild( 0 );
  195. expect( text1 ).to.equal( text );
  196. expect( text1.data ).to.equal( 'foobar' );
  197. } );
  198. } );
  199. } );