model.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. describe( 'model test utils', () => {
  13. let document, root, selection, sandbox;
  14. beforeEach( () => {
  15. document = new Document();
  16. root = document.createRoot();
  17. selection = document.selection;
  18. sandbox = sinon.sandbox.create();
  19. selection.removeAllRanges();
  20. document.schema.registerItem( 'a', '$inline' );
  21. document.schema.allow( { name: 'a', inside: '$root' } );
  22. document.schema.registerItem( 'b', '$inline' );
  23. document.schema.allow( { name: 'b', inside: '$root' } );
  24. document.schema.registerItem( 'c', '$inline' );
  25. document.schema.allow( { name: 'c', inside: '$root' } );
  26. document.schema.registerItem( 'paragraph', '$block' );
  27. document.schema.allow( { name: '$text', inside: '$root' } );
  28. } );
  29. afterEach( () => {
  30. sandbox.restore();
  31. } );
  32. describe( 'getData', () => {
  33. it( 'should use stringify method', () => {
  34. const stringifySpy = sandbox.spy( getData, '_stringify' );
  35. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  36. expect( getData( document, { withoutSelection: true } ) ).to.equal( '<b>btext</b>' );
  37. sinon.assert.calledOnce( stringifySpy );
  38. sinon.assert.calledWithExactly( stringifySpy, root );
  39. } );
  40. it( 'should use stringify method with selection', () => {
  41. const stringifySpy = sandbox.spy( getData, '_stringify' );
  42. root.appendChildren( new Element( 'b', null, new Text( 'btext' ) ) );
  43. document.selection.addRange( Range.createFromParentsAndOffsets( root, 0, root, 1 ) );
  44. expect( getData( document ) ).to.equal( '[<b>btext</b>]' );
  45. sinon.assert.calledOnce( stringifySpy );
  46. sinon.assert.calledWithExactly( stringifySpy, root, document.selection );
  47. } );
  48. it( 'should support unicode', () => {
  49. root.appendChildren( 'நிலைக்கு' );
  50. document.selection.addRange( Range.createFromParentsAndOffsets( root, 2, root, 6 ) );
  51. expect( getData( document ) ).to.equal( 'நி[லைக்]கு' );
  52. } );
  53. it( 'should throw an error when passing invalid document', () => {
  54. expect( () => {
  55. getData( { invalid: 'document' } );
  56. } ).to.throw( TypeError, 'Document needs to be an instance of engine.model.Document.' );
  57. } );
  58. } );
  59. describe( 'setData', () => {
  60. it( 'should use parse method', () => {
  61. const parseSpy = sandbox.spy( setData, '_parse' );
  62. const options = {};
  63. const data = '<b>btext</b>text';
  64. setData( document, data, options );
  65. expect( getData( document, { withoutSelection: true } ) ).to.equal( data );
  66. sinon.assert.calledOnce( parseSpy );
  67. const args = parseSpy.firstCall.args;
  68. expect( args[ 0 ] ).to.equal( data );
  69. } );
  70. it( 'should use parse method with selection', () => {
  71. const parseSpy = sandbox.spy( setData, '_parse' );
  72. const options = {};
  73. const data = '[<b>btext</b>]';
  74. setData( document, data, options );
  75. expect( getData( document ) ).to.equal( data );
  76. sinon.assert.calledOnce( parseSpy );
  77. const args = parseSpy.firstCall.args;
  78. expect( args[ 0 ] ).to.equal( data );
  79. } );
  80. it( 'should insert text', () => {
  81. test( 'this is test text', '[]this is test text' );
  82. } );
  83. it( 'should insert text with selection around', () => {
  84. test( '[this is test text]' );
  85. } );
  86. it( 'should insert text with selection inside #1', () => {
  87. test( 'this [is test] text' );
  88. } );
  89. it( 'should insert text with selection inside #2', () => {
  90. test( '[this is test] text' );
  91. } );
  92. it( 'should insert text with selection inside #2', () => {
  93. test( 'this is [test text]' );
  94. } );
  95. it( 'should insert element', () => {
  96. test( '<b>foo bar</b>', '[]<b>foo bar</b>' );
  97. } );
  98. it( 'should insert element with selection inside #1', () => {
  99. test( '<b>[foo ]bar</b>' );
  100. } );
  101. it( 'should insert element with selection inside #2', () => {
  102. test( '[<b>foo ]bar</b>' );
  103. } );
  104. it( 'should insert element with selection inside #3', () => {
  105. test( '<b>[foo bar</b>]' );
  106. } );
  107. it( 'should insert backward selection', () => {
  108. setData( document, '<b>[foo bar</b>]', { lastRangeBackward: true } );
  109. expect( getData( document ) ).to.equal( '<b>[foo bar</b>]' );
  110. expect( document.selection.isBackward ).to.true;
  111. } );
  112. it( 'should support unicode', () => {
  113. test( 'நி[லைக்]கு' );
  114. } );
  115. it( 'should throw an error when passing invalid document', () => {
  116. expect( () => {
  117. setData( { invalid: 'document' } );
  118. } ).to.throw( TypeError, 'Document needs to be an instance of engine.model.Document.' );
  119. } );
  120. function test( data, expected ) {
  121. expected = expected || data;
  122. setData( document, data );
  123. expect( getData( document ) ).to.equal( expected );
  124. }
  125. } );
  126. describe( 'stringify', () => {
  127. it( 'should stringify text', () => {
  128. const text = new Text( 'text', { underline: true, bold: true } );
  129. expect( stringify( text ) ).to.equal( '<$text bold="true" underline="true">text</$text>' );
  130. } );
  131. it( 'should stringify element', () => {
  132. const element = new Element( 'a', null, [
  133. new Element( 'b', null, new Text( 'btext' ) ),
  134. new Text( 'atext' )
  135. ] );
  136. expect( stringify( element ) ).to.equal( '<a><b>btext</b>atext</a>' );
  137. } );
  138. it( 'should stringify document fragment', () => {
  139. const fragment = new DocumentFragment( [
  140. new Element( 'b', null, new Text( 'btext' ) ),
  141. new Text( 'atext' )
  142. ] );
  143. expect( stringify( fragment ) ).to.equal( '<b>btext</b>atext' );
  144. } );
  145. it( 'writes elements and texts', () => {
  146. root.appendChildren( [
  147. new Element( 'a', null, new Text( 'atext' ) ),
  148. new Element( 'b', null, [
  149. new Element( 'c1' ),
  150. new Text( 'ctext' ),
  151. new Element( 'c2' )
  152. ] ),
  153. new Element( 'd' )
  154. ] );
  155. expect( stringify( root ) ).to.equal(
  156. '<a>atext</a><b><c1></c1>ctext<c2></c2></b><d></d>'
  157. );
  158. } );
  159. it( 'writes element attributes', () => {
  160. root.appendChildren(
  161. new Element( 'a', { foo: true, bar: 1, car: false }, [
  162. new Element( 'b', { fooBar: 'x y', barFoo: JSON.stringify( { x: 1, y: 2 } ) } )
  163. ] )
  164. );
  165. // Note: attributes are written in a very simplistic way, because they are not to be parsed. They are just
  166. // to be compared in the tests with some patterns.
  167. expect( stringify( root ) ).to.equal(
  168. '<a bar="1" car="false" foo="true"><b barFoo="{"x":1,"y":2}" fooBar="x y"></b></a>'
  169. );
  170. } );
  171. it( 'writes text attributes', () => {
  172. root.appendChildren( [
  173. new Text( 'foo', { bold: true } ),
  174. new Text( 'bar' ),
  175. new Text( 'bom', { bold: true, italic: true } ),
  176. new Element( 'a', null, [
  177. new Text( 'pom', { underline: true, bold: true } )
  178. ] )
  179. ] );
  180. expect( stringify( root ) ).to.equal(
  181. '<$text bold="true">foo</$text>' +
  182. 'bar' +
  183. '<$text bold="true" italic="true">bom</$text>' +
  184. '<a><$text bold="true" underline="true">pom</$text></a>'
  185. );
  186. } );
  187. describe( 'selection', () => {
  188. let elA, elB;
  189. beforeEach( () => {
  190. elA = new Element( 'a' );
  191. elB = new Element( 'b' );
  192. root.appendChildren( [
  193. elA,
  194. new Text( 'foo' ),
  195. new Text( 'bar', { bold: true } ),
  196. elB
  197. ] );
  198. } );
  199. it( 'writes selection in an empty root', () => {
  200. const root = document.createRoot( '$root', 'empty' );
  201. selection.collapse( root );
  202. expect( stringify( root, selection ) ).to.equal(
  203. '[]'
  204. );
  205. } );
  206. it( 'writes only requested element', () => {
  207. expect( stringify( elA ) ).to.equal( '<a></a>' );
  208. } );
  209. it( 'writes selection collapsed in an element', () => {
  210. selection.collapse( root );
  211. expect( stringify( root, selection ) ).to.equal(
  212. '[]<a></a>foo<$text bold="true">bar</$text><b></b>'
  213. );
  214. } );
  215. it( 'writes selection collapsed in a text', () => {
  216. selection.collapse( root, 3 );
  217. expect( stringify( root, selection ) ).to.equal(
  218. '<a></a>fo[]o<$text bold="true">bar</$text><b></b>'
  219. );
  220. } );
  221. it( 'writes selection collapsed at the text left boundary', () => {
  222. selection.collapse( elA, 'after' );
  223. expect( stringify( root, selection ) ).to.equal(
  224. '<a></a>[]foo<$text bold="true">bar</$text><b></b>'
  225. );
  226. } );
  227. it( 'writes selection collapsed at the text right boundary', () => {
  228. selection.collapse( elB, 'before' );
  229. expect( stringify( root, selection ) ).to.equal(
  230. '<a></a>foo<$text bold="true">bar</$text>[]<b></b>'
  231. );
  232. expect( selection.getAttribute( 'bold' ) ).to.true;
  233. } );
  234. it( 'writes selection collapsed at the end of the root', () => {
  235. selection.collapse( root, 'end' );
  236. // Needed due to https://github.com/ckeditor/ckeditor5-engine/issues/320.
  237. selection.clearAttributes();
  238. expect( stringify( root, selection ) ).to.equal(
  239. '<a></a>foo<$text bold="true">bar</$text><b></b>[]'
  240. );
  241. } );
  242. it( 'writes selection collapsed selection in a text with attributes', () => {
  243. selection.collapse( root, 5 );
  244. expect( stringify( root, selection ) ).to.equal(
  245. '<a></a>foo<$text bold="true">b[]ar</$text><b></b>'
  246. );
  247. expect( selection.getAttribute( 'bold' ) ).to.true;
  248. } );
  249. it( 'writes flat selection containing couple of nodes', () => {
  250. selection.addRange(
  251. Range.createFromParentsAndOffsets( root, 0, root, 4 )
  252. );
  253. expect( stringify( root, selection ) ).to.equal(
  254. '[<a></a>foo]<$text bold="true">bar</$text><b></b>'
  255. );
  256. } );
  257. it( 'writes flat selection within text', () => {
  258. selection.addRange(
  259. Range.createFromParentsAndOffsets( root, 2, root, 3 )
  260. );
  261. expect( stringify( root, selection ) ).to.equal(
  262. '<a></a>f[o]o<$text bold="true">bar</$text><b></b>'
  263. );
  264. } );
  265. it( 'writes multi-level selection', () => {
  266. selection.addRange(
  267. Range.createFromParentsAndOffsets( elA, 0, elB, 0 )
  268. );
  269. expect( stringify( root, selection ) ).to.equal(
  270. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  271. );
  272. } );
  273. it( 'writes selection when is backward', () => {
  274. selection.addRange(
  275. Range.createFromParentsAndOffsets( elA, 0, elB, 0 ),
  276. true
  277. );
  278. expect( stringify( root, selection ) ).to.equal(
  279. '<a>[</a>foo<$text bold="true">bar</$text><b>]</b>'
  280. );
  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( 'sets elements attributes', {
  336. data: '<a foo="1" bar="true" car="x y"><b x="y"></b></a>',
  337. output: '<a bar="true" car="x y" foo="1"><b x="y"></b></a>',
  338. check( a ) {
  339. expect( a.getAttribute( 'car' ) ).to.equal( 'x y' );
  340. }
  341. } );
  342. test( 'sets text attributes', {
  343. data: '<$text bold="true" italic="true">foo</$text><$text bold="true">bar</$text>bom',
  344. check( root ) {
  345. expect( root.childCount ).to.equal( 3 );
  346. expect( root.maxOffset ).to.equal( 9 );
  347. expect( root.getChild( 0 ) ).to.have.property( 'data', 'foo' );
  348. expect( root.getChild( 0 ).getAttribute( 'italic' ) ).to.equal( 'true' );
  349. expect( root.getChild( 1 ) ).to.have.property( 'data', 'bar' );
  350. expect( root.getChild( 1 ).getAttribute( 'bold' ) ).to.equal( 'true' );
  351. }
  352. } );
  353. test( 'returns single parsed element', {
  354. data: '<paragraph></paragraph>',
  355. check( p ) {
  356. expect( p instanceof Element ).to.be.true;
  357. }
  358. } );
  359. test( 'returns DocumentFragment for multiple parsed elements', {
  360. data: '<paragraph></paragraph><paragraph></paragraph>',
  361. check( fragment ) {
  362. expect( fragment instanceof DocumentFragment ).to.be.true;
  363. }
  364. } );
  365. it( 'throws when unexpected closing tag', () => {
  366. expect( () => {
  367. parse( '<a><b></a></b>' );
  368. } ).to.throw( Error );
  369. } );
  370. it( 'throws when unexpected attribute', () => {
  371. expect( () => {
  372. parse( '<a ?></a>' );
  373. } ).to.throw( Error );
  374. } );
  375. it( 'throws when incorrect tag', () => {
  376. expect( () => {
  377. parse( '<a' );
  378. } ).to.throw( Error );
  379. } );
  380. it( 'throws when missing closing tag', () => {
  381. expect( () => {
  382. parse( '<a><b></b>' );
  383. } ).to.throw( Error );
  384. } );
  385. it( 'throws when missing opening tag for text', () => {
  386. expect( () => {
  387. parse( '</$text>' );
  388. } ).to.throw( Error );
  389. } );
  390. it( 'throws when missing closing tag for text', () => {
  391. expect( () => {
  392. parse( '<$text>' );
  393. } ).to.throw( Error );
  394. } );
  395. describe( 'selection', () => {
  396. test( 'sets collapsed selection in an element', {
  397. data: '<a>[]</a>',
  398. check( root, selection ) {
  399. expect( selection.getFirstPosition().parent ).to.have.property( 'name', 'a' );
  400. }
  401. } );
  402. test( 'sets collapsed selection between elements', {
  403. data: '<a></a>[]<b></b>'
  404. } );
  405. test( 'sets collapsed selection before a text', {
  406. data: '<a></a>[]foo'
  407. } );
  408. test( 'sets collapsed selection after a text', {
  409. data: 'foo[]'
  410. } );
  411. test( 'sets collapsed selection within a text', {
  412. data: 'foo[]bar',
  413. check( text, selection ) {
  414. expect( text.offsetSize ).to.equal( 6 );
  415. expect( text.getPath() ).to.deep.equal( [ 0 ] );
  416. expect( selection.getFirstRange().start.path ).to.deep.equal( [ 3 ] );
  417. expect( selection.getFirstRange().end.path ).to.deep.equal( [ 3 ] );
  418. }
  419. } );
  420. it( 'sets selection attributes', () => {
  421. const result = parse( 'foo[]bar', document.schema, { selectionAttributes: {
  422. bold: true,
  423. italic: true
  424. } } );
  425. expect( stringify( result.model, result.selection ) ).to.equal( 'foo[]bar' );
  426. expect( result.selection.getAttribute( 'italic' ) ).to.be.true;
  427. expect( result.selection.getAttribute( 'bold' ) ).to.be.true;
  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 a backward selection containing an element', () => {
  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. } );