view.js 17 KB

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