8
0

schema.js 18 KB

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