view.js 27 KB

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