view.js 27 KB

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