schema.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Schema from '/ckeditor5/core/treemodel/schema.js';
  7. import { SchemaItem as SchemaItem } from '/ckeditor5/core/treemodel/schema.js';
  8. import Document from '/ckeditor5/core/treemodel/document.js';
  9. import Element from '/ckeditor5/core/treemodel/element.js';
  10. import Position from '/ckeditor5/core/treemodel/position.js';
  11. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  12. let schema;
  13. beforeEach( () => {
  14. schema = new Schema();
  15. } );
  16. describe( 'constructor', () => {
  17. it( 'should register base items: inline, block, root', () => {
  18. sinon.spy( Schema.prototype, 'registerItem' );
  19. schema = new Schema();
  20. expect( schema.registerItem.calledWithExactly( '$inline', null ) );
  21. expect( schema.registerItem.calledWithExactly( '$block', null ) );
  22. Schema.prototype.registerItem.restore();
  23. } );
  24. it( 'should allow inline in block', () => {
  25. expect( schema.checkQuery( { name: '$inline', inside: [ '$block' ] } ) ).to.be.true;
  26. } );
  27. } );
  28. describe( 'registerItem', () => {
  29. it( 'should register in schema item under given name', () => {
  30. schema.registerItem( 'new' );
  31. expect( schema.hasItem( 'new' ) ).to.be.true;
  32. } );
  33. it( 'should build correct base chains', () => {
  34. schema.registerItem( 'first' );
  35. schema.registerItem( 'secondA', 'first' );
  36. schema.registerItem( 'secondB', 'first' );
  37. schema.registerItem( 'third', 'secondA' );
  38. expect( schema._extensionChains.get( 'first' ) ).to.deep.equal( [ 'first' ] );
  39. expect( schema._extensionChains.get( 'secondA' ) ).to.deep.equal( [ 'first', 'secondA' ] );
  40. expect( schema._extensionChains.get( 'secondB' ) ).to.deep.equal( [ 'first', 'secondB' ] );
  41. expect( schema._extensionChains.get( 'third' ) ).to.deep.equal( [ 'first', 'secondA', 'third' ] );
  42. } );
  43. it( 'should make registered item inherit allows from base item', () => {
  44. schema.registerItem( 'image', '$inline' );
  45. expect( schema.checkQuery( { name: 'image', inside: [ '$block' ] } ) ).to.be.true;
  46. } );
  47. it( 'should throw if item with given name has already been registered in schema', () => {
  48. schema.registerItem( 'new' );
  49. expect( () => {
  50. schema.registerItem( 'new' );
  51. } ).to.throw( CKEditorError, /schema-item-exists/ );
  52. } );
  53. it( 'should throw if base item has not been registered in schema', () => {
  54. expect( () => {
  55. schema.registerItem( 'new', 'old' );
  56. } ).to.throw( CKEditorError, /schema-no-item/ );
  57. } );
  58. } );
  59. describe( 'hasItem', () => {
  60. it( 'should return true if given item name has been registered in schema', () => {
  61. expect( schema.hasItem( '$block' ) ).to.be.true;
  62. } );
  63. it( 'should return false if given item name has not been registered in schema', () => {
  64. expect( schema.hasItem( 'new' ) ).to.be.false;
  65. } );
  66. } );
  67. describe( '_getItem', () => {
  68. it( 'should return SchemaItem registered under given name', () => {
  69. schema.registerItem( 'new' );
  70. let item = schema._getItem( 'new' );
  71. expect( item ).to.be.instanceof( SchemaItem );
  72. } );
  73. it( 'should throw if there is no item registered under given name', () => {
  74. expect( () => {
  75. schema._getItem( 'new' );
  76. } ).to.throw( CKEditorError, /schema-no-item/ );
  77. } );
  78. } );
  79. describe( 'allow', () => {
  80. it( 'should add passed query to allowed in schema', () => {
  81. schema.registerItem( 'p', '$block' );
  82. schema.registerItem( 'div', '$block' );
  83. expect( schema.checkQuery( { name: 'p', inside: [ 'div' ] } ) ).to.be.false;
  84. schema.allow( { name: 'p', inside: 'div' } );
  85. expect( schema.checkQuery( { name: 'p', inside: [ 'div' ] } ) ).to.be.true;
  86. } );
  87. } );
  88. describe( 'disallow', () => {
  89. it( 'should add passed query to disallowed in schema', () => {
  90. schema.registerItem( 'p', '$block' );
  91. schema.registerItem( 'div', '$block' );
  92. schema.allow( { name: '$block', attribute: 'bold', inside: 'div' } );
  93. expect( schema.checkQuery( { name: 'p', attribute: 'bold', inside: [ 'div' ] } ) ).to.be.true;
  94. schema.disallow( { name: 'p', attribute: 'bold', inside: 'div' } );
  95. expect( schema.checkQuery( { name: 'p', attribute: 'bold', inside: [ 'div' ] } ) ).to.be.false;
  96. } );
  97. } );
  98. describe( 'checkAtPosition', () => {
  99. let doc, root;
  100. beforeEach( () => {
  101. doc = new Document();
  102. root = doc.createRoot( 'root', 'div' );
  103. root.insertChildren( 0, [
  104. new Element( 'div' ),
  105. new Element( 'header' ),
  106. new Element( 'p' )
  107. ] );
  108. schema.registerItem( 'div', '$block' );
  109. schema.registerItem( 'header', '$block' );
  110. schema.registerItem( 'p', '$block' );
  111. schema.allow( { name: '$block', inside: 'div' } );
  112. schema.allow( { name: '$inline', attribute: 'bold', inside: '$block' } );
  113. schema.disallow( { name: '$inline', attribute: 'bold', inside: 'header' } );
  114. } );
  115. it( 'should return true if given element is allowed by schema at given position', () => {
  116. // Block should be allowed in root.
  117. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), '$block' ) ).to.be.true;
  118. // P is block and block should be allowed in root.
  119. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), 'p' ) ).to.be.true;
  120. // P is allowed in DIV by the set rule.
  121. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), 'p' ) ).to.be.true;
  122. // Inline is allowed in any block and is allowed with attribute bold.
  123. // We do not check if it is allowed in header, because it is disallowed by the set rule.
  124. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), '$inline' ) ).to.be.true;
  125. expect( schema.checkAtPosition( new Position( root, [ 2, 0 ] ), '$inline' ) ).to.be.true;
  126. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), '$inline', 'bold' ) ).to.be.true;
  127. expect( schema.checkAtPosition( new Position( root, [ 2, 0 ] ), '$inline', 'bold' ) ).to.be.true;
  128. // Header is allowed in DIV.
  129. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), 'header' ) ).to.be.true;
  130. // Inline is allowed in block and root is DIV, which is block.
  131. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), '$inline' ) ).to.be.true;
  132. } );
  133. it( 'should return false if given element is not allowed by schema at given position', () => {
  134. // P with attribute is not allowed anywhere.
  135. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), 'p', 'bold' ) ).to.be.false;
  136. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), 'p', 'bold' ) ).to.be.false;
  137. // Bold text is not allowed in header
  138. expect( schema.checkAtPosition( new Position( root, [ 1, 0 ] ), '$text', 'bold' ) ).to.be.false;
  139. } );
  140. it( 'should return false if given element is not registered in schema', () => {
  141. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), 'new' ) ).to.be.false;
  142. } );
  143. } );
  144. describe( 'checkQuery', () => {
  145. it( 'should return false if given element is not registered in schema', () => {
  146. expect( schema.checkQuery( { name: 'new', inside: [ 'div', 'header' ] } ) ).to.be.false;
  147. } );
  148. it( 'should handle path given as string', () => {
  149. expect( schema.checkQuery( { name: '$inline', inside: '$block $block $block' } ) ).to.be.true;
  150. } );
  151. it( 'should handle attributes', () => {
  152. schema.registerItem( 'p', '$block' );
  153. schema.allow( { name: 'p', inside: '$block' } );
  154. expect( schema.checkQuery( { name: 'p', inside: '$block' } ) ).to.be.true;
  155. expect( schema.checkQuery( { name: 'p', attribute: 'bold', inside: '$block' } ) ).to.be.false;
  156. } );
  157. } );