schema.js 7.2 KB

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