view.js 24 KB

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