model.js 18 KB

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