view.js 28 KB

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