utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Range from '../../../src/model/range';
  6. import TreeWalker from '../../../src/model/treewalker';
  7. import Text from '../../../src/model/text';
  8. import TextProxy from '../../../src/model/textproxy';
  9. import Batch from '../../../src/model/batch';
  10. /**
  11. * Returns tree structure as a simplified string. Elements are uppercase and characters are lowercase.
  12. * Start and end of an element is marked the same way, by the element's name (in uppercase).
  13. *
  14. * let element = new Element( 'div', [], [ 'abc', new Element( 'p', [], 'foo' ), 'xyz' ] );
  15. * modelUtils.getNodesAndText( element ); // abcPfooPxyz
  16. *
  17. * @param {engine.model.Range} range Range to stringify.
  18. * @returns {String} String representing element inner structure.
  19. */
  20. export function getNodesAndText( range ) {
  21. let txt = '';
  22. const treeWalker = new TreeWalker( { boundaries: range } );
  23. for ( const value of treeWalker ) {
  24. const node = value.item;
  25. const nodeText = node.data;
  26. if ( nodeText ) {
  27. txt += nodeText.toLowerCase();
  28. } else {
  29. txt += node.name.toUpperCase();
  30. }
  31. }
  32. return txt;
  33. }
  34. /**
  35. * Returns object JSON representation. It passes an object by JSON.stringify and JSON.parse functions.
  36. *
  37. * @param {Object|Array} object
  38. */
  39. export function jsonParseStringify( object ) {
  40. return JSON.parse( JSON.stringify( object ) );
  41. }
  42. /**
  43. * Returns a {@link engine.model.Node} or if it starts at given offset, or {@link engine.model.TextProxy} with one
  44. * character, if given offset is occupied by a {@link engine.model.Text}.
  45. *
  46. * @param {engine.model.Element} parent Element from which item will be returned.
  47. * @param {Number} offset Item's offset.
  48. * @returns {engine.model.Node|engine.model.TextProxy}
  49. */
  50. export function itemAt( parent, offset ) {
  51. const index = parent.offsetToIndex( offset );
  52. const node = parent.getChild( index );
  53. if ( node instanceof Text ) {
  54. const offsetInText = offset - node.startOffset;
  55. return new TextProxy( node, offsetInText, 1 );
  56. }
  57. return node;
  58. }
  59. /**
  60. * Returns all text contents that are inside given element and all it's children.
  61. *
  62. * @param {engine.model.Element} element Element from which text will be returned.
  63. * @returns {String} Text contents of the element.
  64. */
  65. export function getText( element ) {
  66. let text = '';
  67. for ( const child of element.getChildren() ) {
  68. if ( child.data ) {
  69. text += child.data;
  70. } else if ( child.name ) {
  71. text += getText( child );
  72. }
  73. }
  74. return text;
  75. }
  76. /**
  77. * Creates a range on given {@link engine.model.Element element} only. The range starts directly before that element
  78. * and ends before the first child of that element.
  79. *
  80. * @param {engine.model.Element} element Element on which range should be created.
  81. * @returns {engine.model.Range}
  82. */
  83. export function createRangeOnElementOnly( element ) {
  84. return Range.createFromParentsAndOffsets( element.parent, element.startOffset, element, 0 );
  85. }