schemaitem.js 5.0 KB

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