changeoperation.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: document */
  6. 'use strict';
  7. var modules = bender.amd.require(
  8. 'document/document',
  9. 'document/changeoperation',
  10. 'document/position',
  11. 'document/range',
  12. 'document/character',
  13. 'document/attribute',
  14. 'document/nodelist',
  15. 'ckeditorerror' );
  16. describe( 'ChangeOperation', function() {
  17. it( 'should insert attribute to the set of nodes', function() {
  18. var Document = modules[ 'document/document' ];
  19. var ChangeOperation = modules[ 'document/changeoperation' ];
  20. var Position = modules[ 'document/position' ];
  21. var Range = modules[ 'document/range' ];
  22. var Attribute = modules[ 'document/attribute' ];
  23. var doc = new Document();
  24. var newAttr = new Attribute( 'isNew', true );
  25. doc.root.insertChildren( 0, 'bar' );
  26. doc.applyOperation( new ChangeOperation(
  27. new Range( new Position( [ 0 ], doc.root ), new Position( [ 2 ], doc.root ) ),
  28. null,
  29. newAttr,
  30. doc.version ) );
  31. expect( doc.version ).to.be.equal( 1 );
  32. expect( doc.root.children.length ).to.be.equal( 3 );
  33. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  34. expect( doc.root.children.get( 1 ).hasAttr( newAttr ) ).to.be.true;
  35. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 0 );
  36. } );
  37. it( 'should insert attribute to multiple ranges', function() {
  38. var Document = modules[ 'document/document' ];
  39. var ChangeOperation = modules[ 'document/changeoperation' ];
  40. var Position = modules[ 'document/position' ];
  41. var Range = modules[ 'document/range' ];
  42. var Attribute = modules[ 'document/attribute' ];
  43. var doc = new Document();
  44. var newAttr = new Attribute( 'isNew', true );
  45. doc.root.insertChildren( 0, 'bar' );
  46. doc.applyOperation( new ChangeOperation(
  47. [
  48. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  49. new Range( new Position( [ 2 ], doc.root ), new Position( [ 3 ], doc.root ) )
  50. ],
  51. null,
  52. newAttr,
  53. doc.version ) );
  54. expect( doc.version ).to.be.equal( 1 );
  55. expect( doc.root.children.length ).to.be.equal( 3 );
  56. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  57. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 0 );
  58. expect( doc.root.children.get( 2 ).hasAttr( newAttr ) ).to.be.true;
  59. } );
  60. it( 'should add attribute to the existing attributes', function() {
  61. var Document = modules[ 'document/document' ];
  62. var ChangeOperation = modules[ 'document/changeoperation' ];
  63. var Position = modules[ 'document/position' ];
  64. var Range = modules[ 'document/range' ];
  65. var Character = modules[ 'document/character' ];
  66. var Attribute = modules[ 'document/attribute' ];
  67. var doc = new Document();
  68. var newAttr = new Attribute( 'isNew', true );
  69. var fooAttr = new Attribute( 'foo', true );
  70. var barAttr = new Attribute( 'bar', true );
  71. doc.root.insertChildren( 0, new Character( 'x', [ fooAttr, barAttr ] ) );
  72. doc.applyOperation( new ChangeOperation(
  73. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  74. null,
  75. newAttr,
  76. doc.version ) );
  77. expect( doc.version ).to.be.equal( 1 );
  78. expect( doc.root.children.length ).to.be.equal( 1 );
  79. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 3 );
  80. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  81. expect( doc.root.children.get( 0 ).hasAttr( fooAttr ) ).to.be.true;
  82. expect( doc.root.children.get( 0 ).hasAttr( barAttr ) ).to.be.true;
  83. } );
  84. it( 'should change attributes on multiple ranges', function() {
  85. var Document = modules[ 'document/document' ];
  86. var ChangeOperation = modules[ 'document/changeoperation' ];
  87. var Position = modules[ 'document/position' ];
  88. var Range = modules[ 'document/range' ];
  89. var Character = modules[ 'document/character' ];
  90. var Attribute = modules[ 'document/attribute' ];
  91. var doc = new Document();
  92. var oldAttr = new Attribute( 'isNew', false );
  93. var newAttr = new Attribute( 'isNew', true );
  94. doc.root.insertChildren( 0, [
  95. new Character( 'b', [ oldAttr ] ),
  96. new Character( 'a', [ oldAttr ] ),
  97. new Character( 'r', [ oldAttr ] ) ] );
  98. doc.applyOperation( new ChangeOperation(
  99. [
  100. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  101. new Range( new Position( [ 2 ], doc.root ), new Position( [ 3 ], doc.root ) )
  102. ],
  103. oldAttr,
  104. newAttr,
  105. doc.version ) );
  106. expect( doc.version ).to.be.equal( 1 );
  107. expect( doc.root.children.length ).to.be.equal( 3 );
  108. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 1 );
  109. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  110. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 1 );
  111. expect( doc.root.children.get( 1 ).hasAttr( oldAttr ) ).to.be.true;
  112. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 1 );
  113. expect( doc.root.children.get( 2 ).hasAttr( newAttr ) ).to.be.true;
  114. } );
  115. it( 'should change attribute to the set of nodes', function() {
  116. var Document = modules[ 'document/document' ];
  117. var ChangeOperation = modules[ 'document/changeoperation' ];
  118. var Position = modules[ 'document/position' ];
  119. var Range = modules[ 'document/range' ];
  120. var Character = modules[ 'document/character' ];
  121. var Attribute = modules[ 'document/attribute' ];
  122. var doc = new Document();
  123. var oldAttr = new Attribute( 'isNew', false );
  124. var newAttr = new Attribute( 'isNew', true );
  125. doc.root.insertChildren( 0, [
  126. new Character( 'b', [ oldAttr ] ),
  127. new Character( 'a', [ oldAttr ] ),
  128. new Character( 'r', [ oldAttr ] ) ] );
  129. doc.applyOperation( new ChangeOperation(
  130. new Range( new Position( [ 0 ], doc.root ), new Position( [ 2 ], doc.root ) ),
  131. oldAttr,
  132. newAttr,
  133. doc.version ) );
  134. expect( doc.version ).to.be.equal( 1 );
  135. expect( doc.root.children.length ).to.be.equal( 3 );
  136. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 1 );
  137. expect( doc.root.children.get( 0 ).hasAttr( newAttr ) ).to.be.true;
  138. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 1 );
  139. expect( doc.root.children.get( 1 ).hasAttr( newAttr ) ).to.be.true;
  140. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 1 );
  141. expect( doc.root.children.get( 2 ).hasAttr( oldAttr ) ).to.be.true;
  142. } );
  143. it( 'should change attribute in the middle of existing attributes', function() {
  144. var Document = modules[ 'document/document' ];
  145. var ChangeOperation = modules[ 'document/changeoperation' ];
  146. var Position = modules[ 'document/position' ];
  147. var Range = modules[ 'document/range' ];
  148. var Character = modules[ 'document/character' ];
  149. var Attribute = modules[ 'document/attribute' ];
  150. var doc = new Document();
  151. var fooAttr = new Attribute( 'foo', true );
  152. var x1Attr = new Attribute( 'x', 1 );
  153. var x2Attr = new Attribute( 'x', 2 );
  154. var barAttr = new Attribute( 'bar', true );
  155. doc.root.insertChildren( 0, new Character( 'x', [ fooAttr, x1Attr, barAttr ] ) );
  156. doc.applyOperation( new ChangeOperation(
  157. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  158. x1Attr,
  159. x2Attr,
  160. doc.version ) );
  161. expect( doc.version ).to.be.equal( 1 );
  162. expect( doc.root.children.length ).to.be.equal( 1 );
  163. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 3 );
  164. expect( doc.root.children.get( 0 ).hasAttr( fooAttr ) ).to.be.true;
  165. expect( doc.root.children.get( 0 ).hasAttr( x2Attr ) ).to.be.true;
  166. expect( doc.root.children.get( 0 ).hasAttr( barAttr ) ).to.be.true;
  167. } );
  168. it( 'should remove attribute', function() {
  169. var Document = modules[ 'document/document' ];
  170. var ChangeOperation = modules[ 'document/changeoperation' ];
  171. var Position = modules[ 'document/position' ];
  172. var Range = modules[ 'document/range' ];
  173. var Character = modules[ 'document/character' ];
  174. var Attribute = modules[ 'document/attribute' ];
  175. var doc = new Document();
  176. var fooAttr = new Attribute( 'foo', true );
  177. var xAttr = new Attribute( 'x', true );
  178. var barAttr = new Attribute( 'bar', true );
  179. doc.root.insertChildren( 0, new Character( 'x', [ fooAttr, xAttr, barAttr ] ) );
  180. doc.applyOperation( new ChangeOperation(
  181. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  182. xAttr,
  183. null,
  184. doc.version ) );
  185. expect( doc.version ).to.be.equal( 1 );
  186. expect( doc.root.children.length ).to.be.equal( 1 );
  187. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 2 );
  188. expect( doc.root.children.get( 0 ).hasAttr( fooAttr ) ).to.be.true;
  189. expect( doc.root.children.get( 0 ).hasAttr( barAttr ) ).to.be.true;
  190. } );
  191. it( 'should remove attributes on multiple ranges', function() {
  192. var Document = modules[ 'document/document' ];
  193. var ChangeOperation = modules[ 'document/changeoperation' ];
  194. var Position = modules[ 'document/position' ];
  195. var Range = modules[ 'document/range' ];
  196. var Character = modules[ 'document/character' ];
  197. var Attribute = modules[ 'document/attribute' ];
  198. var doc = new Document();
  199. var fooAttr = new Attribute( 'foo', true );
  200. doc.root.insertChildren( 0, [
  201. new Character( 'b', [ fooAttr ] ),
  202. new Character( 'a', [ fooAttr ] ),
  203. new Character( 'r', [ fooAttr ] ) ] );
  204. doc.applyOperation( new ChangeOperation(
  205. [
  206. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  207. new Range( new Position( [ 2 ], doc.root ), new Position( [ 3 ], doc.root ) )
  208. ],
  209. fooAttr,
  210. null,
  211. doc.version ) );
  212. expect( doc.version ).to.be.equal( 1 );
  213. expect( doc.root.children.length ).to.be.equal( 3 );
  214. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 0 );
  215. expect( doc.root.children.get( 1 ).hasAttr( fooAttr ) ).to.be.true;
  216. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 0 );
  217. } );
  218. it( 'should create a change operation as a reverse', function() {
  219. var Document = modules[ 'document/document' ];
  220. var ChangeOperation = modules[ 'document/changeoperation' ];
  221. var Position = modules[ 'document/position' ];
  222. var Range = modules[ 'document/range' ];
  223. var Attribute = modules[ 'document/attribute' ];
  224. var doc = new Document();
  225. var oldAttr = new Attribute( 'x', 'old' );
  226. var newAttr = new Attribute( 'x', 'new' );
  227. var ranges = [ new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ) ];
  228. var oppertaion = new ChangeOperation( ranges, oldAttr, newAttr, doc.version );
  229. var reverse = oppertaion.reverseOperation();
  230. expect( reverse ).to.be.an.instanceof( ChangeOperation );
  231. expect( reverse.baseVersion ).to.be.equals( 1 );
  232. expect( reverse.ranges ).to.be.equals( ranges );
  233. expect( reverse.oldAttr ).to.be.equals( newAttr );
  234. expect( reverse.newAttr ).to.be.equals( oldAttr );
  235. } );
  236. it( 'should undo insert attribute by applying reverse operation', function() {
  237. var Document = modules[ 'document/document' ];
  238. var ChangeOperation = modules[ 'document/changeoperation' ];
  239. var Position = modules[ 'document/position' ];
  240. var Range = modules[ 'document/range' ];
  241. var Attribute = modules[ 'document/attribute' ];
  242. var doc = new Document();
  243. var newAttr = new Attribute( 'isNew', true );
  244. doc.root.insertChildren( 0, 'bar' );
  245. var oppertaion = new ChangeOperation(
  246. new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
  247. null,
  248. newAttr,
  249. doc.version );
  250. var reverse = oppertaion.reverseOperation();
  251. doc.applyOperation( oppertaion );
  252. doc.applyOperation( reverse );
  253. expect( doc.version ).to.be.equal( 2 );
  254. expect( doc.root.children.length ).to.be.equal( 3 );
  255. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 0 );
  256. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 0 );
  257. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 0 );
  258. } );
  259. it( 'should undo change attribute by applying reverse operation', function() {
  260. var Document = modules[ 'document/document' ];
  261. var ChangeOperation = modules[ 'document/changeoperation' ];
  262. var Position = modules[ 'document/position' ];
  263. var Range = modules[ 'document/range' ];
  264. var Character = modules[ 'document/character' ];
  265. var Attribute = modules[ 'document/attribute' ];
  266. var doc = new Document();
  267. var oldAttr = new Attribute( 'isNew', false );
  268. var newAttr = new Attribute( 'isNew', true );
  269. doc.root.insertChildren( 0, [
  270. new Character( 'b', [ oldAttr ] ),
  271. new Character( 'a', [ oldAttr ] ),
  272. new Character( 'r', [ oldAttr ] ) ] );
  273. var oppertaion = new ChangeOperation(
  274. new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
  275. oldAttr,
  276. newAttr,
  277. doc.version );
  278. var reverse = oppertaion.reverseOperation();
  279. doc.applyOperation( oppertaion );
  280. doc.applyOperation( reverse );
  281. expect( doc.version ).to.be.equal( 2 );
  282. expect( doc.root.children.length ).to.be.equal( 3 );
  283. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 1 );
  284. expect( doc.root.children.get( 0 ).hasAttr( oldAttr ) ).to.be.true;
  285. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 1 );
  286. expect( doc.root.children.get( 1 ).hasAttr( oldAttr ) ).to.be.true;
  287. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 1 );
  288. expect( doc.root.children.get( 2 ).hasAttr( oldAttr ) ).to.be.true;
  289. } );
  290. it( 'should undo remove attribute by applying reverse operation', function() {
  291. var Document = modules[ 'document/document' ];
  292. var ChangeOperation = modules[ 'document/changeoperation' ];
  293. var Position = modules[ 'document/position' ];
  294. var Range = modules[ 'document/range' ];
  295. var Character = modules[ 'document/character' ];
  296. var Attribute = modules[ 'document/attribute' ];
  297. var doc = new Document();
  298. var fooAttr = new Attribute( 'foo', false );
  299. doc.root.insertChildren( 0, [
  300. new Character( 'b', [ fooAttr ] ),
  301. new Character( 'a', [ fooAttr ] ),
  302. new Character( 'r', [ fooAttr ] ) ] );
  303. var oppertaion = new ChangeOperation(
  304. new Range( new Position( [ 0 ], doc.root ), new Position( [ 3 ], doc.root ) ),
  305. fooAttr,
  306. null,
  307. doc.version );
  308. var reverse = oppertaion.reverseOperation();
  309. doc.applyOperation( oppertaion );
  310. doc.applyOperation( reverse );
  311. expect( doc.version ).to.be.equal( 2 );
  312. expect( doc.root.children.length ).to.be.equal( 3 );
  313. expect( doc.root.children.get( 0 ).attrs.length ).to.be.equal( 1 );
  314. expect( doc.root.children.get( 0 ).hasAttr( fooAttr ) ).to.be.true;
  315. expect( doc.root.children.get( 1 ).attrs.length ).to.be.equal( 1 );
  316. expect( doc.root.children.get( 1 ).hasAttr( fooAttr ) ).to.be.true;
  317. expect( doc.root.children.get( 2 ).attrs.length ).to.be.equal( 1 );
  318. expect( doc.root.children.get( 2 ).hasAttr( fooAttr ) ).to.be.true;
  319. } );
  320. it( 'should throw an error when one try to remove and the attribute does not exists', function() {
  321. var Document = modules[ 'document/document' ];
  322. var ChangeOperation = modules[ 'document/changeoperation' ];
  323. var Position = modules[ 'document/position' ];
  324. var Range = modules[ 'document/range' ];
  325. var Attribute = modules[ 'document/attribute' ];
  326. var CKEditorError = modules.ckeditorerror;
  327. var doc = new Document();
  328. var fooAttr = new Attribute( 'foo', true );
  329. doc.root.insertChildren( 0, 'x' );
  330. expect( function() {
  331. doc.applyOperation( new ChangeOperation(
  332. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  333. fooAttr,
  334. null,
  335. doc.version ) );
  336. } ).to.throw( CKEditorError, /operation-change-no-attr-to-remove/ );
  337. } );
  338. it( 'should throw an error when one try to insert and the attribute already exists', function() {
  339. var Document = modules[ 'document/document' ];
  340. var ChangeOperation = modules[ 'document/changeoperation' ];
  341. var Position = modules[ 'document/position' ];
  342. var Range = modules[ 'document/range' ];
  343. var Character = modules[ 'document/character' ];
  344. var Attribute = modules[ 'document/attribute' ];
  345. var CKEditorError = modules.ckeditorerror;
  346. var doc = new Document();
  347. var x1Attr = new Attribute( 'x', 1 );
  348. var x2Attr = new Attribute( 'x', 2 );
  349. doc.root.insertChildren( 0, new Character( 'x', [ x1Attr ] ) );
  350. expect( function() {
  351. doc.applyOperation( new ChangeOperation(
  352. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  353. null,
  354. x2Attr,
  355. doc.version ) );
  356. } ).to.throw( CKEditorError, /operation-change-attr-exists/ );
  357. } );
  358. it( 'should throw an error when one try to change and the new and old attributes have different keys', function() {
  359. var Document = modules[ 'document/document' ];
  360. var ChangeOperation = modules[ 'document/changeoperation' ];
  361. var Position = modules[ 'document/position' ];
  362. var Range = modules[ 'document/range' ];
  363. var Attribute = modules[ 'document/attribute' ];
  364. var CKEditorError = modules.ckeditorerror;
  365. var doc = new Document();
  366. var fooAttr = new Attribute( 'foo', true );
  367. var barAttr = new Attribute( 'bar', true );
  368. doc.root.insertChildren( 0, 'x' );
  369. expect( function() {
  370. doc.applyOperation( new ChangeOperation(
  371. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  372. fooAttr,
  373. barAttr,
  374. doc.version ) );
  375. } ).to.throw( CKEditorError, /operation-change-different-keys/ );
  376. } );
  377. it( 'should throw an error when one try to change and the old attribute does not exists', function() {
  378. var Document = modules[ 'document/document' ];
  379. var ChangeOperation = modules[ 'document/changeoperation' ];
  380. var Position = modules[ 'document/position' ];
  381. var Range = modules[ 'document/range' ];
  382. var Attribute = modules[ 'document/attribute' ];
  383. var CKEditorError = modules.ckeditorerror;
  384. var doc = new Document();
  385. var x1Attr = new Attribute( 'x', 1 );
  386. var x2Attr = new Attribute( 'x', 2 );
  387. doc.root.insertChildren( 0, 'x' );
  388. expect( function() {
  389. doc.applyOperation( new ChangeOperation(
  390. new Range( new Position( [ 0 ], doc.root ), new Position( [ 1 ], doc.root ) ),
  391. x1Attr,
  392. x2Attr,
  393. doc.version ) );
  394. } ).to.throw( CKEditorError, /operation-change-no-attr-to-change/ );
  395. } );
  396. } );