documentselection.js 21 KB

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