schema.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. import Schema from 'ckeditor5/engine/model/schema.js';
  7. import { SchemaItem as SchemaItem } from 'ckeditor5/engine/model/schema.js';
  8. import Document from 'ckeditor5/engine/model/document.js';
  9. import Element from 'ckeditor5/engine/model/element.js';
  10. import Position from 'ckeditor5/engine/model/position.js';
  11. import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
  12. import testUtils from 'tests/core/_utils/utils.js';
  13. testUtils.createSinonSandbox();
  14. describe( 'Schema', () => {
  15. let schema;
  16. beforeEach( () => {
  17. schema = new Schema();
  18. } );
  19. describe( 'constructor()', () => {
  20. it( 'should register base items: inline, block, root', () => {
  21. testUtils.sinon.spy( Schema.prototype, 'registerItem' );
  22. schema = new Schema();
  23. expect( schema.registerItem.calledWithExactly( '$root', null ) );
  24. expect( schema.registerItem.calledWithExactly( '$block', null ) );
  25. expect( schema.registerItem.calledWithExactly( '$inline', null ) );
  26. } );
  27. it( 'should allow block in root', () => {
  28. expect( schema.check( { name: '$block', inside: [ '$root' ] } ) ).to.be.true;
  29. } );
  30. it( 'should allow inline in block', () => {
  31. expect( schema.check( { name: '$inline', inside: [ '$block' ] } ) ).to.be.true;
  32. } );
  33. it( 'should create the objects set', () => {
  34. expect( schema.objects ).to.be.instanceOf( Set );
  35. } );
  36. describe( '$clipboardHolder', () => {
  37. it( 'should allow $block', () => {
  38. expect( schema.check( { name: '$block', inside: [ '$clipboardHolder' ] } ) ).to.be.true;
  39. } );
  40. it( 'should allow $inline', () => {
  41. expect( schema.check( { name: '$inline', inside: [ '$clipboardHolder' ] } ) ).to.be.true;
  42. } );
  43. it( 'should allow $text', () => {
  44. expect( schema.check( { name: '$text', inside: [ '$clipboardHolder' ] } ) ).to.be.true;
  45. } );
  46. } );
  47. } );
  48. describe( 'registerItem', () => {
  49. it( 'should register in schema item under given name', () => {
  50. schema.registerItem( 'new' );
  51. expect( schema.hasItem( 'new' ) ).to.be.true;
  52. } );
  53. it( 'should build correct base chains', () => {
  54. schema.registerItem( 'first' );
  55. schema.registerItem( 'secondA', 'first' );
  56. schema.registerItem( 'secondB', 'first' );
  57. schema.registerItem( 'third', 'secondA' );
  58. expect( schema._extensionChains.get( 'first' ) ).to.deep.equal( [ 'first' ] );
  59. expect( schema._extensionChains.get( 'secondA' ) ).to.deep.equal( [ 'first', 'secondA' ] );
  60. expect( schema._extensionChains.get( 'secondB' ) ).to.deep.equal( [ 'first', 'secondB' ] );
  61. expect( schema._extensionChains.get( 'third' ) ).to.deep.equal( [ 'first', 'secondA', 'third' ] );
  62. } );
  63. it( 'should make registered item inherit allows from base item', () => {
  64. schema.registerItem( 'image', '$inline' );
  65. expect( schema.check( { name: 'image', inside: [ '$block' ] } ) ).to.be.true;
  66. } );
  67. it( 'should throw if item with given name has already been registered in schema', () => {
  68. schema.registerItem( 'new' );
  69. expect( () => {
  70. schema.registerItem( 'new' );
  71. } ).to.throw( CKEditorError, /model-schema-item-exists/ );
  72. } );
  73. it( 'should throw if base item has not been registered in schema', () => {
  74. expect( () => {
  75. schema.registerItem( 'new', 'old' );
  76. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  77. } );
  78. } );
  79. describe( 'hasItem', () => {
  80. it( 'should return true if given item name has been registered in schema', () => {
  81. expect( schema.hasItem( '$block' ) ).to.be.true;
  82. } );
  83. it( 'should return false if given item name has not been registered in schema', () => {
  84. expect( schema.hasItem( 'new' ) ).to.be.false;
  85. } );
  86. } );
  87. describe( '_getItem', () => {
  88. it( 'should return SchemaItem registered under given name', () => {
  89. schema.registerItem( 'new' );
  90. let item = schema._getItem( 'new' );
  91. expect( item ).to.be.instanceof( SchemaItem );
  92. } );
  93. it( 'should throw if there is no item registered under given name', () => {
  94. expect( () => {
  95. schema._getItem( 'new' );
  96. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  97. } );
  98. } );
  99. describe( 'allow', () => {
  100. it( 'should add passed query to allowed in schema', () => {
  101. schema.registerItem( 'p', '$block' );
  102. schema.registerItem( 'div', '$block' );
  103. expect( schema.check( { name: 'p', inside: [ 'div' ] } ) ).to.be.false;
  104. schema.allow( { name: 'p', inside: 'div' } );
  105. expect( schema.check( { name: 'p', inside: [ 'div' ] } ) ).to.be.true;
  106. } );
  107. } );
  108. describe( 'disallow', () => {
  109. it( 'should add passed query to disallowed in schema', () => {
  110. schema.registerItem( 'p', '$block' );
  111. schema.registerItem( 'div', '$block' );
  112. schema.allow( { name: '$block', attributes: 'bold', inside: 'div' } );
  113. expect( schema.check( { name: 'p', attributes: 'bold', inside: [ 'div' ] } ) ).to.be.true;
  114. schema.disallow( { name: 'p', attributes: 'bold', inside: 'div' } );
  115. expect( schema.check( { name: 'p', attributes: 'bold', inside: [ 'div' ] } ) ).to.be.false;
  116. } );
  117. } );
  118. describe( 'check', () => {
  119. describe( 'string or array of strings as inside', () => {
  120. it( 'should return false if given element is not registered in schema', () => {
  121. expect( schema.check( { name: 'new', inside: [ 'div', 'header' ] } ) ).to.be.false;
  122. } );
  123. it( 'should handle path given as string', () => {
  124. expect( schema.check( { name: '$inline', inside: '$block $block $block' } ) ).to.be.true;
  125. } );
  126. it( 'should handle attributes', () => {
  127. schema.registerItem( 'p', '$block' );
  128. schema.allow( { name: 'p', inside: '$block' } );
  129. expect( schema.check( { name: 'p', inside: [ '$block' ] } ) ).to.be.true;
  130. expect( schema.check( { name: 'p', inside: [ '$block' ], attributes: 'bold' } ) ).to.be.false;
  131. } );
  132. it( 'should support required attributes', () => {
  133. schema.registerItem( 'a', '$inline' );
  134. schema.requireAttributes( 'a', [ 'name' ] );
  135. schema.requireAttributes( 'a', [ 'href' ] );
  136. schema.allow( { name: 'a', inside: '$block', attributes: [ 'name', 'href', 'title', 'target' ] } );
  137. // Even though a is allowed in $block thanks to inheriting from $inline, we require href or name attribute.
  138. expect( schema.check( { name: 'a', inside: '$block' } ) ).to.be.false;
  139. // Even though a with title is allowed, we have to meet at least on required attributes set.
  140. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'title' ] } ) ).to.be.false;
  141. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name' ] } ) ).to.be.true;
  142. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'href' ] } ) ).to.be.true;
  143. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name', 'href' ] } ) ).to.be.true;
  144. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name', 'title', 'target' ] } ) ).to.be.true;
  145. } );
  146. it( 'should not require attributes from parent schema items', () => {
  147. schema.registerItem( 'parent' );
  148. schema.registerItem( 'child', 'parent' );
  149. schema.allow( { name: 'parent', inside: '$block' } );
  150. schema.requireAttributes( 'parent', [ 'required' ] );
  151. // Even though we require "required" attribute on parent, the requirement should not be inherited.
  152. expect( schema.check( { name: 'child', inside: '$block' } ) ).to.be.true;
  153. } );
  154. it( 'should support multiple attributes', () => {
  155. // Let's take example case, where image item has to have a pair of "alt" and "src" attributes.
  156. // Then it could have other attribute which is allowed on inline elements, i.e. "bold".
  157. schema.registerItem( 'img', '$inline' );
  158. schema.requireAttributes( 'img', [ 'alt', 'src' ] );
  159. schema.allow( { name: '$inline', inside: '$block', attributes: 'bold' } );
  160. schema.allow( { name: 'img', inside: '$block', attributes: [ 'alt', 'src' ] } );
  161. // Image without any attributes is not allowed.
  162. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt' ] } ) ).to.be.false;
  163. // Image can't have just alt or src.
  164. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt' ] } ) ).to.be.false;
  165. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'src' ] } ) ).to.be.false;
  166. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src' ] } ) ).to.be.true;
  167. // Because of inherting from $inline, image can have bold
  168. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src', 'bold' ] } ) ).to.be.true;
  169. // But it can't have only bold without alt or/and src.
  170. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'bold' ] } ) ).to.be.false;
  171. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'src', 'bold' ] } ) ).to.be.false;
  172. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'bold' ] } ) ).to.be.false;
  173. // Even if image has src and alt, it can't have attributes that weren't allowed
  174. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src', 'attr' ] } ) ).to.be.false;
  175. } );
  176. it( 'should omit path elements that are added to schema', () => {
  177. expect( schema.check( { name: '$inline', inside: '$block new $block' } ) ).to.be.true;
  178. } );
  179. } );
  180. describe( 'array of elements as inside', () => {
  181. beforeEach( () => {
  182. schema.registerItem( 'div', '$block' );
  183. schema.registerItem( 'header', '$block' );
  184. schema.registerItem( 'p', '$block' );
  185. schema.registerItem( 'img', '$inline' );
  186. schema.allow( { name: '$block', inside: 'div' } );
  187. schema.allow( { name: '$inline', attributes: 'bold', inside: '$block' } );
  188. schema.disallow( { name: '$inline', attributes: 'bold', inside: 'header' } );
  189. } );
  190. it( 'should return true if given element is allowed by schema at given position', () => {
  191. // P is block and block is allowed in DIV.
  192. expect( schema.check( { name: 'p', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
  193. // IMG is inline and inline is allowed in block.
  194. expect( schema.check( { name: 'img', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
  195. expect( schema.check( { name: 'img', inside: [ new Element( 'p' ) ] } ) ).to.be.true;
  196. // Inline is allowed in any block and is allowed with attribute bold.
  197. expect( schema.check( { name: 'img', inside: [ new Element( 'div' ) ], attributes: [ 'bold' ] } ) ).to.be.true;
  198. expect( schema.check( { name: 'img', inside: [ new Element( 'p' ) ], attributes: [ 'bold' ] } ) ).to.be.true;
  199. // Inline is allowed in header which is allowed in DIV.
  200. expect( schema.check( { name: 'header', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
  201. expect( schema.check( { name: 'img', inside: [ new Element( 'header' ) ] } ) ).to.be.true;
  202. expect( schema.check( { name: 'img', inside: [ new Element( 'div' ), new Element( 'header' ) ] } ) ).to.be.true;
  203. } );
  204. it( 'should return false if given element is not allowed by schema at given position', () => {
  205. // P with attribute is not allowed.
  206. expect( schema.check( { name: 'p', inside: [ new Element( 'div' ) ], attributes: 'bold' } ) ).to.be.false;
  207. // Bold text is not allowed in header
  208. expect( schema.check( { name: '$text', inside: [ new Element( 'header' ) ], attributes: 'bold' } ) ).to.be.false;
  209. } );
  210. it( 'should return false if given element is not registered in schema', () => {
  211. expect( schema.check( { name: 'new', inside: [ new Element( 'div' ) ] } ) ).to.be.false;
  212. } );
  213. } );
  214. describe( 'position as inside', () => {
  215. let doc, root;
  216. beforeEach( () => {
  217. doc = new Document();
  218. root = doc.createRoot( 'div' );
  219. root.insertChildren( 0, [
  220. new Element( 'div' ),
  221. new Element( 'header' ),
  222. new Element( 'p' )
  223. ] );
  224. schema.registerItem( 'div', '$block' );
  225. schema.registerItem( 'header', '$block' );
  226. schema.registerItem( 'p', '$block' );
  227. schema.allow( { name: '$block', inside: 'div' } );
  228. schema.allow( { name: '$inline', attributes: 'bold', inside: '$block' } );
  229. schema.disallow( { name: '$inline', attributes: 'bold', inside: 'header' } );
  230. } );
  231. it( 'should return true if given element is allowed by schema at given position', () => {
  232. // Block should be allowed in root.
  233. expect( schema.check( { name: '$block', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  234. // P is block and block should be allowed in root.
  235. expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  236. // P is allowed in DIV by the set rule.
  237. expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  238. // Inline is allowed in any block and is allowed with attribute bold.
  239. // We do not check if it is allowed in header, because it is disallowed by the set rule.
  240. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  241. expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ) } ) ).to.be.true;
  242. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.true;
  243. expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ), attributes: 'bold' } ) ).to.be.true;
  244. // Header is allowed in DIV.
  245. expect( schema.check( { name: 'header', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  246. // Inline is allowed in block and root is DIV, which is block.
  247. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  248. } );
  249. it( 'should return false if given element is not allowed by schema at given position', () => {
  250. // P with attribute is not allowed anywhere.
  251. expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ), attributes: 'bold' } ) ).to.be.false;
  252. expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.false;
  253. // Bold text is not allowed in header
  254. expect( schema.check( { name: '$text', inside: new Position( root, [ 1, 0 ] ), attributes: 'bold' } ) ).to.be.false;
  255. } );
  256. it( 'should return false if given element is not registered in schema', () => {
  257. expect( schema.check( { name: 'new', inside: new Position( root, [ 0 ] ) } ) ).to.be.false;
  258. } );
  259. } );
  260. describe( 'bug #732', () => {
  261. // Ticket case.
  262. it( 'should return false if given element is allowed in the root but not deeper', () => {
  263. schema.registerItem( 'paragraph', '$block' );
  264. expect( schema.check( { name: 'paragraph', inside: [ '$root', 'paragraph' ] } ) ).to.be.false;
  265. } );
  266. // Two additional, real life cases accompanying the ticket case.
  267. it( 'should return true if checking whether text is allowed in $root > paragraph', () => {
  268. schema.registerItem( 'paragraph', '$block' );
  269. expect( schema.check( { name: '$text', inside: [ '$root', 'paragraph' ] } ) ).to.be.true;
  270. } );
  271. it( 'should return true if checking whether text is allowed in paragraph', () => {
  272. schema.registerItem( 'paragraph', '$block' );
  273. expect( schema.check( { name: '$text', inside: [ 'paragraph' ] } ) ).to.be.true;
  274. } );
  275. // Veryfing the matching algorithm.
  276. // The right ends of the element to check and "inside" paths must match.
  277. describe( 'right ends of paths must match', () => {
  278. beforeEach( () => {
  279. schema.registerItem( 'a' );
  280. schema.registerItem( 'b' );
  281. schema.registerItem( 'c' );
  282. schema.registerItem( 'd' );
  283. schema.registerItem( 'e' );
  284. schema.allow( { name: 'a', inside: [ 'b', 'c', 'd' ] } );
  285. schema.allow( { name: 'e', inside: [ 'a' ] } );
  286. } );
  287. // Simple chains created by a single allow() call.
  288. it( 'a inside b, c', () => {
  289. expect( schema.check( { name: 'a', inside: [ 'b', 'c' ] } ) ).to.be.false;
  290. } );
  291. it( 'a inside b', () => {
  292. expect( schema.check( { name: 'a', inside: [ 'b' ] } ) ).to.be.false;
  293. } );
  294. it( 'a inside b, c, d', () => {
  295. expect( schema.check( { name: 'a', inside: [ 'b', 'c', 'd' ] } ) ).to.be.true;
  296. } );
  297. it( 'a inside c, d', () => {
  298. expect( schema.check( { name: 'a', inside: [ 'c', 'd' ] } ) ).to.be.true;
  299. } );
  300. it( 'a inside d', () => {
  301. expect( schema.check( { name: 'a', inside: [ 'd' ] } ) ).to.be.true;
  302. } );
  303. // "Allowed in" chains created by two separate allow() calls (`e inside a` and `a inside b,c,d`).
  304. it( 'e inside a, d', () => {
  305. expect( schema.check( { name: 'e', inside: [ 'd', 'a' ] } ) ).to.be.true;
  306. } );
  307. it( 'e inside b, c, d', () => {
  308. expect( schema.check( { name: 'e', inside: [ 'b', 'c', 'd' ] } ) ).to.be.false;
  309. } );
  310. } );
  311. } );
  312. } );
  313. describe( 'itemExtends', () => {
  314. it( 'should return true if given item extends another given item', () => {
  315. schema.registerItem( 'div', '$block' );
  316. schema.registerItem( 'myDiv', 'div' );
  317. expect( schema.itemExtends( 'div', '$block' ) ).to.be.true;
  318. expect( schema.itemExtends( 'myDiv', 'div' ) ).to.be.true;
  319. expect( schema.itemExtends( 'myDiv', '$block' ) ).to.be.true;
  320. } );
  321. it( 'should return false if given item does not extend another given item', () => {
  322. schema.registerItem( 'div' );
  323. schema.registerItem( 'myDiv', 'div' );
  324. expect( schema.itemExtends( 'div', '$block' ) ).to.be.false;
  325. expect( schema.itemExtends( 'div', 'myDiv' ) ).to.be.false;
  326. } );
  327. it( 'should throw if one or both given items are not registered in schema', () => {
  328. expect( () => {
  329. schema.itemExtends( 'foo', '$block' );
  330. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  331. expect( () => {
  332. schema.itemExtends( '$block', 'foo' );
  333. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  334. } );
  335. } );
  336. describe( '_normalizeQueryPath', () => {
  337. it( 'should normalize string with spaces to an array of strings', () => {
  338. expect( Schema._normalizeQueryPath( '$root div strong' ) ).to.deep.equal( [ '$root', 'div', 'strong' ] );
  339. } );
  340. it( 'should normalize model position to an array of strings', () => {
  341. let doc = new Document();
  342. let root = doc.createRoot();
  343. root.insertChildren( 0, [
  344. new Element( 'div', null, [
  345. new Element( 'header' )
  346. ] )
  347. ] );
  348. let position = new Position( root, [ 0, 0, 0 ] );
  349. expect( Schema._normalizeQueryPath( position ) ).to.deep.equal( [ '$root', 'div', 'header' ] );
  350. } );
  351. it( 'should normalize array with strings and model elements to an array of strings and drop unrecognized parts', () => {
  352. let input = [
  353. '$root',
  354. [ 'div' ],
  355. new Element( 'div' ),
  356. null,
  357. new Element( 'p' ),
  358. 'strong'
  359. ];
  360. expect( Schema._normalizeQueryPath( input ) ).to.deep.equal( [ '$root', 'div', 'p', 'strong' ] );
  361. } );
  362. } );
  363. } );