8
0

view.js 27 KB

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