schemaitem.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { default as Schema, SchemaItem } from '../../../src/model/schema';
  6. let schema, item;
  7. describe( 'SchemaItem', () => {
  8. beforeEach( () => {
  9. schema = new Schema();
  10. schema.registerItem( 'p', '$block' );
  11. schema.registerItem( 'header', '$block' );
  12. schema.registerItem( 'div', '$block' );
  13. schema.registerItem( 'html', '$block' );
  14. schema.registerItem( 'span', '$inline' );
  15. schema.registerItem( 'image', '$inline' );
  16. item = new SchemaItem( schema );
  17. } );
  18. describe( 'constructor()', () => {
  19. it( 'should create empty schema item', () => {
  20. const item = new SchemaItem( schema );
  21. expect( item._disallowed ).to.deep.equal( [] );
  22. expect( item._allowed ).to.deep.equal( [] );
  23. } );
  24. } );
  25. describe( 'allow', () => {
  26. it( 'should add paths to the item as copies of passed array', () => {
  27. const path1 = [ 'div', 'header' ];
  28. const path2 = [ 'p' ];
  29. item.allow( path1 );
  30. item.allow( path2 );
  31. const paths = item._getPaths( 'allow' );
  32. expect( paths.length ).to.equal( 2 );
  33. expect( paths[ 0 ] ).not.to.equal( path1 );
  34. expect( paths[ 1 ] ).not.to.equal( path2 );
  35. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  36. expect( paths[ 1 ] ).to.deep.equal( [ 'p' ] );
  37. } );
  38. it( 'should group paths by attribute', () => {
  39. item.allow( [ 'p' ], 'bold' );
  40. item.allow( [ 'div' ] );
  41. item.allow( [ 'header' ], 'bold' );
  42. const pathsWithNoAttribute = item._getPaths( 'allow' );
  43. const pathsWithBoldAttribute = item._getPaths( 'allow', 'bold' );
  44. expect( pathsWithNoAttribute.length ).to.equal( 1 );
  45. expect( pathsWithNoAttribute[ 0 ] ).to.deep.equal( [ 'div' ] );
  46. expect( pathsWithBoldAttribute.length ).to.equal( 2 );
  47. expect( pathsWithBoldAttribute[ 0 ] ).to.deep.equal( [ 'p' ] );
  48. expect( pathsWithBoldAttribute[ 1 ] ).to.deep.equal( [ 'header' ] );
  49. } );
  50. } );
  51. describe( 'disallow', () => {
  52. it( 'should add paths to the item as copies of passed array', () => {
  53. const path1 = [ 'div', 'header' ];
  54. const path2 = [ 'p' ];
  55. item.disallow( path1 );
  56. item.disallow( path2 );
  57. const paths = item._getPaths( 'disallow' );
  58. expect( paths.length ).to.equal( 2 );
  59. expect( paths[ 0 ] ).not.to.equal( path1 );
  60. expect( paths[ 1 ] ).not.to.equal( path2 );
  61. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  62. expect( paths[ 1 ] ).to.deep.equal( [ 'p' ] );
  63. } );
  64. it( 'should group paths by attribute', () => {
  65. item.disallow( [ 'p' ], 'bold' );
  66. item.disallow( [ 'div' ] );
  67. item.disallow( [ 'header' ], 'bold' );
  68. const pathsWithNoAttribute = item._getPaths( 'disallow' );
  69. const pathsWithBoldAttribute = item._getPaths( 'disallow', 'bold' );
  70. expect( pathsWithNoAttribute.length ).to.equal( 1 );
  71. expect( pathsWithNoAttribute[ 0 ] ).to.deep.equal( [ 'div' ] );
  72. expect( pathsWithBoldAttribute.length ).to.equal( 2 );
  73. expect( pathsWithBoldAttribute[ 0 ] ).to.deep.equal( [ 'p' ] );
  74. expect( pathsWithBoldAttribute[ 1 ] ).to.deep.equal( [ 'header' ] );
  75. } );
  76. } );
  77. describe( '_hasMatchingPath', () => {
  78. it( 'should return true if there is at least one allowed path that matches query path', () => {
  79. item.allow( [ 'div', 'header' ] );
  80. item.allow( [ 'image' ] );
  81. expect( item._hasMatchingPath( 'allow', [ 'div', 'header' ] ) ).to.be.true;
  82. expect( item._hasMatchingPath( 'allow', [ 'html', 'div', 'header' ] ) ).to.be.true;
  83. } );
  84. it( 'should return false if there are no allowed paths that match query path', () => {
  85. item.allow( [ 'div', 'p' ] );
  86. expect( item._hasMatchingPath( 'allow', [ 'div' ] ) ).to.be.false;
  87. expect( item._hasMatchingPath( 'allow', [ 'p', 'div' ] ) ).to.be.false;
  88. expect( item._hasMatchingPath( 'allow', [ 'div', 'p', 'span' ] ) ).to.be.false;
  89. } );
  90. it( 'should return true if there is at least one disallowed path that matches query path', () => {
  91. item.allow( [ 'div', 'header' ] );
  92. item.disallow( [ 'p', 'header' ] );
  93. expect( item._hasMatchingPath( 'disallow', [ 'html', 'div', 'p', 'header' ] ) ).to.be.true;
  94. } );
  95. it( 'should use only paths that are registered for given attribute', () => {
  96. item.allow( [ 'div', 'p' ] );
  97. item.allow( [ 'div' ], 'bold' );
  98. item.allow( [ 'header' ] );
  99. item.disallow( [ 'header' ], 'bold' );
  100. expect( item._hasMatchingPath( 'allow', [ 'html', 'div', 'p' ] ) ).to.be.true;
  101. expect( item._hasMatchingPath( 'allow', [ 'html', 'div' ] ) ).to.be.false;
  102. expect( item._hasMatchingPath( 'allow', [ 'html', 'div' ], 'bold' ) ).to.be.true;
  103. expect( item._hasMatchingPath( 'disallow', [ 'html', 'div', 'header' ] ) ).to.be.false;
  104. expect( item._hasMatchingPath( 'disallow', [ 'html', 'div', 'p', 'header' ], 'bold' ) ).to.be.true;
  105. } );
  106. } );
  107. describe( 'toJSON', () => {
  108. it( 'should create proper JSON string', () => {
  109. const parsedItem = JSON.parse( JSON.stringify( item ) );
  110. expect( parsedItem._schema ).to.equal( '[model.Schema]' );
  111. } );
  112. } );
  113. } );