tools.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. ( () => {
  7. bender.tools.core = {
  8. /**
  9. * Defines CKEditor plugin which is a mock of an editor creator.
  10. *
  11. * If `proto` is not set or it does not define `create()` and `destroy()` methods,
  12. * then they will be set to Sinon spies. Therefore the shortest usage is:
  13. *
  14. * bender.tools.defineEditorCreatorMock( 'test1' );
  15. *
  16. * The mocked creator is available under:
  17. *
  18. * editor.plugins.get( 'creator-thename' );
  19. *
  20. * @param {String} creatorName Name of the creator.
  21. * @param {Object} [proto] Prototype of the creator. Properties from the proto param will
  22. * be copied to the prototype of the creator.
  23. */
  24. defineEditorCreatorMock: ( creatorName, proto ) => {
  25. CKEDITOR.define( 'plugin!creator-' + creatorName, [ 'creator' ], ( Creator ) => {
  26. return mockCreator( Creator );
  27. } );
  28. function mockCreator( Creator ) {
  29. class TestCreator extends Creator {}
  30. if ( proto ) {
  31. for ( let propName in proto ) {
  32. TestCreator.prototype[ propName ] = proto[ propName ];
  33. }
  34. }
  35. if ( !TestCreator.prototype.create ) {
  36. TestCreator.prototype.create = sinon.spy().named( creatorName + '-create' );
  37. }
  38. if ( !TestCreator.prototype.destroy ) {
  39. TestCreator.prototype.destroy = sinon.spy().named( creatorName + '-destroy' );
  40. }
  41. return TestCreator;
  42. }
  43. },
  44. /**
  45. * Returns the number of elements return by the iterator.
  46. *
  47. * bender.tools.core.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
  48. *
  49. * @param {Iterable.<*>} iterator Any iterator.
  50. * @returns {Number} Number of elements returned by that iterator.
  51. */
  52. getIteratorCount: ( iterator ) => {
  53. let count = 0;
  54. for ( let _ of iterator ) { // jshint ignore:line
  55. count++;
  56. }
  57. return count;
  58. }
  59. };
  60. bender.tools.treemodel = {
  61. /**
  62. * Returns tree structure as a simplified string. Elements are uppercase and characters are lowercase.
  63. * Start and end of an element is marked the same way, by the element's name (in uppercase).
  64. *
  65. * let element = new Element( 'div', [], [ 'abc', new Element( 'p', [], 'foo' ), 'xyz' ] );
  66. * bender.tools.treemodel.getNodesAndText( element ); // abcPfooPxyz
  67. *
  68. * @param {treeModel.Range} range Range to stringify.
  69. * @returns {String} String representing element inner structure.
  70. */
  71. getNodesAndText: ( range ) => {
  72. let txt = '';
  73. for ( let step of range ) {
  74. let node = step.node;
  75. if ( node.character ) {
  76. txt += node.character.toLowerCase();
  77. } else if ( node.name ) {
  78. txt += node.name.toUpperCase();
  79. }
  80. }
  81. return txt;
  82. }
  83. };
  84. } )();