8
0

model.js 16 KB

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