8
0

schema.js 14 KB

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