tools.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @license Copyright (c) 2003-2016, 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. bender.amd.define( 'creator-' + creatorName, [ 'core/creator' ], ( Creator ) => {
  26. class TestCreator extends Creator {}
  27. if ( proto ) {
  28. for ( let propName in proto ) {
  29. TestCreator.prototype[ propName ] = proto[ propName ];
  30. }
  31. }
  32. if ( !TestCreator.prototype.create ) {
  33. TestCreator.prototype.create = sinon.spy().named( creatorName + '-create' );
  34. }
  35. if ( !TestCreator.prototype.destroy ) {
  36. TestCreator.prototype.destroy = sinon.spy().named( creatorName + '-destroy' );
  37. }
  38. return TestCreator;
  39. } );
  40. },
  41. /**
  42. * Returns the number of elements return by the iterator.
  43. *
  44. * bender.tools.core.getIteratorCount( [ 1, 2, 3, 4, 5 ] ); // 5;
  45. *
  46. * @param {Iterable.<*>} iterator Any iterator.
  47. * @returns {Number} Number of elements returned by that iterator.
  48. */
  49. getIteratorCount( iterator ) {
  50. let count = 0;
  51. for ( let _ of iterator ) { // jshint ignore:line
  52. count++;
  53. }
  54. return count;
  55. }
  56. };
  57. bender.tools.treemodel = {
  58. /**
  59. * Returns tree structure as a simplified string. Elements are uppercase and characters are lowercase.
  60. * Start and end of an element is marked the same way, by the element's name (in uppercase).
  61. *
  62. * let element = new Element( 'div', [], [ 'abc', new Element( 'p', [], 'foo' ), 'xyz' ] );
  63. * bender.tools.treemodel.getNodesAndText( element ); // abcPfooPxyz
  64. *
  65. * @param {treeModel.Range} range Range to stringify.
  66. * @returns {String} String representing element inner structure.
  67. */
  68. getNodesAndText: ( range ) => {
  69. let txt = '';
  70. for ( let step of range ) {
  71. let node = step.node;
  72. if ( node.character ) {
  73. txt += node.character.toLowerCase();
  74. } else if ( node.name ) {
  75. txt += node.name.toUpperCase();
  76. }
  77. }
  78. return txt;
  79. }
  80. };
  81. } )();