8
0

view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import { stringify, parse } from '/tests/engine/_utils/view.js';
  7. import DocumentFragment from '/ckeditor5/engine/treeview/documentfragment.js';
  8. import Element from '/ckeditor5/engine/treeview/element.js';
  9. import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js';
  10. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  11. import Text from '/ckeditor5/engine/treeview/text.js';
  12. import Selection from '/ckeditor5/engine/treeview/selection.js';
  13. import Range from '/ckeditor5/engine/treeview/range.js';
  14. describe( 'view test utils', () => {
  15. describe( 'getData', () => {
  16. it( 'should write text', () => {
  17. const text = new Text( 'foobar' );
  18. expect( stringify( text ) ).to.equal( 'foobar' );
  19. } );
  20. it( 'should write elements and texts', () => {
  21. const text = new Text( 'foobar' );
  22. const b = new Element( 'b', null, text );
  23. const p = new Element( 'p', null, b );
  24. expect( stringify( p ) ).to.equal( '<p><b>foobar</b></p>' );
  25. } );
  26. it( 'should write elements with attributes', () => {
  27. const text = new Text( 'foobar' );
  28. const b = new Element( 'b', {
  29. foo: 'bar'
  30. }, text );
  31. const p = new Element( 'p', {
  32. baz: 'qux',
  33. bar: 'taz',
  34. class: 'short wide'
  35. }, b );
  36. expect( stringify( p ) ).to.equal( '<p class="short wide" baz="qux" bar="taz"><b foo="bar">foobar</b></p>' );
  37. } );
  38. it( 'should write selection ranges inside elements', () => {
  39. const text1 = new Text( 'foobar' );
  40. const text2 = new Text( 'bazqux' );
  41. const b1 = new Element( 'b', null, text1 );
  42. const b2 = new Element( 'b', null, text2 );
  43. const p = new Element( 'p', null, [ b1, b2 ] );
  44. const range = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  45. const selection = new Selection();
  46. selection.addRange( range );
  47. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar</b>[<b>bazqux</b>]</p>' );
  48. } );
  49. it( 'should write collapsed selection ranges inside elements', () => {
  50. const text = new Text( 'foobar' );
  51. const p = new Element( 'p', null, text );
  52. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  53. const selection = new Selection();
  54. selection.addRange( range );
  55. expect( stringify( p, selection ) ).to.equal( '<p>[]foobar</p>' );
  56. } );
  57. it( 'should write selection ranges inside text', () => {
  58. const text1 = new Text( 'foobar' );
  59. const text2 = new Text( 'bazqux' );
  60. const b1 = new Element( 'b', null, text1 );
  61. const b2 = new Element( 'b', null, text2 );
  62. const p = new Element( 'p', null, [ b1, b2 ] );
  63. const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 );
  64. const selection = new Selection();
  65. selection.addRange( range );
  66. expect( stringify( p, selection ) ).to.equal( '<p><b>f{ooba}r</b><b>bazqux</b></p>' );
  67. } );
  68. it( 'should write collapsed selection ranges inside texts', () => {
  69. const text = new Text( 'foobar' );
  70. const p = new Element( 'p', null, text );
  71. const range = Range.createFromParentsAndOffsets( text, 0, text, 0 );
  72. const selection = new Selection();
  73. selection.addRange( range );
  74. expect( stringify( p, selection ) ).to.equal( '<p>{}foobar</p>' );
  75. } );
  76. it( 'should write ranges that start inside text end ends between elements', () => {
  77. const text1 = new Text( 'foobar' );
  78. const text2 = new Text( 'bazqux' );
  79. const b1 = new Element( 'b', null, text1 );
  80. const b2 = new Element( 'b', null, text2 );
  81. const p = new Element( 'p', null, [ b1, b2 ] );
  82. const range = Range.createFromParentsAndOffsets( p, 0, text2, 5 );
  83. const selection = new Selection();
  84. selection.addRange( range );
  85. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b><b>bazqu}x</b></p>' );
  86. } );
  87. it( 'should write elements types as namespaces when needed', () => {
  88. const text = new Text( 'foobar' );
  89. const b = new AttributeElement( 'b', null, text );
  90. const p = new ContainerElement( 'p', null, b );
  91. expect( stringify( p, null, { showType: true } ) )
  92. .to.equal( '<container:p><attribute:b>foobar</attribute:b></container:p>' );
  93. } );
  94. it( 'should write elements priorities when needed', () => {
  95. const text = new Text( 'foobar' );
  96. const b = new AttributeElement( 'b', null, text );
  97. const p = new ContainerElement( 'p', null, b );
  98. expect( stringify( p, null, { showPriority: true } ) )
  99. .to.equal( '<p><b:10>foobar</b:10></p>' );
  100. } );
  101. it( 'should parse DocumentFragment as root', () => {
  102. const text1 = new Text( 'foobar' );
  103. const text2 = new Text( 'bazqux' );
  104. const b1 = new Element( 'b', null, text1 );
  105. const b2 = new Element( 'b', null, text2 );
  106. const fragment = new DocumentFragment( [ b1, b2 ] );
  107. expect( stringify( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
  108. } );
  109. it( 'should not write ranges outside elements', () => {
  110. const text = new Text( 'foobar' );
  111. const b = new Element( 'b', null, text );
  112. const p = new Element( 'p', null, b );
  113. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 5 );
  114. const range2 = Range.createFromParentsAndOffsets( p, -1, p, 1 );
  115. const range3 = Range.createFromParentsAndOffsets( text, 0, text, 7 );
  116. const range4 = Range.createFromParentsAndOffsets( text, -1, text, 2 );
  117. const range5 = Range.createFromParentsAndOffsets( text, 6, text, 8 );
  118. const selection = new Selection();
  119. selection.addRange( range1 );
  120. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b></p>' );
  121. selection.setRanges( [ range2 ] );
  122. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar</b>]</p>' );
  123. selection.setRanges( [ range3 ] );
  124. expect( stringify( p, selection ) ).to.equal( '<p><b>{foobar</b></p>' );
  125. selection.setRanges( [ range4 ] );
  126. expect( stringify( p, selection ) ).to.equal( '<p><b>fo}obar</b></p>' );
  127. selection.setRanges( [ range5 ] );
  128. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar{</b></p>' );
  129. } );
  130. it( 'should write multiple ranges from selection #1', () => {
  131. const text1 = new Text( 'foobar' );
  132. const text2 = new Text( 'bazqux' );
  133. const b1 = new Element( 'b', null, text1 );
  134. const b2 = new Element( 'b', null, text2 );
  135. const p = new Element( 'p', null, [ b1, b2 ] );
  136. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  137. const range2 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  138. const selection = new Selection();
  139. selection.setRanges( [ range2, range1 ] );
  140. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]<b>bazqux</b></p>' );
  141. } );
  142. it( 'should write multiple ranges from selection #2', () => {
  143. const text1 = new Text( 'foobar' );
  144. const text2 = new Text( 'bazqux' );
  145. const b = new Element( 'b', null, text1 );
  146. const p = new Element( 'p', null, [ b, text2 ] );
  147. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  148. const range2 = Range.createFromParentsAndOffsets( text2, 0, text2, 3 );
  149. const range3 = Range.createFromParentsAndOffsets( text2, 3, text2, 4 );
  150. const range4 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  151. const selection = new Selection();
  152. selection.setRanges( [ range1, range2, range3, range4 ] );
  153. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]{baz}{q}ux</p>' );
  154. } );
  155. } );
  156. describe( 'parse', () => {
  157. it( 'should parse elements and texts', () => {
  158. const view = parse( '<b>foobar</b>' );
  159. const element = new Element( 'b' );
  160. expect( view ).to.be.instanceof( Element );
  161. expect( view.isSimilar( element ) ).to.be.true;
  162. expect( view.getChildCount() ).to.equal( 1 );
  163. const text = view.getChild( 0 );
  164. expect( text ).to.be.instanceof( Text );
  165. expect( text.data ).to.equal( 'foobar' );
  166. } );
  167. it( 'should parse element attributes', () => {
  168. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  169. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  170. expect( view ).to.be.instanceof( Element );
  171. expect( view.isSimilar( element ) ).to.be.true;
  172. expect( view.getChildCount() ).to.equal( 0 );
  173. } );
  174. it( 'should parse element type', () => {
  175. const view1 = parse( '<attribute:b></attribute:b>' );
  176. const attribute = new AttributeElement( 'b' );
  177. const view2 = parse( '<container:p></container:p>' );
  178. const container = new ContainerElement( 'p' );
  179. expect( view1 ).to.be.instanceof( AttributeElement );
  180. expect( view1.isSimilar( attribute ) ).to.be.true;
  181. expect( view2 ).to.be.instanceof( ContainerElement );
  182. expect( view2.isSimilar( container ) ).to.be.true;
  183. } );
  184. it( 'should parse element priority', () => {
  185. const parsed1 = parse( '<b:12></b:12>' );
  186. const attribute1 = new AttributeElement( 'b' );
  187. attribute1.priority = 12;
  188. const parsed2 = parse( '<attribute:b:44></attribute:b:44>' );
  189. const attribute2 = new AttributeElement( 'b' );
  190. attribute2.priority = 44;
  191. parsed1.isSimilar( attribute1 );
  192. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  193. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  194. } );
  195. it( 'should paste nested elements and texts', () => {
  196. const parsed = parse( '<container:p>foo<b:12>bar<i:25>qux</i:25></b:12></container:p>' );
  197. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  198. expect( parsed.getChildCount() ).to.equal( 2 );
  199. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  200. const b = parsed.getChild( 1 );
  201. expect( b ).to.be.instanceof( AttributeElement );
  202. expect( b.priority ).to.equal( 12 );
  203. expect( b.getChildCount() ).to.equal( 2 );
  204. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  205. const i = b.getChild( 1 );
  206. expect( i ).to.be.instanceof( AttributeElement );
  207. expect( i.priority ).to.equal( 25 );
  208. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  209. } );
  210. it( 'should parse selection range inside text', () => {
  211. const { view, selection } = parse( 'f{oo}b{}ar' );
  212. expect( view ).to.be.instanceof( Text );
  213. expect( view.data ).to.equal( 'foobar' );
  214. expect( selection.rangeCount ).to.equal( 2 );
  215. const ranges = Array.from( selection.getRanges() );
  216. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  217. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  218. } );
  219. it( 'should parse selection range between elements', () => {
  220. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  221. expect( view ).to.be.instanceof( Element );
  222. expect( view.getChildCount() ).to.equal( 1 );
  223. const b = view.getChild( 0 );
  224. expect( b ).to.be.instanceof( Element );
  225. expect( b.name ).to.equal( 'b' );
  226. expect( b.getChildCount() ).to.equal( 1 );
  227. const text = b.getChild( 0 );
  228. expect( text ).to.be.instanceof( Text );
  229. expect( text.data ).to.equal( 'foobar' );
  230. expect( selection.rangeCount ).to.equal( 2 );
  231. const ranges = Array.from( selection.getRanges() );
  232. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  233. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  234. } );
  235. it( 'should parse ranges #1', () => {
  236. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  237. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  238. expect( view.getChildCount() ).to.equal( 1 );
  239. const text = view.getChild( 0 );
  240. expect( text ).to.be.instanceof( Text );
  241. expect( text.data ).to.equal( 'foobar' );
  242. expect( selection.rangeCount ).to.equal( 1 );
  243. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  244. } );
  245. it( 'should parse ranges #2', () => {
  246. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  247. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  248. expect( view.getChildCount() ).to.equal( 2 );
  249. const text1 = view.getChild( 0 );
  250. expect( text1 ).to.be.instanceof( Text );
  251. expect( text1.data ).to.equal( 'foobar' );
  252. const i = view.getChild( 1 );
  253. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  254. expect( i.getChildCount() ).to.equal( 1 );
  255. const text2 = i.getChild( 0 );
  256. expect( text2 ).to.be.instanceof( Text );
  257. expect( text2.data ).to.equal( 'baz' );
  258. expect( selection.rangeCount ).to.equal( 2 );
  259. const ranges = Array.from( selection.getRanges() );
  260. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  261. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  262. } );
  263. } );
  264. } );