view.js 25 KB

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