view.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  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 write collapsed selection ranges inside elements', () => {
  134. const text = new Text( 'foobar' );
  135. const p = new Element( 'p', null, text );
  136. const range = Range.createFromParentsAndOffsets( p, 0, p, 0 );
  137. const selection = new Selection();
  138. selection.addRange( range );
  139. expect( stringify( p, selection ) ).to.equal( '<p>[]foobar</p>' );
  140. } );
  141. it( 'should write selection ranges inside text', () => {
  142. const text1 = new Text( 'foobar' );
  143. const text2 = new Text( 'bazqux' );
  144. const b1 = new Element( 'b', null, text1 );
  145. const b2 = new Element( 'b', null, text2 );
  146. const p = new Element( 'p', null, [ b1, b2 ] );
  147. const range = Range.createFromParentsAndOffsets( text1, 1, text1, 5 );
  148. const selection = new Selection();
  149. selection.addRange( range );
  150. expect( stringify( p, selection ) ).to.equal( '<p><b>f{ooba}r</b><b>bazqux</b></p>' );
  151. } );
  152. it( 'should write collapsed selection ranges inside texts', () => {
  153. const text = new Text( 'foobar' );
  154. const p = new Element( 'p', null, text );
  155. const range = Range.createFromParentsAndOffsets( text, 0, text, 0 );
  156. const selection = new Selection();
  157. selection.addRange( range );
  158. expect( stringify( p, selection ) ).to.equal( '<p>{}foobar</p>' );
  159. } );
  160. it( 'should write ranges that start inside text end ends between elements', () => {
  161. const text1 = new Text( 'foobar' );
  162. const text2 = new Text( 'bazqux' );
  163. const b1 = new Element( 'b', null, text1 );
  164. const b2 = new Element( 'b', null, text2 );
  165. const p = new Element( 'p', null, [ b1, b2 ] );
  166. const range = Range.createFromParentsAndOffsets( p, 0, text2, 5 );
  167. const selection = new Selection();
  168. selection.addRange( range );
  169. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b><b>bazqu}x</b></p>' );
  170. } );
  171. it( 'should write elements types as namespaces 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, { showType: true } ) )
  176. .to.equal( '<container:p><attribute:b>foobar</attribute:b></container:p>' );
  177. } );
  178. it( 'should not write element type when type is not specified', () => {
  179. const p = new Element( 'p' );
  180. expect( stringify( p, null, { showType: true } ) ).to.equal( '<p></p>' );
  181. } );
  182. it( 'should write elements priorities when needed', () => {
  183. const text = new Text( 'foobar' );
  184. const b = new AttributeElement( 'b', null, text );
  185. const p = new ContainerElement( 'p', null, b );
  186. expect( stringify( p, null, { showPriority: true } ) )
  187. .to.equal( '<p><b:10>foobar</b:10></p>' );
  188. } );
  189. it( 'should parse DocumentFragment as root', () => {
  190. const text1 = new Text( 'foobar' );
  191. const text2 = new Text( 'bazqux' );
  192. const b1 = new Element( 'b', null, text1 );
  193. const b2 = new Element( 'b', null, text2 );
  194. const fragment = new DocumentFragment( [ b1, b2 ] );
  195. expect( stringify( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
  196. } );
  197. it( 'should not write ranges outside elements - end position outside element', () => {
  198. const text = new Text( 'foobar' );
  199. const b = new Element( 'b', null, text );
  200. const p = new Element( 'p', null, b );
  201. const range = Range.createFromParentsAndOffsets( p, 0, p, 5 );
  202. expect( stringify( p, range ) ).to.equal( '<p>[<b>foobar</b></p>' );
  203. } );
  204. it( 'should not write ranges outside elements - start position outside element', () => {
  205. const text = new Text( 'foobar' );
  206. const b = new Element( 'b', null, text );
  207. const p = new Element( 'p', null, b );
  208. const range = Range.createFromParentsAndOffsets( p, -1, p, 1 );
  209. expect( stringify( p, range ) ).to.equal( '<p><b>foobar</b>]</p>' );
  210. } );
  211. it( 'should not write ranges outside elements - end position outside text', () => {
  212. const text = new Text( 'foobar' );
  213. const b = new Element( 'b', null, text );
  214. const p = new Element( 'p', null, b );
  215. const range = Range.createFromParentsAndOffsets( text, 0, text, 7 );
  216. expect( stringify( p, range ) ).to.equal( '<p><b>{foobar</b></p>' );
  217. } );
  218. it( 'should not write ranges outside elements - start position outside text', () => {
  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( text, -1, text, 2 );
  223. expect( stringify( p, range ) ).to.equal( '<p><b>fo}obar</b></p>' );
  224. } );
  225. it( 'should write multiple ranges from selection #1', () => {
  226. const text1 = new Text( 'foobar' );
  227. const text2 = new Text( 'bazqux' );
  228. const b1 = new Element( 'b', null, text1 );
  229. const b2 = new Element( 'b', null, text2 );
  230. const p = new Element( 'p', null, [ b1, b2 ] );
  231. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  232. const range2 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  233. const selection = new Selection();
  234. selection.setRanges( [ range2, range1 ] );
  235. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]<b>bazqux</b></p>' );
  236. } );
  237. it( 'should write multiple ranges from selection #2', () => {
  238. const text1 = new Text( 'foobar' );
  239. const text2 = new Text( 'bazqux' );
  240. const b = new Element( 'b', null, text1 );
  241. const p = new Element( 'p', null, [ b, text2 ] );
  242. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  243. const range2 = Range.createFromParentsAndOffsets( text2, 0, text2, 3 );
  244. const range3 = Range.createFromParentsAndOffsets( text2, 3, text2, 4 );
  245. const range4 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  246. const selection = new Selection();
  247. selection.setRanges( [ range1, range2, range3, range4 ] );
  248. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]{baz}{q}ux</p>' );
  249. } );
  250. it( 'should use Position instance instead of Selection', () => {
  251. const text = new Text( 'foobar' );
  252. const position = new Position( text, 3 );
  253. const string = stringify( text, position );
  254. expect( string ).to.equal( 'foo{}bar' );
  255. } );
  256. it( 'should use Range instance instead of Selection', () => {
  257. const text = new Text( 'foobar' );
  258. const range = Range.createFromParentsAndOffsets( text, 3, text, 4 );
  259. const string = stringify( text, range );
  260. expect( string ).to.equal( 'foo{b}ar' );
  261. } );
  262. } );
  263. describe( 'parse', () => {
  264. it( 'should parse text', () => {
  265. const text = parse( 'foobar' );
  266. expect( text ).to.be.instanceOf( Text );
  267. expect( text.data ).to.equal( 'foobar' );
  268. } );
  269. it( 'should parse elements and texts', () => {
  270. const view = parse( '<b>foobar</b>' );
  271. const element = new Element( 'b' );
  272. expect( view ).to.be.instanceof( Element );
  273. expect( view.isSimilar( element ) ).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. } );
  279. it( 'should parse element attributes', () => {
  280. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  281. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  282. expect( view ).to.be.instanceof( Element );
  283. expect( view.isSimilar( element ) ).to.be.true;
  284. expect( view.getChildCount() ).to.equal( 0 );
  285. } );
  286. it( 'should parse element type', () => {
  287. const view1 = parse( '<attribute:b></attribute:b>' );
  288. const attribute = new AttributeElement( 'b' );
  289. const view2 = parse( '<container:p></container:p>' );
  290. const container = new ContainerElement( 'p' );
  291. expect( view1 ).to.be.instanceof( AttributeElement );
  292. expect( view1.isSimilar( attribute ) ).to.be.true;
  293. expect( view2 ).to.be.instanceof( ContainerElement );
  294. expect( view2.isSimilar( container ) ).to.be.true;
  295. } );
  296. it( 'should parse element priority', () => {
  297. const parsed1 = parse( '<b:12></b:12>' );
  298. const attribute1 = new AttributeElement( 'b' );
  299. attribute1.priority = 12;
  300. const parsed2 = parse( '<attribute:b:44></attribute:b:44>' );
  301. const attribute2 = new AttributeElement( 'b' );
  302. attribute2.priority = 44;
  303. parsed1.isSimilar( attribute1 );
  304. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  305. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  306. } );
  307. it( 'should create DocumentFragment when multiple elements on root', () => {
  308. const view = parse( '<b></b><i></i>' );
  309. expect( view ).to.be.instanceOf( DocumentFragment );
  310. expect( view.getChildCount() ).to.equal( 2 );
  311. expect( view.getChild( 0 ).isSimilar( new Element( 'b' ) ) ).to.be.true;
  312. expect( view.getChild( 1 ).isSimilar( new Element( 'i' ) ) ).to.be.true;
  313. } );
  314. it( 'should paste nested elements and texts', () => {
  315. const parsed = parse( '<container:p>foo<b:12>bar<i:25>qux</i:25></b:12></container:p>' );
  316. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  317. expect( parsed.getChildCount() ).to.equal( 2 );
  318. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  319. const b = parsed.getChild( 1 );
  320. expect( b ).to.be.instanceof( AttributeElement );
  321. expect( b.priority ).to.equal( 12 );
  322. expect( b.getChildCount() ).to.equal( 2 );
  323. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  324. const i = b.getChild( 1 );
  325. expect( i ).to.be.instanceof( AttributeElement );
  326. expect( i.priority ).to.equal( 25 );
  327. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  328. } );
  329. it( 'should parse selection range inside text', () => {
  330. const { view, selection } = parse( 'f{oo}b{}ar' );
  331. expect( view ).to.be.instanceof( Text );
  332. expect( view.data ).to.equal( 'foobar' );
  333. expect( selection.rangeCount ).to.equal( 2 );
  334. const ranges = [ ...selection.getRanges() ];
  335. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  336. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  337. } );
  338. it( 'should parse selection range between elements', () => {
  339. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  340. expect( view ).to.be.instanceof( Element );
  341. expect( view.getChildCount() ).to.equal( 1 );
  342. const b = view.getChild( 0 );
  343. expect( b ).to.be.instanceof( Element );
  344. expect( b.name ).to.equal( 'b' );
  345. expect( b.getChildCount() ).to.equal( 1 );
  346. const text = b.getChild( 0 );
  347. expect( text ).to.be.instanceof( Text );
  348. expect( text.data ).to.equal( 'foobar' );
  349. expect( selection.rangeCount ).to.equal( 2 );
  350. const ranges = [ ...selection.getRanges() ];
  351. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  352. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  353. } );
  354. it( 'should parse ranges #1', () => {
  355. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  356. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  357. expect( view.getChildCount() ).to.equal( 1 );
  358. const text = view.getChild( 0 );
  359. expect( text ).to.be.instanceof( Text );
  360. expect( text.data ).to.equal( 'foobar' );
  361. expect( selection.rangeCount ).to.equal( 1 );
  362. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  363. } );
  364. it( 'should parse ranges #2', () => {
  365. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  366. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  367. expect( view.getChildCount() ).to.equal( 2 );
  368. const text1 = view.getChild( 0 );
  369. expect( text1 ).to.be.instanceof( Text );
  370. expect( text1.data ).to.equal( 'foobar' );
  371. const i = view.getChild( 1 );
  372. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  373. expect( i.getChildCount() ).to.equal( 1 );
  374. const text2 = i.getChild( 0 );
  375. expect( text2 ).to.be.instanceof( Text );
  376. expect( text2.data ).to.equal( 'baz' );
  377. expect( selection.rangeCount ).to.equal( 2 );
  378. const ranges = [ ...selection.getRanges() ];
  379. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  380. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  381. } );
  382. it( 'should use ranges order when provided', () => {
  383. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ] } );
  384. expect( selection.rangeCount ).to.equal( 3 );
  385. const ranges = [ ...selection.getRanges() ];
  386. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  387. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  388. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  389. expect( selection.anchor.isEqual( ranges[ 2 ].start ) ).to.be.true;
  390. expect( selection.focus.isEqual( ranges[ 2 ].end ) ).to.be.true;
  391. } );
  392. it( 'should set last range backward if needed', () => {
  393. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ], lastRangeBackward: true } );
  394. expect( selection.rangeCount ).to.equal( 3 );
  395. const ranges = [ ...selection.getRanges() ];
  396. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  397. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  398. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  399. expect( selection.anchor.isEqual( ranges[ 2 ].end ) ).to.be.true;
  400. expect( selection.focus.isEqual( ranges[ 2 ].start ) ).to.be.true;
  401. } );
  402. it( 'should return DocumentFragment if range is around single element', () => {
  403. const { view, selection } = parse( '[<b>foobar</b>]' );
  404. expect( view ).to.be.instanceOf( DocumentFragment );
  405. expect( selection.rangeCount ).to.equal( 1 );
  406. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  407. } );
  408. it( 'should throw when ranges order does not include all ranges', () => {
  409. expect( () => {
  410. parse( '{}foobar{}', { order: [ 1 ] } );
  411. } ).to.throw( Error );
  412. } );
  413. it( 'should throw when ranges order is invalid', () => {
  414. expect( () => {
  415. parse( '{}foobar{}', { order: [ 1, 4 ] } );
  416. } ).to.throw( Error );
  417. } );
  418. it( 'should throw when element range delimiter is inside text node', () => {
  419. expect( () => {
  420. parse( 'foo[bar' );
  421. } ).to.throw( Error );
  422. } );
  423. it( 'should throw when text range delimiter is inside empty text node', () => {
  424. expect( () => {
  425. parse( '<b>foo</b>}' );
  426. } ).to.throw( Error );
  427. } );
  428. it( 'should throw when end of range is found before start', () => {
  429. expect( () => {
  430. parse( 'fo}obar' );
  431. } ).to.throw( Error );
  432. } );
  433. it( 'should throw when intersecting ranges found', () => {
  434. expect( () => {
  435. parse( '[fo{o}bar]' );
  436. } ).to.throw( Error );
  437. } );
  438. it( 'should throw when opened ranges are left', () => {
  439. expect( () => {
  440. parse( 'fo{obar' );
  441. } ).to.throw( Error );
  442. } );
  443. it( 'should throw when wrong tag name is provided #1', () => {
  444. expect( () => {
  445. parse( '<b:bar></b:bar>' );
  446. } ).to.throw( Error );
  447. } );
  448. it( 'should throw when wrong tag name is provided #2', () => {
  449. expect( () => {
  450. parse( '<container:b:bar></container:b:bar>' );
  451. } ).to.throw( Error );
  452. } );
  453. it( 'should throw when wrong tag name is provided #3', () => {
  454. expect( () => {
  455. parse( '<container:b:10:test></container:b:10:test>' );
  456. } ).to.throw( Error );
  457. } );
  458. it( 'should use provided root element #1', () => {
  459. const root = new Element( 'p' );
  460. const data = parse( '<span>text</span>', { rootElement: root } );
  461. expect( stringify( data ) ).to.equal( '<p><span>text</span></p>' );
  462. } );
  463. it( 'should use provided root element #2', () => {
  464. const root = new Element( 'p' );
  465. const data = parse( '<span>text</span><b>test</b>', { rootElement: root } );
  466. expect( stringify( data ) ).to.equal( '<p><span>text</span><b>test</b></p>' );
  467. } );
  468. } );
  469. } );