view.js 24 KB

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