writer.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Position from './position.js';
  7. import Element from './element.js';
  8. import Text from './text.js';
  9. import Range from './range.js';
  10. import CKEditorError from '../../utils/ckeditorerror.js';
  11. /**
  12. * Tree View Writer class.
  13. * Writer defines a high-level API for TreeView manipulations.
  14. *
  15. * @memberOf core.treeView
  16. */
  17. export default class Writer {
  18. constructor() {
  19. /**
  20. * Priorities map. Maps node to priority.
  21. * Nodes with priorities are considered as attributes.
  22. *
  23. * @member {WeakMap} core.treeView.Writer#_priorities
  24. * @protected
  25. */
  26. this._priorities = new WeakMap();
  27. }
  28. /**
  29. * Returns `true` if provided node is a `container` node.
  30. *
  31. * `Container` nodes are mostly elements like `<p>` or `<div>`. Break and merge operations are performed only in a
  32. * bounds of a container nodes. Containers will not be broken or merged by
  33. * {@link core.treeView.Writer#breakAttributes breakAttributes} and
  34. * {@link core.treeView.Writer#mergeAttributes mergeAttributes}.
  35. *
  36. * `Attribute` nodes are mostly elements like `<b>` or `<span>`. Attributes can be broken and merged. Merging requires
  37. * that attribute nodes are {@link core.treeView.Element#isSimilar similar} and have same priority. Setting different
  38. * priorities on similar nodes may prevent merging, eg. two `<abbr>` nodes next ot each other shouldn't be merged.
  39. * There might be a need to mark `<span>` element as a container node, for example in situation when it will be a
  40. * container of an inline widget:
  41. *
  42. * <span color="red">foobar</span> // attribute
  43. * <span data-widget>foobar</span> // container
  44. *
  45. * @param {core.treeView.Element} node Node to check.
  46. * @returns {Boolean} `True` if provided node is a container.
  47. */
  48. isContainer( node ) {
  49. const isElement = node instanceof Element;
  50. return isElement && !this._priorities.has( node );
  51. }
  52. /**
  53. * Returns `true` if provided node is an `attribute` node.
  54. * For more information about attribute and container nodes see {@link core.treeView.Writer#isContainer isContainer}
  55. * method description.
  56. *
  57. * @see core.treeView.Writer#isContainer
  58. * @param {core.treeView.Element} node Node to check.
  59. * @returns {Boolean} `True` if provided node is an attribute.
  60. */
  61. isAttribute( node ) {
  62. const isElement = node instanceof Element;
  63. return isElement && this._priorities.has( node );
  64. }
  65. /**
  66. * Sets node priority. When priority is defined, node is considered as `attribute`.
  67. *
  68. * @see core.treeView.Writer#isContainer
  69. * @param {core.treeView.Node} node
  70. * @param {Number} priority
  71. */
  72. setPriority( node, priority ) {
  73. this._priorities.set( node, priority );
  74. }
  75. /**
  76. * Returns node's priority.
  77. *
  78. * @param {core.treeView.Node} node Node to check its priority.
  79. * @returns {Number|undefined} Priority or `undefined` if there is no priority defined.
  80. */
  81. getPriority( node ) {
  82. return this._priorities.get( node );
  83. }
  84. /**
  85. * Returns first parent container of specified {@link core.treeView.Position Position}.
  86. * Position's parent node is checked as first, then next parents are checked.
  87. *
  88. * @param {core.treeView.Position} position Position used as a start point to locate parent container.
  89. * @returns {core.treeView.Element|undefined} Parent container element or `undefined` if container is not found.
  90. */
  91. getParentContainer( position ) {
  92. let parent = position.parent;
  93. while ( !this.isContainer( parent ) ) {
  94. if ( !parent ) {
  95. return undefined;
  96. }
  97. parent = parent.parent;
  98. }
  99. return parent;
  100. }
  101. /**
  102. * Breaks attribute nodes at provided position. It breaks `attribute` nodes inside `container` node.
  103. *
  104. * In following examples `<p>` is a container, `<b>` and `<u>` are attribute nodes:
  105. *
  106. * <p>{foo}<b><u>{bar}|</u></b></p> -> <p>{foo}<b><u>{bar}</u></b>|</p>
  107. * <p>{foo}<b><u>|{bar}</u></b></p> -> <p>{foo}|<b><u>{bar}</u></b></p>
  108. * <p>{foo}<b><u>{b|ar}</u></b></p> -> <p>{foo}<b><u>{b}</u></b>|<b><u>{ar}</u></b></p>
  109. *
  110. * @see core.treeView.Writer#isContainer
  111. * @see core.treeView.Writer#isAttribute
  112. *
  113. * @param {core.treeView.Position} position Position where to break attributes.
  114. * @returns {core.treeView.Position} New position after breaking the attributes.
  115. */
  116. breakAttributes( position ) {
  117. const positionOffset = position.offset;
  118. const positionParent = position.parent;
  119. // Position's parent is container, so no attributes to break.
  120. if ( this.isContainer( positionParent ) ) {
  121. return Position.createFromPosition( position );
  122. }
  123. const parentIsText = positionParent instanceof Text;
  124. const length = parentIsText ? positionParent.data.length : positionParent.getChildCount();
  125. // <p>foo<b><u>bar|</u></b></p>
  126. // <p>foo<b><u>bar</u>|</b></p>
  127. // <p>foo<b><u>bar</u></b>|</p>
  128. if ( positionOffset == length ) {
  129. const newPosition = new Position( positionParent.parent, positionParent.getIndex() + 1 );
  130. return this.breakAttributes( newPosition );
  131. } else
  132. // <p>foo<b><u>|bar</u></b></p>
  133. // <p>foo<b>|<u>bar</u></b></p>
  134. // <p>foo|<b><u>bar</u></b></p>
  135. if ( positionOffset === 0 ) {
  136. const newPosition = new Position( positionParent.parent, positionParent.getIndex() );
  137. return this.breakAttributes( newPosition );
  138. }
  139. // <p>foo<b><u>"b|ar"</u></b></p>
  140. // <p>foo<b><u>"b"|"ar"</u></b></p>
  141. // <p>foo<b><u>b</u>|<u>ar</u></b></p>
  142. // <p>foo<b><u>b</u></b>|<b><u>ar</u></b></p>
  143. else {
  144. const offsetAfter = positionParent.getIndex() + 1;
  145. if ( parentIsText ) {
  146. // Break text.
  147. // Get part of the text that need to be moved.
  148. const textToMove = positionParent.data.slice( positionOffset );
  149. // Leave rest of the text in position's parent.
  150. positionParent.data = positionParent.data.slice( 0, positionOffset );
  151. // Insert new text node after position's parent text node.
  152. positionParent.parent.insertChildren( offsetAfter, new Text( textToMove ) );
  153. // Create new position to work on.
  154. const newPosition = new Position( positionParent.parent, offsetAfter );
  155. return this.breakAttributes( newPosition );
  156. } else {
  157. // Break element.
  158. const clonedNode = positionParent.clone();
  159. // Clone priority.
  160. this.setPriority( clonedNode, this.getPriority( positionParent ) );
  161. // Insert cloned node to position's parent node.
  162. positionParent.parent.insertChildren( offsetAfter, clonedNode );
  163. // Get nodes to move.
  164. const count = positionParent.getChildCount() - positionOffset;
  165. const nodesToMove = positionParent.removeChildren( positionOffset, count );
  166. // Move nodes to cloned node.
  167. clonedNode.appendChildren( nodesToMove );
  168. // Create new position to work on.
  169. const newPosition = new Position( positionParent.parent, offsetAfter );
  170. return this.breakAttributes( newPosition );
  171. }
  172. }
  173. }
  174. /**
  175. * Uses {@link core.treeView.Writer#breakAttributes breakAttribute} method to break attributes on
  176. * {@link core.treeView.Range#start start} and {@link core.treeView.Range#end end} positions of
  177. * provided {@link core.treeView.Range Range}.
  178. *
  179. * Throws {@link core.CKEditorError CKEditorError} `treeview-writer-invalid-range-container` when
  180. * {@link core.treeView.Range#start start} and {@link core.treeView.Range#end end} positions are not placed inside
  181. * same parent container.
  182. *
  183. * @see core.treeView.Writer#breakAttribute
  184. * @param {core.treeView.Range} range Range which `start` and `end` positions will be used to break attributes.
  185. * @returns {core.treeView.Range} New range with located at break positions.
  186. */
  187. breakRange( range ) {
  188. const rangeStart = range.start;
  189. const rangeEnd = range.end;
  190. // Range should be placed inside one container.
  191. if ( this.getParentContainer( rangeStart ) !== this.getParentContainer( rangeEnd ) ) {
  192. /**
  193. * Range is not placed inside same container.
  194. *
  195. * @error treeview-writer-invalid-range-container
  196. */
  197. throw new CKEditorError( 'treeview-writer-invalid-range-container' );
  198. }
  199. // Break at the collapsed position. Return new collapsed range.
  200. if ( range.isCollapsed ) {
  201. const position = this.breakAttributes( range.start );
  202. return new Range( position, position );
  203. }
  204. const breakEnd = this.breakAttributes( rangeEnd );
  205. const count = breakEnd.parent.getChildCount();
  206. const breakStart = this.breakAttributes( rangeStart );
  207. // Calculate new break end offset.
  208. breakEnd.offset += breakEnd.parent.getChildCount() - count;
  209. return new Range( breakStart, breakEnd );
  210. }
  211. /**
  212. * Merges attribute nodes. It also merges text nodes if needed.
  213. * Only {@link core.treeView.Element#isSimilar similar} `attribute` nodes, with same priority can be merged.
  214. *
  215. * In following examples `<p>` is a container and `<b>` is an attribute node:
  216. *
  217. * <p>{foo}|{bar}</p> -> <p>{foo|bar}</p>
  218. * <p><b>{foo}</b>|<b>{bar}</b> -> <p><b>{foo|bar}</b></b>
  219. * <p><b foo="bar">{a}</b>|<b foo="baz">{b}</b> -> <p><b foo="bar">{a}</b>|<b foo="baz">{b}</b>
  220. *
  221. * @see core.treeView.Writer#isContainer
  222. * @see core.treeView.Writer#isAttribute
  223. * @param {core.treeView.Position} position Merge position.
  224. * @returns {core.treeView.Position} Position after merge.
  225. */
  226. mergeAttributes( position ) {
  227. const positionOffset = position.offset;
  228. const positionParent = position.parent;
  229. // When inside text node - nothing to merge.
  230. if ( positionParent instanceof Text ) {
  231. return position;
  232. }
  233. const nodeBefore = positionParent.getChild( positionOffset - 1 );
  234. const nodeAfter = positionParent.getChild( positionOffset );
  235. // Position should be placed between two nodes.
  236. if ( !nodeBefore || !nodeAfter ) {
  237. return position;
  238. }
  239. // When one or both nodes are containers - no attributes to merge.
  240. if ( this.isContainer( nodeBefore ) || this.isContainer( nodeAfter ) ) {
  241. return position;
  242. }
  243. if ( nodeBefore instanceof Text && nodeAfter instanceof Text ) {
  244. // When selection is between two text nodes.
  245. // Merge text data into first text node and remove second one.
  246. const nodeBeforeLength = nodeBefore.data.length;
  247. nodeBefore.data += nodeAfter.data;
  248. positionParent.removeChildren( positionOffset );
  249. return new Position( nodeBefore, nodeBeforeLength );
  250. } else if ( nodeBefore.isSimilar( nodeAfter ) ) {
  251. // When selection is between same nodes.
  252. const nodeBeforePriority = this._priorities.get( nodeBefore );
  253. const nodeAfterPriority = this._priorities.get( nodeAfter );
  254. // Do not merge same nodes with different priorities.
  255. if ( nodeBeforePriority === undefined || nodeBeforePriority !== nodeAfterPriority ) {
  256. return Position.createFromPosition( position );
  257. }
  258. // Move all children nodes from node placed after selection and remove that node.
  259. const count = nodeBefore.getChildCount();
  260. nodeBefore.appendChildren( nodeAfter.getChildren() );
  261. nodeAfter.remove();
  262. // New position is located inside the first node, before new nodes.
  263. // Call this method recursively to merge again if needed.
  264. return this.mergeAttributes( new Position( nodeBefore, count ) );
  265. }
  266. return position;
  267. }
  268. /**
  269. * Insert node or nodes at specified position. Takes care about breaking attributes before insertion
  270. * and merging them afterwards.
  271. *
  272. * @param {core.treeView.Position} position Insertion position.
  273. * @param {core.treeView.Node|Iterable.<core.treeView.Node>} nodes Node or nodes to insert.
  274. * @returns {core.treeView.Range} Range around inserted nodes.
  275. */
  276. insert( position, nodes ) {
  277. const container = this.getParentContainer( position );
  278. const insertionPosition = this.breakAttributes( position );
  279. const length = container.insertChildren( insertionPosition.offset, nodes );
  280. const endPosition = insertionPosition.getShiftedBy( length );
  281. const start = this.mergeAttributes( insertionPosition );
  282. // When no nodes were inserted - return collapsed range.
  283. if ( length === 0 ) {
  284. return new Range( start, start );
  285. } else {
  286. // If start position was merged - move end position.
  287. if ( !start.isEqual( insertionPosition ) ) {
  288. endPosition.offset--;
  289. }
  290. const end = this.mergeAttributes( endPosition );
  291. return new Range( start, end );
  292. }
  293. }
  294. /**
  295. * Removes provided range from the container.
  296. *
  297. * Throws {@link core.CKEditorError CKEditorError} `treeview-writer-invalid-range-container` when
  298. * {@link core.treeView.Range#start start} and {@link core.treeView.Range#end end} positions are not placed inside
  299. * same parent container.
  300. *
  301. * @param {core.treeView.Range} range Range to remove from container. After removing, it will be updated
  302. * to a collapsed range showing the new position.
  303. * @returns {Array.<core.treeView.Node>} The array of removed nodes.
  304. */
  305. remove( range ) {
  306. // Range should be placed inside one container.
  307. if ( this.getParentContainer( range.start ) !== this.getParentContainer( range.end ) ) {
  308. /**
  309. * Range is not placed inside same container.
  310. *
  311. * @error treeview-writer-invalid-range-container
  312. */
  313. throw new CKEditorError( 'treeview-writer-invalid-range-container' );
  314. }
  315. // If range is collapsed - nothing to remove.
  316. if ( range.isCollapsed ) {
  317. return [];
  318. }
  319. // Break attributes at range start and end.
  320. const { start: breakStart, end: breakEnd } = this.breakRange( range );
  321. const parentContainer = breakStart.parent;
  322. const count = breakEnd.offset - breakStart.offset;
  323. // Remove nodes in range.
  324. const removed = parentContainer.removeChildren( breakStart.offset, count );
  325. // Merge after removing.
  326. const mergePosition = this.mergeAttributes( breakStart );
  327. range.start = mergePosition;
  328. range.end = Position.createFromPosition( mergePosition );
  329. // Return removed nodes.
  330. return removed;
  331. }
  332. /**
  333. * Moves nodes from provided range to target position.
  334. *
  335. * Throws {@link core.CKEditorError CKEditorError} `treeview-writer-invalid-range-container` when
  336. * {@link core.treeView.Range#start start} and {@link core.treeView.Range#end end} positions are not placed inside
  337. * same parent container.
  338. *
  339. * @param {core.treeView.Range} sourceRange Range containing nodes to move.
  340. * @param {core.treeView.Position} targetPosition Position to insert.
  341. * @returns {core.treeView.Range} Range in target container. Inserted nodes are placed between
  342. * {@link core.treeView.Range#start start} and {@link core.treeView.Range#end end} positions.
  343. */
  344. move( sourceRange, targetPosition ) {
  345. const nodes = this.remove( sourceRange );
  346. return this.insert( targetPosition, nodes );
  347. }
  348. /**
  349. * Wraps elements within range with provided attribute element.
  350. *
  351. * Throws {@link core.CKEditorError} `treeview-writer-invalid-range-container` when {@link core.treeView.Range#start}
  352. * and {@link core.treeView.Range#end} positions are not placed inside same parent container.
  353. *
  354. * @param {core.treeView.Range} range Range to wrap.
  355. * @param {core.treeView.Element} attribute Attribute element to use as wrapper.
  356. * @param {Number} priority Priority to set.
  357. */
  358. wrap( range, attribute, priority ) {
  359. // Range should be placed inside one container.
  360. if ( this.getParentContainer( range.start ) !== this.getParentContainer( range.end ) ) {
  361. /**
  362. * Range is not placed inside same container.
  363. *
  364. * @error treeview-writer-invalid-range-container
  365. */
  366. throw new CKEditorError( 'treeview-writer-invalid-range-container' );
  367. }
  368. // If range is collapsed - nothing to wrap.
  369. if ( range.isCollapsed ) {
  370. return range;
  371. }
  372. // Sets wrapper element priority.
  373. this.setPriority( attribute, priority );
  374. // Break attributes at range start and end.
  375. const { start: breakStart, end: breakEnd } = this.breakRange( range );
  376. const parentContainer = breakStart.parent;
  377. // Unwrap children located between break points.
  378. const unwrappedRange = unwrapChildren( this, parentContainer, breakStart.offset, breakEnd.offset, attribute );
  379. // Wrap all children with attribute.
  380. const newRange = wrapChildren( this, parentContainer, unwrappedRange.start.offset, unwrappedRange.end.offset, attribute );
  381. // Merge attributes at the both ends and return a new range.
  382. const start = this.mergeAttributes( newRange.start );
  383. // If start position was merged - move end position back.
  384. if ( !start.isEqual( newRange.start ) ) {
  385. newRange.end.offset--;
  386. }
  387. const end = this.mergeAttributes( newRange.end );
  388. return new Range( start, end );
  389. }
  390. /**
  391. * Unwraps nodes within provided range from attribute element.
  392. *
  393. * Throws {@link core.CKEditorError CKEditorError} `treeview-writer-invalid-range-container` when
  394. * {@link core.treeView.Range#start start} and {@link core.treeView.Range#end end} positions are not placed inside
  395. * same parent container.
  396. *
  397. * @param {core.treeView.Range} range
  398. * @param {core.treeView.Element} element
  399. */
  400. unwrap( range, attribute ) {
  401. // Range should be placed inside one container.
  402. if ( this.getParentContainer( range.start ) !== this.getParentContainer( range.end ) ) {
  403. /**
  404. * Range is not placed inside same container.
  405. *
  406. * @error treeview-writer-invalid-range-container
  407. */
  408. throw new CKEditorError( 'treeview-writer-invalid-range-container' );
  409. }
  410. // If range is collapsed - nothing to unwrap.
  411. if ( range.isCollapsed ) {
  412. return range;
  413. }
  414. // Break attributes at range start and end.
  415. const { start: breakStart, end: breakEnd } = this.breakRange( range );
  416. const parentContainer = breakStart.parent;
  417. // Unwrap children located between break points.
  418. const newRange = unwrapChildren( this, parentContainer, breakStart.offset, breakEnd.offset, attribute );
  419. // Merge attributes at the both ends and return a new range.
  420. const start = this.mergeAttributes( newRange.start );
  421. // If start position was merged - move end position back.
  422. if ( !start.isEqual( newRange.start ) ) {
  423. newRange.end.offset--;
  424. }
  425. const end = this.mergeAttributes( newRange.end );
  426. return new Range( start, end );
  427. }
  428. }
  429. // Unwraps children from provided `attribute`. Only children contained in `parent` element between
  430. // `startOffset` and `endOffset` will be unwrapped.
  431. //
  432. // @private
  433. // @param {core.treeView.Writer} writer
  434. // @param {core.treeView.Element} parent
  435. // @param {Number} startOffset
  436. // @param {Number} endOffset
  437. // @param {core.treeView.Element} attribute
  438. function unwrapChildren( writer, parent, startOffset, endOffset, attribute ) {
  439. let i = startOffset;
  440. const unwrapPositions = [];
  441. // Iterate over each element between provided offsets inside parent.
  442. while ( i < endOffset ) {
  443. const child = parent.getChild( i );
  444. // If attributes are the similar and have same priority, then unwrap.
  445. if ( child.isSimilar( attribute ) && writer.getPriority( child ) == writer.getPriority( attribute ) ) {
  446. const unwrapped = child.getChildren();
  447. const count = child.getChildCount();
  448. // Replace wrapper element with its children
  449. child.remove();
  450. parent.insertChildren( i, unwrapped );
  451. // Save start and end position of moved items.
  452. unwrapPositions.push(
  453. new Position( parent, i ),
  454. new Position( parent, i + count )
  455. );
  456. // Skip elements that were unwrapped. Assuming that there won't be another element to unwrap in child
  457. // elements.
  458. i += count;
  459. endOffset += count - 1;
  460. } else {
  461. // If other nested attribute is found start unwrapping there.
  462. if ( writer.isAttribute( child ) ) {
  463. unwrapChildren( writer, child, 0, child.getChildCount(), attribute );
  464. }
  465. i++;
  466. }
  467. }
  468. // Merge at each unwrap.
  469. let offsetChange = 0;
  470. for ( let position of unwrapPositions ) {
  471. position.offset -= offsetChange;
  472. // Do not merge with elements outside selected children.
  473. if ( position.offset == startOffset || position.offset == endOffset ) {
  474. continue;
  475. }
  476. const newPosition = writer.mergeAttributes( position );
  477. // If nodes were merged - other merge offsets will change.
  478. if ( !newPosition.isEqual( position ) ) {
  479. offsetChange++;
  480. endOffset--;
  481. }
  482. }
  483. return Range.createFromParentsAndOffsets( parent, startOffset, parent, endOffset );
  484. }
  485. // Wraps children with provided `attribute`. Only children contained in `parent` element between
  486. // `startOffset` and `endOffset` will be wrapped.
  487. // @private
  488. // @param {core.treeView.Writer} writer
  489. // @param {core.treeView.Element} parent
  490. // @param {Number} startOffset
  491. // @param {Number} endOffset
  492. // @param {core.treeView.Element} attribute
  493. function wrapChildren( writer, parent, startOffset, endOffset, attribute ) {
  494. let i = startOffset;
  495. const wrapPositions = [];
  496. while ( i < endOffset ) {
  497. const child = parent.getChild( i );
  498. const isText = child instanceof Text;
  499. const isAttribute = writer.isAttribute( child );
  500. const priority = writer.getPriority( attribute );
  501. // Wrap text or attributes with higher or equal priority.
  502. if ( isText || ( isAttribute && priority <= writer.getPriority( child ) ) ) {
  503. // Clone attribute.
  504. const newAttribute = attribute.clone();
  505. writer.setPriority( newAttribute, priority );
  506. // Wrap current node with new attribute;
  507. child.remove();
  508. newAttribute.appendChildren( child );
  509. parent.insertChildren( i, newAttribute );
  510. wrapPositions.push( new Position( parent, i ) );
  511. } else {
  512. // If other nested attribute is found start wrapping there.
  513. if ( writer.isAttribute( child ) ) {
  514. wrapChildren( writer, child, 0, child.getChildCount(), attribute );
  515. }
  516. }
  517. i++;
  518. }
  519. // Merge at each wrap.
  520. let offsetChange = 0;
  521. for ( let position of wrapPositions ) {
  522. // Do not merge with elements outside selected children.
  523. if ( position.offset == startOffset ) {
  524. continue;
  525. }
  526. const newPosition = writer.mergeAttributes( position );
  527. // If nodes were merged - other merge offsets will change.
  528. if ( !newPosition.isEqual( position ) ) {
  529. offsetChange++;
  530. endOffset--;
  531. }
  532. }
  533. return Range.createFromParentsAndOffsets( parent, startOffset, parent, endOffset );
  534. }