view.js 30 KB

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