8
0

schema.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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/schema
  7. */
  8. import Position from './position';
  9. import Element from './element';
  10. import clone from '@ckeditor/ckeditor5-utils/src/lib/lodash/clone';
  11. import isArray from '@ckeditor/ckeditor5-utils/src/lib/lodash/isArray';
  12. import isString from '@ckeditor/ckeditor5-utils/src/lib/lodash/isString';
  13. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. import Range from './range';
  15. /**
  16. * Schema is a definition of the structure of the document. It allows to define which tree model items (element, text, etc.)
  17. * can be nested within which ones and which attributes can be applied to them. It's created during the run-time of the application,
  18. * typically by features. Also, the features can query the schema to learn what structure is allowed and act accordingly.
  19. *
  20. * For instance, if a feature wants to define that an attribute bold is allowed on the text it needs to register this rule like this:
  21. *
  22. * editor.document.schema.allow( '$text', 'bold' );
  23. *
  24. * Note: items prefixed with `$` are special group of items. By default, `Schema` defines three special items:
  25. *
  26. * * `$inline` represents all inline elements,
  27. * * `$text` is a sub-group of `$inline` and represents text nodes,
  28. * * `$block` represents block elements,
  29. * * `$root` represents default editing roots (those that allow only `$block`s inside them).
  30. *
  31. * When registering an item it's possible to tell that this item should inherit from some other existing item.
  32. * E.g. `p` can inherit from `$block`, so whenever given attribute is allowed on the `$block` it will automatically be
  33. * also allowed on the `p` element. By default, `$text` item already inherits from `$inline`.
  34. */
  35. export default class Schema {
  36. /**
  37. * Creates Schema instance.
  38. */
  39. constructor() {
  40. /**
  41. * Names of elements which have "object" nature. This means that these
  42. * elements should be treated as whole, never merged, can be selected from outside, etc.
  43. * Just like images, placeholder widgets, etc.
  44. *
  45. * @member {Set.<String>} module:engine/model/schema~Schema#objects
  46. */
  47. this.objects = new Set();
  48. /**
  49. * Names of elements to which editing operations should be limited.
  50. * For example, the <kbd>Enter</kbd> should not split such elements and
  51. * <kbd>Backspace</kbd> should not be able to leave or modify such elements.
  52. *
  53. * @member {Set.<String>} module:engine/model/schema~Schema#limits
  54. */
  55. this.limits = new Set();
  56. /**
  57. * Schema items registered in the schema.
  58. *
  59. * @private
  60. * @member {Map} module:engine/model/schema~Schema#_items
  61. */
  62. this._items = new Map();
  63. /**
  64. * Description of what entities are a base for given entity.
  65. *
  66. * @private
  67. * @member {Map} module:engine/model/schema~Schema#_extensionChains
  68. */
  69. this._extensionChains = new Map();
  70. // Register some default abstract entities.
  71. this.registerItem( '$root' );
  72. this.registerItem( '$block' );
  73. this.registerItem( '$inline' );
  74. this.registerItem( '$text', '$inline' );
  75. this.allow( { name: '$block', inside: '$root' } );
  76. this.allow( { name: '$inline', inside: '$block' } );
  77. // TMP!
  78. // Create an "all allowed" context in the schema for processing the pasted content.
  79. // Read: https://github.com/ckeditor/ckeditor5-engine/issues/638#issuecomment-255086588
  80. this.registerItem( '$clipboardHolder', '$root' );
  81. this.allow( { name: '$inline', inside: '$clipboardHolder' } );
  82. }
  83. /**
  84. * Allows given query in the schema.
  85. *
  86. * // Allow text with bold attribute in all P elements.
  87. * schema.registerItem( 'p', '$block' );
  88. * schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } );
  89. *
  90. * // Allow header in Ps that are in DIVs
  91. * schema.registerItem( 'header', '$block' );
  92. * schema.registerItem( 'div', '$block' );
  93. * schema.allow( { name: 'header', inside: 'div p' } ); // inside: [ 'div', 'p' ] would also work.
  94. *
  95. * @param {module:engine/model/schema~SchemaQuery} query Allowed query.
  96. */
  97. allow( query ) {
  98. this._getItem( query.name ).allow( Schema._normalizeQueryPath( query.inside ), query.attributes );
  99. }
  100. /**
  101. * Disallows given query in the schema.
  102. *
  103. * @see #allow
  104. * @param {module:engine/model/schema~SchemaQuery} query Disallowed query.
  105. */
  106. disallow( query ) {
  107. this._getItem( query.name ).disallow( Schema._normalizeQueryPath( query.inside ), query.attributes );
  108. }
  109. /**
  110. * Makes a requirement in schema that entity represented by given item has to have given set of attributes. Some
  111. * elements in the model might require some attributes to be set. If multiple sets of attributes are required it
  112. * is enough that the entity fulfills only one set.
  113. *
  114. * // "a" element must either have "href" attribute or "name" attribute
  115. * schema.requireAttributes( 'a', [ 'href' ] );
  116. * schema.requireAttributes( 'a', [ 'name' ] );
  117. * // "img" element must have both "src" and "alt" attributes
  118. * schema.requireAttributes( 'img', [ 'src', 'alt' ] );
  119. *
  120. * @param {String} name Entity name.
  121. * @param {Array.<String>} attributes Attributes that has to be set on the entity to make it valid.
  122. */
  123. requireAttributes( name, attributes ) {
  124. this._getItem( name ).requireAttributes( attributes );
  125. }
  126. /**
  127. * Checks whether given query is allowed in schema.
  128. *
  129. * // Check whether bold text is allowed in header element.
  130. * let query = {
  131. * name: '$text',
  132. * attributes: 'bold',
  133. * inside: 'header'
  134. * };
  135. * if ( schema.check( query ) ) { ... }
  136. *
  137. * // Check whether bold and italic text can be placed at caret position.
  138. * let caretPos = editor.document.selection.getFirstPosition();
  139. * let query = {
  140. * name: '$text',
  141. * attributes: [ 'bold', 'italic' ],
  142. * inside: caretPos
  143. * };
  144. * if ( schema.check( query ) ) { ... }
  145. *
  146. * // Check whether image with alt, src and title is allowed in given elements path.
  147. * let quoteElement = new Element( 'quote' );
  148. * let query = {
  149. * name: 'img',
  150. * attributes: [ 'alt', 'src', 'title' ],
  151. * // It is possible to mix strings with elements.
  152. * // Query will check whether "img" can be inside "quoteElement" that is inside a block element.
  153. * inside: [ '$block', quoteElement ]
  154. * };
  155. * if ( schema.check( query ) ) { ... }
  156. *
  157. * @param {module:engine/model/schema~SchemaQuery} query Query to check.
  158. * @returns {Boolean} `true` if given query is allowed in schema, `false` otherwise.
  159. */
  160. check( query ) {
  161. if ( !this.hasItem( query.name ) ) {
  162. return false;
  163. }
  164. // If attributes property is a string or undefined, wrap it in an array for easier processing.
  165. if ( !isArray( query.attributes ) ) {
  166. query.attributes = [ query.attributes ];
  167. } else if ( query.attributes.length === 0 ) {
  168. // To simplify algorithms, when a SchemaItem path is added "without" attribute, it is added with
  169. // attribute equal to undefined. This means that algorithms can work the same way for specified attributes
  170. // and no-atrtibutes, but we have to fill empty array with "fake" undefined value for algorithms reasons.
  171. query.attributes.push( undefined );
  172. }
  173. // Normalize the path to an array of strings.
  174. const path = Schema._normalizeQueryPath( query.inside );
  175. // Get extension chain of given item and retrieve all schema items that are extended by given item.
  176. const schemaItems = this._extensionChains.get( query.name ).map( name => {
  177. return this._getItem( name );
  178. } );
  179. // First check if the query meets at required attributes for this item.
  180. if ( !this._getItem( query.name )._checkRequiredAttributes( query.attributes ) ) {
  181. return false;
  182. }
  183. // If there is matching disallow path, this query is not valid with schema.
  184. for ( const attribute of query.attributes ) {
  185. for ( const schemaItem of schemaItems ) {
  186. if ( schemaItem._hasMatchingPath( 'disallow', path, attribute ) ) {
  187. return false;
  188. }
  189. }
  190. }
  191. // At this point, the query is not disallowed.
  192. // If there are correct allow paths that match the query, this query is valid with schema.
  193. // Since we are supporting multiple attributes, we have to make sure that if attributes are set,
  194. // we have allowed paths for all of them.
  195. // Keep in mind that if the query has no attributes, query.attribute was converted to an array
  196. // with a single `undefined` value. This fits the algorithm well.
  197. for ( const attribute of query.attributes ) {
  198. let matched = false;
  199. for ( const schemaItem of schemaItems ) {
  200. if ( schemaItem._hasMatchingPath( 'allow', path, attribute ) ) {
  201. matched = true;
  202. break;
  203. }
  204. }
  205. // The attribute has not been matched, so it is not allowed by any schema item.
  206. // The query is disallowed.
  207. if ( !matched ) {
  208. return false;
  209. }
  210. }
  211. return true;
  212. }
  213. /**
  214. * Checks whether there is an item registered under given name in schema.
  215. *
  216. * @param itemName
  217. * @returns {Boolean}
  218. */
  219. hasItem( itemName ) {
  220. return this._items.has( itemName );
  221. }
  222. /**
  223. * Registers given item name in schema.
  224. *
  225. * // Register P element that should be treated like all block elements.
  226. * schema.registerItem( 'p', '$block' );
  227. *
  228. * @param {String} itemName Name to register.
  229. * @param [isExtending] If set, new item will extend item with given name.
  230. */
  231. registerItem( itemName, isExtending ) {
  232. if ( this.hasItem( itemName ) ) {
  233. /**
  234. * Item with specified name already exists in schema.
  235. *
  236. * @error model-schema-item-exists
  237. */
  238. throw new CKEditorError( 'model-schema-item-exists: Item with specified name already exists in schema.' );
  239. }
  240. if ( !!isExtending && !this.hasItem( isExtending ) ) {
  241. /**
  242. * Item with specified name does not exist in schema.
  243. *
  244. * @error model-schema-no-item
  245. */
  246. throw new CKEditorError( 'model-schema-no-item: Item with specified name does not exist in schema.' );
  247. }
  248. // Create new SchemaItem and add it to the items store.
  249. this._items.set( itemName, new SchemaItem( this ) );
  250. // Create an extension chain.
  251. // Extension chain has all item names that should be checked when that item is on path to check.
  252. // This simply means, that if item is not extending anything, it should have only itself in it's extension chain.
  253. // Since extending is not dynamic, we can simply get extension chain of extended item and expand it with registered name,
  254. // if the registered item is extending something.
  255. const chain = this.hasItem( isExtending ) ? this._extensionChains.get( isExtending ).concat( itemName ) : [ itemName ];
  256. this._extensionChains.set( itemName, chain );
  257. }
  258. /**
  259. * Checks whether item of given name is extending item of another given name.
  260. *
  261. * @param {String} childItemName Name of the child item.
  262. * @param {String} parentItemName Name of the parent item.
  263. * @returns {Boolean} `true` if child item extends parent item, `false` otherwise.
  264. */
  265. itemExtends( childItemName, parentItemName ) {
  266. if ( !this.hasItem( childItemName ) || !this.hasItem( parentItemName ) ) {
  267. /**
  268. * Item with specified name does not exist in schema.
  269. *
  270. * @error model-schema-no-item
  271. */
  272. throw new CKEditorError( 'model-schema-no-item: Item with specified name does not exist in schema.' );
  273. }
  274. const chain = this._extensionChains.get( childItemName );
  275. return chain.some( itemName => itemName == parentItemName );
  276. }
  277. /**
  278. * Checks whether the attribute is allowed in selection:
  279. *
  280. * * if the selection is not collapsed, then checks if the attribute is allowed on any of nodes in that range,
  281. * * if the selection is collapsed, then checks if on the selection position there's a text with the
  282. * specified attribute allowed.
  283. *
  284. * @param {module:engine/model/selection~Selection} selection Selection which will be checked.
  285. * @param {String} attribute The name of the attribute to check.
  286. * @returns {Boolean}
  287. */
  288. checkAttributeInSelection( selection, attribute ) {
  289. if ( selection.isCollapsed ) {
  290. // Check whether schema allows for a text with the attribute in the selection.
  291. return this.check( { name: '$text', inside: selection.getFirstPosition(), attributes: attribute } );
  292. } else {
  293. const ranges = selection.getRanges();
  294. // For all ranges, check nodes in them until you find a node that is allowed to have the attribute.
  295. for ( const range of ranges ) {
  296. for ( const value of range ) {
  297. // If returned item does not have name property, it is a TextFragment.
  298. const name = value.item.name || '$text';
  299. if ( this.check( { name, inside: value.previousPosition, attributes: attribute } ) ) {
  300. // If we found a node that is allowed to have the attribute, return true.
  301. return true;
  302. }
  303. }
  304. }
  305. }
  306. // If we haven't found such node, return false.
  307. return false;
  308. }
  309. /**
  310. * Transforms the given set ranges into a set of ranges where the given attribute is allowed (and can be applied).
  311. *
  312. * @param {Array.<module:engine/model/range~Range>} ranges Ranges to be validated.
  313. * @param {String} attribute The name of the attribute to check.
  314. * @returns {Array.<module:engine/model/range~Range>} Ranges in which the attribute is allowed.
  315. */
  316. getValidRanges( ranges, attribute ) {
  317. const validRanges = [];
  318. for ( const range of ranges ) {
  319. let last = range.start;
  320. let from = range.start;
  321. const to = range.end;
  322. for ( const value of range.getWalker() ) {
  323. const name = value.item.name || '$text';
  324. const itemPosition = Position.createBefore( value.item );
  325. if ( !this.check( { name, inside: itemPosition, attributes: attribute } ) ) {
  326. if ( !from.isEqual( last ) ) {
  327. validRanges.push( new Range( from, last ) );
  328. }
  329. from = value.nextPosition;
  330. }
  331. last = value.nextPosition;
  332. }
  333. if ( from && !from.isEqual( to ) ) {
  334. validRanges.push( new Range( from, to ) );
  335. }
  336. }
  337. return validRanges;
  338. }
  339. /**
  340. * Returns {@link module:engine/model/schema~SchemaItem schema item} that was registered in the schema under given name.
  341. * If item has not been found, throws error.
  342. *
  343. * @private
  344. * @param {String} itemName Name to look for in schema.
  345. * @returns {module:engine/model/schema~SchemaItem} Schema item registered under given name.
  346. */
  347. _getItem( itemName ) {
  348. if ( !this.hasItem( itemName ) ) {
  349. /**
  350. * Item with specified name does not exist in schema.
  351. *
  352. * @error model-schema-no-item
  353. */
  354. throw new CKEditorError( 'model-schema-no-item: Item with specified name does not exist in schema.' );
  355. }
  356. return this._items.get( itemName );
  357. }
  358. /**
  359. * Normalizes a path to an entity by converting it from {@link module:engine/model/schema~SchemaPath} to an array of strings.
  360. *
  361. * @protected
  362. * @param {module:engine/model/schema~SchemaPath} path Path to normalize.
  363. * @returns {Array.<String>} Normalized path.
  364. */
  365. static _normalizeQueryPath( path ) {
  366. let normalized = [];
  367. if ( isArray( path ) ) {
  368. for ( const pathItem of path ) {
  369. if ( pathItem instanceof Element ) {
  370. normalized.push( pathItem.name );
  371. } else if ( isString( pathItem ) ) {
  372. normalized.push( pathItem );
  373. }
  374. }
  375. } else if ( path instanceof Position ) {
  376. let parent = path.parent;
  377. while ( parent !== null ) {
  378. normalized.push( parent.name );
  379. parent = parent.parent;
  380. }
  381. normalized.reverse();
  382. } else if ( isString( path ) ) {
  383. normalized = path.split( ' ' );
  384. }
  385. return normalized;
  386. }
  387. }
  388. /**
  389. * SchemaItem is a singular registry item in {@link module:engine/model/schema~Schema} that groups and holds allow/disallow rules for
  390. * one entity. This class is used internally in {@link module:engine/model/schema~Schema} and should not be used outside it.
  391. *
  392. * @see module:engine/model/schema~Schema
  393. * @protected
  394. */
  395. export class SchemaItem {
  396. /**
  397. * Creates SchemaItem instance.
  398. *
  399. * @param {module:engine/model/schema~Schema} schema Schema instance that owns this item.
  400. */
  401. constructor( schema ) {
  402. /**
  403. * Schema instance that owns this item.
  404. *
  405. * @private
  406. * @member {module:engine/model/schema~Schema} module:engine/model/schema~SchemaItem#_schema
  407. */
  408. this._schema = schema;
  409. /**
  410. * Paths in which the entity, represented by this item, is allowed.
  411. *
  412. * @private
  413. * @member {Array} module:engine/model/schema~SchemaItem#_allowed
  414. */
  415. this._allowed = [];
  416. /**
  417. * Paths in which the entity, represented by this item, is disallowed.
  418. *
  419. * @private
  420. * @member {Array} module:engine/model/schema~SchemaItem#_disallowed
  421. */
  422. this._disallowed = [];
  423. /**
  424. * Attributes that are required by the entity represented by this item.
  425. *
  426. * @protected
  427. * @member {Array} module:engine/model/schema~SchemaItem#_requiredAttributes
  428. */
  429. this._requiredAttributes = [];
  430. }
  431. /**
  432. * Allows entity, represented by this item, to be in given path.
  433. *
  434. * @param {Array.<String>} path Path in which entity is allowed.
  435. * @param {Array.<String>|String} [attributes] If set, this path will be used only for entities that have attribute(s) with this key.
  436. */
  437. allow( path, attributes ) {
  438. this._addPath( '_allowed', path, attributes );
  439. }
  440. /**
  441. * Disallows entity, represented by this item, to be in given path.
  442. *
  443. * @param {Array.<String>} path Path in which entity is disallowed.
  444. * @param {Array.<String>|String} [attributes] If set, this path will be used only for entities that have an attribute(s) with this key.
  445. */
  446. disallow( path, attributes ) {
  447. this._addPath( '_disallowed', path, attributes );
  448. }
  449. /**
  450. * Specifies that the entity, to be valid, requires given attributes set. It is possible to register multiple
  451. * different attributes set. If there are more than one attributes set required, the entity will be valid if
  452. * at least one of them is fulfilled.
  453. *
  454. * @param {Array.<String>} attributes Attributes that has to be set on the entity to make it valid.
  455. */
  456. requireAttributes( attributes ) {
  457. this._requiredAttributes.push( attributes );
  458. }
  459. /**
  460. * Custom toJSON method to solve child-parent circular dependencies.
  461. *
  462. * @returns {Object} Clone of this object with the parent property replaced with its name.
  463. */
  464. toJSON() {
  465. const json = clone( this );
  466. // Due to circular references we need to remove parent reference.
  467. json._schema = '[model.Schema]';
  468. return json;
  469. }
  470. /**
  471. * Adds path to the SchemaItem instance.
  472. *
  473. * @private
  474. * @param {String} member Name of the array member into which the path will be added. Possible values are `_allowed` or `_disallowed`.
  475. * @param {Array.<String>} path Path to add.
  476. * @param {Array.<String>|String} [attributes] If set, this path will be used only for entities that have attribute(s) with this key.
  477. */
  478. _addPath( member, path, attributes ) {
  479. path = path.slice();
  480. if ( !isArray( attributes ) ) {
  481. attributes = [ attributes ];
  482. }
  483. for ( const attribute of attributes ) {
  484. this[ member ].push( { path, attribute } );
  485. }
  486. }
  487. /**
  488. * Returns all paths of given type that were previously registered in the item.
  489. *
  490. * @private
  491. * @param {String} type Paths' type. Possible values are `allow` or `disallow`.
  492. * @param {String} [attribute] If set, only paths registered for given attribute will be returned.
  493. * @returns {Array} Paths registered in the item.
  494. */
  495. _getPaths( type, attribute ) {
  496. const source = type === 'allow' ? this._allowed : this._disallowed;
  497. const paths = [];
  498. for ( const item of source ) {
  499. if ( item.attribute === attribute ) {
  500. paths.push( item.path );
  501. }
  502. }
  503. return paths;
  504. }
  505. /**
  506. * Checks whether given set of attributes fulfills required attributes of this item.
  507. *
  508. * @protected
  509. * @see module:engine/model/schema~SchemaItem#requireAttributes
  510. * @param {Array.<String>} attributesToCheck Attributes to check.
  511. * @returns {Boolean} `true` if given set or attributes fulfills required attributes, `false` otherwise.
  512. */
  513. _checkRequiredAttributes( attributesToCheck ) {
  514. let found = true;
  515. for ( const attributeSet of this._requiredAttributes ) {
  516. found = true;
  517. for ( const attribute of attributeSet ) {
  518. if ( attributesToCheck.indexOf( attribute ) == -1 ) {
  519. found = false;
  520. break;
  521. }
  522. }
  523. if ( found ) {
  524. break;
  525. }
  526. }
  527. return found;
  528. }
  529. /**
  530. * Checks whether this item has any registered path of given type that matches the provided path.
  531. *
  532. * @protected
  533. * @param {String} type Paths' type. Possible values are `allow` or `disallow`.
  534. * @param {Array.<String>} pathToCheck Path to check.
  535. * @param {String} [attribute] If set, only paths registered for given attribute will be checked.
  536. * @returns {Boolean} `true` if item has any registered matching path, `false` otherwise.
  537. */
  538. _hasMatchingPath( type, pathToCheck, attribute ) {
  539. const registeredPaths = this._getPaths( type, attribute );
  540. for ( const registeredPathPath of registeredPaths ) {
  541. if ( matchPaths( this._schema, pathToCheck, registeredPathPath ) ) {
  542. return true;
  543. }
  544. }
  545. return false;
  546. }
  547. }
  548. /**
  549. * Object with query used by {@link module:engine/model/schema~Schema} to query schema or add allow/disallow rules to schema.
  550. *
  551. * @typedef {Object} module:engine/model/schema~SchemaQuery
  552. * @property {String} name Entity name.
  553. * @property {module:engine/model/schema~SchemaPath} inside Path inside which the entity is placed.
  554. * @property {Array.<String>|String} [attributes] If set, the query applies only to entities that has attribute(s) with given key.
  555. */
  556. /**
  557. * Path to an entity, begins from the top-most ancestor. Can be passed in multiple formats. Internally, normalized to
  558. * an array of strings. If string is passed, entities from the path should be divided by ` ` (space character). If
  559. * an array is passed, unrecognized items are skipped. If position is passed, it is assumed that the entity is at given position.
  560. *
  561. * @typedef {String|Array.<String|module:engine/model/element~Element>|module:engine/model/position~Position}
  562. * module:engine/model/schema~SchemaPath
  563. */
  564. // Checks whether the given pathToCheck and registeredPath right ends match.
  565. //
  566. // pathToCheck: C, D
  567. // registeredPath: A, B, C, D
  568. // result: OK
  569. //
  570. // pathToCheck: A, B, C
  571. // registeredPath: A, B, C, D
  572. // result: NOK
  573. //
  574. // Note – when matching paths, element extension chains (inheritance) are taken into consideration.
  575. //
  576. // @param {Schema} schema
  577. // @param {Array.<String>} pathToCheck
  578. // @param {Array.<String>} registeredPath
  579. function matchPaths( schema, pathToCheck, registeredPath ) {
  580. // Start checking from the right end of both tables.
  581. let registeredPathIndex = registeredPath.length - 1;
  582. let pathToCheckIndex = pathToCheck.length - 1;
  583. // And finish once reaching an end of the shorter table.
  584. while ( registeredPathIndex >= 0 && pathToCheckIndex >= 0 ) {
  585. const checkName = pathToCheck[ pathToCheckIndex ];
  586. // Fail when checking a path which contains element which aren't even registered to the schema.
  587. if ( !schema.hasItem( checkName ) ) {
  588. return false;
  589. }
  590. const extChain = schema._extensionChains.get( checkName );
  591. if ( extChain.includes( registeredPath[ registeredPathIndex ] ) ) {
  592. registeredPathIndex--;
  593. pathToCheckIndex--;
  594. } else {
  595. return false;
  596. }
  597. }
  598. return true;
  599. }