view.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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( 'stringify', () => {
  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 not write element type when type is not specified', () => {
  95. const p = new Element( 'p' );
  96. expect( stringify( p, null, { showType: true } ) ).to.equal( '<p></p>' );
  97. } );
  98. it( 'should write elements priorities when needed', () => {
  99. const text = new Text( 'foobar' );
  100. const b = new AttributeElement( 'b', null, text );
  101. const p = new ContainerElement( 'p', null, b );
  102. expect( stringify( p, null, { showPriority: true } ) )
  103. .to.equal( '<p><b:10>foobar</b:10></p>' );
  104. } );
  105. it( 'should parse DocumentFragment as root', () => {
  106. const text1 = new Text( 'foobar' );
  107. const text2 = new Text( 'bazqux' );
  108. const b1 = new Element( 'b', null, text1 );
  109. const b2 = new Element( 'b', null, text2 );
  110. const fragment = new DocumentFragment( [ b1, b2 ] );
  111. expect( stringify( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
  112. } );
  113. it( 'should not write ranges outside elements', () => {
  114. const text = new Text( 'foobar' );
  115. const b = new Element( 'b', null, text );
  116. const p = new Element( 'p', null, b );
  117. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 5 );
  118. const range2 = Range.createFromParentsAndOffsets( p, -1, p, 1 );
  119. const range3 = Range.createFromParentsAndOffsets( text, 0, text, 7 );
  120. const range4 = Range.createFromParentsAndOffsets( text, -1, text, 2 );
  121. const range5 = Range.createFromParentsAndOffsets( text, 6, text, 8 );
  122. const selection = new Selection();
  123. selection.addRange( range1 );
  124. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b></p>' );
  125. selection.setRanges( [ range2 ] );
  126. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar</b>]</p>' );
  127. selection.setRanges( [ range3 ] );
  128. expect( stringify( p, selection ) ).to.equal( '<p><b>{foobar</b></p>' );
  129. selection.setRanges( [ range4 ] );
  130. expect( stringify( p, selection ) ).to.equal( '<p><b>fo}obar</b></p>' );
  131. selection.setRanges( [ range5 ] );
  132. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar{</b></p>' );
  133. } );
  134. it( 'should write multiple ranges from selection #1', () => {
  135. const text1 = new Text( 'foobar' );
  136. const text2 = new Text( 'bazqux' );
  137. const b1 = new Element( 'b', null, text1 );
  138. const b2 = new Element( 'b', null, text2 );
  139. const p = new Element( 'p', null, [ b1, b2 ] );
  140. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  141. const range2 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  142. const selection = new Selection();
  143. selection.setRanges( [ range2, range1 ] );
  144. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]<b>bazqux</b></p>' );
  145. } );
  146. it( 'should write multiple ranges from selection #2', () => {
  147. const text1 = new Text( 'foobar' );
  148. const text2 = new Text( 'bazqux' );
  149. const b = new Element( 'b', null, text1 );
  150. const p = new Element( 'p', null, [ b, text2 ] );
  151. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  152. const range2 = Range.createFromParentsAndOffsets( text2, 0, text2, 3 );
  153. const range3 = Range.createFromParentsAndOffsets( text2, 3, text2, 4 );
  154. const range4 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  155. const selection = new Selection();
  156. selection.setRanges( [ range1, range2, range3, range4 ] );
  157. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]{baz}{q}ux</p>' );
  158. } );
  159. } );
  160. describe( 'parse', () => {
  161. it( 'should parse elements and texts', () => {
  162. const view = parse( '<b>foobar</b>' );
  163. const element = new Element( 'b' );
  164. expect( view ).to.be.instanceof( Element );
  165. expect( view.isSimilar( element ) ).to.be.true;
  166. expect( view.getChildCount() ).to.equal( 1 );
  167. const text = view.getChild( 0 );
  168. expect( text ).to.be.instanceof( Text );
  169. expect( text.data ).to.equal( 'foobar' );
  170. } );
  171. it( 'should parse element attributes', () => {
  172. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  173. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  174. expect( view ).to.be.instanceof( Element );
  175. expect( view.isSimilar( element ) ).to.be.true;
  176. expect( view.getChildCount() ).to.equal( 0 );
  177. } );
  178. it( 'should parse element type', () => {
  179. const view1 = parse( '<attribute:b></attribute:b>' );
  180. const attribute = new AttributeElement( 'b' );
  181. const view2 = parse( '<container:p></container:p>' );
  182. const container = new ContainerElement( 'p' );
  183. expect( view1 ).to.be.instanceof( AttributeElement );
  184. expect( view1.isSimilar( attribute ) ).to.be.true;
  185. expect( view2 ).to.be.instanceof( ContainerElement );
  186. expect( view2.isSimilar( container ) ).to.be.true;
  187. } );
  188. it( 'should parse element priority', () => {
  189. const parsed1 = parse( '<b:12></b:12>' );
  190. const attribute1 = new AttributeElement( 'b' );
  191. attribute1.priority = 12;
  192. const parsed2 = parse( '<attribute:b:44></attribute:b:44>' );
  193. const attribute2 = new AttributeElement( 'b' );
  194. attribute2.priority = 44;
  195. parsed1.isSimilar( attribute1 );
  196. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  197. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  198. } );
  199. it( 'should create DocumentFragment when multiple elements on root', () => {
  200. const view = parse( '<b></b><i></i>' );
  201. expect( view ).to.be.instanceOf( DocumentFragment );
  202. expect( view.getChildCount() ).to.equal( 2 );
  203. expect( view.getChild( 0 ).isSimilar( new Element( 'b' ) ) ).to.be.true;
  204. expect( view.getChild( 1 ).isSimilar( new Element( 'i' ) ) ).to.be.true;
  205. } );
  206. it( 'should paste nested elements and texts', () => {
  207. const parsed = parse( '<container:p>foo<b:12>bar<i:25>qux</i:25></b:12></container:p>' );
  208. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  209. expect( parsed.getChildCount() ).to.equal( 2 );
  210. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  211. const b = parsed.getChild( 1 );
  212. expect( b ).to.be.instanceof( AttributeElement );
  213. expect( b.priority ).to.equal( 12 );
  214. expect( b.getChildCount() ).to.equal( 2 );
  215. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  216. const i = b.getChild( 1 );
  217. expect( i ).to.be.instanceof( AttributeElement );
  218. expect( i.priority ).to.equal( 25 );
  219. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  220. } );
  221. it( 'should parse selection range inside text', () => {
  222. const { view, selection } = parse( 'f{oo}b{}ar' );
  223. expect( view ).to.be.instanceof( Text );
  224. expect( view.data ).to.equal( 'foobar' );
  225. expect( selection.rangeCount ).to.equal( 2 );
  226. const ranges = Array.from( selection.getRanges() );
  227. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  228. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  229. } );
  230. it( 'should parse selection range between elements', () => {
  231. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  232. expect( view ).to.be.instanceof( Element );
  233. expect( view.getChildCount() ).to.equal( 1 );
  234. const b = view.getChild( 0 );
  235. expect( b ).to.be.instanceof( Element );
  236. expect( b.name ).to.equal( 'b' );
  237. expect( b.getChildCount() ).to.equal( 1 );
  238. const text = b.getChild( 0 );
  239. expect( text ).to.be.instanceof( Text );
  240. expect( text.data ).to.equal( 'foobar' );
  241. expect( selection.rangeCount ).to.equal( 2 );
  242. const ranges = Array.from( selection.getRanges() );
  243. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  244. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  245. } );
  246. it( 'should parse ranges #1', () => {
  247. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  248. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  249. expect( view.getChildCount() ).to.equal( 1 );
  250. const text = view.getChild( 0 );
  251. expect( text ).to.be.instanceof( Text );
  252. expect( text.data ).to.equal( 'foobar' );
  253. expect( selection.rangeCount ).to.equal( 1 );
  254. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  255. } );
  256. it( 'should parse ranges #2', () => {
  257. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  258. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  259. expect( view.getChildCount() ).to.equal( 2 );
  260. const text1 = view.getChild( 0 );
  261. expect( text1 ).to.be.instanceof( Text );
  262. expect( text1.data ).to.equal( 'foobar' );
  263. const i = view.getChild( 1 );
  264. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  265. expect( i.getChildCount() ).to.equal( 1 );
  266. const text2 = i.getChild( 0 );
  267. expect( text2 ).to.be.instanceof( Text );
  268. expect( text2.data ).to.equal( 'baz' );
  269. expect( selection.rangeCount ).to.equal( 2 );
  270. const ranges = Array.from( selection.getRanges() );
  271. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  272. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  273. } );
  274. it( 'should use ranges order when provided', () => {
  275. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ] } );
  276. expect( selection.rangeCount ).to.equal( 3 );
  277. const ranges = Array.from( selection.getRanges() );
  278. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  279. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  280. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  281. } );
  282. it( 'should throw when ranges order does not include all ranges', () => {
  283. expect( () => {
  284. parse( '{}foobar{}', { order: [ 1 ] } );
  285. } ).to.throw( Error );
  286. } );
  287. it( 'should throw when ranges order is invalid', () => {
  288. expect( () => {
  289. parse( '{}foobar{}', { order: [ 1, 4 ] } );
  290. } ).to.throw( Error );
  291. } );
  292. it( 'should throw when element range delimiter is inside text node', () => {
  293. expect( () => {
  294. parse( 'foo[bar' );
  295. } ).to.throw( Error );
  296. } );
  297. it( 'should throw when text range delimiter is inside empty text node', () => {
  298. expect( () => {
  299. parse( '<b>foo</b>}' );
  300. } ).to.throw( Error );
  301. } );
  302. it( 'should throw when end of range is found before start', () => {
  303. expect( () => {
  304. parse( 'fo}obar' );
  305. } ).to.throw( Error );
  306. } );
  307. it( 'should throw when intersecting ranges found', () => {
  308. expect( () => {
  309. parse( '[fo{o}bar]' );
  310. } ).to.throw( Error );
  311. } );
  312. it( 'should throw when opened ranges are left', () => {
  313. expect( () => {
  314. parse( 'fo{obar' );
  315. } ).to.throw( Error );
  316. } );
  317. it( 'should throw when wrong tag name is provided #1', () => {
  318. expect( () => {
  319. parse( '<b:bar></b:bar>' );
  320. } ).to.throw( Error );
  321. } );
  322. it( 'should throw when wrong tag name is provided #2', () => {
  323. expect( () => {
  324. parse( '<container:b:bar></container:b:bar>' );
  325. } ).to.throw( Error );
  326. } );
  327. it( 'should throw when wrong tag name is provided #3', () => {
  328. expect( () => {
  329. parse( '<container:b:10:test></container:b:10:test>' );
  330. } ).to.throw( Error );
  331. } );
  332. } );
  333. } );