schema.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. 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. describe( '$clipboardHolder', () => {
  36. it( 'should allow $block', () => {
  37. expect( schema.check( { name: '$block', inside: [ '$clipboardHolder' ] } ) ).to.be.true;
  38. } );
  39. it( 'should allow $inline', () => {
  40. expect( schema.check( { name: '$inline', inside: [ '$clipboardHolder' ] } ) ).to.be.true;
  41. } );
  42. it( 'should allow $text', () => {
  43. expect( schema.check( { name: '$text', inside: [ '$clipboardHolder' ] } ) ).to.be.true;
  44. } );
  45. } );
  46. } );
  47. describe( 'registerItem', () => {
  48. it( 'should register in schema item under given name', () => {
  49. schema.registerItem( 'new' );
  50. expect( schema.hasItem( 'new' ) ).to.be.true;
  51. } );
  52. it( 'should build correct base chains', () => {
  53. schema.registerItem( 'first' );
  54. schema.registerItem( 'secondA', 'first' );
  55. schema.registerItem( 'secondB', 'first' );
  56. schema.registerItem( 'third', 'secondA' );
  57. expect( schema._extensionChains.get( 'first' ) ).to.deep.equal( [ 'first' ] );
  58. expect( schema._extensionChains.get( 'secondA' ) ).to.deep.equal( [ 'first', 'secondA' ] );
  59. expect( schema._extensionChains.get( 'secondB' ) ).to.deep.equal( [ 'first', 'secondB' ] );
  60. expect( schema._extensionChains.get( 'third' ) ).to.deep.equal( [ 'first', 'secondA', 'third' ] );
  61. } );
  62. it( 'should make registered item inherit allows from base item', () => {
  63. schema.registerItem( 'image', '$inline' );
  64. expect( schema.check( { name: 'image', inside: [ '$block' ] } ) ).to.be.true;
  65. } );
  66. it( 'should throw if item with given name has already been registered in schema', () => {
  67. schema.registerItem( 'new' );
  68. expect( () => {
  69. schema.registerItem( 'new' );
  70. } ).to.throw( CKEditorError, /model-schema-item-exists/ );
  71. } );
  72. it( 'should throw if base item has not been registered in schema', () => {
  73. expect( () => {
  74. schema.registerItem( 'new', 'old' );
  75. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  76. } );
  77. } );
  78. describe( 'hasItem', () => {
  79. it( 'should return true if given item name has been registered in schema', () => {
  80. expect( schema.hasItem( '$block' ) ).to.be.true;
  81. } );
  82. it( 'should return false if given item name has not been registered in schema', () => {
  83. expect( schema.hasItem( 'new' ) ).to.be.false;
  84. } );
  85. } );
  86. describe( '_getItem', () => {
  87. it( 'should return SchemaItem registered under given name', () => {
  88. schema.registerItem( 'new' );
  89. let item = schema._getItem( 'new' );
  90. expect( item ).to.be.instanceof( SchemaItem );
  91. } );
  92. it( 'should throw if there is no item registered under given name', () => {
  93. expect( () => {
  94. schema._getItem( 'new' );
  95. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  96. } );
  97. } );
  98. describe( 'allow', () => {
  99. it( 'should add passed query to allowed in schema', () => {
  100. schema.registerItem( 'p', '$block' );
  101. schema.registerItem( 'div', '$block' );
  102. expect( schema.check( { name: 'p', inside: [ 'div' ] } ) ).to.be.false;
  103. schema.allow( { name: 'p', inside: 'div' } );
  104. expect( schema.check( { name: 'p', inside: [ 'div' ] } ) ).to.be.true;
  105. } );
  106. } );
  107. describe( 'disallow', () => {
  108. it( 'should add passed query to disallowed in schema', () => {
  109. schema.registerItem( 'p', '$block' );
  110. schema.registerItem( 'div', '$block' );
  111. schema.allow( { name: '$block', attributes: 'bold', inside: 'div' } );
  112. expect( schema.check( { name: 'p', attributes: 'bold', inside: [ 'div' ] } ) ).to.be.true;
  113. schema.disallow( { name: 'p', attributes: 'bold', inside: 'div' } );
  114. expect( schema.check( { name: 'p', attributes: 'bold', inside: [ 'div' ] } ) ).to.be.false;
  115. } );
  116. } );
  117. describe( 'check', () => {
  118. describe( 'string or array of strings as inside', () => {
  119. it( 'should return false if given element is not registered in schema', () => {
  120. expect( schema.check( { name: 'new', inside: [ 'div', 'header' ] } ) ).to.be.false;
  121. } );
  122. it( 'should handle path given as string', () => {
  123. expect( schema.check( { name: '$inline', inside: '$block $block $block' } ) ).to.be.true;
  124. } );
  125. it( 'should handle attributes', () => {
  126. schema.registerItem( 'p', '$block' );
  127. schema.allow( { name: 'p', inside: '$block' } );
  128. expect( schema.check( { name: 'p', inside: [ '$block' ] } ) ).to.be.true;
  129. expect( schema.check( { name: 'p', inside: [ '$block' ], attributes: 'bold' } ) ).to.be.false;
  130. } );
  131. it( 'should support required attributes', () => {
  132. schema.registerItem( 'a', '$inline' );
  133. schema.requireAttributes( 'a', [ 'name' ] );
  134. schema.requireAttributes( 'a', [ 'href' ] );
  135. schema.allow( { name: 'a', inside: '$block', attributes: [ 'name', 'href', 'title', 'target' ] } );
  136. // Even though a is allowed in $block thanks to inheriting from $inline, we require href or name attribute.
  137. expect( schema.check( { name: 'a', inside: '$block' } ) ).to.be.false;
  138. // Even though a with title is allowed, we have to meet at least on required attributes set.
  139. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'title' ] } ) ).to.be.false;
  140. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name' ] } ) ).to.be.true;
  141. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'href' ] } ) ).to.be.true;
  142. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name', 'href' ] } ) ).to.be.true;
  143. expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name', 'title', 'target' ] } ) ).to.be.true;
  144. } );
  145. it( 'should not require attributes from parent schema items', () => {
  146. schema.registerItem( 'parent' );
  147. schema.registerItem( 'child', 'parent' );
  148. schema.allow( { name: 'parent', inside: '$block' } );
  149. schema.requireAttributes( 'parent', [ 'required' ] );
  150. // Even though we require "required" attribute on parent, the requirement should not be inherited.
  151. expect( schema.check( { name: 'child', inside: '$block' } ) ).to.be.true;
  152. } );
  153. it( 'should support multiple attributes', () => {
  154. // Let's take example case, where image item has to have a pair of "alt" and "src" attributes.
  155. // Then it could have other attribute which is allowed on inline elements, i.e. "bold".
  156. schema.registerItem( 'img', '$inline' );
  157. schema.requireAttributes( 'img', [ 'alt', 'src' ] );
  158. schema.allow( { name: '$inline', inside: '$block', attributes: 'bold' } );
  159. schema.allow( { name: 'img', inside: '$block', attributes: [ 'alt', 'src' ] } );
  160. // Image without any attributes is not allowed.
  161. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt' ] } ) ).to.be.false;
  162. // Image can't have just alt or src.
  163. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt' ] } ) ).to.be.false;
  164. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'src' ] } ) ).to.be.false;
  165. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src' ] } ) ).to.be.true;
  166. // Because of inherting from $inline, image can have bold
  167. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src', 'bold' ] } ) ).to.be.true;
  168. // But it can't have only bold without alt or/and src.
  169. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'bold' ] } ) ).to.be.false;
  170. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'src', 'bold' ] } ) ).to.be.false;
  171. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'bold' ] } ) ).to.be.false;
  172. // Even if image has src and alt, it can't have attributes that weren't allowed
  173. expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src', 'attr' ] } ) ).to.be.false;
  174. } );
  175. it( 'should omit path elements that are added to schema', () => {
  176. expect( schema.check( { name: '$inline', inside: '$block new $block' } ) ).to.be.true;
  177. } );
  178. } );
  179. describe( 'array of elements as inside', () => {
  180. beforeEach( () => {
  181. schema.registerItem( 'div', '$block' );
  182. schema.registerItem( 'header', '$block' );
  183. schema.registerItem( 'p', '$block' );
  184. schema.registerItem( 'img', '$inline' );
  185. schema.allow( { name: '$block', inside: 'div' } );
  186. schema.allow( { name: '$inline', attributes: 'bold', inside: '$block' } );
  187. schema.disallow( { name: '$inline', attributes: 'bold', inside: 'header' } );
  188. } );
  189. it( 'should return true if given element is allowed by schema at given position', () => {
  190. // P is block and block is allowed in DIV.
  191. expect( schema.check( { name: 'p', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
  192. // IMG is inline and inline is allowed in block.
  193. expect( schema.check( { name: 'img', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
  194. expect( schema.check( { name: 'img', inside: [ new Element( 'p' ) ] } ) ).to.be.true;
  195. // Inline is allowed in any block and is allowed with attribute bold.
  196. expect( schema.check( { name: 'img', inside: [ new Element( 'div' ) ], attributes: [ 'bold' ] } ) ).to.be.true;
  197. expect( schema.check( { name: 'img', inside: [ new Element( 'p' ) ], attributes: [ 'bold' ] } ) ).to.be.true;
  198. // Inline is allowed in header which is allowed in DIV.
  199. expect( schema.check( { name: 'header', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
  200. expect( schema.check( { name: 'img', inside: [ new Element( 'header' ) ] } ) ).to.be.true;
  201. expect( schema.check( { name: 'img', inside: [ new Element( 'div' ), new Element( 'header' ) ] } ) ).to.be.true;
  202. } );
  203. it( 'should return false if given element is not allowed by schema at given position', () => {
  204. // P with attribute is not allowed.
  205. expect( schema.check( { name: 'p', inside: [ new Element( 'div' ) ], attributes: 'bold' } ) ).to.be.false;
  206. // Bold text is not allowed in header
  207. expect( schema.check( { name: '$text', inside: [ new Element( 'header' ) ], attributes: 'bold' } ) ).to.be.false;
  208. } );
  209. it( 'should return false if given element is not registered in schema', () => {
  210. expect( schema.check( { name: 'new', inside: [ new Element( 'div' ) ] } ) ).to.be.false;
  211. } );
  212. } );
  213. describe( 'position as inside', () => {
  214. let doc, root;
  215. beforeEach( () => {
  216. doc = new Document();
  217. root = doc.createRoot( 'div' );
  218. root.insertChildren( 0, [
  219. new Element( 'div' ),
  220. new Element( 'header' ),
  221. new Element( 'p' )
  222. ] );
  223. schema.registerItem( 'div', '$block' );
  224. schema.registerItem( 'header', '$block' );
  225. schema.registerItem( 'p', '$block' );
  226. schema.allow( { name: '$block', inside: 'div' } );
  227. schema.allow( { name: '$inline', attributes: 'bold', inside: '$block' } );
  228. schema.disallow( { name: '$inline', attributes: 'bold', inside: 'header' } );
  229. } );
  230. it( 'should return true if given element is allowed by schema at given position', () => {
  231. // Block should be allowed in root.
  232. expect( schema.check( { name: '$block', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  233. // P is block and block should be allowed in root.
  234. expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  235. // P is allowed in DIV by the set rule.
  236. expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  237. // Inline is allowed in any block and is allowed with attribute bold.
  238. // We do not check if it is allowed in header, because it is disallowed by the set rule.
  239. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  240. expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ) } ) ).to.be.true;
  241. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.true;
  242. expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ), attributes: 'bold' } ) ).to.be.true;
  243. // Header is allowed in DIV.
  244. expect( schema.check( { name: 'header', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
  245. // Inline is allowed in block and root is DIV, which is block.
  246. expect( schema.check( { name: '$inline', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
  247. } );
  248. it( 'should return false if given element is not allowed by schema at given position', () => {
  249. // P with attribute is not allowed anywhere.
  250. expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ), attributes: 'bold' } ) ).to.be.false;
  251. expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.false;
  252. // Bold text is not allowed in header
  253. expect( schema.check( { name: '$text', inside: new Position( root, [ 1, 0 ] ), attributes: 'bold' } ) ).to.be.false;
  254. } );
  255. it( 'should return false if given element is not registered in schema', () => {
  256. expect( schema.check( { name: 'new', inside: new Position( root, [ 0 ] ) } ) ).to.be.false;
  257. } );
  258. } );
  259. } );
  260. describe( 'itemExtends', () => {
  261. it( 'should return true if given item extends another given item', () => {
  262. schema.registerItem( 'div', '$block' );
  263. schema.registerItem( 'myDiv', 'div' );
  264. expect( schema.itemExtends( 'div', '$block' ) ).to.be.true;
  265. expect( schema.itemExtends( 'myDiv', 'div' ) ).to.be.true;
  266. expect( schema.itemExtends( 'myDiv', '$block' ) ).to.be.true;
  267. } );
  268. it( 'should return false if given item does not extend another given item', () => {
  269. schema.registerItem( 'div' );
  270. schema.registerItem( 'myDiv', 'div' );
  271. expect( schema.itemExtends( 'div', '$block' ) ).to.be.false;
  272. expect( schema.itemExtends( 'div', 'myDiv' ) ).to.be.false;
  273. } );
  274. it( 'should throw if one or both given items are not registered in schema', () => {
  275. expect( () => {
  276. schema.itemExtends( 'foo', '$block' );
  277. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  278. expect( () => {
  279. schema.itemExtends( '$block', 'foo' );
  280. } ).to.throw( CKEditorError, /model-schema-no-item/ );
  281. } );
  282. } );
  283. describe( '_normalizeQueryPath', () => {
  284. it( 'should normalize string with spaces to an array of strings', () => {
  285. expect( Schema._normalizeQueryPath( '$root div strong' ) ).to.deep.equal( [ '$root', 'div', 'strong' ] );
  286. } );
  287. it( 'should normalize model position to an array of strings', () => {
  288. let doc = new Document();
  289. let root = doc.createRoot();
  290. root.insertChildren( 0, [
  291. new Element( 'div', null, [
  292. new Element( 'header' )
  293. ] )
  294. ] );
  295. let position = new Position( root, [ 0, 0, 0 ] );
  296. expect( Schema._normalizeQueryPath( position ) ).to.deep.equal( [ '$root', 'div', 'header' ] );
  297. } );
  298. it( 'should normalize array with strings and model elements to an array of strings and drop unrecognized parts', () => {
  299. let input = [
  300. '$root',
  301. [ 'div' ],
  302. new Element( 'div' ),
  303. null,
  304. new Element( 'p' ),
  305. 'strong'
  306. ];
  307. expect( Schema._normalizeQueryPath( input ) ).to.deep.equal( [ '$root', 'div', 'p', 'strong' ] );
  308. } );
  309. } );