view.js 30 KB

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