view.js 21 KB

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