writer.js 22 KB

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