utils.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /**
  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 ( const value of treeWalker ) {
  23. const node = value.item;
  24. const 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 passes 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. * Returns a {@link engine.model.Node} or if it starts at given offset, or {@link engine.model.TextProxy} with one
  43. * character, if given offset is occupied by a {@link engine.model.Text}.
  44. *
  45. * @param {engine.model.Element} parent Element from which item will be returned.
  46. * @param {Number} offset Item's offset.
  47. * @returns {engine.model.Node|engine.model.TextProxy}
  48. */
  49. export function itemAt( parent, offset ) {
  50. const index = parent.offsetToIndex( offset );
  51. const node = parent.getChild( index );
  52. if ( node instanceof Text ) {
  53. const offsetInText = offset - node.startOffset;
  54. return new TextProxy( node, offsetInText, 1 );
  55. }
  56. return node;
  57. }
  58. /**
  59. * Returns all text contents that are inside given element and all it's children.
  60. *
  61. * @param {engine.model.Element} element Element from which text will be returned.
  62. * @returns {String} Text contents of the element.
  63. */
  64. export function getText( element ) {
  65. let text = '';
  66. for ( const child of element.getChildren() ) {
  67. if ( child.data ) {
  68. text += child.data;
  69. } else if ( child.name ) {
  70. text += getText( child );
  71. }
  72. }
  73. return text;
  74. }
  75. /**
  76. * Creates a range on given {@link engine.model.Element element} only. The range starts directly before that element
  77. * and ends before the first child of that element.
  78. *
  79. * @param {engine.model.Element} element Element on which range should be created.
  80. * @returns {engine.model.Range}
  81. */
  82. export function createRangeOnElementOnly( element ) {
  83. return Range.createFromParentsAndOffsets( element.parent, element.startOffset, element, 0 );
  84. }