schema.js 14 KB

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