view.js 26 KB

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