utils.js 962 B

12345678910111213141516171819202122232425262728293031323334353637
  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.item;
  21. let nodeText = node.text || node.character;
  22. if ( nodeText ) {
  23. txt += nodeText.toLowerCase();
  24. } else {
  25. txt += node.name.toUpperCase();
  26. }
  27. }
  28. return txt;
  29. }
  30. };
  31. export default utils;