view.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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._appendChildren( 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._appendChildren( 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 parse DocumentFragment as root', () => {
  217. const text1 = new Text( 'foobar' );
  218. const text2 = new Text( 'bazqux' );
  219. const b1 = new Element( 'b', null, text1 );
  220. const b2 = new Element( 'b', null, text2 );
  221. const fragment = new DocumentFragment( [ b1, b2 ] );
  222. expect( stringify( fragment, null ) ).to.equal( '<b>foobar</b><b>bazqux</b>' );
  223. } );
  224. it( 'should not write ranges outside elements - end position outside element', () => {
  225. const text = new Text( 'foobar' );
  226. const b = new Element( 'b', null, text );
  227. const p = new Element( 'p', null, b );
  228. const range = Range.createFromParentsAndOffsets( p, 0, p, 5 );
  229. expect( stringify( p, range ) ).to.equal( '<p>[<b>foobar</b></p>' );
  230. } );
  231. it( 'should not write ranges outside elements - start position outside element', () => {
  232. const text = new Text( 'foobar' );
  233. const b = new Element( 'b', null, text );
  234. const p = new Element( 'p', null, b );
  235. const range = Range.createFromParentsAndOffsets( p, -1, p, 1 );
  236. expect( stringify( p, range ) ).to.equal( '<p><b>foobar</b>]</p>' );
  237. } );
  238. it( 'should not write ranges outside elements - end position outside text', () => {
  239. const text = new Text( 'foobar' );
  240. const b = new Element( 'b', null, text );
  241. const p = new Element( 'p', null, b );
  242. const range = Range.createFromParentsAndOffsets( text, 0, text, 7 );
  243. expect( stringify( p, range ) ).to.equal( '<p><b>{foobar</b></p>' );
  244. } );
  245. it( 'should not write ranges outside elements - start position outside text', () => {
  246. const text = new Text( 'foobar' );
  247. const b = new Element( 'b', null, text );
  248. const p = new Element( 'p', null, b );
  249. const range = Range.createFromParentsAndOffsets( text, -1, text, 2 );
  250. expect( stringify( p, range ) ).to.equal( '<p><b>fo}obar</b></p>' );
  251. } );
  252. it( 'should write multiple ranges from selection #1', () => {
  253. const text1 = new Text( 'foobar' );
  254. const text2 = new Text( 'bazqux' );
  255. const b1 = new Element( 'b', null, text1 );
  256. const b2 = new Element( 'b', null, text2 );
  257. const p = new Element( 'p', null, [ b1, b2 ] );
  258. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  259. const range2 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  260. const selection = new DocumentSelection( [ range2, range1 ] );
  261. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]<b>bazqux</b></p>' );
  262. } );
  263. it( 'should write multiple ranges from selection #2', () => {
  264. const text1 = new Text( 'foobar' );
  265. const text2 = new Text( 'bazqux' );
  266. const b = new Element( 'b', null, text1 );
  267. const p = new Element( 'p', null, [ b, text2 ] );
  268. const range1 = Range.createFromParentsAndOffsets( p, 0, p, 1 );
  269. const range2 = Range.createFromParentsAndOffsets( text2, 0, text2, 3 );
  270. const range3 = Range.createFromParentsAndOffsets( text2, 3, text2, 4 );
  271. const range4 = Range.createFromParentsAndOffsets( p, 1, p, 1 );
  272. const selection = new DocumentSelection( [ range1, range2, range3, range4 ] );
  273. expect( stringify( p, selection ) ).to.equal( '<p>[<b>foobar</b>][]{baz}{q}ux</p>' );
  274. } );
  275. it( 'should use Position instance instead of Selection', () => {
  276. const text = new Text( 'foobar' );
  277. const position = new Position( text, 3 );
  278. const string = stringify( text, position );
  279. expect( string ).to.equal( 'foo{}bar' );
  280. } );
  281. it( 'should use Range instance instead of Selection', () => {
  282. const text = new Text( 'foobar' );
  283. const range = Range.createFromParentsAndOffsets( text, 3, text, 4 );
  284. const string = stringify( text, range );
  285. expect( string ).to.equal( 'foo{b}ar' );
  286. } );
  287. it( 'should stringify EmptyElement', () => {
  288. const img = new EmptyElement( 'img' );
  289. const p = new ContainerElement( 'p', null, img );
  290. expect( stringify( p, null, { showType: true } ) )
  291. .to.equal( '<container:p><empty:img></empty:img></container:p>' );
  292. } );
  293. it( 'should stringify UIElement', () => {
  294. const span = new UIElement( 'span' );
  295. const p = new ContainerElement( 'p', null, span );
  296. expect( stringify( p, null, { showType: true } ) )
  297. .to.equal( '<container:p><ui:span></ui:span></container:p>' );
  298. } );
  299. it( 'should sort classes in specified element', () => {
  300. const text = new Text( 'foobar' );
  301. const b = new Element( 'b', {
  302. class: 'zz xx aa'
  303. }, text );
  304. expect( stringify( b ) ).to.equal( '<b class="aa xx zz">foobar</b>' );
  305. } );
  306. it( 'should sort styles in specified element', () => {
  307. const text = new Text( 'foobar' );
  308. const i = new Element( 'i', {
  309. style: 'text-decoration: underline; font-weight: bold'
  310. }, text );
  311. expect( stringify( i ) ).to.equal( '<i style="font-weight:bold;text-decoration:underline">foobar</i>' );
  312. } );
  313. } );
  314. describe( 'parse', () => {
  315. it( 'should return empty DocumentFragment for empty string', () => {
  316. const fragment = parse( '' );
  317. expect( fragment ).to.be.instanceOf( DocumentFragment );
  318. expect( fragment.childCount ).to.equal( 0 );
  319. } );
  320. it( 'should return empty DocumentFragment and Selection for string containing range only', () => {
  321. const { view, selection } = parse( '[]' );
  322. expect( view ).to.be.instanceOf( DocumentFragment );
  323. expect( selection.rangeCount ).to.equal( 1 );
  324. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( view, 0, view, 0 ) ) ).to.be.true;
  325. } );
  326. it( 'should return Element if range is around single element', () => {
  327. const { view, selection } = parse( '[<b>foobar</b>]' );
  328. const parent = view.parent;
  329. expect( view ).to.be.instanceOf( Element );
  330. expect( parent ).to.be.instanceOf( DocumentFragment );
  331. expect( selection.rangeCount ).to.equal( 1 );
  332. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( parent, 0, parent, 1 ) ) ).to.be.true;
  333. } );
  334. it( 'should create DocumentFragment when multiple elements on root', () => {
  335. const view = parse( '<b></b><i></i>' );
  336. expect( view ).to.be.instanceOf( DocumentFragment );
  337. expect( view.childCount ).to.equal( 2 );
  338. expect( view.getChild( 0 ).isSimilar( new Element( 'b' ) ) ).to.be.true;
  339. expect( view.getChild( 1 ).isSimilar( new Element( 'i' ) ) ).to.be.true;
  340. } );
  341. it( 'should parse text', () => {
  342. const text = parse( 'foobar' );
  343. expect( text ).to.be.instanceOf( Text );
  344. expect( text.data ).to.equal( 'foobar' );
  345. } );
  346. it( 'should parse text with spaces', () => {
  347. const text = parse( 'foo bar' );
  348. expect( text ).to.be.instanceOf( Text );
  349. expect( text.data ).to.equal( 'foo bar' );
  350. } );
  351. it( 'should parse elements and texts', () => {
  352. const view = parse( '<b>foobar</b>' );
  353. const element = new Element( 'b' );
  354. expect( view ).to.be.instanceof( Element );
  355. expect( view.isSimilar( element ) ).to.be.true;
  356. expect( view.childCount ).to.equal( 1 );
  357. const text = view.getChild( 0 );
  358. expect( text ).to.be.instanceof( Text );
  359. expect( text.data ).to.equal( 'foobar' );
  360. } );
  361. it( 'should parse element attributes', () => {
  362. const view = parse( '<b name="foo" title="bar" class="foo bar" style="color:red;"></b>' );
  363. const element = new Element( 'b', { name: 'foo', title: 'bar', class: 'foo bar', style: 'color:red;' } );
  364. expect( view ).to.be.instanceof( Element );
  365. expect( view.isSimilar( element ) ).to.be.true;
  366. expect( view.childCount ).to.equal( 0 );
  367. } );
  368. it( 'should parse element type', () => {
  369. const view1 = parse( '<attribute:b></attribute:b>' );
  370. const attribute = new AttributeElement( 'b' );
  371. const view2 = parse( '<container:p></container:p>' );
  372. const container = new ContainerElement( 'p' );
  373. expect( view1 ).to.be.instanceof( AttributeElement );
  374. expect( view1.isSimilar( attribute ) ).to.be.true;
  375. expect( view2 ).to.be.instanceof( ContainerElement );
  376. expect( view2.isSimilar( container ) ).to.be.true;
  377. } );
  378. it( 'should parse element priority', () => {
  379. const parsed1 = parse( '<b view-priority="12"></b>' );
  380. const attribute1 = new AttributeElement( 'b' );
  381. attribute1._priority = 12;
  382. const parsed2 = parse( '<attribute:b view-priority="44"></attribute:b>' );
  383. const attribute2 = new AttributeElement( 'b' );
  384. attribute2._priority = 44;
  385. parsed1.isSimilar( attribute1 );
  386. expect( parsed1.isSimilar( attribute1 ) ).to.be.true;
  387. expect( parsed2.isSimilar( attribute2 ) ).to.be.true;
  388. } );
  389. it( 'should paste nested elements and texts', () => {
  390. const parsed = parse( '<container:p>foo<b view-priority="12">bar<i view-priority="25">qux</i></b></container:p>' );
  391. expect( parsed.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  392. expect( parsed.childCount ).to.equal( 2 );
  393. expect( parsed.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'foo' );
  394. const b = parsed.getChild( 1 );
  395. expect( b ).to.be.instanceof( AttributeElement );
  396. expect( b.priority ).to.equal( 12 );
  397. expect( b.childCount ).to.equal( 2 );
  398. expect( b.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'bar' );
  399. const i = b.getChild( 1 );
  400. expect( i ).to.be.instanceof( AttributeElement );
  401. expect( i.priority ).to.equal( 25 );
  402. expect( i.getChild( 0 ) ).to.be.instanceof( Text ).and.have.property( 'data' ).that.equal( 'qux' );
  403. } );
  404. it( 'should parse selection range inside text', () => {
  405. const { view, selection } = parse( 'f{oo}b{}ar' );
  406. expect( view ).to.be.instanceof( Text );
  407. expect( view.data ).to.equal( 'foobar' );
  408. expect( selection.rangeCount ).to.equal( 2 );
  409. const ranges = [ ...selection.getRanges() ];
  410. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 3 ) ) ).to.be.true;
  411. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 4, view, 4 ) ) ).to.be.true;
  412. } );
  413. it( 'should parse selection range between elements', () => {
  414. const { view, selection } = parse( '<p>[<b>foobar]</b>[]</p>' );
  415. expect( view ).to.be.instanceof( Element );
  416. expect( view.childCount ).to.equal( 1 );
  417. const b = view.getChild( 0 );
  418. expect( b ).to.be.instanceof( Element );
  419. expect( b.name ).to.equal( 'b' );
  420. expect( b.childCount ).to.equal( 1 );
  421. const text = b.getChild( 0 );
  422. expect( text ).to.be.instanceof( Text );
  423. expect( text.data ).to.equal( 'foobar' );
  424. expect( selection.rangeCount ).to.equal( 2 );
  425. const ranges = [ ...selection.getRanges() ];
  426. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, b, 1 ) ) ).to.be.true;
  427. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 1, view, 1 ) ) ).to.be.true;
  428. } );
  429. it( 'should support unicode', () => {
  430. const { view, selection } = parse( '<p>[<b>நிலை}க்கு</b></p>' );
  431. expect( view ).to.be.instanceof( Element );
  432. expect( view.name ).to.equal( 'p' );
  433. expect( view.childCount ).to.equal( 1 );
  434. const b = view.getChild( 0 );
  435. expect( b.name ).to.equal( 'b' );
  436. expect( b.childCount ).to.equal( 1 );
  437. const text = b.getChild( 0 );
  438. expect( text.data ).to.equal( 'நிலைக்கு' );
  439. expect( selection.rangeCount ).to.equal( 1 );
  440. const range = selection.getFirstRange();
  441. expect( range.start.parent ).to.equal( view );
  442. expect( range.start.offset ).to.equal( 0 );
  443. expect( range.end.parent ).to.equal( text );
  444. expect( range.end.offset ).to.equal( 4 );
  445. } );
  446. it( 'should parse ranges #1', () => {
  447. const { view, selection } = parse( '<container:p>foo{bar]</container:p>' );
  448. expect( view.isSimilar( new ContainerElement( 'p' ) ) ).to.be.true;
  449. expect( view.childCount ).to.equal( 1 );
  450. const text = view.getChild( 0 );
  451. expect( text ).to.be.instanceof( Text );
  452. expect( text.data ).to.equal( 'foobar' );
  453. expect( selection.rangeCount ).to.equal( 1 );
  454. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( text, 3, view, 1 ) ) ).to.be.true;
  455. } );
  456. it( 'should parse ranges #2', () => {
  457. const { view, selection } = parse( '<attribute:b>[foob}ar<i>{baz</i>]</attribute:b>' );
  458. expect( view.isSimilar( new AttributeElement( 'b' ) ) ).to.be.true;
  459. expect( view.childCount ).to.equal( 2 );
  460. const text1 = view.getChild( 0 );
  461. expect( text1 ).to.be.instanceof( Text );
  462. expect( text1.data ).to.equal( 'foobar' );
  463. const i = view.getChild( 1 );
  464. expect( i.isSimilar( new Element( 'i' ) ) ).to.be.true;
  465. expect( i.childCount ).to.equal( 1 );
  466. const text2 = i.getChild( 0 );
  467. expect( text2 ).to.be.instanceof( Text );
  468. expect( text2.data ).to.equal( 'baz' );
  469. expect( selection.rangeCount ).to.equal( 2 );
  470. const ranges = [ ...selection.getRanges() ];
  471. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 0, text1, 4 ) ) ).to.be.true;
  472. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( text2, 0, view, 2 ) ) ).to.be.true;
  473. } );
  474. it( 'should use ranges order when provided', () => {
  475. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ] } );
  476. expect( selection.rangeCount ).to.equal( 3 );
  477. const ranges = [ ...selection.getRanges() ];
  478. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  479. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  480. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  481. expect( selection.anchor.isEqual( ranges[ 2 ].start ) ).to.be.true;
  482. expect( selection.focus.isEqual( ranges[ 2 ].end ) ).to.be.true;
  483. } );
  484. it( 'should set last range backward if needed', () => {
  485. const { view, selection } = parse( '{f}oo{b}arb{a}z', { order: [ 3, 1, 2 ], lastRangeBackward: true } );
  486. expect( selection.rangeCount ).to.equal( 3 );
  487. const ranges = [ ...selection.getRanges() ];
  488. expect( ranges[ 0 ].isEqual( Range.createFromParentsAndOffsets( view, 3, view, 4 ) ) ).to.be.true;
  489. expect( ranges[ 1 ].isEqual( Range.createFromParentsAndOffsets( view, 7, view, 8 ) ) ).to.be.true;
  490. expect( ranges[ 2 ].isEqual( Range.createFromParentsAndOffsets( view, 0, view, 1 ) ) ).to.be.true;
  491. expect( selection.anchor.isEqual( ranges[ 2 ].end ) ).to.be.true;
  492. expect( selection.focus.isEqual( ranges[ 2 ].start ) ).to.be.true;
  493. } );
  494. it( 'should throw when ranges order does not include all ranges', () => {
  495. expect( () => {
  496. parse( '{}foobar{}', { order: [ 1 ] } );
  497. } ).to.throw( Error );
  498. } );
  499. it( 'should throw when ranges order is invalid', () => {
  500. expect( () => {
  501. parse( '{}foobar{}', { order: [ 1, 4 ] } );
  502. } ).to.throw( Error );
  503. } );
  504. it( 'should throw when element range delimiter is inside text node', () => {
  505. expect( () => {
  506. parse( 'foo[bar' );
  507. } ).to.throw( Error );
  508. } );
  509. it( 'should throw when text range delimiter is inside empty text node', () => {
  510. expect( () => {
  511. parse( '<b>foo</b>}' );
  512. } ).to.throw( Error );
  513. } );
  514. it( 'should throw when end of range is found before start', () => {
  515. expect( () => {
  516. parse( 'fo}obar' );
  517. } ).to.throw( Error );
  518. } );
  519. it( 'should throw when intersecting ranges found', () => {
  520. expect( () => {
  521. parse( '[fo{o}bar]' );
  522. } ).to.throw( Error );
  523. } );
  524. it( 'should throw when opened ranges are left', () => {
  525. expect( () => {
  526. parse( 'fo{obar' );
  527. } ).to.throw( Error );
  528. } );
  529. it( 'should throw when wrong type is provided', () => {
  530. sinon.stub( XmlDataProcessor.prototype, 'toView' ).returns( new ContainerElement( 'invalidType:b' ) );
  531. expect( () => {
  532. parse( 'sth' );
  533. } ).to.throw( Error, 'Parse error - cannot parse element\'s name: invalidType:b.' );
  534. XmlDataProcessor.prototype.toView.restore();
  535. } );
  536. it( 'should use provided root element #1', () => {
  537. const root = new Element( 'p' );
  538. const data = parse( '<span>text</span>', { rootElement: root } );
  539. expect( stringify( data ) ).to.equal( '<p><span>text</span></p>' );
  540. } );
  541. it( 'should use provided root element #2', () => {
  542. const root = new Element( 'p' );
  543. const data = parse( '<span>text</span><b>test</b>', { rootElement: root } );
  544. expect( stringify( data ) ).to.equal( '<p><span>text</span><b>test</b></p>' );
  545. } );
  546. it( 'should parse EmptyElement', () => {
  547. const parsed = parse( '<empty:img></empty:img>' );
  548. expect( parsed ).to.be.instanceof( EmptyElement );
  549. } );
  550. it( 'should parse UIElement', () => {
  551. const parsed = parse( '<ui:span></ui:span>' );
  552. expect( parsed ).to.be.instanceof( UIElement );
  553. } );
  554. it( 'should throw an error if EmptyElement is not empty', () => {
  555. expect( () => {
  556. parse( '<empty:img>foo bar</empty:img>' );
  557. } ).to.throw( Error, 'Parse error - cannot parse inside EmptyElement.' );
  558. } );
  559. it( 'should throw an error if UIElement is not empty', () => {
  560. expect( () => {
  561. parse( '<ui:span>foo bar</ui:span>' );
  562. } ).to.throw( Error, 'Parse error - cannot parse inside UIElement.' );
  563. } );
  564. } );
  565. } );
  566. function createAttachedRoot( viewDocument, element ) {
  567. const root = createViewRoot( viewDocument );
  568. // Set name of view root the same as dom root.
  569. // This is a mock of attaching view root to dom root.
  570. root._name = element.tagName.toLowerCase();
  571. return root;
  572. }