view.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 collapsed selection ranges inside texts', () => {
  162. const text = new Text( 'foobar' );
  163. const p = new Element( 'p', null, text );
  164. const range = Range.createFromParentsAndOffsets( text, 0, text, 0 );
  165. const selection = new Selection();
  166. selection.addRange( range );
  167. expect( stringify( p, selection ) ).to.equal( '<p>{}foobar</p>' );
  168. } );
  169. it( 'should write ranges that start inside text end ends between elements', () => {
  170. const text1 = new Text( 'foobar' );
  171. const text2 = new Text( 'bazqux' );
  172. const b1 = new Element( 'b', null, text1 );
  173. const b2 = new Element( 'b', null, text2 );
  174. const p = new Element( 'p', null, [ b1, b2 ] );
  175. const range = Range.createFromParentsAndOffsets( p, 0, text2, 5 );
  176. const selection = new Selection();
  177. selection.addRange( range );
  178. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b><b>bazqu}x</b></p>' );
  179. } );
  180. it( 'should write elements types as namespaces when needed', () => {
  181. const text = new Text( 'foobar' );
  182. const b = new AttributeElement( 'b', null, text );
  183. const p = new ContainerElement( 'p', null, b );
  184. expect( stringify( p, null, { showType: true } ) )
  185. .to.equal( '<container:p><attribute:b>foobar</attribute:b></container:p>' );
  186. } );
  187. it( 'should not write element type when type is not specified', () => {
  188. const p = new Element( 'p' );
  189. expect( stringify( p, null, { showType: true } ) ).to.equal( '<p></p>' );
  190. } );
  191. it( 'should write elements priorities when needed', () => {
  192. const text = new Text( 'foobar' );
  193. const b = new AttributeElement( 'b', null, text );
  194. const p = new ContainerElement( 'p', null, b );
  195. expect( stringify( p, null, { showPriority: true } ) )
  196. .to.equal( '<p><b:10>foobar</b:10></p>' );
  197. } );
  198. it( 'should parse DocumentFragment as root', () => {
  199. const text1 = new Text( 'foobar' );
  200. const text2 = new Text( 'bazqux' );
  201. const b1 = new Element( 'b', null, text1 );
  202. const b2 = new Element( 'b', null, text2 );
  203. const fragment = new DocumentFragment( [ b1, b2 ] );
  204. expect( stringify( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
  205. } );
  206. it( 'should not write ranges outside elements - end position outside element', () => {
  207. const text = new Text( 'foobar' );
  208. const b = new Element( 'b', null, text );
  209. const p = new Element( 'p', null, b );
  210. const range = Range.createFromParentsAndOffsets( p, 0, p, 5 );
  211. expect( stringify( p, range ) ).to.equal( '<p>[<b>foobar</b></p>' );
  212. } );
  213. it( 'should not write ranges outside elements - start position outside element', () => {
  214. const text = new Text( 'foobar' );
  215. const b = new Element( 'b', null, text );
  216. const p = new Element( 'p', null, b );
  217. const range = Range.createFromParentsAndOffsets( p, -1, p, 1 );
  218. expect( stringify( p, range ) ).to.equal( '<p><b>foobar</b>]</p>' );
  219. } );
  220. it( 'should not write ranges outside elements - end position outside text', () => {
  221. const text = new Text( 'foobar' );
  222. const b = new Element( 'b', null, text );
  223. const p = new Element( 'p', null, b );
  224. const range = Range.createFromParentsAndOffsets( text, 0, text, 7 );
  225. expect( stringify( p, range ) ).to.equal( '<p><b>{foobar</b></p>' );
  226. } );
  227. it( 'should not write ranges outside elements - start position outside text', () => {
  228. const text = new Text( 'foobar' );
  229. const b = new Element( 'b', null, text );
  230. const p = new Element( 'p', null, b );
  231. const range = Range.createFromParentsAndOffsets( text, -1, text, 2 );
  232. expect( stringify( p, range ) ).to.equal( '<p><b>fo}obar</b></p>' );
  233. } );
  234. it( 'should write multiple ranges from selection #1', () => {
  235. const text1 = new Text( 'foobar' );
  236. const text2 = new Text( 'bazqux' );
  237. const b1 = new Element( 'b', null, text1 );
  238. const b2 = new Element( 'b', null, text2 );
  239. const p = new Element( 'p', null, [ b1, b2 ] );
  240. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  241. const range2 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  242. const selection = new Selection();
  243. selection.setRanges( [ range2, range1 ] );
  244. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]<b>bazqux</b></p>' );
  245. } );
  246. it( 'should write multiple ranges from selection #2', () => {
  247. const text1 = new Text( 'foobar' );
  248. const text2 = new Text( 'bazqux' );
  249. const b = new Element( 'b', null, text1 );
  250. const p = new Element( 'p', null, [ b, text2 ] );
  251. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  252. const range2 = Range.createFromParentsAndOffsets( text2, 0, text2, 3 );
  253. const range3 = Range.createFromParentsAndOffsets( text2, 3, text2, 4 );
  254. const range4 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  255. const selection = new Selection();
  256. selection.setRanges( [ range1, range2, range3, range4 ] );
  257. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]{baz}{q}ux</p>' );
  258. } );
  259. it( 'should use Position instance instead of Selection', () => {
  260. const text = new Text( 'foobar' );
  261. const position = new Position( text, 3 );
  262. const string = stringify( text, position );
  263. expect( string ).to.equal( 'foo{}bar' );
  264. } );
  265. it( 'should use Range instance instead of Selection', () => {
  266. const text = new Text( 'foobar' );
  267. const range = Range.createFromParentsAndOffsets( text, 3, text, 4 );
  268. const string = stringify( text, range );
  269. expect( string ).to.equal( 'foo{b}ar' );
  270. } );
  271. } );
  272. describe( 'parse', () => {
  273. it( 'should return empty DocumentFragment for empty string', () => {
  274. const fragment = parse( '' );
  275. expect( fragment ).to.be.instanceOf( DocumentFragment );
  276. expect( fragment.childCount ).to.equal( 0 );
  277. } );
  278. it( 'should return empty DocumentFragment and Selection for string containing range only', () => {
  279. const { view, selection } = parse( '[]' );
  280. expect( view ).to.be.instanceOf( DocumentFragment );
  281. expect( selection.rangeCount ).to.equal( 1 );
  282. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( view, 0, view, 0 ) ) ).to.be.true;
  283. } );
  284. it( 'should return Element if range is around single element', () => {
  285. const { view, selection } = parse( '[<b>foobar</b>]' );
  286. const parent = view.parent;
  287. expect( view ).to.be.instanceOf( Element );
  288. expect( parent ).to.be.instanceOf( DocumentFragment );
  289. expect( selection.rangeCount ).to.equal( 1 );
  290. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( parent, 0, parent, 1 ) ) ).to.be.true;
  291. } );
  292. it( 'should create DocumentFragment when multiple elements on root', () => {
  293. const view = parse( '<b></b><i></i>' );
  294. expect( view ).to.be.instanceOf( DocumentFragment );
  295. expect( view.childCount ).to.equal( 2 );
  296. expect( view.getChild( 0 ).isSimilar( new Element( 'b' ) ) ).to.be.true;
  297. expect( view.getChild( 1 ).isSimilar( new Element( 'i' ) ) ).to.be.true;
  298. } );
  299. it( 'should parse text', () => {
  300. const text = parse( 'foobar' );
  301. expect( text ).to.be.instanceOf( Text );
  302. expect( text.data ).to.equal( 'foobar' );
  303. } );
  304. it( 'should parse text with spaces', () => {
  305. const text = parse( 'foo bar' );
  306. expect( text ).to.be.instanceOf( Text );
  307. expect( text.data ).to.equal( 'foo bar' );
  308. } );
  309. it( 'should parse elements and texts', () => {
  310. const view = parse( '<b>foobar</b>' );
  311. const element = new Element( 'b' );
  312. expect( view ).to.be.instanceof( Element );
  313. expect( view.isSimilar( element ) ).to.be.true;
  314. expect( view.childCount ).to.equal( 1 );
  315. const text = view.getChild( 0 );
  316. expect( text ).to.be.instanceof( Text );
  317. expect( text.data ).to.equal( 'foobar' );
  318. } );
  319. it( 'should parse element attributes', () => {
  320. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  321. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  322. expect( view ).to.be.instanceof( Element );
  323. expect( view.isSimilar( element ) ).to.be.true;
  324. expect( view.childCount ).to.equal( 0 );
  325. } );
  326. it( 'should parse element type', () => {
  327. const view1 = parse( '<attribute:b></attribute:b>' );
  328. const attribute = new AttributeElement( 'b' );
  329. const view2 = parse( '<container:p></container:p>' );
  330. const container = new ContainerElement( 'p' );
  331. expect( view1 ).to.be.instanceof( AttributeElement );
  332. expect( view1.isSimilar( attribute ) ).to.be.true;
  333. expect( view2 ).to.be.instanceof( ContainerElement );
  334. expect( view2.isSimilar( container ) ).to.be.true;
  335. } );
  336. it( 'should parse element priority', () => {
  337. const parsed1 = parse( '<b:12></b:12>' );
  338. const attribute1 = new AttributeElement( 'b' );
  339. attribute1.priority = 12;
  340. const parsed2 = parse( '<attribute:b:44></attribute:b:44>' );
  341. const attribute2 = new AttributeElement( 'b' );
  342. attribute2.priority = 44;
  343. parsed1.isSimilar( attribute1 );
  344. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  345. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  346. } );
  347. it( 'should paste nested elements and texts', () => {
  348. const parsed = parse( '<container:p>foo<b:12>bar<i:25>qux</i:25></b:12></container:p>' );
  349. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  350. expect( parsed.childCount ).to.equal( 2 );
  351. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  352. const b = parsed.getChild( 1 );
  353. expect( b ).to.be.instanceof( AttributeElement );
  354. expect( b.priority ).to.equal( 12 );
  355. expect( b.childCount ).to.equal( 2 );
  356. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  357. const i = b.getChild( 1 );
  358. expect( i ).to.be.instanceof( AttributeElement );
  359. expect( i.priority ).to.equal( 25 );
  360. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  361. } );
  362. it( 'should parse selection range inside text', () => {
  363. const { view, selection } = parse( 'f{oo}b{}ar' );
  364. expect( view ).to.be.instanceof( Text );
  365. expect( view.data ).to.equal( 'foobar' );
  366. expect( selection.rangeCount ).to.equal( 2 );
  367. const ranges = [ ...selection.getRanges() ];
  368. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  369. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  370. } );
  371. it( 'should parse selection range between elements', () => {
  372. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  373. expect( view ).to.be.instanceof( Element );
  374. expect( view.childCount ).to.equal( 1 );
  375. const b = view.getChild( 0 );
  376. expect( b ).to.be.instanceof( Element );
  377. expect( b.name ).to.equal( 'b' );
  378. expect( b.childCount ).to.equal( 1 );
  379. const text = b.getChild( 0 );
  380. expect( text ).to.be.instanceof( Text );
  381. expect( text.data ).to.equal( 'foobar' );
  382. expect( selection.rangeCount ).to.equal( 2 );
  383. const ranges = [ ...selection.getRanges() ];
  384. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  385. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  386. } );
  387. it( 'should support unicode', () => {
  388. const { view, selection } = parse( '<p>[<b>நிலை}க்கு</b></p>' );
  389. expect( view ).to.be.instanceof( Element );
  390. expect( view.name ).to.equal( 'p' );
  391. expect( view.childCount ).to.equal( 1 );
  392. const b = view.getChild( 0 );
  393. expect( b.name ).to.equal( 'b' );
  394. expect( b.childCount ).to.equal( 1 );
  395. const text = b.getChild( 0 );
  396. expect( text.data ).to.equal( 'நிலைக்கு' );
  397. expect( selection.rangeCount ).to.equal( 1 );
  398. const range = selection.getFirstRange();
  399. expect( range.start.parent ).to.equal( view );
  400. expect( range.start.offset ).to.equal( 0 );
  401. expect( range.end.parent ).to.equal( text );
  402. expect( range.end.offset ).to.equal( 4 );
  403. } );
  404. it( 'should parse ranges #1', () => {
  405. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  406. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  407. expect( view.childCount ).to.equal( 1 );
  408. const text = view.getChild( 0 );
  409. expect( text ).to.be.instanceof( Text );
  410. expect( text.data ).to.equal( 'foobar' );
  411. expect( selection.rangeCount ).to.equal( 1 );
  412. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  413. } );
  414. it( 'should parse ranges #2', () => {
  415. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  416. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  417. expect( view.childCount ).to.equal( 2 );
  418. const text1 = view.getChild( 0 );
  419. expect( text1 ).to.be.instanceof( Text );
  420. expect( text1.data ).to.equal( 'foobar' );
  421. const i = view.getChild( 1 );
  422. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  423. expect( i.childCount ).to.equal( 1 );
  424. const text2 = i.getChild( 0 );
  425. expect( text2 ).to.be.instanceof( Text );
  426. expect( text2.data ).to.equal( 'baz' );
  427. expect( selection.rangeCount ).to.equal( 2 );
  428. const ranges = [ ...selection.getRanges() ];
  429. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  430. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  431. } );
  432. it( 'should use ranges order when provided', () => {
  433. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ] } );
  434. expect( selection.rangeCount ).to.equal( 3 );
  435. const ranges = [ ...selection.getRanges() ];
  436. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  437. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  438. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  439. expect( selection.anchor.isEqual( ranges[ 2 ].start ) ).to.be.true;
  440. expect( selection.focus.isEqual( ranges[ 2 ].end ) ).to.be.true;
  441. } );
  442. it( 'should set last range backward if needed', () => {
  443. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ], lastRangeBackward: true } );
  444. expect( selection.rangeCount ).to.equal( 3 );
  445. const ranges = [ ...selection.getRanges() ];
  446. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  447. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  448. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  449. expect( selection.anchor.isEqual( ranges[ 2 ].end ) ).to.be.true;
  450. expect( selection.focus.isEqual( ranges[ 2 ].start ) ).to.be.true;
  451. } );
  452. it( 'should throw when ranges order does not include all ranges', () => {
  453. expect( () => {
  454. parse( '{}foobar{}', { order: [ 1 ] } );
  455. } ).to.throw( Error );
  456. } );
  457. it( 'should throw when ranges order is invalid', () => {
  458. expect( () => {
  459. parse( '{}foobar{}', { order: [ 1, 4 ] } );
  460. } ).to.throw( Error );
  461. } );
  462. it( 'should throw when element range delimiter is inside text node', () => {
  463. expect( () => {
  464. parse( 'foo[bar' );
  465. } ).to.throw( Error );
  466. } );
  467. it( 'should throw when text range delimiter is inside empty text node', () => {
  468. expect( () => {
  469. parse( '<b>foo</b>}' );
  470. } ).to.throw( Error );
  471. } );
  472. it( 'should throw when end of range is found before start', () => {
  473. expect( () => {
  474. parse( 'fo}obar' );
  475. } ).to.throw( Error );
  476. } );
  477. it( 'should throw when intersecting ranges found', () => {
  478. expect( () => {
  479. parse( '[fo{o}bar]' );
  480. } ).to.throw( Error );
  481. } );
  482. it( 'should throw when opened ranges are left', () => {
  483. expect( () => {
  484. parse( 'fo{obar' );
  485. } ).to.throw( Error );
  486. } );
  487. it( 'should throw when wrong tag name is provided #1', () => {
  488. expect( () => {
  489. parse( '<b:bar></b:bar>' );
  490. } ).to.throw( Error );
  491. } );
  492. it( 'should throw when wrong tag name is provided #2', () => {
  493. expect( () => {
  494. parse( '<container:b:bar></container:b:bar>' );
  495. } ).to.throw( Error );
  496. } );
  497. it( 'should throw when wrong tag name is provided #3', () => {
  498. expect( () => {
  499. parse( '<container:b:10:test></container:b:10:test>' );
  500. } ).to.throw( Error );
  501. } );
  502. it( 'should use provided root element #1', () => {
  503. const root = new Element( 'p' );
  504. const data = parse( '<span>text</span>', { rootElement: root } );
  505. expect( stringify( data ) ).to.equal( '<p><span>text</span></p>' );
  506. } );
  507. it( 'should use provided root element #2', () => {
  508. const root = new Element( 'p' );
  509. const data = parse( '<span>text</span><b>test</b>', { rootElement: root } );
  510. expect( stringify( data ) ).to.equal( '<p><span>text</span><b>test</b></p>' );
  511. } );
  512. } );
  513. } );