model.js 11 KB

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