utils.js 3.5 KB

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