utils.js 944 B

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. const utils = {
  7. /**
  8. * Returns tree structure as a simplified string. Elements are uppercase and characters are lowercase.
  9. * Start and end of an element is marked the same way, by the element's name (in uppercase).
  10. *
  11. * let element = new Element( 'div', [], [ 'abc', new Element( 'p', [], 'foo' ), 'xyz' ] );
  12. * treemodelUtils.getNodesAndText( element ); // abcPfooPxyz
  13. *
  14. * @param {treeModel.Range} range Range to stringify.
  15. * @returns {String} String representing element inner structure.
  16. */
  17. getNodesAndText( range ) {
  18. let txt = '';
  19. for ( let step of range ) {
  20. let node = step.node;
  21. if ( node.character ) {
  22. txt += node.character.toLowerCase();
  23. } else if ( node.name ) {
  24. txt += node.name.toUpperCase();
  25. }
  26. }
  27. return txt;
  28. }
  29. };
  30. export default utils;