view.js 21 KB

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