8
0

model.js 13 KB

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