view.js 28 KB

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