model.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { stringify, parse, getData, setData } from '/tests/engine/_utils/model.js';
  6. import Document from '/ckeditor5/engine/model/document.js';
  7. import DocumentFragment from '/ckeditor5/engine/model/documentfragment.js';
  8. import Element from '/ckeditor5/engine/model/element.js';
  9. import Text from '/ckeditor5/engine/model/text.js';
  10. import Range from '/ckeditor5/engine/model/range.js';
  11. import Position from '/ckeditor5/engine/model/position.js';
  12. import count from '/ckeditor5/utils/count.js';
  13. describe( 'model test utils', () => {
  14. let document, root, selection, sandbox;
  15. beforeEach( () => {
  16. document = new Document();
  17. root = document.createRoot();
  18. selection = document.selection;
  19. sandbox = sinon.sandbox.create();
  20. selection.removeAllRanges();
  21. document.schema.registerItem( 'a', '$inline' );
  22. document.schema.allow( { name: 'a', inside: '$root' } );
  23. document.schema.registerItem( 'b', '$inline' );
  24. document.schema.allow( { name: 'b', inside: '$root' } );
  25. document.schema.registerItem( 'c', '$inline' );
  26. document.schema.allow( { name: 'c', inside: '$root' } );
  27. document.schema.registerItem( 'paragraph', '$block' );
  28. document.schema.allow( { name: '$text', inside: '$root' } );
  29. } );
  30. afterEach( () => {
  31. sandbox.restore();
  32. } );
  33. describe( 'getData', () => {
  34. it( 'should use stringify method', () => {
  35. const stringifySpy = sandbox.spy( getData, '_stringify' );
  36. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  37. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<b>btext</b>' );
  38. sinon.assert.calledOnce( stringifySpy );
  39. sinon.assert.calledWithExactly( stringifySpy, root );
  40. } );
  41. it( 'should use stringify method with selection', () => {
  42. const stringifySpy = sandbox.spy( getData, '_stringify' );
  43. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  44. document.selection.addRange( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  45. expect( getData( document ) ).to.equal( '[<b>btext</b>]' );
  46. sinon.assert.calledOnce( stringifySpy );
  47. sinon.assert.calledWithExactly( stringifySpy, root, document.selection );
  48. } );
  49. it( 'should throw an error when passing invalid document', () => {
  50. expect( () => {
  51. getData( { invalid: 'document' } );
  52. } ).to.throw( TypeError, 'Document needs to be an instance of engine.model.Document.' );
  53. } );
  54. } );
  55. describe( 'setData', () => {
  56. it( 'should use parse method', () => {
  57. const parseSpy = sandbox.spy( setData, '_parse' );
  58. const options = {};
  59. const data = '<b>btext</b>text';
  60. setData( document, data, options );
  61. expect( getData( document, { withoutSelection: true } ) ).to.equal( data );
  62. sinon.assert.calledOnce( parseSpy );
  63. const args = parseSpy.firstCall.args;
  64. expect( args[ 0 ] ).to.equal( data );
  65. } );
  66. it( 'should use parse method with selection', () => {
  67. const parseSpy = sandbox.spy( setData, '_parse' );
  68. const options = {};
  69. const data = '[<b>btext</b>]';
  70. setData( document, data, options );
  71. expect( getData( document ) ).to.equal( data );
  72. sinon.assert.calledOnce( parseSpy );
  73. const args = parseSpy.firstCall.args;
  74. expect( args[ 0 ] ).to.equal( data );
  75. } );
  76. it( 'should insert text', () => {
  77. test( 'this is test text', '[]this is test text' );
  78. } );
  79. it( 'should insert text with selection around', () => {
  80. test( '[this is test text]' );
  81. } );
  82. it( 'should insert text with selection inside #1', () => {
  83. test( 'this [is test] text' );
  84. } );
  85. it( 'should insert text with selection inside #2', () => {
  86. test( '[this is test] text' );
  87. } );
  88. it( 'should insert text with selection inside #2', () => {
  89. test( 'this is [test text]' );
  90. } );
  91. it( 'should insert unicode text with selection', () => {
  92. test( 'நி[லைக்]கு' );
  93. } );
  94. it( 'should insert element', () => {
  95. test( '<b>foo bar</b>', '[]<b>foo bar</b>' );
  96. } );
  97. it( 'should insert element with selection inside #1', () => {
  98. test( '<b>[foo ]bar</b>' );
  99. } );
  100. it( 'should insert element with selection inside #2', () => {
  101. test( '[<b>foo ]bar</b>' );
  102. } );
  103. it( 'should insert element with selection inside #3', () => {
  104. test( '<b>[foo bar</b>]' );
  105. } );
  106. it( 'should insert backward selection', () => {
  107. setData( document, '<b>[foo bar</b>]', { lastRangeBackward: true } );
  108. expect( getData( document ) ).to.equal( '<b>[foo bar</b>]' );
  109. expect( document.selection.isBackward ).to.true;
  110. } );
  111. it( 'should throw an error when passing invalid document', () => {
  112. expect( () => {
  113. setData( { invalid: 'document' } );
  114. } ).to.throw( TypeError, 'Document needs to be an instance of engine.model.Document.' );
  115. } );
  116. function test( data, expected ) {
  117. expected = expected || data;
  118. setData( document, data );
  119. expect( getData( document ) ).to.equal( expected );
  120. }
  121. } );
  122. describe( 'stringify', () => {
  123. it( 'should stringify text', () => {
  124. const text = new Text( 'text', { underline: true, bold: true } );
  125. expect( stringify( text ) ).to.equal( '<$text bold="true" underline="true">text</$text>' );
  126. } );
  127. it( 'should stringify element', () => {
  128. const element = new Element( 'a', null, [
  129. new Element( 'b', null, new Text( 'btext' ) ),
  130. new Text( 'atext' )
  131. ] );
  132. expect( stringify( element ) ).to.equal( '<a><b>btext</b>atext</a>' );
  133. } );
  134. it( 'should stringify document fragment', () => {
  135. const fragment = new DocumentFragment( [
  136. new Element( 'b', null, new Text( 'btext' ) ),
  137. new Text( 'atext' )
  138. ] );
  139. expect( stringify( fragment ) ).to.equal( '<b>btext</b>atext' );
  140. } );
  141. it( 'writes elements and texts', () => {
  142. root.appendChildren( [
  143. new Element( 'a', null, new Text( 'atext' ) ),
  144. new Element( 'b', null, [
  145. new Element( 'c1' ),
  146. new Text( 'ctext' ),
  147. new Element( 'c2' )
  148. ] ),
  149. new Element( 'd' )
  150. ] );
  151. expect( stringify( root ) ).to.equal(
  152. '<a>atext</a><b><c1></c1>ctext<c2></c2></b><d></d>'
  153. );
  154. } );
  155. it( 'writes element attributes', () => {
  156. root.appendChildren(
  157. new Element( 'a', { foo: true, bar: 1, car: false }, [
  158. new Element( 'b', { fooBar: 'x y', barFoo: { x: 1, y: 2 } } )
  159. ] )
  160. );
  161. // Note: attributes are written in a very simplistic way, because they are not to be parsed. They are just
  162. // to be compared in the tests with some patterns.
  163. expect( stringify( root ) ).to.equal(
  164. '<a bar="1" car="false" foo="true"><b barFoo="{"x":1,"y":2}" fooBar="x y"></b></a>'
  165. );
  166. } );
  167. it( 'writes text attributes', () => {
  168. root.appendChildren( [
  169. new Text( 'foo', { bold: true } ),
  170. new Text( 'bar' ),
  171. new Text( 'bom', { bold: true, italic: true } ),
  172. new Element( 'a', null, [
  173. new Text( 'pom', { underline: true, bold: true } )
  174. ] )
  175. ] );
  176. expect( stringify( root ) ).to.equal(
  177. // Because of https://github.com/ckeditor/ckeditor5-engine/issues/562 attributes are not merged
  178. '<$text bold="true">foo</$text>bar<$text italic="true"><$text bold="true">bom</$text></$text>' +
  179. '<a><$text bold="true" underline="true">pom</$text></a>'
  180. );
  181. } );
  182. it( 'writes unicode text', () => {
  183. root.appendChildren( new Text( 'நிலைக்கு' ) );
  184. expect( stringify( root ) ).to.equal( 'நிலைக்கு' );
  185. } );
  186. describe( 'selection', () => {
  187. let elA, elB;
  188. beforeEach( () => {
  189. elA = new Element( 'a' );
  190. elB = new Element( 'b' );
  191. root.appendChildren( [
  192. elA,
  193. new Text( 'foo' ),
  194. new Text( 'bar', { bold: true } ),
  195. elB
  196. ] );
  197. } );
  198. it( 'writes selection in an empty root', () => {
  199. const root = document.createRoot( '$root', 'empty' );
  200. selection.collapse( root );
  201. expect( stringify( root, selection ) ).to.equal(
  202. '[]'
  203. );
  204. } );
  205. it( 'writes selection collapsed in an element', () => {
  206. selection.collapse( root );
  207. expect( stringify( root, selection ) ).to.equal(
  208. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  209. );
  210. } );
  211. it( 'writes selection collapsed in a text', () => {
  212. selection.collapse( root, 3 );
  213. expect( stringify( root, selection ) ).to.equal(
  214. '<a></a>fo[]o<$text bold="true">bar</$text><b></b>'
  215. );
  216. } );
  217. it( 'writes selection collapsed at the text left boundary', () => {
  218. selection.collapse( elA, 'after' );
  219. expect( stringify( root, selection ) ).to.equal(
  220. '<a></a>[]foo<$text bold="true">bar</$text><b></b>'
  221. );
  222. } );
  223. it( 'writes selection collapsed at the text right boundary', () => {
  224. selection.collapse( elB, 'before' );
  225. expect( stringify( root, selection ) ).to.equal(
  226. '<a></a>foo<$text bold="true">bar[]</$text><b></b>'
  227. );
  228. } );
  229. it( 'writes selection collapsed at the end of the root', () => {
  230. selection.collapse( root, 'end' );
  231. // Needed due to https://github.com/ckeditor/ckeditor5-engine/issues/320.
  232. selection.clearAttributes();
  233. expect( stringify( root, selection ) ).to.equal(
  234. '<a></a>foo<$text bold="true">bar</$text><b></b>[]'
  235. );
  236. } );
  237. it( 'writes selection collapsed selection in a text with attributes', () => {
  238. selection.collapse( root, 5 );
  239. expect( stringify( root, selection ) ).to.equal(
  240. '<a></a>foo<$text bold="true">b[]ar</$text><b></b>'
  241. );
  242. } );
  243. it( 'writes flat selection containing couple of nodes', () => {
  244. selection.addRange(
  245. Range.createFromParentsAndOffsets( root, 0, root, 4 )
  246. );
  247. expect( stringify( root, selection ) ).to.equal(
  248. '[<a></a>foo]<$text bold="true">bar</$text><b></b>'
  249. );
  250. } );
  251. it( 'writes flat selection within text', () => {
  252. selection.addRange(
  253. Range.createFromParentsAndOffsets( root, 2, root, 3 )
  254. );
  255. expect( stringify( root, selection ) ).to.equal(
  256. '<a></a>f[o]o<$text bold="true">bar</$text><b></b>'
  257. );
  258. } );
  259. it( 'writes multi-level selection', () => {
  260. selection.addRange(
  261. Range.createFromParentsAndOffsets( elA, 0, elB, 0 )
  262. );
  263. expect( stringify( root, selection ) ).to.equal(
  264. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  265. );
  266. } );
  267. it( 'writes selection when is backward', () => {
  268. selection.addRange(
  269. Range.createFromParentsAndOffsets( elA, 0, elB, 0 ),
  270. true
  271. );
  272. expect( stringify( root, selection ) ).to.equal(
  273. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  274. );
  275. } );
  276. it( 'writes selection in unicode text', () => {
  277. const root = document.createRoot( '$root', 'empty' );
  278. root.appendChildren( new Text( 'நிலைக்கு' ) );
  279. selection.addRange( Range.createFromParentsAndOffsets( root, 2, root, 6 ) );
  280. expect( stringify( root, selection ) ).to.equal( 'நி[லைக்]கு' );
  281. } );
  282. it( 'uses range and coverts it to selection', () => {
  283. const range = Range.createFromParentsAndOffsets( elA, 0, elB, 0 );
  284. expect( stringify( root, range ) ).to.equal(
  285. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  286. );
  287. } );
  288. it( 'uses position and converts it to collapsed selection', () => {
  289. const position = new Position( root, [ 0 ] );
  290. expect( stringify( root, position ) ).to.equal(
  291. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  292. );
  293. } );
  294. } );
  295. } );
  296. describe( 'parse', () => {
  297. test( 'creates empty DocumentFragment from empty string', {
  298. data: '',
  299. check( fragment ) {
  300. expect( fragment ).to.be.instanceOf( DocumentFragment );
  301. }
  302. } );
  303. test( 'creates empty DocumentFragment with selection', {
  304. data: '[]',
  305. check( fragment, selection ) {
  306. expect( fragment ).to.be.instanceOf( DocumentFragment );
  307. expect( fragment.childCount ).to.equal( 0 );
  308. expect( selection.rangeCount ).to.equal( 1 );
  309. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( fragment, 0, fragment, 0 ) ) ).to.be.true;
  310. }
  311. } );
  312. test( 'returns Element if range is around single element', {
  313. data: '[<a></a>]',
  314. check( el, selection ) {
  315. const fragment = el.parent;
  316. expect( el ).to.be.instanceOf( Element );
  317. expect( fragment ).to.be.instanceOf( DocumentFragment );
  318. expect( selection.rangeCount ).to.equal( 1 );
  319. expect( selection.getFirstRange().isEqual( Range.createFromParentsAndOffsets( fragment, 0, fragment, 1 ) ) ).to.be.true;
  320. }
  321. } );
  322. test( 'returns DocumentFragment when multiple elements on root', {
  323. data: '<a></a><b></b>',
  324. check( fragment ) {
  325. expect( fragment ).to.be.instanceOf( DocumentFragment );
  326. expect( fragment.childCount ).to.equal( 2 );
  327. }
  328. } );
  329. test( 'creates elements', {
  330. data: '<a></a><b><c></c></b>'
  331. } );
  332. test( 'creates text nodes', {
  333. data: 'foo<a>bar</a>bom'
  334. } );
  335. test( 'creates text nodes with unicode text', {
  336. data: 'நிலைக்கு'
  337. } );
  338. test( 'sets elements attributes', {
  339. data: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
  340. output: '<a bar="true" car="x y" foo="1"></a><b x="y"></b>',
  341. check( root ) {
  342. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
  343. expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
  344. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
  345. expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
  346. }
  347. } );
  348. test( 'sets text attributes', {
  349. data: '<$text bar="true" car="x y" foo="1">foo</$text><$text x="y">bar</$text>bom',
  350. check( root ) {
  351. expect( root.childCount ).to.equal( 3 );
  352. expect( root.maxOffset ).to.equal( 9 );
  353. expect( root.getChild( 0 ).getAttribute( 'bar' ) ).to.equal( true );
  354. expect( root.getChild( 0 ).getAttribute( 'car' ) ).to.equal( 'x y' );
  355. expect( root.getChild( 0 ).getAttribute( 'foo' ) ).to.equal( 1 );
  356. expect( root.getChild( 1 ).getAttribute( 'x' ) ).to.equal( 'y' );
  357. expect( count( root.getChild( 2 ).getAttributes() ) ).to.equal( 0 );
  358. }
  359. } );
  360. test( 'creates element with complex attributes', {
  361. data: `<a foo='{"x":1,"y":2}'></a>`,
  362. output: '<a foo="{"x":1,"y":2}"></a>',
  363. check( a ) {
  364. expect( count( a.getAttributes() ) ).to.equal( 1 );
  365. expect( a.getAttribute( 'foo' ) ).to.have.property( 'x', 1 );
  366. expect( a.getAttribute( 'foo' ) ).to.have.property( 'y', 2 );
  367. }
  368. } );
  369. test( 'returns single parsed element', {
  370. data: '<paragraph></paragraph>',
  371. check( p ) {
  372. expect( p instanceof Element ).to.be.true;
  373. }
  374. } );
  375. test( 'returns DocumentFragment for multiple parsed elements', {
  376. data: '<paragraph></paragraph><paragraph></paragraph>',
  377. check( fragment ) {
  378. expect( fragment instanceof DocumentFragment ).to.be.true;
  379. }
  380. } );
  381. it( 'throws when invalid XML', () => {
  382. expect( () => {
  383. parse( '<a><b></a></b>', document.schema );
  384. } ).to.throw( Error, /Parse error/ );
  385. } );
  386. it( 'throws when try to set element not registered in schema', () => {
  387. expect( () => {
  388. parse( '<xyz></xyz>', document.schema );
  389. } ).to.throw( Error, `Element 'xyz' not allowed in context.` );
  390. } );
  391. it( 'throws when try to set text directly to $root without registering it', () => {
  392. const doc = new Document();
  393. expect( () => {
  394. parse( 'text', doc.schema );
  395. } ).to.throw( Error, `Element '$text' not allowed in context.` );
  396. } );
  397. describe( 'selection', () => {
  398. test( 'sets collapsed selection in an element', {
  399. data: '<a>[]</a>',
  400. check( root, selection ) {
  401. expect( selection.getFirstPosition().parent ).to.have.property( 'name', 'a' );
  402. }
  403. } );
  404. test( 'sets collapsed selection between elements', {
  405. data: '<a></a>[]<b></b>'
  406. } );
  407. test( 'sets collapsed selection before a text', {
  408. data: '<a></a>[]foo'
  409. } );
  410. test( 'sets collapsed selection after a text', {
  411. data: 'foo[]'
  412. } );
  413. test( 'sets collapsed selection within a text', {
  414. data: 'foo[]bar',
  415. check( text, selection ) {
  416. expect( text.offsetSize ).to.equal( 6 );
  417. expect( text.getPath() ).to.deep.equal( [ 0 ] );
  418. expect( selection.getFirstRange().start.path ).to.deep.equal( [ 3 ] );
  419. expect( selection.getFirstRange().end.path ).to.deep.equal( [ 3 ] );
  420. }
  421. } );
  422. it( 'sets selection attributes', () => {
  423. const result = parse( 'foo[]bar', document.schema, { selectionAttributes: {
  424. bold: true,
  425. italic: true
  426. } } );
  427. expect( stringify( result.model, result.selection ) ).to.equal( 'foo<$text bold="true" italic="true">[]</$text>bar' );
  428. } );
  429. test( 'sets collapsed selection between text and text with attributes', {
  430. data: 'foo[]<$text bold="true">bar</$text>',
  431. check( root, selection ) {
  432. expect( root.maxOffset ).to.equal( 6 );
  433. expect( selection.getAttribute( 'bold' ) ).to.be.undefined;
  434. }
  435. } );
  436. test( 'sets selection containing an element', {
  437. data: 'x[<a></a>]'
  438. } );
  439. it( 'sets selection with attribute containing an element', () => {
  440. const result = parse( 'x[<a></a>]', document.schema, { selectionAttributes: {
  441. bold: true
  442. } } );
  443. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  444. expect( result.selection.getAttribute( 'bold' ) ).to.be.true;
  445. } );
  446. it( 'sets a backward selection containing an element', () => {
  447. const result = parse( 'x[<a></a>]', document.schema, {
  448. lastRangeBackward: true
  449. } );
  450. expect( stringify( result.model, result.selection ) ).to.equal( 'x[<a></a>]' );
  451. expect( result.selection.isBackward ).to.true;
  452. } );
  453. test( 'sets selection within a text', {
  454. data: 'x[y]z'
  455. } );
  456. it( 'sets selection within a text with different attributes', () => {
  457. const result = parse( '<$text bold="true">fo[o</$text>ba]r', document.schema, {
  458. selectionAttributes: { bold: true }
  459. } );
  460. expect( stringify( result.model, result.selection ) ).to.equal( '<$text bold="true">fo[o</$text>ba]r' );
  461. expect( result.selection.getAttribute( 'bold' ) ).to.true;
  462. } );
  463. it( 'throws when missing selection start', () => {
  464. expect( () => {
  465. parse( 'foo]' );
  466. } ).to.throw( Error );
  467. } );
  468. it( 'throws when missing selection end', () => {
  469. expect( () => {
  470. parse( '[foo' );
  471. } ).to.throw( Error );
  472. } );
  473. } );
  474. function test( title, options ) {
  475. it( title, () => {
  476. const output = options.output || options.data;
  477. const data = parse( options.data, document.schema );
  478. let model, selection;
  479. if ( data.selection && data.model ) {
  480. model = data.model;
  481. selection = data.selection;
  482. } else {
  483. model = data;
  484. }
  485. expect( stringify( model, selection ) ).to.equal( output );
  486. if ( options.check ) {
  487. options.check( model, selection );
  488. }
  489. } );
  490. }
  491. } );
  492. } );