8
0

schema.js 18 KB

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