8
0

model.js 9.7 KB

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