schema.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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/core/treemodel/schema.js';
  8. import { SchemaItem as SchemaItem } from '/ckeditor5/core/treemodel/schema.js';
  9. import Document from '/ckeditor5/core/treemodel/document.js';
  10. import Element from '/ckeditor5/core/treemodel/element.js';
  11. import Position from '/ckeditor5/core/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.checkQuery( { 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.checkQuery( { 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.checkQuery( { name: 'p', inside: [ 'div' ] } ) ).to.be.false;
  85. schema.allow( { name: 'p', inside: 'div' } );
  86. expect( schema.checkQuery( { 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', attribute: 'bold', inside: 'div' } );
  94. expect( schema.checkQuery( { name: 'p', attribute: 'bold', inside: [ 'div' ] } ) ).to.be.true;
  95. schema.disallow( { name: 'p', attribute: 'bold', inside: 'div' } );
  96. expect( schema.checkQuery( { name: 'p', attribute: 'bold', inside: [ 'div' ] } ) ).to.be.false;
  97. } );
  98. } );
  99. describe( 'checkAtPosition', () => {
  100. let doc, root;
  101. beforeEach( () => {
  102. doc = new Document();
  103. root = doc.createRoot( 'root', 'div' );
  104. root.insertChildren( 0, [
  105. new Element( 'div' ),
  106. new Element( 'header' ),
  107. new Element( 'p' )
  108. ] );
  109. schema.registerItem( 'div', '$block' );
  110. schema.registerItem( 'header', '$block' );
  111. schema.registerItem( 'p', '$block' );
  112. schema.allow( { name: '$block', inside: 'div' } );
  113. schema.allow( { name: '$inline', attribute: 'bold', inside: '$block' } );
  114. schema.disallow( { name: '$inline', attribute: 'bold', inside: 'header' } );
  115. } );
  116. it( 'should return true if given element is allowed by schema at given position', () => {
  117. // Block should be allowed in root.
  118. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), '$block' ) ).to.be.true;
  119. // P is block and block should be allowed in root.
  120. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), 'p' ) ).to.be.true;
  121. // P is allowed in DIV by the set rule.
  122. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), 'p' ) ).to.be.true;
  123. // Inline is allowed in any block and is allowed with attribute bold.
  124. // We do not check if it is allowed in header, because it is disallowed by the set rule.
  125. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), '$inline' ) ).to.be.true;
  126. expect( schema.checkAtPosition( new Position( root, [ 2, 0 ] ), '$inline' ) ).to.be.true;
  127. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), '$inline', 'bold' ) ).to.be.true;
  128. expect( schema.checkAtPosition( new Position( root, [ 2, 0 ] ), '$inline', 'bold' ) ).to.be.true;
  129. // Header is allowed in DIV.
  130. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), 'header' ) ).to.be.true;
  131. // Inline is allowed in block and root is DIV, which is block.
  132. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), '$inline' ) ).to.be.true;
  133. } );
  134. it( 'should return false if given element is not allowed by schema at given position', () => {
  135. // P with attribute is not allowed anywhere.
  136. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), 'p', 'bold' ) ).to.be.false;
  137. expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), 'p', 'bold' ) ).to.be.false;
  138. // Bold text is not allowed in header
  139. expect( schema.checkAtPosition( new Position( root, [ 1, 0 ] ), '$text', 'bold' ) ).to.be.false;
  140. } );
  141. it( 'should return false if given element is not registered in schema', () => {
  142. expect( schema.checkAtPosition( new Position( root, [ 0 ] ), 'new' ) ).to.be.false;
  143. } );
  144. } );
  145. describe( 'checkQuery', () => {
  146. it( 'should return false if given element is not registered in schema', () => {
  147. expect( schema.checkQuery( { name: 'new', inside: [ 'div', 'header' ] } ) ).to.be.false;
  148. } );
  149. it( 'should handle path given as string', () => {
  150. expect( schema.checkQuery( { name: '$inline', inside: '$block $block $block' } ) ).to.be.true;
  151. } );
  152. it( 'should handle attributes', () => {
  153. schema.registerItem( 'p', '$block' );
  154. schema.allow( { name: 'p', inside: '$block' } );
  155. expect( schema.checkQuery( { name: 'p', inside: '$block' } ) ).to.be.true;
  156. expect( schema.checkQuery( { name: 'p', attribute: 'bold', inside: '$block' } ) ).to.be.false;
  157. } );
  158. } );