8
0

view.js 25 KB

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