8
0

utils.js 3.0 KB

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