8
0

view.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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 stringify UIElement (custom render function)', () => {
  308. const span = new UIElement( 'span' );
  309. span.render = function( domDocument ) {
  310. const domElement = this.toDomElement( domDocument );
  311. domElement.innerHTML = '<b>foo</b>';
  312. return domElement;
  313. };
  314. const p = new ContainerElement( 'p', null, span );
  315. expect( stringify( p, null, { showType: true } ) )
  316. .to.equal( '<container:p><ui:span><b>foo</b></ui:span></container:p>' );
  317. } );
  318. it( 'should sort classes in specified element', () => {
  319. const text = new Text( 'foobar' );
  320. const b = new Element( 'b', {
  321. class: 'zz xx aa'
  322. }, text );
  323. expect( stringify( b ) ).to.equal( '<b class="aa xx zz">foobar</b>' );
  324. } );
  325. it( 'should sort styles in specified element', () => {
  326. const text = new Text( 'foobar' );
  327. const i = new Element( 'i', {
  328. style: 'text-decoration: underline; font-weight: bold'
  329. }, text );
  330. expect( stringify( i ) ).to.equal( '<i style="font-weight:bold;text-decoration:underline">foobar</i>' );
  331. } );
  332. } );
  333. describe( 'parse', () => {
  334. it( 'should return empty DocumentFragment for empty string', () => {
  335. const fragment = parse( '' );
  336. expect( fragment ).to.be.instanceOf( DocumentFragment );
  337. expect( fragment.childCount ).to.equal( 0 );
  338. } );
  339. it( 'should return empty DocumentFragment and Selection for string containing range only', () => {
  340. const { view, selection } = parse( '[]' );
  341. expect( view ).to.be.instanceOf( DocumentFragment );
  342. expect( selection.rangeCount ).to.equal( 1 );
  343. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( view, 0, view, 0 ) ) ).to.be.true;
  344. } );
  345. it( 'should return Element if range is around single element', () => {
  346. const { view, selection } = parse( '[<b>foobar</b>]' );
  347. const parent = view.parent;
  348. expect( view ).to.be.instanceOf( Element );
  349. expect( parent ).to.be.instanceOf( DocumentFragment );
  350. expect( selection.rangeCount ).to.equal( 1 );
  351. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( parent, 0, parent, 1 ) ) ).to.be.true;
  352. } );
  353. it( 'should create DocumentFragment when multiple elements on root', () => {
  354. const view = parse( '<b></b><i></i>' );
  355. expect( view ).to.be.instanceOf( DocumentFragment );
  356. expect( view.childCount ).to.equal( 2 );
  357. expect( view.getChild( 0 ).isSimilar( new Element( 'b' ) ) ).to.be.true;
  358. expect( view.getChild( 1 ).isSimilar( new Element( 'i' ) ) ).to.be.true;
  359. } );
  360. it( 'should parse text', () => {
  361. const text = parse( 'foobar' );
  362. expect( text ).to.be.instanceOf( Text );
  363. expect( text.data ).to.equal( 'foobar' );
  364. } );
  365. it( 'should parse text with spaces', () => {
  366. const text = parse( 'foo bar' );
  367. expect( text ).to.be.instanceOf( Text );
  368. expect( text.data ).to.equal( 'foo bar' );
  369. } );
  370. it( 'should parse elements and texts', () => {
  371. const view = parse( '<b>foobar</b>' );
  372. const element = new Element( 'b' );
  373. expect( view ).to.be.instanceof( Element );
  374. expect( view.isSimilar( element ) ).to.be.true;
  375. expect( view.childCount ).to.equal( 1 );
  376. const text = view.getChild( 0 );
  377. expect( text ).to.be.instanceof( Text );
  378. expect( text.data ).to.equal( 'foobar' );
  379. } );
  380. it( 'should parse element attributes', () => {
  381. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  382. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  383. expect( view ).to.be.instanceof( Element );
  384. expect( view.isSimilar( element ) ).to.be.true;
  385. expect( view.childCount ).to.equal( 0 );
  386. } );
  387. it( 'should parse element type', () => {
  388. const view1 = parse( '<attribute:b></attribute:b>' );
  389. const attribute = new AttributeElement( 'b' );
  390. const view2 = parse( '<container:p></container:p>' );
  391. const container = new ContainerElement( 'p' );
  392. expect( view1 ).to.be.instanceof( AttributeElement );
  393. expect( view1.isSimilar( attribute ) ).to.be.true;
  394. expect( view2 ).to.be.instanceof( ContainerElement );
  395. expect( view2.isSimilar( container ) ).to.be.true;
  396. } );
  397. it( 'should parse element priority', () => {
  398. const parsed1 = parse( '<b view-priority="12"></b>' );
  399. const attribute1 = new AttributeElement( 'b' );
  400. attribute1._priority = 12;
  401. const parsed2 = parse( '<attribute:b view-priority="44"></attribute:b>' );
  402. const attribute2 = new AttributeElement( 'b' );
  403. attribute2._priority = 44;
  404. parsed1.isSimilar( attribute1 );
  405. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  406. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  407. } );
  408. it( 'should parse attribute element id', () => {
  409. const parsed1 = parse( '<attribute:span view-id="foo"></attribute:span>' );
  410. expect( parsed1.id ).to.equal( 'foo' );
  411. const parsed2 = parse( '<container:div view-id="bar"></container:div>' );
  412. expect( parsed2.id ).to.be.undefined;
  413. } );
  414. it( 'should paste nested elements and texts', () => {
  415. const parsed = parse( '<container:p>foo<b view-priority="12">bar<i view-priority="25">qux</i></b></container:p>' );
  416. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  417. expect( parsed.childCount ).to.equal( 2 );
  418. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  419. const b = parsed.getChild( 1 );
  420. expect( b ).to.be.instanceof( AttributeElement );
  421. expect( b.priority ).to.equal( 12 );
  422. expect( b.childCount ).to.equal( 2 );
  423. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  424. const i = b.getChild( 1 );
  425. expect( i ).to.be.instanceof( AttributeElement );
  426. expect( i.priority ).to.equal( 25 );
  427. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  428. } );
  429. it( 'should parse selection range inside text', () => {
  430. const { view, selection } = parse( 'f{oo}b{}ar' );
  431. expect( view ).to.be.instanceof( Text );
  432. expect( view.data ).to.equal( 'foobar' );
  433. expect( selection.rangeCount ).to.equal( 2 );
  434. const ranges = [ ...selection.getRanges() ];
  435. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  436. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  437. } );
  438. it( 'should parse selection range between elements', () => {
  439. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  440. expect( view ).to.be.instanceof( Element );
  441. expect( view.childCount ).to.equal( 1 );
  442. const b = view.getChild( 0 );
  443. expect( b ).to.be.instanceof( Element );
  444. expect( b.name ).to.equal( 'b' );
  445. expect( b.childCount ).to.equal( 1 );
  446. const text = b.getChild( 0 );
  447. expect( text ).to.be.instanceof( Text );
  448. expect( text.data ).to.equal( 'foobar' );
  449. expect( selection.rangeCount ).to.equal( 2 );
  450. const ranges = [ ...selection.getRanges() ];
  451. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  452. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  453. } );
  454. it( 'should support unicode', () => {
  455. const { view, selection } = parse( '<p>[<b>நிலை}க்கு</b></p>' );
  456. expect( view ).to.be.instanceof( Element );
  457. expect( view.name ).to.equal( 'p' );
  458. expect( view.childCount ).to.equal( 1 );
  459. const b = view.getChild( 0 );
  460. expect( b.name ).to.equal( 'b' );
  461. expect( b.childCount ).to.equal( 1 );
  462. const text = b.getChild( 0 );
  463. expect( text.data ).to.equal( 'நிலைக்கு' );
  464. expect( selection.rangeCount ).to.equal( 1 );
  465. const range = selection.getFirstRange();
  466. expect( range.start.parent ).to.equal( view );
  467. expect( range.start.offset ).to.equal( 0 );
  468. expect( range.end.parent ).to.equal( text );
  469. expect( range.end.offset ).to.equal( 4 );
  470. } );
  471. it( 'should parse ranges #1', () => {
  472. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  473. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  474. expect( view.childCount ).to.equal( 1 );
  475. const text = view.getChild( 0 );
  476. expect( text ).to.be.instanceof( Text );
  477. expect( text.data ).to.equal( 'foobar' );
  478. expect( selection.rangeCount ).to.equal( 1 );
  479. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  480. } );
  481. it( 'should parse ranges #2', () => {
  482. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  483. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  484. expect( view.childCount ).to.equal( 2 );
  485. const text1 = view.getChild( 0 );
  486. expect( text1 ).to.be.instanceof( Text );
  487. expect( text1.data ).to.equal( 'foobar' );
  488. const i = view.getChild( 1 );
  489. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  490. expect( i.childCount ).to.equal( 1 );
  491. const text2 = i.getChild( 0 );
  492. expect( text2 ).to.be.instanceof( Text );
  493. expect( text2.data ).to.equal( 'baz' );
  494. expect( selection.rangeCount ).to.equal( 2 );
  495. const ranges = [ ...selection.getRanges() ];
  496. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  497. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  498. } );
  499. it( 'should use ranges order when provided', () => {
  500. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ] } );
  501. expect( selection.rangeCount ).to.equal( 3 );
  502. const ranges = [ ...selection.getRanges() ];
  503. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  504. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  505. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  506. expect( selection.anchor.isEqual( ranges[ 2 ].start ) ).to.be.true;
  507. expect( selection.focus.isEqual( ranges[ 2 ].end ) ).to.be.true;
  508. } );
  509. it( 'should set last range backward if needed', () => {
  510. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ], lastRangeBackward: true } );
  511. expect( selection.rangeCount ).to.equal( 3 );
  512. const ranges = [ ...selection.getRanges() ];
  513. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  514. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  515. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  516. expect( selection.anchor.isEqual( ranges[ 2 ].end ) ).to.be.true;
  517. expect( selection.focus.isEqual( ranges[ 2 ].start ) ).to.be.true;
  518. } );
  519. it( 'should throw when ranges order does not include all ranges', () => {
  520. expect( () => {
  521. parse( '{}foobar{}', { order: [ 1 ] } );
  522. } ).to.throw( Error );
  523. } );
  524. it( 'should throw when ranges order is invalid', () => {
  525. expect( () => {
  526. parse( '{}foobar{}', { order: [ 1, 4 ] } );
  527. } ).to.throw( Error );
  528. } );
  529. it( 'should throw when element range delimiter is inside text node', () => {
  530. expect( () => {
  531. parse( 'foo[bar' );
  532. } ).to.throw( Error );
  533. } );
  534. it( 'should throw when text range delimiter is inside empty text node', () => {
  535. expect( () => {
  536. parse( '<b>foo</b>}' );
  537. } ).to.throw( Error );
  538. } );
  539. it( 'should throw when end of range is found before start', () => {
  540. expect( () => {
  541. parse( 'fo}obar' );
  542. } ).to.throw( Error );
  543. } );
  544. it( 'should throw when intersecting ranges found', () => {
  545. expect( () => {
  546. parse( '[fo{o}bar]' );
  547. } ).to.throw( Error );
  548. } );
  549. it( 'should throw when opened ranges are left', () => {
  550. expect( () => {
  551. parse( 'fo{obar' );
  552. } ).to.throw( Error );
  553. } );
  554. it( 'should throw when wrong type is provided', () => {
  555. sinon.stub( XmlDataProcessor.prototype, 'toView' ).returns( new ContainerElement( 'invalidType:b' ) );
  556. expect( () => {
  557. parse( 'sth' );
  558. } ).to.throw( Error, 'Parse error - cannot parse element\'s name: invalidType:b.' );
  559. XmlDataProcessor.prototype.toView.restore();
  560. } );
  561. it( 'should use provided root element #1', () => {
  562. const root = new Element( 'p' );
  563. const data = parse( '<span>text</span>', { rootElement: root } );
  564. expect( stringify( data ) ).to.equal( '<p><span>text</span></p>' );
  565. } );
  566. it( 'should use provided root element #2', () => {
  567. const root = new Element( 'p' );
  568. const data = parse( '<span>text</span><b>test</b>', { rootElement: root } );
  569. expect( stringify( data ) ).to.equal( '<p><span>text</span><b>test</b></p>' );
  570. } );
  571. it( 'should parse EmptyElement', () => {
  572. const parsed = parse( '<empty:img></empty:img>' );
  573. expect( parsed ).to.be.instanceof( EmptyElement );
  574. } );
  575. it( 'should parse UIElement', () => {
  576. const parsed = parse( '<ui:span></ui:span>' );
  577. expect( parsed ).to.be.instanceof( UIElement );
  578. } );
  579. it( 'should throw an error if EmptyElement is not empty', () => {
  580. expect( () => {
  581. parse( '<empty:img>foo bar</empty:img>' );
  582. } ).to.throw( Error, 'Parse error - cannot parse inside EmptyElement.' );
  583. } );
  584. it( 'should throw an error if UIElement is not empty', () => {
  585. expect( () => {
  586. parse( '<ui:span>foo bar</ui:span>' );
  587. } ).to.throw( Error, 'Parse error - cannot parse inside UIElement.' );
  588. } );
  589. } );
  590. } );
  591. function createAttachedRoot( viewDocument, element ) {
  592. const root = createViewRoot( viewDocument );
  593. // Set name of view root the same as dom root.
  594. // This is a mock of attaching view root to dom root.
  595. root._name = element.tagName.toLowerCase();
  596. return root;
  597. }