8
0

utils.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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/engine/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 {engine.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. jsonParseStringify( object ) {
  33. return JSON.parse( JSON.stringify( object ) );
  34. }
  35. };
  36. export default utils;