8
0

schema.js 13 KB

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