insertcontent.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/model/utils/insertcontent
  7. */
  8. import Position from '../position';
  9. import LivePosition from '../liveposition';
  10. import Element from '../element';
  11. import Range from '../range';
  12. import DocumentSelection from '../documentselection';
  13. import Selection from '../selection';
  14. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  15. /**
  16. * Inserts content into the editor (specified selection) as one would expect the paste
  17. * functionality to work.
  18. *
  19. * If an instance of {@link module:engine/model/selection~Selection} is passed as `selectable` it will be modified
  20. * to the insertion selection (equal to a range to be selected after insertion).
  21. *
  22. * If `selectable` is not passed, the content will be inserted using the current selection of the model document.
  23. *
  24. * **Note:** Use {@link module:engine/model/model~Model#insertContent} instead of this function.
  25. * This function is only exposed to be reusable in algorithms which change the {@link module:engine/model/model~Model#insertContent}
  26. * method's behavior.
  27. *
  28. * @param {module:engine/model/model~Model} model The model in context of which the insertion
  29. * should be performed.
  30. * @param {module:engine/model/documentfragment~DocumentFragment|module:engine/model/item~Item} content The content to insert.
  31. * @param {module:engine/model/selection~Selectable} [selectable=model.document.selection]
  32. * Selection into which the content should be inserted.
  33. * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Sets place or offset of the selection.
  34. * @returns {module:engine/model/range~Range} Range which contains all the performed changes. This is a range that, if removed,
  35. * would return the model to the state before the insertion. If no changes were preformed by `insertContent`, returns a range collapsed
  36. * at the insertion position.
  37. */
  38. export default function insertContent( model, content, selectable, placeOrOffset ) {
  39. return model.change( writer => {
  40. let selection;
  41. if ( !selectable ) {
  42. selection = model.document.selection;
  43. } else if ( selectable instanceof Selection || selectable instanceof DocumentSelection ) {
  44. selection = selectable;
  45. } else {
  46. selection = writer.createSelection( selectable, placeOrOffset );
  47. }
  48. if ( !selection.isCollapsed ) {
  49. model.deleteContent( selection, { doNotAutoparagraph: true } );
  50. }
  51. const insertion = new Insertion( model, writer, selection.anchor );
  52. let nodesToInsert;
  53. if ( content.is( 'documentFragment' ) ) {
  54. nodesToInsert = content.getChildren();
  55. } else {
  56. nodesToInsert = [ content ];
  57. }
  58. insertion.handleNodes( nodesToInsert, {
  59. // The set of children being inserted is the only set in this context
  60. // so it's the first and last (it's a hack ;)).
  61. isFirst: true,
  62. isLast: true
  63. } );
  64. const newRange = insertion.getSelectionRange();
  65. /* istanbul ignore else */
  66. if ( newRange ) {
  67. if ( selection instanceof DocumentSelection ) {
  68. writer.setSelection( newRange );
  69. } else {
  70. selection.setTo( newRange );
  71. }
  72. } else {
  73. // We are not testing else because it's a safe check for unpredictable edge cases:
  74. // an insertion without proper range to select.
  75. //
  76. // @if CK_DEBUG // console.warn( 'Cannot determine a proper selection range after insertion.' );
  77. }
  78. const affectedRange = insertion.getAffectedRange() || model.createRange( selection.anchor );
  79. insertion.destroy();
  80. return affectedRange;
  81. } );
  82. }
  83. /**
  84. * Utility class for performing content insertion.
  85. *
  86. * @private
  87. */
  88. class Insertion {
  89. constructor( model, writer, position ) {
  90. /**
  91. * The model in context of which the insertion should be performed.
  92. *
  93. * @member {module:engine/model~Model} #model
  94. */
  95. this.model = model;
  96. /**
  97. * Batch to which operations will be added.
  98. *
  99. * @member {module:engine/controller/writer~Batch} #writer
  100. */
  101. this.writer = writer;
  102. /**
  103. * The position at which (or near which) the next node will be inserted.
  104. *
  105. * @member {module:engine/model/position~Position} #position
  106. */
  107. this.position = position;
  108. /**
  109. * Elements with which the inserted elements can be merged.
  110. *
  111. * <p>x^</p><p>y</p> + <p>z</p> (can merge to <p>x</p>)
  112. * <p>x</p><p>^y</p> + <p>z</p> (can merge to <p>y</p>)
  113. * <p>x^y</p> + <p>z</p> (can merge to <p>xy</p> which will be split during the action,
  114. * so both its pieces will be added to this set)
  115. *
  116. *
  117. * @member {Set} #canMergeWith
  118. */
  119. this.canMergeWith = new Set( [ this.position.parent ] );
  120. /**
  121. * Schema of the model.
  122. *
  123. * @member {module:engine/model/schema~Schema} #schema
  124. */
  125. this.schema = model.schema;
  126. this._filterAttributesOf = [];
  127. /**
  128. * Beginning of the affected range. See {@link module:engine/model/utils/insertcontent~Insertion#getAffectedRange}.
  129. *
  130. * @private
  131. * @member {module:engine/model/liveposition~LivePosition|null} #_affectedStart
  132. */
  133. this._affectedStart = null;
  134. /**
  135. * End of the affected range. See {@link module:engine/model/utils/insertcontent~Insertion#getAffectedRange}.
  136. *
  137. * @private
  138. * @member {module:engine/model/liveposition~LivePosition|null} #_affectedEnd
  139. */
  140. this._affectedEnd = null;
  141. }
  142. /**
  143. * Handles insertion of a set of nodes.
  144. *
  145. * @param {Iterable.<module:engine/model/node~Node>} nodes Nodes to insert.
  146. * @param {Object} parentContext Context in which parent of these nodes was supposed to be inserted.
  147. * If the parent context is passed it means that the parent element was stripped (was not allowed).
  148. */
  149. handleNodes( nodes, parentContext ) {
  150. nodes = Array.from( nodes );
  151. for ( let i = 0; i < nodes.length; i++ ) {
  152. const node = nodes[ i ];
  153. this._handleNode( node, {
  154. isFirst: i === 0 && parentContext.isFirst,
  155. isLast: ( i === ( nodes.length - 1 ) ) && parentContext.isLast
  156. } );
  157. }
  158. // TMP this will become a post-fixer.
  159. this.schema.removeDisallowedAttributes( this._filterAttributesOf, this.writer );
  160. this._filterAttributesOf = [];
  161. }
  162. /**
  163. * Returns range to be selected after insertion.
  164. * Returns `null` if there is no valid range to select after insertion.
  165. *
  166. * @returns {module:engine/model/range~Range|null}
  167. */
  168. getSelectionRange() {
  169. if ( this.nodeToSelect ) {
  170. return Range._createOn( this.nodeToSelect );
  171. }
  172. return this.model.schema.getNearestSelectionRange( this.position );
  173. }
  174. /**
  175. * Returns a range which contains all the performed changes. This is a range that, if removed, would return the model to the state
  176. * before the insertion. Returns `null` if no changes were done.
  177. *
  178. * @returns {module:engine/model/range~Range|null}
  179. */
  180. getAffectedRange() {
  181. if ( !this._affectedStart ) {
  182. return null;
  183. }
  184. return new Range( this._affectedStart, this._affectedEnd );
  185. }
  186. /**
  187. * Destroys `Insertion` instance.
  188. */
  189. destroy() {
  190. if ( this._affectedStart ) {
  191. this._affectedStart.detach();
  192. }
  193. if ( this._affectedEnd ) {
  194. this._affectedEnd.detach();
  195. }
  196. }
  197. /**
  198. * Handles insertion of a single node.
  199. *
  200. * @private
  201. * @param {module:engine/model/node~Node} node
  202. * @param {Object} context
  203. * @param {Boolean} context.isFirst Whether the given node is the first one in the content to be inserted.
  204. * @param {Boolean} context.isLast Whether the given node is the last one in the content to be inserted.
  205. */
  206. _handleNode( node, context ) {
  207. // Let's handle object in a special way.
  208. // * They should never be merged with other elements.
  209. // * If they are not allowed in any of the selection ancestors, they could be either autoparagraphed or totally removed.
  210. if ( this.schema.isObject( node ) ) {
  211. this._handleObject( node, context );
  212. return;
  213. }
  214. // Try to find a place for the given node.
  215. // Split the position.parent's branch up to a point where the node can be inserted.
  216. // If it isn't allowed in the whole branch, then of course don't split anything.
  217. const isAllowed = this._checkAndSplitToAllowedPosition( node, context );
  218. if ( !isAllowed ) {
  219. this._handleDisallowedNode( node, context );
  220. return;
  221. }
  222. this._insert( node );
  223. // After the node was inserted we may try to merge it with its siblings.
  224. // This should happen only if it was the first and/or last of the nodes (so only with boundary nodes)
  225. // and only if the selection was in those elements initially.
  226. //
  227. // E.g.:
  228. // <p>x^</p> + <p>y</p> => <p>x</p><p>y</p> => <p>xy[]</p>
  229. // and:
  230. // <p>x^y</p> + <p>z</p> => <p>x</p>^<p>y</p> + <p>z</p> => <p>x</p><p>z</p><p>y</p> => <p>xz[]y</p>
  231. // but:
  232. // <p>x</p><p>^</p><p>z</p> + <p>y</p> => <p>x</p><p>y</p><p>z</p> (no merging)
  233. // <p>x</p>[<img>]<p>z</p> + <p>y</p> => <p>x</p><p>y</p><p>z</p> (no merging, note: after running deleteContents
  234. // it's exactly the same case as above)
  235. this._mergeSiblingsOf( node, context );
  236. }
  237. /**
  238. * @private
  239. * @param {module:engine/model/element~Element} node The object element.
  240. * @param {Object} context
  241. */
  242. _handleObject( node, context ) {
  243. // Try finding it a place in the tree.
  244. if ( this._checkAndSplitToAllowedPosition( node ) ) {
  245. this._insert( node );
  246. }
  247. // Try autoparagraphing.
  248. else {
  249. this._tryAutoparagraphing( node, context );
  250. }
  251. }
  252. /**
  253. * @private
  254. * @param {module:engine/model/node~Node} node The disallowed node which needs to be handled.
  255. * @param {Object} context
  256. */
  257. _handleDisallowedNode( node, context ) {
  258. // If the node is an element, try inserting its children (strip the parent).
  259. if ( node.is( 'element' ) ) {
  260. this.handleNodes( node.getChildren(), context );
  261. }
  262. // If text is not allowed, try autoparagraphing it.
  263. else {
  264. this._tryAutoparagraphing( node, context );
  265. }
  266. }
  267. /**
  268. * @private
  269. * @param {module:engine/model/node~Node} node The node to insert.
  270. */
  271. _insert( node ) {
  272. /* istanbul ignore if */
  273. if ( !this.schema.checkChild( this.position, node ) ) {
  274. // Algorithm's correctness check. We should never end up here but it's good to know that we did.
  275. // Note that it would often be a silent issue if we insert node in a place where it's not allowed.
  276. /**
  277. * Given node cannot be inserted on the given position.
  278. *
  279. * @error insertcontent-wrong-position
  280. * @param {module:engine/model/node~Node} node Node to insert.
  281. * @param {module:engine/model/position~Position} position Position to insert the node at.
  282. */
  283. throw new CKEditorError(
  284. 'insertcontent-wrong-position: Given node cannot be inserted on the given position.',
  285. this,
  286. { node, position: this.position }
  287. );
  288. }
  289. const livePos = LivePosition.fromPosition( this.position, 'toNext' );
  290. this._setAffectedBoundaries( this.position );
  291. this.writer.insert( node, this.position );
  292. this.position = livePos.toPosition();
  293. livePos.detach();
  294. // The last inserted object should be selected because we can't put a collapsed selection after it.
  295. if ( this.schema.isObject( node ) && !this.schema.checkChild( this.position, '$text' ) ) {
  296. this.nodeToSelect = node;
  297. } else {
  298. this.nodeToSelect = null;
  299. }
  300. this._filterAttributesOf.push( node );
  301. }
  302. /**
  303. * Sets `_affectedStart` and `_affectedEnd` to the given `position`. Should be used before a change is done during insertion process to
  304. * mark the affected range.
  305. *
  306. * This method is used before inserting a node or splitting a parent node. `_affectedStart` and `_affectedEnd` are also changed
  307. * during merging, but the logic there is more complicated so it is left out of this function.
  308. *
  309. * @private
  310. * @param {module:engine/model/position~Position} position
  311. */
  312. _setAffectedBoundaries( position ) {
  313. // Set affected boundaries stickiness so that those position will "expand" when something is inserted in between them:
  314. // <paragraph>Foo][bar</paragraph> -> <paragraph>Foo]xx[bar</paragraph>
  315. // This is why it cannot be a range but two separate positions.
  316. if ( !this._affectedStart ) {
  317. this._affectedStart = LivePosition.fromPosition( position, 'toPrevious' );
  318. }
  319. // If `_affectedEnd` is before the new boundary position, expand `_affectedEnd`. This can happen if first inserted node was
  320. // inserted into the parent but the next node is moved-out of that parent:
  321. // (1) <paragraph>Foo][</paragraph> -> <paragraph>Foo]xx[</paragraph>
  322. // (2) <paragraph>Foo]xx[</paragraph> -> <paragraph>Foo]xx</paragraph><widget></widget>[
  323. if ( !this._affectedEnd || this._affectedEnd.isBefore( position ) ) {
  324. if ( this._affectedEnd ) {
  325. this._affectedEnd.detach();
  326. }
  327. this._affectedEnd = LivePosition.fromPosition( position, 'toNext' );
  328. }
  329. }
  330. /**
  331. * @private
  332. * @param {module:engine/model/node~Node} node The node which could potentially be merged.
  333. * @param {Object} context
  334. */
  335. _mergeSiblingsOf( node, context ) {
  336. if ( !( node instanceof Element ) ) {
  337. return;
  338. }
  339. const mergeLeft = this._canMergeLeft( node, context );
  340. const mergeRight = this._canMergeRight( node, context );
  341. const mergePosLeft = LivePosition._createBefore( node );
  342. mergePosLeft.stickiness = 'toNext';
  343. const mergePosRight = LivePosition._createAfter( node );
  344. mergePosRight.stickiness = 'toNext';
  345. if ( mergeLeft ) {
  346. const livePosition = LivePosition.fromPosition( this.position );
  347. livePosition.stickiness = 'toNext';
  348. // If `_affectedStart` is sames as merge position, it means that the element "marked" by `_affectedStart` is going to be
  349. // removed and its contents will be moved. This won't transform `LivePosition` so `_affectedStart` needs to be moved
  350. // by hand to properly reflect affected range. (Due to `_affectedStart` and `_affectedEnd` stickiness, the "range" is
  351. // shown as `][`).
  352. //
  353. // Example - insert `<paragraph>Abc</paragraph><paragraph>Xyz</paragraph>` at the end of `<paragraph>Foo^</paragraph>`:
  354. //
  355. // <paragraph>Foo</paragraph><paragraph>Bar</paragraph> -->
  356. // <paragraph>Foo</paragraph>]<paragraph>Abc</paragraph><paragraph>Xyz</paragraph>[<paragraph>Bar</paragraph> -->
  357. // <paragraph>Foo]Abc</paragraph><paragraph>Xyz</paragraph>[<paragraph>Bar</paragraph>
  358. //
  359. // Note, that if we are here then something must have been inserted, so `_affectedStart` and `_affectedEnd` have to be set.
  360. if ( this._affectedStart.isEqual( mergePosLeft ) ) {
  361. this._affectedStart.detach();
  362. this._affectedStart = LivePosition._createAt( mergePosLeft.nodeBefore, 'end', 'toPrevious' );
  363. }
  364. this.writer.merge( mergePosLeft );
  365. // If only one element (the merged one) is in the "affected range", also move the affected range end appropriately.
  366. //
  367. // Example - insert `<paragraph>Abc</paragraph>` at the of `<paragraph>Foo^</paragraph>`:
  368. //
  369. // <paragraph>Foo</paragraph><paragraph>Bar</paragraph> -->
  370. // <paragraph>Foo</paragraph>]<paragraph>Abc</paragraph>[<paragraph>Bar</paragraph> -->
  371. // <paragraph>Foo]Abc</paragraph>[<paragraph>Bar</paragraph> -->
  372. // <paragraph>Foo]Abc[</paragraph><paragraph>Bar</paragraph>
  373. if ( mergePosLeft.isEqual( this._affectedEnd ) && context.isLast ) {
  374. this._affectedEnd.detach();
  375. this._affectedEnd = LivePosition._createAt( mergePosLeft.nodeBefore, 'end', 'toNext' );
  376. }
  377. this.position = livePosition.toPosition();
  378. livePosition.detach();
  379. }
  380. if ( mergeRight ) {
  381. /* istanbul ignore if */
  382. if ( !this.position.isEqual( mergePosRight ) ) {
  383. // Algorithm's correctness check. We should never end up here but it's good to know that we did.
  384. // At this point the insertion position should be after the node we'll merge. If it isn't,
  385. // it should need to be secured as in the left merge case.
  386. /**
  387. * An internal error occured during merging insertion content with siblings.
  388. * The insertion position should equal to the merge position.
  389. *
  390. * @error insertcontent-invalid-insertion-position
  391. */
  392. throw new CKEditorError( 'insertcontent-invalid-insertion-position', this );
  393. }
  394. // Move the position to the previous node, so it isn't moved to the graveyard on merge.
  395. // <p>x</p>[]<p>y</p> => <p>x[]</p><p>y</p>
  396. this.position = Position._createAt( mergePosRight.nodeBefore, 'end' );
  397. // OK: <p>xx[]</p> + <p>yy</p> => <p>xx[]yy</p> (when sticks to previous)
  398. // NOK: <p>xx[]</p> + <p>yy</p> => <p>xxyy[]</p> (when sticks to next)
  399. const livePosition = LivePosition.fromPosition( this.position, 'toPrevious' );
  400. // See comment above on moving `_affectedStart`.
  401. if ( this._affectedEnd.isEqual( mergePosRight ) ) {
  402. this._affectedEnd.detach();
  403. this._affectedEnd = LivePosition._createAt( mergePosRight.nodeBefore, 'end', 'toNext' );
  404. }
  405. this.writer.merge( mergePosRight );
  406. // See comment above on moving `_affectedStart`.
  407. if ( mergePosRight.getShiftedBy( -1 ).isEqual( this._affectedStart ) && context.isFirst ) {
  408. this._affectedStart.detach();
  409. this._affectedStart = LivePosition._createAt( mergePosRight.nodeBefore, 0, 'toPrevious' );
  410. }
  411. this.position = livePosition.toPosition();
  412. livePosition.detach();
  413. }
  414. if ( mergeLeft || mergeRight ) {
  415. // After merge elements that were marked by _insert() to be filtered might be gone so
  416. // we need to mark the new container.
  417. this._filterAttributesOf.push( this.position.parent );
  418. }
  419. mergePosLeft.detach();
  420. mergePosRight.detach();
  421. }
  422. /**
  423. * Checks whether specified node can be merged with previous sibling element.
  424. *
  425. * @private
  426. * @param {module:engine/model/node~Node} node The node which could potentially be merged.
  427. * @param {Object} context
  428. * @returns {Boolean}
  429. */
  430. _canMergeLeft( node, context ) {
  431. const previousSibling = node.previousSibling;
  432. return context.isFirst &&
  433. ( previousSibling instanceof Element ) &&
  434. this.canMergeWith.has( previousSibling ) &&
  435. this.model.schema.checkMerge( previousSibling, node );
  436. }
  437. /**
  438. * Checks whether specified node can be merged with next sibling element.
  439. *
  440. * @private
  441. * @param {module:engine/model/node~Node} node The node which could potentially be merged.
  442. * @param {Object} context
  443. * @returns {Boolean}
  444. */
  445. _canMergeRight( node, context ) {
  446. const nextSibling = node.nextSibling;
  447. return context.isLast &&
  448. ( nextSibling instanceof Element ) &&
  449. this.canMergeWith.has( nextSibling ) &&
  450. this.model.schema.checkMerge( node, nextSibling );
  451. }
  452. /**
  453. * Tries wrapping the node in a new paragraph and inserting it this way.
  454. *
  455. * @private
  456. * @param {module:engine/model/node~Node} node The node which needs to be autoparagraphed.
  457. * @param {Object} context
  458. */
  459. _tryAutoparagraphing( node, context ) {
  460. const paragraph = this.writer.createElement( 'paragraph' );
  461. // Do not autoparagraph if the paragraph won't be allowed there,
  462. // cause that would lead to an infinite loop. The paragraph would be rejected in
  463. // the next _handleNode() call and we'd be here again.
  464. if ( this._getAllowedIn( paragraph, this.position.parent ) && this.schema.checkChild( paragraph, node ) ) {
  465. paragraph._appendChild( node );
  466. this._handleNode( paragraph, context );
  467. }
  468. }
  469. /**
  470. * @private
  471. * @param {module:engine/model/node~Node} node
  472. * @returns {Boolean} Whether an allowed position was found.
  473. * `false` is returned if the node isn't allowed at any position up in the tree, `true` if was.
  474. */
  475. _checkAndSplitToAllowedPosition( node ) {
  476. const allowedIn = this._getAllowedIn( node, this.position.parent );
  477. if ( !allowedIn ) {
  478. return false;
  479. }
  480. while ( allowedIn != this.position.parent ) {
  481. // If a parent which we'd need to leave is a limit element, break.
  482. if ( this.schema.isLimit( this.position.parent ) ) {
  483. return false;
  484. }
  485. if ( this.position.isAtStart ) {
  486. // If insertion position is at the beginning of the parent, move it out instead of splitting.
  487. // <p>^Foo</p> -> ^<p>Foo</p>
  488. const parent = this.position.parent;
  489. this.position = this.writer.createPositionBefore( parent );
  490. // Special case – parent is empty (<p>^</p>).
  491. //
  492. // 1. parent.isEmpty
  493. // We can remove the element after moving insertion position out of it.
  494. //
  495. // 2. parent.parent === allowedIn
  496. // However parent should remain in place when allowed element is above limit element in document tree.
  497. // For example there shouldn't be allowed to remove empty paragraph from tableCell, when is pasted
  498. // content allowed in $root.
  499. if ( parent.isEmpty && parent.parent === allowedIn ) {
  500. this.writer.remove( parent );
  501. }
  502. } else if ( this.position.isAtEnd ) {
  503. // If insertion position is at the end of the parent, move it out instead of splitting.
  504. // <p>Foo^</p> -> <p>Foo</p>^
  505. this.position = this.writer.createPositionAfter( this.position.parent );
  506. } else {
  507. const tempPos = this.writer.createPositionAfter( this.position.parent );
  508. this._setAffectedBoundaries( this.position );
  509. this.writer.split( this.position );
  510. this.position = tempPos;
  511. this.canMergeWith.add( this.position.nodeAfter );
  512. }
  513. }
  514. return true;
  515. }
  516. /**
  517. * Gets the element in which the given node is allowed. It checks the passed element and all its ancestors.
  518. *
  519. * @private
  520. * @param {module:engine/model/node~Node} node The node to check.
  521. * @param {module:engine/model/element~Element} element The element in which the node's correctness should be checked.
  522. * @returns {module:engine/model/element~Element|null}
  523. */
  524. _getAllowedIn( node, element ) {
  525. if ( this.schema.checkChild( element, node ) ) {
  526. return element;
  527. }
  528. if ( element.parent ) {
  529. return this._getAllowedIn( node, element.parent );
  530. }
  531. return null;
  532. }
  533. }