utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 Delta from '../../../src/model/delta/delta';
  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 pases 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. * Adds given {@link module:engine/model/operation/operation~Operation operation} to a newly created
  44. * {@link module:engine/model/delta/delta~Delta delta}
  45. * and returns it back. Every operation, when applied, have to be added to a delta. This helper function is useful in those
  46. * tests which focus on operations, not deltas.
  47. *
  48. * @param {module:engine/model/operation/operation~Operation} operation Operation to wrap
  49. * @returns {module:engine/model/operation/operation~Operation}
  50. */
  51. export function wrapInDelta( operation ) {
  52. const delta = new Delta();
  53. delta.addOperation( operation );
  54. return operation;
  55. }
  56. /**
  57. * Returns a {@link engine.model.Node} or if it starts at given offset, or {@link engine.model.TextProxy} with one
  58. * character, if given offset is occupied by a {@link engine.model.Text}.
  59. *
  60. * @param {engine.model.Element} parent Element from which item will be returned.
  61. * @param {Number} offset Item's offset.
  62. * @returns {engine.model.Node|engine.model.TextProxy}
  63. */
  64. export function itemAt( parent, offset ) {
  65. const index = parent.offsetToIndex( offset );
  66. const node = parent.getChild( index );
  67. if ( node instanceof Text ) {
  68. const offsetInText = offset - node.startOffset;
  69. return new TextProxy( node, offsetInText, 1 );
  70. }
  71. return node;
  72. }
  73. /**
  74. * Returns all text contents that are inside given element and all it's children.
  75. *
  76. * @param {engine.model.Element} element Element from which text will be returned.
  77. * @returns {String} Text contents of the element.
  78. */
  79. export function getText( element ) {
  80. let text = '';
  81. for ( const child of element.getChildren() ) {
  82. if ( child.data ) {
  83. text += child.data;
  84. } else if ( child.name ) {
  85. text += getText( child );
  86. }
  87. }
  88. return text;
  89. }
  90. /**
  91. * Creates a range on given {@link engine.model.Element element} only. The range starts directly before that element
  92. * and ends before the first child of that element.
  93. *
  94. * @param {engine.model.Element} element Element on which range should be created.
  95. * @returns {engine.model.Range}
  96. */
  97. export function createRangeOnElementOnly( element ) {
  98. return Range.createFromParentsAndOffsets( element.parent, element.startOffset, element, 0 );
  99. }