view.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: browser-only */
  7. import { parse, stringify, getData, setData }from 'ckeditor5/engine/dev-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 EmptyElement from 'ckeditor5/engine/view/emptyelement.js';
  14. import Text from 'ckeditor5/engine/view/text.js';
  15. import Selection from 'ckeditor5/engine/view/selection.js';
  16. import Range from 'ckeditor5/engine/view/range.js';
  17. import Document from 'ckeditor5/engine/view/document.js';
  18. import XmlDataProcessor from 'ckeditor5/engine/dataprocessor/xmldataprocessor.js';
  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 engine.view.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 engine.view.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. } );
  296. describe( 'parse', () => {
  297. it( 'should return empty DocumentFragment for empty string', () => {
  298. const fragment = parse( '' );
  299. expect( fragment ).to.be.instanceOf( DocumentFragment );
  300. expect( fragment.childCount ).to.equal( 0 );
  301. } );
  302. it( 'should return empty DocumentFragment and Selection for string containing range only', () => {
  303. const { view, selection } = parse( '[]' );
  304. expect( view ).to.be.instanceOf( DocumentFragment );
  305. expect( selection.rangeCount ).to.equal( 1 );
  306. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( view, 0, view, 0 ) ) ).to.be.true;
  307. } );
  308. it( 'should return Element if range is around single element', () => {
  309. const { view, selection } = parse( '[<b>foobar</b>]' );
  310. const parent = view.parent;
  311. expect( view ).to.be.instanceOf( Element );
  312. expect( parent ).to.be.instanceOf( DocumentFragment );
  313. expect( selection.rangeCount ).to.equal( 1 );
  314. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( parent, 0, parent, 1 ) ) ).to.be.true;
  315. } );
  316. it( 'should create DocumentFragment when multiple elements on root', () => {
  317. const view = parse( '<b></b><i></i>' );
  318. expect( view ).to.be.instanceOf( DocumentFragment );
  319. expect( view.childCount ).to.equal( 2 );
  320. expect( view.getChild( 0 ).isSimilar( new Element( 'b' ) ) ).to.be.true;
  321. expect( view.getChild( 1 ).isSimilar( new Element( 'i' ) ) ).to.be.true;
  322. } );
  323. it( 'should parse text', () => {
  324. const text = parse( 'foobar' );
  325. expect( text ).to.be.instanceOf( Text );
  326. expect( text.data ).to.equal( 'foobar' );
  327. } );
  328. it( 'should parse text with spaces', () => {
  329. const text = parse( 'foo bar' );
  330. expect( text ).to.be.instanceOf( Text );
  331. expect( text.data ).to.equal( 'foo bar' );
  332. } );
  333. it( 'should parse elements and texts', () => {
  334. const view = parse( '<b>foobar</b>' );
  335. const element = new Element( 'b' );
  336. expect( view ).to.be.instanceof( Element );
  337. expect( view.isSimilar( element ) ).to.be.true;
  338. expect( view.childCount ).to.equal( 1 );
  339. const text = view.getChild( 0 );
  340. expect( text ).to.be.instanceof( Text );
  341. expect( text.data ).to.equal( 'foobar' );
  342. } );
  343. it( 'should parse element attributes', () => {
  344. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  345. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  346. expect( view ).to.be.instanceof( Element );
  347. expect( view.isSimilar( element ) ).to.be.true;
  348. expect( view.childCount ).to.equal( 0 );
  349. } );
  350. it( 'should parse element type', () => {
  351. const view1 = parse( '<attribute:b></attribute:b>' );
  352. const attribute = new AttributeElement( 'b' );
  353. const view2 = parse( '<container:p></container:p>' );
  354. const container = new ContainerElement( 'p' );
  355. expect( view1 ).to.be.instanceof( AttributeElement );
  356. expect( view1.isSimilar( attribute ) ).to.be.true;
  357. expect( view2 ).to.be.instanceof( ContainerElement );
  358. expect( view2.isSimilar( container ) ).to.be.true;
  359. } );
  360. it( 'should parse element priority', () => {
  361. const parsed1 = parse( '<b view-priority="12"></b>' );
  362. const attribute1 = new AttributeElement( 'b' );
  363. attribute1.priority = 12;
  364. const parsed2 = parse( '<attribute:b view-priority="44"></attribute:b>' );
  365. const attribute2 = new AttributeElement( 'b' );
  366. attribute2.priority = 44;
  367. parsed1.isSimilar( attribute1 );
  368. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  369. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  370. } );
  371. it( 'should paste nested elements and texts', () => {
  372. const parsed = parse( '<container:p>foo<b view-priority="12">bar<i view-priority="25">qux</i></b></container:p>' );
  373. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  374. expect( parsed.childCount ).to.equal( 2 );
  375. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  376. const b = parsed.getChild( 1 );
  377. expect( b ).to.be.instanceof( AttributeElement );
  378. expect( b.priority ).to.equal( 12 );
  379. expect( b.childCount ).to.equal( 2 );
  380. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  381. const i = b.getChild( 1 );
  382. expect( i ).to.be.instanceof( AttributeElement );
  383. expect( i.priority ).to.equal( 25 );
  384. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  385. } );
  386. it( 'should parse selection range inside text', () => {
  387. const { view, selection } = parse( 'f{oo}b{}ar' );
  388. expect( view ).to.be.instanceof( Text );
  389. expect( view.data ).to.equal( 'foobar' );
  390. expect( selection.rangeCount ).to.equal( 2 );
  391. const ranges = [ ...selection.getRanges() ];
  392. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  393. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  394. } );
  395. it( 'should parse selection range between elements', () => {
  396. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  397. expect( view ).to.be.instanceof( Element );
  398. expect( view.childCount ).to.equal( 1 );
  399. const b = view.getChild( 0 );
  400. expect( b ).to.be.instanceof( Element );
  401. expect( b.name ).to.equal( 'b' );
  402. expect( b.childCount ).to.equal( 1 );
  403. const text = b.getChild( 0 );
  404. expect( text ).to.be.instanceof( Text );
  405. expect( text.data ).to.equal( 'foobar' );
  406. expect( selection.rangeCount ).to.equal( 2 );
  407. const ranges = [ ...selection.getRanges() ];
  408. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  409. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  410. } );
  411. it( 'should support unicode', () => {
  412. const { view, selection } = parse( '<p>[<b>நிலை}க்கு</b></p>' );
  413. expect( view ).to.be.instanceof( Element );
  414. expect( view.name ).to.equal( 'p' );
  415. expect( view.childCount ).to.equal( 1 );
  416. const b = view.getChild( 0 );
  417. expect( b.name ).to.equal( 'b' );
  418. expect( b.childCount ).to.equal( 1 );
  419. const text = b.getChild( 0 );
  420. expect( text.data ).to.equal( 'நிலைக்கு' );
  421. expect( selection.rangeCount ).to.equal( 1 );
  422. const range = selection.getFirstRange();
  423. expect( range.start.parent ).to.equal( view );
  424. expect( range.start.offset ).to.equal( 0 );
  425. expect( range.end.parent ).to.equal( text );
  426. expect( range.end.offset ).to.equal( 4 );
  427. } );
  428. it( 'should parse ranges #1', () => {
  429. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  430. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  431. expect( view.childCount ).to.equal( 1 );
  432. const text = view.getChild( 0 );
  433. expect( text ).to.be.instanceof( Text );
  434. expect( text.data ).to.equal( 'foobar' );
  435. expect( selection.rangeCount ).to.equal( 1 );
  436. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  437. } );
  438. it( 'should parse ranges #2', () => {
  439. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  440. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  441. expect( view.childCount ).to.equal( 2 );
  442. const text1 = view.getChild( 0 );
  443. expect( text1 ).to.be.instanceof( Text );
  444. expect( text1.data ).to.equal( 'foobar' );
  445. const i = view.getChild( 1 );
  446. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  447. expect( i.childCount ).to.equal( 1 );
  448. const text2 = i.getChild( 0 );
  449. expect( text2 ).to.be.instanceof( Text );
  450. expect( text2.data ).to.equal( 'baz' );
  451. expect( selection.rangeCount ).to.equal( 2 );
  452. const ranges = [ ...selection.getRanges() ];
  453. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  454. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  455. } );
  456. it( 'should use ranges order when provided', () => {
  457. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ] } );
  458. expect( selection.rangeCount ).to.equal( 3 );
  459. const ranges = [ ...selection.getRanges() ];
  460. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  461. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  462. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  463. expect( selection.anchor.isEqual( ranges[ 2 ].start ) ).to.be.true;
  464. expect( selection.focus.isEqual( ranges[ 2 ].end ) ).to.be.true;
  465. } );
  466. it( 'should set last range backward if needed', () => {
  467. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ], lastRangeBackward: true } );
  468. expect( selection.rangeCount ).to.equal( 3 );
  469. const ranges = [ ...selection.getRanges() ];
  470. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  471. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  472. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  473. expect( selection.anchor.isEqual( ranges[ 2 ].end ) ).to.be.true;
  474. expect( selection.focus.isEqual( ranges[ 2 ].start ) ).to.be.true;
  475. } );
  476. it( 'should throw when ranges order does not include all ranges', () => {
  477. expect( () => {
  478. parse( '{}foobar{}', { order: [ 1 ] } );
  479. } ).to.throw( Error );
  480. } );
  481. it( 'should throw when ranges order is invalid', () => {
  482. expect( () => {
  483. parse( '{}foobar{}', { order: [ 1, 4 ] } );
  484. } ).to.throw( Error );
  485. } );
  486. it( 'should throw when element range delimiter is inside text node', () => {
  487. expect( () => {
  488. parse( 'foo[bar' );
  489. } ).to.throw( Error );
  490. } );
  491. it( 'should throw when text range delimiter is inside empty text node', () => {
  492. expect( () => {
  493. parse( '<b>foo</b>}' );
  494. } ).to.throw( Error );
  495. } );
  496. it( 'should throw when end of range is found before start', () => {
  497. expect( () => {
  498. parse( 'fo}obar' );
  499. } ).to.throw( Error );
  500. } );
  501. it( 'should throw when intersecting ranges found', () => {
  502. expect( () => {
  503. parse( '[fo{o}bar]' );
  504. } ).to.throw( Error );
  505. } );
  506. it( 'should throw when opened ranges are left', () => {
  507. expect( () => {
  508. parse( 'fo{obar' );
  509. } ).to.throw( Error );
  510. } );
  511. it( 'should throw when wrong type is provided', () => {
  512. sinon.stub( XmlDataProcessor.prototype, 'toView', () => {
  513. return new ContainerElement( 'invalidType:b' );
  514. } );
  515. expect( () => {
  516. parse( 'sth' );
  517. } ).to.throw( Error, `Parse error - cannot parse element's name: invalidType:b.` );
  518. XmlDataProcessor.prototype.toView.restore();
  519. } );
  520. it( 'should use provided root element #1', () => {
  521. const root = new Element( 'p' );
  522. const data = parse( '<span>text</span>', { rootElement: root } );
  523. expect( stringify( data ) ).to.equal( '<p><span>text</span></p>' );
  524. } );
  525. it( 'should use provided root element #2', () => {
  526. const root = new Element( 'p' );
  527. const data = parse( '<span>text</span><b>test</b>', { rootElement: root } );
  528. expect( stringify( data ) ).to.equal( '<p><span>text</span><b>test</b></p>' );
  529. } );
  530. it( 'should parse EmptyElement', () => {
  531. const parsed = parse( '<empty:img></empty:img>' );
  532. expect( parsed ).to.be.instanceof( EmptyElement );
  533. } );
  534. it( 'should throw an error if EmptyElement is not empty', () => {
  535. expect( () => {
  536. parse( '<empty:img>foo bar</empty:img>' );
  537. } ).to.throw( Error, 'Parse error - cannot parse inside EmptyElement.' );
  538. } );
  539. } );
  540. } );