documentselection.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/model/documentselection
  7. */
  8. import Position from './position';
  9. import Range from './range';
  10. import LiveRange from './liverange';
  11. import Text from './text';
  12. import TextProxy from './textproxy';
  13. import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
  14. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  15. import log from '@ckeditor/ckeditor5-utils/src/log';
  16. import Selection from './selection';
  17. const storePrefix = 'selection:';
  18. const attrOpTypes = new Set(
  19. [ 'addAttribute', 'removeAttribute', 'changeAttribute', 'addRootAttribute', 'removeRootAttribute', 'changeRootAttribute' ]
  20. );
  21. /**
  22. * `DocumentSelection` is a special selection which is used as the
  23. * {@link module:engine/model/document~Document#selection document's selection}.
  24. * There can be only one instance of `DocumentSelection` per document.
  25. *
  26. * `DocumentSelection` is automatically updated upon changes in the {@link module:engine/model/document~Document document}
  27. * to always contain valid ranges. Its attributes are inherited from the text unless set explicitly.
  28. *
  29. * Differences between {@link module:engine/model/selection~Selection} and `DocumentSelection` are:
  30. * * there is always a range in `DocumentSelection` - even if no ranges were added there is a "default range"
  31. * present in the selection,
  32. * * ranges added to this selection updates automatically when the document changes,
  33. * * attributes of `DocumentSelection` are updated automatically according to selection ranges.
  34. *
  35. * Since `DocumentSelection` uses {@link module:engine/model/liverange~LiveRange live ranges}
  36. * and is updated when {@link module:engine/model/document~Document document}
  37. * changes, it cannot be set on {@link module:engine/model/node~Node nodes}
  38. * that are inside {@link module:engine/model/documentfragment~DocumentFragment document fragment}.
  39. * If you need to represent a selection in document fragment,
  40. * use {@link module:engine/model/selection~Selection Selection class} instead.
  41. *
  42. * @extends module:engine/model/selection~Selection
  43. */
  44. export default class DocumentSelection extends Selection {
  45. /**
  46. * Creates an empty live selection for given {@link module:engine/model/document~Document}.
  47. *
  48. * @param {module:engine/model/document~Document} document Document which owns this selection.
  49. */
  50. constructor( document ) {
  51. super();
  52. /**
  53. * Document which owns this selection.
  54. *
  55. * @protected
  56. * @member {module:engine/model/document~Document} module:engine/model/documentselection~DocumentSelection#_document
  57. */
  58. this._document = document;
  59. /**
  60. * Keeps mapping of attribute name to priority with which the attribute got modified (added/changed/removed)
  61. * last time. Possible values of priority are: `'low'` and `'normal'`.
  62. *
  63. * Priorities are used by internal `DocumentSelection` mechanisms. All attributes set using `DocumentSelection`
  64. * attributes API are set with `'normal'` priority.
  65. *
  66. * @private
  67. * @member {Map} module:engine/model/documentselection~DocumentSelection#_attributePriority
  68. */
  69. this._attributePriority = new Map();
  70. this.listenTo( this._document, 'change', ( evt, type, changes, batch ) => {
  71. // Whenever attribute operation is performed on document, update selection attributes.
  72. // This is not the most efficient way to update selection attributes, but should be okay for now.
  73. if ( attrOpTypes.has( type ) ) {
  74. this._updateAttributes( false );
  75. }
  76. // Whenever element which had selection's attributes stored in it stops being empty,
  77. // the attributes need to be removed.
  78. clearAttributesStoredInElement( changes, batch, this._document );
  79. } );
  80. }
  81. /**
  82. * @inheritDoc
  83. */
  84. get isCollapsed() {
  85. const length = this._ranges.length;
  86. return length === 0 ? this._document._getDefaultRange().isCollapsed : super.isCollapsed;
  87. }
  88. /**
  89. * @inheritDoc
  90. */
  91. get anchor() {
  92. return super.anchor || this._document._getDefaultRange().start;
  93. }
  94. /**
  95. * @inheritDoc
  96. */
  97. get focus() {
  98. return super.focus || this._document._getDefaultRange().end;
  99. }
  100. /**
  101. * @inheritDoc
  102. */
  103. get rangeCount() {
  104. return this._ranges.length ? this._ranges.length : 1;
  105. }
  106. /**
  107. * Unbinds all events previously bound by document selection.
  108. */
  109. destroy() {
  110. for ( let i = 0; i < this._ranges.length; i++ ) {
  111. this._ranges[ i ].detach();
  112. }
  113. this.stopListening();
  114. }
  115. /**
  116. * @inheritDoc
  117. */
  118. * getRanges() {
  119. if ( this._ranges.length ) {
  120. yield* super.getRanges();
  121. } else {
  122. yield this._document._getDefaultRange();
  123. }
  124. }
  125. /**
  126. * @inheritDoc
  127. */
  128. getFirstRange() {
  129. return super.getFirstRange() || this._document._getDefaultRange();
  130. }
  131. /**
  132. * @inheritDoc
  133. */
  134. getLastRange() {
  135. return super.getLastRange() || this._document._getDefaultRange();
  136. }
  137. /**
  138. * @inheritDoc
  139. */
  140. addRange( range, isBackward = false ) {
  141. super.addRange( range, isBackward );
  142. this.refreshAttributes();
  143. }
  144. /**
  145. * @inheritDoc
  146. */
  147. removeAllRanges() {
  148. super.removeAllRanges();
  149. this.refreshAttributes();
  150. }
  151. /**
  152. * @inheritDoc
  153. */
  154. setRanges( newRanges, isLastBackward = false ) {
  155. super.setRanges( newRanges, isLastBackward );
  156. this.refreshAttributes();
  157. }
  158. /**
  159. * @inheritDoc
  160. */
  161. setAttribute( key, value ) {
  162. // Store attribute in parent element if the selection is collapsed in an empty node.
  163. if ( this.isCollapsed && this.anchor.parent.isEmpty ) {
  164. this._storeAttribute( key, value );
  165. }
  166. if ( this._setAttribute( key, value ) ) {
  167. // Fire event with exact data.
  168. const attributeKeys = [ key ];
  169. this.fire( 'change:attribute', { attributeKeys, directChange: true } );
  170. }
  171. }
  172. /**
  173. * @inheritDoc
  174. */
  175. removeAttribute( key ) {
  176. // Remove stored attribute from parent element if the selection is collapsed in an empty node.
  177. if ( this.isCollapsed && this.anchor.parent.isEmpty ) {
  178. this._removeStoredAttribute( key );
  179. }
  180. if ( this._removeAttribute( key ) ) {
  181. // Fire event with exact data.
  182. const attributeKeys = [ key ];
  183. this.fire( 'change:attribute', { attributeKeys, directChange: true } );
  184. }
  185. }
  186. /**
  187. * @inheritDoc
  188. */
  189. setAttributesTo( attrs ) {
  190. attrs = toMap( attrs );
  191. if ( this.isCollapsed && this.anchor.parent.isEmpty ) {
  192. this._setStoredAttributesTo( attrs );
  193. }
  194. const changed = this._setAttributesTo( attrs );
  195. if ( changed.size > 0 ) {
  196. // Fire event with exact data (fire only if anything changed).
  197. const attributeKeys = Array.from( changed );
  198. this.fire( 'change:attribute', { attributeKeys, directChange: true } );
  199. }
  200. }
  201. /**
  202. * @inheritDoc
  203. */
  204. clearAttributes() {
  205. this.setAttributesTo( [] );
  206. }
  207. /**
  208. * Removes all attributes from the selection and sets attributes according to the surrounding nodes.
  209. */
  210. refreshAttributes() {
  211. this._updateAttributes( true );
  212. }
  213. /**
  214. * This method is not available in `DocumentSelection`. There can be only one
  215. * `DocumentSelection` per document instance, so creating new `DocumentSelection`s this way
  216. * would be unsafe.
  217. */
  218. static createFromSelection() {
  219. /**
  220. * `DocumentSelection#createFromSelection()` is not available. There can be only one
  221. * `DocumentSelection` per document instance, so creating new `DocumentSelection`s this way
  222. * would be unsafe.
  223. *
  224. * @error documentselection-cannot-create
  225. */
  226. throw new CKEditorError( 'documentselection-cannot-create: Cannot create new DocumentSelection instance.' );
  227. }
  228. /**
  229. * @inheritDoc
  230. */
  231. _popRange() {
  232. this._ranges.pop().detach();
  233. }
  234. /**
  235. * @inheritDoc
  236. */
  237. _pushRange( range ) {
  238. const liveRange = this._prepareRange( range );
  239. // `undefined` is returned when given `range` is in graveyard root.
  240. if ( liveRange ) {
  241. this._ranges.push( liveRange );
  242. }
  243. }
  244. /**
  245. * Prepares given range to be added to selection. Checks if it is correct,
  246. * converts it to {@link module:engine/model/liverange~LiveRange LiveRange}
  247. * and sets listeners listening to the range's change event.
  248. *
  249. * @private
  250. * @param {module:engine/model/range~Range} range
  251. */
  252. _prepareRange( range ) {
  253. if ( !( range instanceof Range ) ) {
  254. /**
  255. * Trying to add an object that is not an instance of Range.
  256. *
  257. * @error model-selection-added-not-range
  258. */
  259. throw new CKEditorError( 'model-selection-added-not-range: Trying to add an object that is not an instance of Range.' );
  260. }
  261. if ( range.root == this._document.graveyard ) {
  262. /**
  263. * Trying to add a Range that is in the graveyard root. Range rejected.
  264. *
  265. * @warning model-selection-range-in-graveyard
  266. */
  267. log.warn( 'model-selection-range-in-graveyard: Trying to add a Range that is in the graveyard root. Range rejected.' );
  268. return;
  269. }
  270. this._checkRange( range );
  271. const liveRange = LiveRange.createFromRange( range );
  272. this.listenTo( liveRange, 'change:range', ( evt, oldRange, data ) => {
  273. // If `LiveRange` is in whole moved to the graveyard, fix that range.
  274. if ( liveRange.root == this._document.graveyard ) {
  275. const sourceStart = data.sourcePosition;
  276. const howMany = data.range.end.offset - data.range.start.offset;
  277. const sourceRange = Range.createFromPositionAndShift( sourceStart, howMany );
  278. this._fixGraveyardSelection( liveRange, sourceRange );
  279. }
  280. // Whenever a live range from selection changes, fire an event informing about that change.
  281. this.fire( 'change:range', { directChange: false } );
  282. } );
  283. return liveRange;
  284. }
  285. /**
  286. * Updates this selection attributes according to its ranges and the {@link module:engine/model/document~Document model document}.
  287. *
  288. * @protected
  289. * @param {Boolean} clearAll
  290. * @fires change:attribute
  291. */
  292. _updateAttributes( clearAll ) {
  293. const newAttributes = toMap( this._getSurroundingAttributes() );
  294. const oldAttributes = toMap( this.getAttributes() );
  295. if ( clearAll ) {
  296. // If `clearAll` remove all attributes and reset priorities.
  297. this._attributePriority = new Map();
  298. this._attrs = new Map();
  299. } else {
  300. // If not, remove only attributes added with `low` priority.
  301. for ( const [ key, priority ] of this._attributePriority ) {
  302. if ( priority == 'low' ) {
  303. this._attrs.delete( key );
  304. this._attributePriority.delete( key );
  305. }
  306. }
  307. }
  308. this._setAttributesTo( newAttributes, false );
  309. // Let's evaluate which attributes really changed.
  310. const changed = [];
  311. // First, loop through all attributes that are set on selection right now.
  312. // Check which of them are different than old attributes.
  313. for ( const [ newKey, newValue ] of this.getAttributes() ) {
  314. if ( !oldAttributes.has( newKey ) || oldAttributes.get( newKey ) !== newValue ) {
  315. changed.push( newKey );
  316. }
  317. }
  318. // Then, check which of old attributes got removed.
  319. for ( const [ oldKey ] of oldAttributes ) {
  320. if ( !this.hasAttribute( oldKey ) ) {
  321. changed.push( oldKey );
  322. }
  323. }
  324. // Fire event with exact data (fire only if anything changed).
  325. if ( changed.length > 0 ) {
  326. this.fire( 'change:attribute', { attributeKeys: changed, directChange: false } );
  327. }
  328. }
  329. /**
  330. * Generates and returns an attribute key for selection attributes store, basing on original attribute key.
  331. *
  332. * @protected
  333. * @param {String} key Attribute key to convert.
  334. * @returns {String} Converted attribute key, applicable for selection store.
  335. */
  336. static _getStoreAttributeKey( key ) {
  337. return storePrefix + key;
  338. }
  339. /**
  340. * Internal method for setting `DocumentSelection` attribute. Supports attribute priorities (through `directChange`
  341. * parameter).
  342. *
  343. * @private
  344. * @param {String} key Attribute key.
  345. * @param {*} value Attribute value.
  346. * @param {Boolean} [directChange=true] `true` if the change is caused by `Selection` API, `false` if change
  347. * is caused by `Batch` API.
  348. * @returns {Boolean} Whether value has changed.
  349. */
  350. _setAttribute( key, value, directChange = true ) {
  351. const priority = directChange ? 'normal' : 'low';
  352. if ( priority == 'low' && this._attributePriority.get( key ) == 'normal' ) {
  353. // Priority too low.
  354. return false;
  355. }
  356. const oldValue = super.getAttribute( key );
  357. // Don't do anything if value has not changed.
  358. if ( oldValue === value ) {
  359. return false;
  360. }
  361. this._attrs.set( key, value );
  362. // Update priorities map.
  363. this._attributePriority.set( key, priority );
  364. return true;
  365. }
  366. /**
  367. * Internal method for removing `DocumentSelection` attribute. Supports attribute priorities (through `directChange`
  368. * parameter).
  369. *
  370. * @private
  371. * @param {String} key Attribute key.
  372. * @param {Boolean} [directChange=true] `true` if the change is caused by `Selection` API, `false` if change
  373. * is caused by `Batch` API.
  374. * @returns {Boolean} Whether attribute was removed. May not be true if such attributes didn't exist or the
  375. * existing attribute had higher priority.
  376. */
  377. _removeAttribute( key, directChange = true ) {
  378. const priority = directChange ? 'normal' : 'low';
  379. if ( priority == 'low' && this._attributePriority.get( key ) == 'normal' ) {
  380. // Priority too low.
  381. return false;
  382. }
  383. // Don't do anything if value has not changed.
  384. if ( !super.hasAttribute( key ) ) {
  385. return false;
  386. }
  387. this._attrs.delete( key );
  388. // Update priorities map.
  389. this._attributePriority.set( key, priority );
  390. return true;
  391. }
  392. /**
  393. * Internal method for setting multiple `DocumentSelection` attributes. Supports attribute priorities (through
  394. * `directChange` parameter).
  395. *
  396. * @private
  397. * @param {Iterable|Object} attrs Iterable object containing attributes to be set.
  398. * @param {Boolean} [directChange=true] `true` if the change is caused by `Selection` API, `false` if change
  399. * is caused by `Batch` API.
  400. * @returns {Set.<String>} Changed attribute keys.
  401. */
  402. _setAttributesTo( attrs, directChange = true ) {
  403. const changed = new Set();
  404. for ( const [ oldKey, oldValue ] of this.getAttributes() ) {
  405. // Do not remove attribute if attribute with same key and value is about to be set.
  406. if ( attrs.get( oldKey ) === oldValue ) {
  407. continue;
  408. }
  409. // Attribute still might not get removed because of priorities.
  410. if ( this._removeAttribute( oldKey, directChange ) ) {
  411. changed.add( oldKey );
  412. }
  413. }
  414. for ( const [ key, value ] of attrs ) {
  415. // Attribute may not be set because of attributes or because same key/value is already added.
  416. const gotAdded = this._setAttribute( key, value, directChange );
  417. if ( gotAdded ) {
  418. changed.add( key );
  419. }
  420. }
  421. return changed;
  422. }
  423. /**
  424. * Returns an iterator that iterates through all selection attributes stored in current selection's parent.
  425. *
  426. * @private
  427. * @returns {Iterable.<*>}
  428. */
  429. * _getStoredAttributes() {
  430. const selectionParent = this.getFirstPosition().parent;
  431. if ( this.isCollapsed && selectionParent.isEmpty ) {
  432. for ( const key of selectionParent.getAttributeKeys() ) {
  433. if ( key.startsWith( storePrefix ) ) {
  434. const realKey = key.substr( storePrefix.length );
  435. yield [ realKey, selectionParent.getAttribute( key ) ];
  436. }
  437. }
  438. }
  439. }
  440. /**
  441. * Removes attribute with given key from attributes stored in current selection's parent node.
  442. *
  443. * @private
  444. * @param {String} key Key of attribute to remove.
  445. */
  446. _removeStoredAttribute( key ) {
  447. const storeKey = DocumentSelection._getStoreAttributeKey( key );
  448. this._document.batch().removeAttribute( this.anchor.parent, storeKey );
  449. }
  450. /**
  451. * Stores given attribute key and value in current selection's parent node.
  452. *
  453. * @private
  454. * @param {String} key Key of attribute to set.
  455. * @param {*} value Attribute value.
  456. */
  457. _storeAttribute( key, value ) {
  458. const storeKey = DocumentSelection._getStoreAttributeKey( key );
  459. this._document.batch().setAttribute( this.anchor.parent, storeKey, value );
  460. }
  461. /**
  462. * Sets selection attributes stored in current selection's parent node to given set of attributes.
  463. *
  464. * @private
  465. * @param {Iterable|Object} attrs Iterable object containing attributes to be set.
  466. */
  467. _setStoredAttributesTo( attrs ) {
  468. const selectionParent = this.anchor.parent;
  469. const batch = this._document.batch();
  470. for ( const [ oldKey ] of this._getStoredAttributes() ) {
  471. const storeKey = DocumentSelection._getStoreAttributeKey( oldKey );
  472. batch.removeAttribute( selectionParent, storeKey );
  473. }
  474. for ( const [ key, value ] of attrs ) {
  475. const storeKey = DocumentSelection._getStoreAttributeKey( key );
  476. batch.setAttribute( selectionParent, storeKey, value );
  477. }
  478. }
  479. /**
  480. * Checks model text nodes that are closest to the selection's first position and returns attributes of first
  481. * found element. If there are no text nodes in selection's first position parent, it returns selection
  482. * attributes stored in that parent.
  483. *
  484. * @private
  485. * @returns {Iterable.<*>} Collection of attributes.
  486. */
  487. _getSurroundingAttributes() {
  488. const position = this.getFirstPosition();
  489. const schema = this._document.schema;
  490. let attrs = null;
  491. if ( !this.isCollapsed ) {
  492. // 1. If selection is a range...
  493. const range = this.getFirstRange();
  494. // ...look for a first character node in that range and take attributes from it.
  495. for ( const value of range ) {
  496. // If the item is an object, we don't want to get attributes from its children.
  497. if ( value.item.is( 'element' ) && schema.objects.has( value.item.name ) ) {
  498. break;
  499. }
  500. // This is not an optimal solution because of https://github.com/ckeditor/ckeditor5-engine/issues/454.
  501. // It can be done better by using `break;` instead of checking `attrs === null`.
  502. if ( value.type == 'text' && attrs === null ) {
  503. attrs = value.item.getAttributes();
  504. }
  505. }
  506. } else {
  507. // 2. If the selection is a caret or the range does not contain a character node...
  508. const nodeBefore = position.textNode ? position.textNode : position.nodeBefore;
  509. const nodeAfter = position.textNode ? position.textNode : position.nodeAfter;
  510. // ...look at the node before caret and take attributes from it if it is a character node.
  511. attrs = getAttrsIfCharacter( nodeBefore );
  512. // 3. If not, look at the node after caret...
  513. if ( !attrs ) {
  514. attrs = getAttrsIfCharacter( nodeAfter );
  515. }
  516. // 4. If not, try to find the first character on the left, that is in the same node.
  517. if ( !attrs ) {
  518. let node = nodeBefore;
  519. while ( node && !attrs ) {
  520. node = node.previousSibling;
  521. attrs = getAttrsIfCharacter( node );
  522. }
  523. }
  524. // 5. If not found, try to find the first character on the right, that is in the same node.
  525. if ( !attrs ) {
  526. let node = nodeAfter;
  527. while ( node && !attrs ) {
  528. node = node.nextSibling;
  529. attrs = getAttrsIfCharacter( node );
  530. }
  531. }
  532. // 6. If not found, selection should retrieve attributes from parent.
  533. if ( !attrs ) {
  534. attrs = this._getStoredAttributes();
  535. }
  536. }
  537. return attrs;
  538. }
  539. /**
  540. * Fixes a selection range after it ends up in graveyard root.
  541. *
  542. * @private
  543. * @param {module:engine/model/liverange~LiveRange} liveRange The range from selection, that ended up in the graveyard root.
  544. * @param {module:engine/model/range~Range} removedRange Range which removing had caused moving `liveRange` to the graveyard root.
  545. */
  546. _fixGraveyardSelection( liveRange, removedRange ) {
  547. // The start of the removed range is the closest position to the `liveRange` - the original selection range.
  548. // This is a good candidate for a fixed selection range.
  549. const positionCandidate = Position.createFromPosition( removedRange.start );
  550. // Find a range that is a correct selection range and is closest to the start of removed range.
  551. const selectionRange = this._document.getNearestSelectionRange( positionCandidate );
  552. // Remove the old selection range before preparing and adding new selection range. This order is important,
  553. // because new range, in some cases, may intersect with old range (it depends on `getNearestSelectionRange()` result).
  554. const index = this._ranges.indexOf( liveRange );
  555. this._ranges.splice( index, 1 );
  556. liveRange.detach();
  557. // If nearest valid selection range has been found - add it in the place of old range.
  558. if ( selectionRange ) {
  559. // Check the range, convert it to live range, bind events, etc.
  560. const newRange = this._prepareRange( selectionRange );
  561. // Add new range in the place of old range.
  562. this._ranges.splice( index, 0, newRange );
  563. }
  564. // If nearest valid selection range cannot be found - just removing the old range is fine.
  565. // Fire an event informing about selection change.
  566. this.fire( 'change:range', { directChange: false } );
  567. }
  568. }
  569. /**
  570. * @event change:attribute
  571. */
  572. // Helper function for {@link module:engine/model/documentselection~DocumentSelection#_updateAttributes}.
  573. //
  574. // It takes model item, checks whether it is a text node (or text proxy) and, if so, returns it's attributes. If not, returns `null`.
  575. //
  576. // @param {module:engine/model/item~Item|null} node
  577. // @returns {Boolean}
  578. function getAttrsIfCharacter( node ) {
  579. if ( node instanceof TextProxy || node instanceof Text ) {
  580. return node.getAttributes();
  581. }
  582. return null;
  583. }
  584. // Removes selection attributes from element which is not empty anymore.
  585. function clearAttributesStoredInElement( changes, batch, document ) {
  586. // Batch may not be passed to the document#change event in some tests.
  587. // See https://github.com/ckeditor/ckeditor5-engine/issues/1001#issuecomment-314202352
  588. // Ignore also transparent batches because they are... transparent.
  589. if ( !batch || batch.type == 'transparent' ) {
  590. return;
  591. }
  592. const changeParent = changes.range && changes.range.start.parent;
  593. // `changes.range` is not set in case of rename, root and marker operations.
  594. // None of them may lead to the element becoming non-empty.
  595. if ( !changeParent || changeParent.isEmpty ) {
  596. return;
  597. }
  598. document.enqueueChanges( () => {
  599. const storedAttributes = Array.from( changeParent.getAttributeKeys() ).filter( key => key.startsWith( storePrefix ) );
  600. for ( const key of storedAttributes ) {
  601. batch.removeAttribute( changeParent, key );
  602. }
  603. } );
  604. }