8
0

view.js 32 KB

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