view.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: browser-only */
  7. import { parse, stringify, getData, setData }from '/tests/engine/_utils/view.js';
  8. import DocumentFragment from '/ckeditor5/engine/view/documentfragment.js';
  9. import Position from '/ckeditor5/engine/view/position.js';
  10. import Element from '/ckeditor5/engine/view/element.js';
  11. import AttributeElement from '/ckeditor5/engine/view/attributeelement.js';
  12. import ContainerElement from '/ckeditor5/engine/view/containerelement.js';
  13. import Text from '/ckeditor5/engine/view/text.js';
  14. import Selection from '/ckeditor5/engine/view/selection.js';
  15. import Range from '/ckeditor5/engine/view/range.js';
  16. import Document from '/ckeditor5/engine/view/document.js';
  17. describe( 'view test utils', () => {
  18. describe( 'getData, setData', () => {
  19. let sandbox;
  20. beforeEach( () => {
  21. sandbox = sinon.sandbox.create();
  22. } );
  23. afterEach( () => {
  24. sandbox.restore();
  25. } );
  26. describe( 'getData', () => {
  27. it( 'should use stringify method', () => {
  28. const element = document.createElement( 'div' );
  29. const stringifySpy = sandbox.spy( getData, '_stringify' );
  30. const viewDocument = new Document();
  31. const options = { showType: false, showPriority: false, withoutSelection: true };
  32. const root = viewDocument.createRoot( element );
  33. root.appendChildren( new Element( 'p' ) );
  34. expect( getData( viewDocument, options ) ).to.equal( '<p></p>' );
  35. sinon.assert.calledOnce( stringifySpy );
  36. expect( stringifySpy.firstCall.args[ 0 ] ).to.equal( root );
  37. expect( stringifySpy.firstCall.args[ 1 ] ).to.equal( null );
  38. const stringifyOptions = stringifySpy.firstCall.args[ 2 ];
  39. expect( stringifyOptions ).to.have.property( 'showType' ).that.equals( false );
  40. expect( stringifyOptions ).to.have.property( 'showPriority' ).that.equals( false );
  41. expect( stringifyOptions ).to.have.property( 'ignoreRoot' ).that.equals( true );
  42. } );
  43. it( 'should use stringify method with selection', () => {
  44. const element = document.createElement( 'div' );
  45. const stringifySpy = sandbox.spy( getData, '_stringify' );
  46. const viewDocument = new Document();
  47. const options = { showType: false, showPriority: false };
  48. const root = viewDocument.createRoot( element );
  49. root.appendChildren( new Element( 'p' ) );
  50. viewDocument.selection.addRange( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  51. expect( getData( viewDocument, options ) ).to.equal( '[<p></p>]' );
  52. sinon.assert.calledOnce( stringifySpy );
  53. expect( stringifySpy.firstCall.args[ 0 ] ).to.equal( root );
  54. expect( stringifySpy.firstCall.args[ 1 ] ).to.equal( viewDocument.selection );
  55. const stringifyOptions = stringifySpy.firstCall.args[ 2 ];
  56. expect( stringifyOptions ).to.have.property( 'showType' ).that.equals( false );
  57. expect( stringifyOptions ).to.have.property( 'showPriority' ).that.equals( false );
  58. expect( stringifyOptions ).to.have.property( 'ignoreRoot' ).that.equals( true );
  59. } );
  60. it( 'should throw an error when passing invalid document', () => {
  61. expect( () => {
  62. getData( { invalid: 'document' } );
  63. } ).to.throw( TypeError, 'Document needs to be an instance of engine.view.Document.' );
  64. } );
  65. } );
  66. describe( 'setData', () => {
  67. it( 'should use parse method', () => {
  68. const viewDocument = new Document();
  69. const data = 'foobar<b>baz</b>';
  70. const parseSpy = sandbox.spy( setData, '_parse' );
  71. viewDocument.createRoot( document.createElement( 'div' ) );
  72. setData( viewDocument, data );
  73. expect( getData( viewDocument ) ).to.equal( 'foobar<b>baz</b>' );
  74. sinon.assert.calledOnce( parseSpy );
  75. const args = parseSpy.firstCall.args;
  76. expect( args[ 0 ] ).to.equal( data );
  77. expect( args[ 1 ] ).to.be.an( 'object' );
  78. expect( args[ 1 ].rootElement ).to.equal( viewDocument.getRoot() );
  79. } );
  80. it( 'should use parse method with selection', () => {
  81. const viewDocument = new Document();
  82. const data = '[<b>baz</b>]';
  83. const parseSpy = sandbox.spy( setData, '_parse' );
  84. viewDocument.createRoot( document.createElement( 'div' ) );
  85. setData( viewDocument, data );
  86. expect( getData( viewDocument ) ).to.equal( '[<b>baz</b>]' );
  87. const args = parseSpy.firstCall.args;
  88. expect( args[ 0 ] ).to.equal( data );
  89. expect( args[ 1 ] ).to.be.an( 'object' );
  90. expect( args[ 1 ].rootElement ).to.equal( viewDocument.getRoot() );
  91. } );
  92. it( 'should throw an error when passing invalid document', () => {
  93. expect( () => {
  94. setData( { invalid: 'document' } );
  95. } ).to.throw( TypeError, 'Document needs to be an instance of engine.view.Document.' );
  96. } );
  97. } );
  98. } );
  99. describe( 'stringify', () => {
  100. it( 'should write text', () => {
  101. const text = new Text( 'foobar' );
  102. expect( stringify( text ) ).to.equal( 'foobar' );
  103. } );
  104. it( 'should write elements and texts', () => {
  105. const text = new Text( 'foobar' );
  106. const b = new Element( 'b', null, text );
  107. const p = new Element( 'p', null, b );
  108. expect( stringify( p ) ).to.equal( '<p><b>foobar</b></p>' );
  109. } );
  110. it( 'should write elements with attributes (attributes in alphabetical order)', () => {
  111. const text = new Text( 'foobar' );
  112. const b = new Element( 'b', {
  113. foo: 'bar'
  114. }, text );
  115. const p = new Element( 'p', {
  116. baz: 'qux',
  117. bar: 'taz',
  118. class: 'short wide'
  119. }, b );
  120. expect( stringify( p ) ).to.equal( '<p bar="taz" baz="qux" class="short wide"><b foo="bar">foobar</b></p>' );
  121. } );
  122. it( 'should write selection ranges inside elements', () => {
  123. const text1 = new Text( 'foobar' );
  124. const text2 = new Text( 'bazqux' );
  125. const b1 = new Element( 'b', null, text1 );
  126. const b2 = new Element( 'b', null, text2 );
  127. const p = new Element( 'p', null, [ b1, b2 ] );
  128. const range = Range.createFromParentsAndOffsets( p, 1, p, 2 );
  129. const selection = new Selection();
  130. selection.addRange( range );
  131. expect( stringify( p, selection ) ).to.equal( '<p><b>foobar</b>[<b>bazqux</b>]</p>' );
  132. } );
  133. it( 'should support unicode', () => {
  134. const text = new Text( 'நிலைக்கு' );
  135. const b = new Element( 'b', null, text );
  136. const p = new Element( 'p', null, b );
  137. const range = Range.createFromParentsAndOffsets( p, 0, text, 4 );
  138. const selection = new Selection();
  139. selection.addRange( range );
  140. expect( stringify( p, selection ) ).to.equal( '<p>[<b>நிலை}க்கு</b></p>' );
  141. } );
  142. it( 'should write collapsed selection ranges inside elements', () => {
  143. const text = new Text( 'foobar' );
  144. const p = new Element( 'p', null, text );
  145. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  146. const selection = new Selection();
  147. selection.addRange( range );
  148. expect( stringify( p, selection ) ).to.equal( '<p>[]foobar</p>' );
  149. } );
  150. it( 'should write selection ranges inside text', () => {
  151. const text1 = new Text( 'foobar' );
  152. const text2 = new Text( 'bazqux' );
  153. const b1 = new Element( 'b', null, text1 );
  154. const b2 = new Element( 'b', null, text2 );
  155. const p = new Element( 'p', null, [ b1, b2 ] );
  156. const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 );
  157. const selection = new Selection();
  158. selection.addRange( range );
  159. expect( stringify( p, selection ) ).to.equal( '<p><b>f{ooba}r</b><b>bazqux</b></p>' );
  160. } );
  161. it( 'should write selection ranges inside text represented by `[` and `]` characters', () => {
  162. const text1 = new Text( 'foobar' );
  163. const text2 = new Text( 'bazqux' );
  164. const b1 = new Element( 'b', null, text1 );
  165. const b2 = new Element( 'b', null, text2 );
  166. const p = new Element( 'p', null, [ b1, b2 ] );
  167. const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 );
  168. const selection = new Selection();
  169. selection.addRange( range );
  170. expect( stringify( p, selection, { sameSelectionCharacters: true } ) )
  171. .to.equal( '<p><b>f[ooba]r</b><b>bazqux</b></p>' );
  172. } );
  173. it( 'should write collapsed selection ranges inside texts', () => {
  174. const text = new Text( 'foobar' );
  175. const p = new Element( 'p', null, text );
  176. const range = Range.createFromParentsAndOffsets( text, 0, text, 0 );
  177. const selection = new Selection();
  178. selection.addRange( range );
  179. expect( stringify( p, selection ) ).to.equal( '<p>{}foobar</p>' );
  180. } );
  181. it( 'should write ranges that start inside text end ends between elements', () => {
  182. const text1 = new Text( 'foobar' );
  183. const text2 = new Text( 'bazqux' );
  184. const b1 = new Element( 'b', null, text1 );
  185. const b2 = new Element( 'b', null, text2 );
  186. const p = new Element( 'p', null, [ b1, b2 ] );
  187. const range = Range.createFromParentsAndOffsets( p, 0, text2, 5 );
  188. const selection = new Selection();
  189. selection.addRange( range );
  190. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b><b>bazqu}x</b></p>' );
  191. } );
  192. it( 'should write elements types as namespaces when needed', () => {
  193. const text = new Text( 'foobar' );
  194. const b = new AttributeElement( 'b', null, text );
  195. const p = new ContainerElement( 'p', null, b );
  196. expect( stringify( p, null, { showType: true } ) )
  197. .to.equal( '<container:p><attribute:b>foobar</attribute:b></container:p>' );
  198. } );
  199. it( 'should not write element type when type is not specified', () => {
  200. const p = new Element( 'p' );
  201. expect( stringify( p, null, { showType: true } ) ).to.equal( '<p></p>' );
  202. } );
  203. it( 'should write elements priorities when needed', () => {
  204. const text = new Text( 'foobar' );
  205. const b = new AttributeElement( 'b', null, text );
  206. const p = new ContainerElement( 'p', null, b );
  207. expect( stringify( p, null, { showPriority: true } ) )
  208. .to.equal( '<p><b view-priority="10">foobar</b></p>' );
  209. } );
  210. it( 'should parse DocumentFragment as root', () => {
  211. const text1 = new Text( 'foobar' );
  212. const text2 = new Text( 'bazqux' );
  213. const b1 = new Element( 'b', null, text1 );
  214. const b2 = new Element( 'b', null, text2 );
  215. const fragment = new DocumentFragment( [ b1, b2 ] );
  216. expect( stringify( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
  217. } );
  218. it( 'should not write ranges outside elements - end position outside element', () => {
  219. const text = new Text( 'foobar' );
  220. const b = new Element( 'b', null, text );
  221. const p = new Element( 'p', null, b );
  222. const range = Range.createFromParentsAndOffsets( p, 0, p, 5 );
  223. expect( stringify( p, range ) ).to.equal( '<p>[<b>foobar</b></p>' );
  224. } );
  225. it( 'should not write ranges outside elements - start position outside element', () => {
  226. const text = new Text( 'foobar' );
  227. const b = new Element( 'b', null, text );
  228. const p = new Element( 'p', null, b );
  229. const range = Range.createFromParentsAndOffsets( p, -1, p, 1 );
  230. expect( stringify( p, range ) ).to.equal( '<p><b>foobar</b>]</p>' );
  231. } );
  232. it( 'should not write ranges outside elements - end position outside text', () => {
  233. const text = new Text( 'foobar' );
  234. const b = new Element( 'b', null, text );
  235. const p = new Element( 'p', null, b );
  236. const range = Range.createFromParentsAndOffsets( text, 0, text, 7 );
  237. expect( stringify( p, range ) ).to.equal( '<p><b>{foobar</b></p>' );
  238. } );
  239. it( 'should not write ranges outside elements - start position outside text', () => {
  240. const text = new Text( 'foobar' );
  241. const b = new Element( 'b', null, text );
  242. const p = new Element( 'p', null, b );
  243. const range = Range.createFromParentsAndOffsets( text, -1, text, 2 );
  244. expect( stringify( p, range ) ).to.equal( '<p><b>fo}obar</b></p>' );
  245. } );
  246. it( 'should write multiple ranges from selection #1', () => {
  247. const text1 = new Text( 'foobar' );
  248. const text2 = new Text( 'bazqux' );
  249. const b1 = new Element( 'b', null, text1 );
  250. const b2 = new Element( 'b', null, text2 );
  251. const p = new Element( 'p', null, [ b1, b2 ] );
  252. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  253. const range2 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  254. const selection = new Selection();
  255. selection.setRanges( [ range2, range1 ] );
  256. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]<b>bazqux</b></p>' );
  257. } );
  258. it( 'should write multiple ranges from selection #2', () => {
  259. const text1 = new Text( 'foobar' );
  260. const text2 = new Text( 'bazqux' );
  261. const b = new Element( 'b', null, text1 );
  262. const p = new Element( 'p', null, [ b, text2 ] );
  263. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  264. const range2 = Range.createFromParentsAndOffsets( text2, 0, text2, 3 );
  265. const range3 = Range.createFromParentsAndOffsets( text2, 3, text2, 4 );
  266. const range4 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  267. const selection = new Selection();
  268. selection.setRanges( [ range1, range2, range3, range4 ] );
  269. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]{baz}{q}ux</p>' );
  270. } );
  271. it( 'should use Position instance instead of Selection', () => {
  272. const text = new Text( 'foobar' );
  273. const position = new Position( text, 3 );
  274. const string = stringify( text, position );
  275. expect( string ).to.equal( 'foo{}bar' );
  276. } );
  277. it( 'should use Range instance instead of Selection', () => {
  278. const text = new Text( 'foobar' );
  279. const range = Range.createFromParentsAndOffsets( text, 3, text, 4 );
  280. const string = stringify( text, range );
  281. expect( string ).to.equal( 'foo{b}ar' );
  282. } );
  283. } );
  284. describe( 'parse', () => {
  285. it( 'should return empty DocumentFragment for empty string', () => {
  286. const fragment = parse( '' );
  287. expect( fragment ).to.be.instanceOf( DocumentFragment );
  288. expect( fragment.childCount ).to.equal( 0 );
  289. } );
  290. it( 'should return empty DocumentFragment and Selection for string containing range only', () => {
  291. const { view, selection } = parse( '[]' );
  292. expect( view ).to.be.instanceOf( DocumentFragment );
  293. expect( selection.rangeCount ).to.equal( 1 );
  294. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( view, 0, view, 0 ) ) ).to.be.true;
  295. } );
  296. it( 'should return Element if range is around single element', () => {
  297. const { view, selection } = parse( '[<b>foobar</b>]' );
  298. const parent = view.parent;
  299. expect( view ).to.be.instanceOf( Element );
  300. expect( parent ).to.be.instanceOf( DocumentFragment );
  301. expect( selection.rangeCount ).to.equal( 1 );
  302. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( parent, 0, parent, 1 ) ) ).to.be.true;
  303. } );
  304. it( 'should create DocumentFragment when multiple elements on root', () => {
  305. const view = parse( '<b></b><i></i>' );
  306. expect( view ).to.be.instanceOf( DocumentFragment );
  307. expect( view.childCount ).to.equal( 2 );
  308. expect( view.getChild( 0 ).isSimilar( new Element( 'b' ) ) ).to.be.true;
  309. expect( view.getChild( 1 ).isSimilar( new Element( 'i' ) ) ).to.be.true;
  310. } );
  311. it( 'should parse text', () => {
  312. const text = parse( 'foobar' );
  313. expect( text ).to.be.instanceOf( Text );
  314. expect( text.data ).to.equal( 'foobar' );
  315. } );
  316. it( 'should parse text with spaces', () => {
  317. const text = parse( 'foo bar' );
  318. expect( text ).to.be.instanceOf( Text );
  319. expect( text.data ).to.equal( 'foo bar' );
  320. } );
  321. it( 'should parse elements and texts', () => {
  322. const view = parse( '<b>foobar</b>' );
  323. const element = new Element( 'b' );
  324. expect( view ).to.be.instanceof( Element );
  325. expect( view.isSimilar( element ) ).to.be.true;
  326. expect( view.childCount ).to.equal( 1 );
  327. const text = view.getChild( 0 );
  328. expect( text ).to.be.instanceof( Text );
  329. expect( text.data ).to.equal( 'foobar' );
  330. } );
  331. it( 'should parse element attributes', () => {
  332. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  333. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  334. expect( view ).to.be.instanceof( Element );
  335. expect( view.isSimilar( element ) ).to.be.true;
  336. expect( view.childCount ).to.equal( 0 );
  337. } );
  338. it( 'should parse element type', () => {
  339. const view1 = parse( '<attribute:b></attribute:b>' );
  340. const attribute = new AttributeElement( 'b' );
  341. const view2 = parse( '<container:p></container:p>' );
  342. const container = new ContainerElement( 'p' );
  343. expect( view1 ).to.be.instanceof( AttributeElement );
  344. expect( view1.isSimilar( attribute ) ).to.be.true;
  345. expect( view2 ).to.be.instanceof( ContainerElement );
  346. expect( view2.isSimilar( container ) ).to.be.true;
  347. } );
  348. it( 'should parse element priority', () => {
  349. const parsed1 = parse( '<b view-priority="12"></b>' );
  350. const attribute1 = new AttributeElement( 'b' );
  351. attribute1.priority = 12;
  352. const parsed2 = parse( '<attribute:b view-priority="44"></attribute:b>' );
  353. const attribute2 = new AttributeElement( 'b' );
  354. attribute2.priority = 44;
  355. parsed1.isSimilar( attribute1 );
  356. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  357. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  358. } );
  359. it( 'should paste nested elements and texts', () => {
  360. const parsed = parse( '<container:p>foo<b view-priority="12">bar<i view-priority="25">qux</i></b></container:p>' );
  361. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  362. expect( parsed.childCount ).to.equal( 2 );
  363. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  364. const b = parsed.getChild( 1 );
  365. expect( b ).to.be.instanceof( AttributeElement );
  366. expect( b.priority ).to.equal( 12 );
  367. expect( b.childCount ).to.equal( 2 );
  368. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  369. const i = b.getChild( 1 );
  370. expect( i ).to.be.instanceof( AttributeElement );
  371. expect( i.priority ).to.equal( 25 );
  372. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  373. } );
  374. it( 'should parse selection range inside text', () => {
  375. const { view, selection } = parse( 'f{oo}b{}ar' );
  376. expect( view ).to.be.instanceof( Text );
  377. expect( view.data ).to.equal( 'foobar' );
  378. expect( selection.rangeCount ).to.equal( 2 );
  379. const ranges = [ ...selection.getRanges() ];
  380. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  381. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  382. } );
  383. it( 'should parse selection range between elements', () => {
  384. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  385. expect( view ).to.be.instanceof( Element );
  386. expect( view.childCount ).to.equal( 1 );
  387. const b = view.getChild( 0 );
  388. expect( b ).to.be.instanceof( Element );
  389. expect( b.name ).to.equal( 'b' );
  390. expect( b.childCount ).to.equal( 1 );
  391. const text = b.getChild( 0 );
  392. expect( text ).to.be.instanceof( Text );
  393. expect( text.data ).to.equal( 'foobar' );
  394. expect( selection.rangeCount ).to.equal( 2 );
  395. const ranges = [ ...selection.getRanges() ];
  396. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  397. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  398. } );
  399. it( 'should support unicode', () => {
  400. const { view, selection } = parse( '<p>[<b>நிலை}க்கு</b></p>' );
  401. expect( view ).to.be.instanceof( Element );
  402. expect( view.name ).to.equal( 'p' );
  403. expect( view.childCount ).to.equal( 1 );
  404. const b = view.getChild( 0 );
  405. expect( b.name ).to.equal( 'b' );
  406. expect( b.childCount ).to.equal( 1 );
  407. const text = b.getChild( 0 );
  408. expect( text.data ).to.equal( 'நிலைக்கு' );
  409. expect( selection.rangeCount ).to.equal( 1 );
  410. const range = selection.getFirstRange();
  411. expect( range.start.parent ).to.equal( view );
  412. expect( range.start.offset ).to.equal( 0 );
  413. expect( range.end.parent ).to.equal( text );
  414. expect( range.end.offset ).to.equal( 4 );
  415. } );
  416. it( 'should parse ranges #1', () => {
  417. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  418. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  419. expect( view.childCount ).to.equal( 1 );
  420. const text = view.getChild( 0 );
  421. expect( text ).to.be.instanceof( Text );
  422. expect( text.data ).to.equal( 'foobar' );
  423. expect( selection.rangeCount ).to.equal( 1 );
  424. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  425. } );
  426. it( 'should parse ranges #2', () => {
  427. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  428. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  429. expect( view.childCount ).to.equal( 2 );
  430. const text1 = view.getChild( 0 );
  431. expect( text1 ).to.be.instanceof( Text );
  432. expect( text1.data ).to.equal( 'foobar' );
  433. const i = view.getChild( 1 );
  434. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  435. expect( i.childCount ).to.equal( 1 );
  436. const text2 = i.getChild( 0 );
  437. expect( text2 ).to.be.instanceof( Text );
  438. expect( text2.data ).to.equal( 'baz' );
  439. expect( selection.rangeCount ).to.equal( 2 );
  440. const ranges = [ ...selection.getRanges() ];
  441. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  442. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  443. } );
  444. it( 'should use ranges order when provided', () => {
  445. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ] } );
  446. expect( selection.rangeCount ).to.equal( 3 );
  447. const ranges = [ ...selection.getRanges() ];
  448. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  449. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  450. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  451. expect( selection.anchor.isEqual( ranges[ 2 ].start ) ).to.be.true;
  452. expect( selection.focus.isEqual( ranges[ 2 ].end ) ).to.be.true;
  453. } );
  454. it( 'should set last range backward if needed', () => {
  455. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ], lastRangeBackward: true } );
  456. expect( selection.rangeCount ).to.equal( 3 );
  457. const ranges = [ ...selection.getRanges() ];
  458. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  459. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  460. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  461. expect( selection.anchor.isEqual( ranges[ 2 ].end ) ).to.be.true;
  462. expect( selection.focus.isEqual( ranges[ 2 ].start ) ).to.be.true;
  463. } );
  464. it( 'should throw when ranges order does not include all ranges', () => {
  465. expect( () => {
  466. parse( '{}foobar{}', { order: [ 1 ] } );
  467. } ).to.throw( Error );
  468. } );
  469. it( 'should throw when ranges order is invalid', () => {
  470. expect( () => {
  471. parse( '{}foobar{}', { order: [ 1, 4 ] } );
  472. } ).to.throw( Error );
  473. } );
  474. it( 'should throw when element range delimiter is inside text node', () => {
  475. expect( () => {
  476. parse( 'foo[bar' );
  477. } ).to.throw( Error );
  478. } );
  479. it( 'should throw when text range delimiter is inside empty text node', () => {
  480. expect( () => {
  481. parse( '<b>foo</b>}' );
  482. } ).to.throw( Error );
  483. } );
  484. it( 'should throw when end of range is found before start', () => {
  485. expect( () => {
  486. parse( 'fo}obar' );
  487. } ).to.throw( Error );
  488. } );
  489. it( 'should throw when intersecting ranges found', () => {
  490. expect( () => {
  491. parse( '[fo{o}bar]' );
  492. } ).to.throw( Error );
  493. } );
  494. it( 'should throw when opened ranges are left', () => {
  495. expect( () => {
  496. parse( 'fo{obar' );
  497. } ).to.throw( Error );
  498. } );
  499. it( 'should throw when wrong tag name is provided #1', () => {
  500. expect( () => {
  501. parse( '<b:bar></b:bar>' );
  502. } ).to.throw( Error );
  503. } );
  504. it( 'should throw when wrong tag name is provided #2', () => {
  505. expect( () => {
  506. parse( '<container:b:bar></container:b:bar>' );
  507. } ).to.throw( Error );
  508. } );
  509. it( 'should throw when wrong tag name is provided #3', () => {
  510. expect( () => {
  511. parse( '<container:b:10:test></container:b:10:test>' );
  512. } ).to.throw( Error );
  513. } );
  514. it( 'should use provided root element #1', () => {
  515. const root = new Element( 'p' );
  516. const data = parse( '<span>text</span>', { rootElement: root } );
  517. expect( stringify( data ) ).to.equal( '<p><span>text</span></p>' );
  518. } );
  519. it( 'should use provided root element #2', () => {
  520. const root = new Element( 'p' );
  521. const data = parse( '<span>text</span><b>test</b>', { rootElement: root } );
  522. expect( stringify( data ) ).to.equal( '<p><span>text</span><b>test</b></p>' );
  523. } );
  524. } );
  525. } );