8
0

utils.js 1.1 KB

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