8
0

attributeoperation.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. /* global describe, before, beforeEach, it, expect */
  7. /* bender-include: ../../_tools/tools.js */
  8. 'use strict';
  9. const getIteratorCount = bender.tools.core.getIteratorCount;
  10. const modules = bender.amd.require(
  11. 'document/document',
  12. 'document/element',
  13. 'document/operation/attributeoperation',
  14. 'document/position',
  15. 'document/range',
  16. 'document/character',
  17. 'document/attribute',
  18. 'document/text',
  19. 'ckeditorerror'
  20. );
  21. describe( 'AttributeOperation', () => {
  22. let Document, Element, AttributeOperation, Position, Range, Character, Attribute, Text, CKEditorError;
  23. before( () => {
  24. Document = modules[ 'document/document' ];
  25. Element = modules[ 'document/element' ];
  26. AttributeOperation = modules[ 'document/operation/attributeoperation' ];
  27. Position = modules[ 'document/position' ];
  28. Range = modules[ 'document/range' ];
  29. Character = modules[ 'document/character' ];
  30. Attribute = modules[ 'document/attribute' ];
  31. Text = modules[ 'document/text' ];
  32. CKEditorError = modules.ckeditorerror;
  33. } );
  34. let doc, root;
  35. beforeEach( () => {
  36. doc = new Document();
  37. root = doc.createRoot( 'root' );
  38. } );
  39. it( 'should have proper type', () => {
  40. const op = new AttributeOperation(
  41. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  42. null,
  43. new Attribute( 'isNew', true ),
  44. doc.version
  45. );
  46. expect( op.type ).to.equal( 'attr' );
  47. } );
  48. it( 'should insert attribute to the set of nodes', () => {
  49. let newAttr = new Attribute( 'isNew', true );
  50. root.insertChildren( 0, 'bar' );
  51. doc.applyOperation(
  52. new AttributeOperation(
  53. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  54. null,
  55. newAttr,
  56. doc.version
  57. )
  58. );
  59. expect( doc.version ).to.equal( 1 );
  60. expect( root.getChildCount() ).to.equal( 3 );
  61. expect( root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
  62. expect( root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
  63. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 0 );
  64. } );
  65. it( 'should add attribute to the existing attributes', () => {
  66. let newAttr = new Attribute( 'isNew', true );
  67. let fooAttr = new Attribute( 'foo', true );
  68. let barAttr = new Attribute( 'bar', true );
  69. root.insertChildren( 0, new Character( 'x', [ fooAttr, barAttr ] ) );
  70. doc.applyOperation(
  71. new AttributeOperation(
  72. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  73. null,
  74. newAttr,
  75. doc.version
  76. )
  77. );
  78. expect( doc.version ).to.equal( 1 );
  79. expect( root.getChildCount() ).to.equal( 1 );
  80. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 3 );
  81. expect( root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
  82. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  83. expect( root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
  84. } );
  85. it( 'should change attribute to the set of nodes', () => {
  86. let oldAttr = new Attribute( 'isNew', false );
  87. let newAttr = new Attribute( 'isNew', true );
  88. root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
  89. doc.applyOperation(
  90. new AttributeOperation(
  91. new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ),
  92. oldAttr,
  93. newAttr,
  94. doc.version
  95. )
  96. );
  97. expect( doc.version ).to.equal( 1 );
  98. expect( root.getChildCount() ).to.equal( 3 );
  99. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 1 );
  100. expect( root.getChild( 0 ).hasAttr( newAttr ) ).to.be.true;
  101. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 1 );
  102. expect( root.getChild( 1 ).hasAttr( newAttr ) ).to.be.true;
  103. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 1 );
  104. expect( root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
  105. } );
  106. it( 'should change attribute in the middle of existing attributes', () => {
  107. let fooAttr = new Attribute( 'foo', true );
  108. let x1Attr = new Attribute( 'x', 1 );
  109. let x2Attr = new Attribute( 'x', 2 );
  110. let barAttr = new Attribute( 'bar', true );
  111. root.insertChildren( 0, new Character( 'x', [ fooAttr, x1Attr, barAttr ] ) );
  112. doc.applyOperation(
  113. new AttributeOperation(
  114. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  115. x1Attr,
  116. x2Attr,
  117. doc.version
  118. )
  119. );
  120. expect( doc.version ).to.equal( 1 );
  121. expect( root.getChildCount() ).to.equal( 1 );
  122. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 3 );
  123. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  124. expect( root.getChild( 0 ).hasAttr( x2Attr ) ).to.be.true;
  125. expect( root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
  126. } );
  127. it( 'should remove attribute', () => {
  128. let fooAttr = new Attribute( 'foo', true );
  129. let xAttr = new Attribute( 'x', true );
  130. let barAttr = new Attribute( 'bar', true );
  131. root.insertChildren( 0, new Character( 'x', [ fooAttr, xAttr, barAttr ] ) );
  132. doc.applyOperation(
  133. new AttributeOperation(
  134. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  135. xAttr,
  136. null,
  137. doc.version
  138. )
  139. );
  140. expect( doc.version ).to.equal( 1 );
  141. expect( root.getChildCount() ).to.equal( 1 );
  142. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 2 );
  143. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  144. expect( root.getChild( 0 ).hasAttr( barAttr ) ).to.be.true;
  145. } );
  146. it( 'should create an AttributeOperation as a reverse', () => {
  147. let oldAttr = new Attribute( 'x', 'old' );
  148. let newAttr = new Attribute( 'x', 'new' );
  149. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) );
  150. let operation = new AttributeOperation( range, oldAttr, newAttr, doc.version );
  151. let reverse = operation.getReversed();
  152. expect( reverse ).to.be.an.instanceof( AttributeOperation );
  153. expect( reverse.baseVersion ).to.equal( 1 );
  154. expect( reverse.range ).to.equal( range );
  155. expect( reverse.oldAttr ).to.equal( newAttr );
  156. expect( reverse.newAttr ).to.equal( oldAttr );
  157. } );
  158. it( 'should undo adding attribute by applying reverse operation', () => {
  159. let newAttr = new Attribute( 'isNew', true );
  160. root.insertChildren( 0, 'bar' );
  161. let operation = new AttributeOperation(
  162. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  163. null,
  164. newAttr,
  165. doc.version
  166. );
  167. let reverse = operation.getReversed();
  168. doc.applyOperation( operation );
  169. doc.applyOperation( reverse );
  170. expect( doc.version ).to.equal( 2 );
  171. expect( root.getChildCount() ).to.equal( 3 );
  172. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 0 );
  173. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 0 );
  174. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 0 );
  175. } );
  176. it( 'should not set attribute of element if change range starts in the middle of that element', () => {
  177. let fooAttr = new Attribute( 'foo', true );
  178. let eleA = new Element( 'a', [], 'abc' );
  179. let eleB = new Element( 'b', [], 'xyz' );
  180. root.insertChildren( 0, [ eleA, eleB ] );
  181. doc.applyOperation(
  182. new AttributeOperation(
  183. new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 1, 2 ] ) ),
  184. null,
  185. fooAttr,
  186. doc.version
  187. )
  188. );
  189. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.false;
  190. } );
  191. it( 'should not remove attribute of element if change range starts in the middle of that element', () => {
  192. let fooAttr = new Attribute( 'foo', true );
  193. let eleA = new Element( 'a', [ fooAttr ], 'abc' );
  194. let eleB = new Element( 'b', [ fooAttr ], 'xyz' );
  195. root.insertChildren( 0, [ eleA, eleB ] );
  196. doc.applyOperation(
  197. new AttributeOperation(
  198. new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 1, 0 ] ) ),
  199. fooAttr,
  200. null,
  201. doc.version
  202. )
  203. );
  204. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  205. } );
  206. it( 'should undo changing attribute by applying reverse operation', () => {
  207. let oldAttr = new Attribute( 'isNew', false );
  208. let newAttr = new Attribute( 'isNew', true );
  209. root.insertChildren( 0, new Text( 'bar', [ oldAttr ] ) );
  210. let operation = new AttributeOperation(
  211. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  212. oldAttr,
  213. newAttr,
  214. doc.version
  215. );
  216. let reverse = operation.getReversed();
  217. doc.applyOperation( operation );
  218. doc.applyOperation( reverse );
  219. expect( doc.version ).to.equal( 2 );
  220. expect( root.getChildCount() ).to.equal( 3 );
  221. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 1 );
  222. expect( root.getChild( 0 ).hasAttr( oldAttr ) ).to.be.true;
  223. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 1 );
  224. expect( root.getChild( 1 ).hasAttr( oldAttr ) ).to.be.true;
  225. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 1 );
  226. expect( root.getChild( 2 ).hasAttr( oldAttr ) ).to.be.true;
  227. } );
  228. it( 'should undo remove attribute by applying reverse operation', () => {
  229. let fooAttr = new Attribute( 'foo', false );
  230. root.insertChildren( 0, new Text( 'bar', [ fooAttr ] ) );
  231. let operation = new AttributeOperation(
  232. new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ),
  233. fooAttr,
  234. null,
  235. doc.version
  236. );
  237. let reverse = operation.getReversed();
  238. doc.applyOperation( operation );
  239. doc.applyOperation( reverse );
  240. expect( doc.version ).to.equal( 2 );
  241. expect( root.getChildCount() ).to.equal( 3 );
  242. expect( getIteratorCount( root.getChild( 0 ).getAttrs() ) ).to.equal( 1 );
  243. expect( root.getChild( 0 ).hasAttr( fooAttr ) ).to.be.true;
  244. expect( getIteratorCount( root.getChild( 1 ).getAttrs() ) ).to.equal( 1 );
  245. expect( root.getChild( 1 ).hasAttr( fooAttr ) ).to.be.true;
  246. expect( getIteratorCount( root.getChild( 2 ).getAttrs() ) ).to.equal( 1 );
  247. expect( root.getChild( 2 ).hasAttr( fooAttr ) ).to.be.true;
  248. } );
  249. it( 'should throw an error when one try to remove and the attribute does not exists', () => {
  250. let fooAttr = new Attribute( 'foo', true );
  251. root.insertChildren( 0, 'x' );
  252. expect( () => {
  253. doc.applyOperation(
  254. new AttributeOperation(
  255. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  256. fooAttr,
  257. null,
  258. doc.version
  259. )
  260. );
  261. } ).to.throw( CKEditorError, /operation-attribute-no-attr-to-remove/ );
  262. } );
  263. it( 'should throw an error when one try to insert and the attribute already exists', () => {
  264. let x1Attr = new Attribute( 'x', 1 );
  265. let x2Attr = new Attribute( 'x', 2 );
  266. root.insertChildren( 0, new Character( 'x', [ x1Attr ] ) );
  267. expect( () => {
  268. doc.applyOperation(
  269. new AttributeOperation(
  270. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  271. null,
  272. x2Attr,
  273. doc.version
  274. )
  275. );
  276. } ).to.throw( CKEditorError, /operation-attribute-attr-exists/ );
  277. } );
  278. it( 'should throw an error when one try to change and the new and old attributes have different keys', () => {
  279. let fooAttr = new Attribute( 'foo', true );
  280. let barAttr = new Attribute( 'bar', true );
  281. root.insertChildren( 0, 'x' );
  282. expect( () => {
  283. doc.applyOperation(
  284. new AttributeOperation(
  285. new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ),
  286. fooAttr,
  287. barAttr,
  288. doc.version
  289. )
  290. );
  291. } ).to.throw( CKEditorError, /operation-attribute-different-keys/ );
  292. } );
  293. it( 'should create an AttributeOperation with the same parameters when cloned', () => {
  294. let range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) );
  295. let oldAttr = new Attribute( 'foo', 'old' );
  296. let newAttr = new Attribute( 'foo', 'bar' );
  297. let baseVersion = doc.version;
  298. let op = new AttributeOperation( range, oldAttr, newAttr, baseVersion );
  299. let clone = op.clone();
  300. // New instance rather than a pointer to the old instance.
  301. expect( clone ).not.to.be.equal( op );
  302. expect( clone ).to.be.instanceof( AttributeOperation );
  303. expect( clone.range.isEqual( range ) ).to.be.true;
  304. expect( clone.oldAttr.isEqual( oldAttr ) ).to.be.true;
  305. expect( clone.newAttr.isEqual( newAttr ) ).to.be.true;
  306. expect( clone.baseVersion ).to.equal( baseVersion );
  307. } );
  308. } );