schema.js 14 KB

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