view.js 30 KB

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