8
0

view.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 Position from '/ckeditor5/engine/treeview/position.js';
  9. import Element from '/ckeditor5/engine/treeview/element.js';
  10. import AttributeElement from '/ckeditor5/engine/treeview/attributeelement.js';
  11. import ContainerElement from '/ckeditor5/engine/treeview/containerelement.js';
  12. import Text from '/ckeditor5/engine/treeview/text.js';
  13. import Selection from '/ckeditor5/engine/treeview/selection.js';
  14. import Range from '/ckeditor5/engine/treeview/range.js';
  15. describe( 'view test utils', () => {
  16. describe( 'stringify', () => {
  17. it( 'should write text', () => {
  18. const text = new Text( 'foobar' );
  19. expect( stringify( text ) ).to.equal( 'foobar' );
  20. } );
  21. it( 'should write elements and texts', () => {
  22. const text = new Text( 'foobar' );
  23. const b = new Element( 'b', null, text );
  24. const p = new Element( 'p', null, b );
  25. expect( stringify( p ) ).to.equal( '<p><b>foobar</b></p>' );
  26. } );
  27. it( 'should write elements with attributes', () => {
  28. const text = new Text( 'foobar' );
  29. const b = new Element( 'b', {
  30. foo: 'bar'
  31. }, text );
  32. const p = new Element( 'p', {
  33. baz: 'qux',
  34. bar: 'taz',
  35. class: 'short wide'
  36. }, b );
  37. expect( stringify( p ) ).to.equal( '<p class="short wide" baz="qux" bar="taz"><b foo="bar">foobar</b></p>' );
  38. } );
  39. it( 'should write selection ranges inside elements', () => {
  40. const text1 = new Text( 'foobar' );
  41. const text2 = new Text( 'bazqux' );
  42. const b1 = new Element( 'b', null, text1 );
  43. const b2 = new Element( 'b', null, text2 );
  44. const p = new Element( 'p', null, [ b1, b2 ] );
  45. const range = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  46. const selection = new Selection();
  47. selection.addRange( range );
  48. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar</b>[<b>bazqux</b>]</p>' );
  49. } );
  50. it( 'should write collapsed selection ranges inside elements', () => {
  51. const text = new Text( 'foobar' );
  52. const p = new Element( 'p', null, text );
  53. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  54. const selection = new Selection();
  55. selection.addRange( range );
  56. expect( stringify( p, selection ) ).to.equal( '<p>[]foobar</p>' );
  57. } );
  58. it( 'should write selection ranges inside text', () => {
  59. const text1 = new Text( 'foobar' );
  60. const text2 = new Text( 'bazqux' );
  61. const b1 = new Element( 'b', null, text1 );
  62. const b2 = new Element( 'b', null, text2 );
  63. const p = new Element( 'p', null, [ b1, b2 ] );
  64. const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 );
  65. const selection = new Selection();
  66. selection.addRange( range );
  67. expect( stringify( p, selection ) ).to.equal( '<p><b>f{ooba}r</b><b>bazqux</b></p>' );
  68. } );
  69. it( 'should write collapsed selection ranges inside texts', () => {
  70. const text = new Text( 'foobar' );
  71. const p = new Element( 'p', null, text );
  72. const range = Range.createFromParentsAndOffsets( text, 0, text, 0 );
  73. const selection = new Selection();
  74. selection.addRange( range );
  75. expect( stringify( p, selection ) ).to.equal( '<p>{}foobar</p>' );
  76. } );
  77. it( 'should write ranges that start inside text end ends between elements', () => {
  78. const text1 = new Text( 'foobar' );
  79. const text2 = new Text( 'bazqux' );
  80. const b1 = new Element( 'b', null, text1 );
  81. const b2 = new Element( 'b', null, text2 );
  82. const p = new Element( 'p', null, [ b1, b2 ] );
  83. const range = Range.createFromParentsAndOffsets( p, 0, text2, 5 );
  84. const selection = new Selection();
  85. selection.addRange( range );
  86. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b><b>bazqu}x</b></p>' );
  87. } );
  88. it( 'should write elements types as namespaces when needed', () => {
  89. const text = new Text( 'foobar' );
  90. const b = new AttributeElement( 'b', null, text );
  91. const p = new ContainerElement( 'p', null, b );
  92. expect( stringify( p, null, { showType: true } ) )
  93. .to.equal( '<container:p><attribute:b>foobar</attribute:b></container:p>' );
  94. } );
  95. it( 'should not write element type when type is not specified', () => {
  96. const p = new Element( 'p' );
  97. expect( stringify( p, null, { showType: true } ) ).to.equal( '<p></p>' );
  98. } );
  99. it( 'should write elements priorities when needed', () => {
  100. const text = new Text( 'foobar' );
  101. const b = new AttributeElement( 'b', null, text );
  102. const p = new ContainerElement( 'p', null, b );
  103. expect( stringify( p, null, { showPriority: true } ) )
  104. .to.equal( '<p><b:10>foobar</b:10></p>' );
  105. } );
  106. it( 'should parse DocumentFragment as root', () => {
  107. const text1 = new Text( 'foobar' );
  108. const text2 = new Text( 'bazqux' );
  109. const b1 = new Element( 'b', null, text1 );
  110. const b2 = new Element( 'b', null, text2 );
  111. const fragment = new DocumentFragment( [ b1, b2 ] );
  112. expect( stringify( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
  113. } );
  114. it( 'should not write ranges outside elements', () => {
  115. const text = new Text( 'foobar' );
  116. const b = new Element( 'b', null, text );
  117. const p = new Element( 'p', null, b );
  118. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 5 );
  119. const range2 = Range.createFromParentsAndOffsets( p, -1, p, 1 );
  120. const range3 = Range.createFromParentsAndOffsets( text, 0, text, 7 );
  121. const range4 = Range.createFromParentsAndOffsets( text, -1, text, 2 );
  122. const range5 = Range.createFromParentsAndOffsets( text, 6, text, 8 );
  123. const selection = new Selection();
  124. selection.addRange( range1 );
  125. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b></p>' );
  126. selection.setRanges( [ range2 ] );
  127. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar</b>]</p>' );
  128. selection.setRanges( [ range3 ] );
  129. expect( stringify( p, selection ) ).to.equal( '<p><b>{foobar</b></p>' );
  130. selection.setRanges( [ range4 ] );
  131. expect( stringify( p, selection ) ).to.equal( '<p><b>fo}obar</b></p>' );
  132. selection.setRanges( [ range5 ] );
  133. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar{</b></p>' );
  134. } );
  135. it( 'should write multiple ranges from selection #1', () => {
  136. const text1 = new Text( 'foobar' );
  137. const text2 = new Text( 'bazqux' );
  138. const b1 = new Element( 'b', null, text1 );
  139. const b2 = new Element( 'b', null, text2 );
  140. const p = new Element( 'p', null, [ b1, b2 ] );
  141. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  142. const range2 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  143. const selection = new Selection();
  144. selection.setRanges( [ range2, range1 ] );
  145. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]<b>bazqux</b></p>' );
  146. } );
  147. it( 'should write multiple ranges from selection #2', () => {
  148. const text1 = new Text( 'foobar' );
  149. const text2 = new Text( 'bazqux' );
  150. const b = new Element( 'b', null, text1 );
  151. const p = new Element( 'p', null, [ b, text2 ] );
  152. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  153. const range2 = Range.createFromParentsAndOffsets( text2, 0, text2, 3 );
  154. const range3 = Range.createFromParentsAndOffsets( text2, 3, text2, 4 );
  155. const range4 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  156. const selection = new Selection();
  157. selection.setRanges( [ range1, range2, range3, range4 ] );
  158. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]{baz}{q}ux</p>' );
  159. } );
  160. it( 'should use Position instance instead of Selection', () => {
  161. const text = new Text( 'foobar' );
  162. const position = new Position( text, 3 );
  163. const string = stringify( text, position );
  164. expect( string ).to.equal( 'foo{}bar' );
  165. } );
  166. it( 'should use Range instance instead of Selection', () => {
  167. const text = new Text( 'foobar' );
  168. const range = Range.createFromParentsAndOffsets( text, 3, text, 4 );
  169. const string = stringify( text, range );
  170. expect( string ).to.equal( 'foo{b}ar' );
  171. } );
  172. } );
  173. describe( 'parse', () => {
  174. it( 'should parse text', () => {
  175. const text = parse( 'foobar' );
  176. expect( text ).to.be.instanceOf( Text );
  177. expect( text.data ).to.equal( 'foobar' );
  178. } );
  179. it( 'should parse elements and texts', () => {
  180. const view = parse( '<b>foobar</b>' );
  181. const element = new Element( 'b' );
  182. expect( view ).to.be.instanceof( Element );
  183. expect( view.isSimilar( element ) ).to.be.true;
  184. expect( view.getChildCount() ).to.equal( 1 );
  185. const text = view.getChild( 0 );
  186. expect( text ).to.be.instanceof( Text );
  187. expect( text.data ).to.equal( 'foobar' );
  188. } );
  189. it( 'should parse element attributes', () => {
  190. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  191. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  192. expect( view ).to.be.instanceof( Element );
  193. expect( view.isSimilar( element ) ).to.be.true;
  194. expect( view.getChildCount() ).to.equal( 0 );
  195. } );
  196. it( 'should parse element type', () => {
  197. const view1 = parse( '<attribute:b></attribute:b>' );
  198. const attribute = new AttributeElement( 'b' );
  199. const view2 = parse( '<container:p></container:p>' );
  200. const container = new ContainerElement( 'p' );
  201. expect( view1 ).to.be.instanceof( AttributeElement );
  202. expect( view1.isSimilar( attribute ) ).to.be.true;
  203. expect( view2 ).to.be.instanceof( ContainerElement );
  204. expect( view2.isSimilar( container ) ).to.be.true;
  205. } );
  206. it( 'should parse element priority', () => {
  207. const parsed1 = parse( '<b:12></b:12>' );
  208. const attribute1 = new AttributeElement( 'b' );
  209. attribute1.priority = 12;
  210. const parsed2 = parse( '<attribute:b:44></attribute:b:44>' );
  211. const attribute2 = new AttributeElement( 'b' );
  212. attribute2.priority = 44;
  213. parsed1.isSimilar( attribute1 );
  214. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  215. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  216. } );
  217. it( 'should create DocumentFragment when multiple elements on root', () => {
  218. const view = parse( '<b></b><i></i>' );
  219. expect( view ).to.be.instanceOf( DocumentFragment );
  220. expect( view.getChildCount() ).to.equal( 2 );
  221. expect( view.getChild( 0 ).isSimilar( new Element( 'b' ) ) ).to.be.true;
  222. expect( view.getChild( 1 ).isSimilar( new Element( 'i' ) ) ).to.be.true;
  223. } );
  224. it( 'should paste nested elements and texts', () => {
  225. const parsed = parse( '<container:p>foo<b:12>bar<i:25>qux</i:25></b:12></container:p>' );
  226. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  227. expect( parsed.getChildCount() ).to.equal( 2 );
  228. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  229. const b = parsed.getChild( 1 );
  230. expect( b ).to.be.instanceof( AttributeElement );
  231. expect( b.priority ).to.equal( 12 );
  232. expect( b.getChildCount() ).to.equal( 2 );
  233. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  234. const i = b.getChild( 1 );
  235. expect( i ).to.be.instanceof( AttributeElement );
  236. expect( i.priority ).to.equal( 25 );
  237. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  238. } );
  239. it( 'should parse selection range inside text', () => {
  240. const { view, selection } = parse( 'f{oo}b{}ar' );
  241. expect( view ).to.be.instanceof( Text );
  242. expect( view.data ).to.equal( 'foobar' );
  243. expect( selection.rangeCount ).to.equal( 2 );
  244. const ranges = [ ...selection.getRanges() ];
  245. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  246. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  247. } );
  248. it( 'should parse selection range between elements', () => {
  249. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  250. expect( view ).to.be.instanceof( Element );
  251. expect( view.getChildCount() ).to.equal( 1 );
  252. const b = view.getChild( 0 );
  253. expect( b ).to.be.instanceof( Element );
  254. expect( b.name ).to.equal( 'b' );
  255. expect( b.getChildCount() ).to.equal( 1 );
  256. const text = b.getChild( 0 );
  257. expect( text ).to.be.instanceof( Text );
  258. expect( text.data ).to.equal( 'foobar' );
  259. expect( selection.rangeCount ).to.equal( 2 );
  260. const ranges = [ ...selection.getRanges() ];
  261. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  262. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  263. } );
  264. it( 'should parse ranges #1', () => {
  265. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  266. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  267. expect( view.getChildCount() ).to.equal( 1 );
  268. const text = view.getChild( 0 );
  269. expect( text ).to.be.instanceof( Text );
  270. expect( text.data ).to.equal( 'foobar' );
  271. expect( selection.rangeCount ).to.equal( 1 );
  272. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  273. } );
  274. it( 'should parse ranges #2', () => {
  275. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  276. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  277. expect( view.getChildCount() ).to.equal( 2 );
  278. const text1 = view.getChild( 0 );
  279. expect( text1 ).to.be.instanceof( Text );
  280. expect( text1.data ).to.equal( 'foobar' );
  281. const i = view.getChild( 1 );
  282. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  283. expect( i.getChildCount() ).to.equal( 1 );
  284. const text2 = i.getChild( 0 );
  285. expect( text2 ).to.be.instanceof( Text );
  286. expect( text2.data ).to.equal( 'baz' );
  287. expect( selection.rangeCount ).to.equal( 2 );
  288. const ranges = [ ...selection.getRanges() ];
  289. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  290. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  291. } );
  292. it( 'should use ranges order when provided', () => {
  293. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ] } );
  294. expect( selection.rangeCount ).to.equal( 3 );
  295. const ranges = [ ...selection.getRanges() ];
  296. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  297. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  298. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  299. expect( selection.anchor.isEqual( ranges[ 2 ].start ) ).to.be.true;
  300. expect( selection.focus.isEqual( ranges[ 2 ].end ) ).to.be.true;
  301. } );
  302. it( 'should set last range backward if needed', () => {
  303. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ], lastRangeBackward: true } );
  304. expect( selection.rangeCount ).to.equal( 3 );
  305. const ranges = [ ...selection.getRanges() ];
  306. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  307. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  308. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  309. expect( selection.anchor.isEqual( ranges[ 2 ].end ) ).to.be.true;
  310. expect( selection.focus.isEqual( ranges[ 2 ].start ) ).to.be.true;
  311. } );
  312. it( 'should return DocumentFragment if range is around single element', () => {
  313. const { view, selection } = parse( '[<b>foobar</b>]' );
  314. expect( view ).to.be.instanceOf( DocumentFragment );
  315. expect( selection.rangeCount ).to.equal( 1 );
  316. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  317. } );
  318. it( 'should throw when ranges order does not include all ranges', () => {
  319. expect( () => {
  320. parse( '{}foobar{}', { order: [ 1 ] } );
  321. } ).to.throw( Error );
  322. } );
  323. it( 'should throw when ranges order is invalid', () => {
  324. expect( () => {
  325. parse( '{}foobar{}', { order: [ 1, 4 ] } );
  326. } ).to.throw( Error );
  327. } );
  328. it( 'should throw when element range delimiter is inside text node', () => {
  329. expect( () => {
  330. parse( 'foo[bar' );
  331. } ).to.throw( Error );
  332. } );
  333. it( 'should throw when text range delimiter is inside empty text node', () => {
  334. expect( () => {
  335. parse( '<b>foo</b>}' );
  336. } ).to.throw( Error );
  337. } );
  338. it( 'should throw when end of range is found before start', () => {
  339. expect( () => {
  340. parse( 'fo}obar' );
  341. } ).to.throw( Error );
  342. } );
  343. it( 'should throw when intersecting ranges found', () => {
  344. expect( () => {
  345. parse( '[fo{o}bar]' );
  346. } ).to.throw( Error );
  347. } );
  348. it( 'should throw when opened ranges are left', () => {
  349. expect( () => {
  350. parse( 'fo{obar' );
  351. } ).to.throw( Error );
  352. } );
  353. it( 'should throw when wrong tag name is provided #1', () => {
  354. expect( () => {
  355. parse( '<b:bar></b:bar>' );
  356. } ).to.throw( Error );
  357. } );
  358. it( 'should throw when wrong tag name is provided #2', () => {
  359. expect( () => {
  360. parse( '<container:b:bar></container:b:bar>' );
  361. } ).to.throw( Error );
  362. } );
  363. it( 'should throw when wrong tag name is provided #3', () => {
  364. expect( () => {
  365. parse( '<container:b:10:test></container:b:10:test>' );
  366. } ).to.throw( Error );
  367. } );
  368. } );
  369. } );