8
0

schemaitem.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. 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( 'addAllowed', () => {
  27. it( 'should add paths to the item as copies of passed array', () => {
  28. let path1 = [ 'div', 'header' ];
  29. let path2 = [ 'p' ];
  30. item.addAllowed( path1 );
  31. item.addAllowed( 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 accept paths as string with element names separated with space', () => {
  40. item.addAllowed( 'div header' );
  41. let paths = item._getPaths( 'ALLOW' );
  42. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  43. } );
  44. it( 'should group paths by attribute', () => {
  45. item.addAllowed( 'p', 'bold' );
  46. item.addAllowed( 'div' );
  47. item.addAllowed( 'header', 'bold' );
  48. let pathsWithNoAttribute = item._getPaths( 'ALLOW' );
  49. let pathsWithBoldAttribute = item._getPaths( 'ALLOW', 'bold' );
  50. expect( pathsWithNoAttribute.length ).to.equal( 1 );
  51. expect( pathsWithNoAttribute[ 0 ] ).to.deep.equal( [ 'div' ] );
  52. expect( pathsWithBoldAttribute.length ).to.equal( 2 );
  53. expect( pathsWithBoldAttribute[ 0 ] ).to.deep.equal( [ 'p' ] );
  54. expect( pathsWithBoldAttribute[ 1 ] ).to.deep.equal( [ 'header' ] );
  55. } );
  56. } );
  57. describe( 'addDisallowed', () => {
  58. it( 'should add paths to the item as copies of passed array', () => {
  59. let path1 = [ 'div', 'header' ];
  60. let path2 = [ 'p' ];
  61. item.addDisallowed( path1 );
  62. item.addDisallowed( path2 );
  63. let paths = item._getPaths( 'DISALLOW' );
  64. expect( paths.length ).to.equal( 2 );
  65. expect( paths[ 0 ] ).not.to.equal( path1 );
  66. expect( paths[ 1 ] ).not.to.equal( path2 );
  67. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  68. expect( paths[ 1 ] ).to.deep.equal( [ 'p' ] );
  69. } );
  70. it( 'should accept paths as string with element names separated with space', () => {
  71. item.addDisallowed( 'div header' );
  72. let paths = item._getPaths( 'DISALLOW' );
  73. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  74. } );
  75. it( 'should group paths by attribute', () => {
  76. item.addDisallowed( 'p', 'bold' );
  77. item.addDisallowed( 'div' );
  78. item.addDisallowed( 'header', 'bold' );
  79. let pathsWithNoAttribute = item._getPaths( 'DISALLOW' );
  80. let pathsWithBoldAttribute = item._getPaths( 'DISALLOW', 'bold' );
  81. expect( pathsWithNoAttribute.length ).to.equal( 1 );
  82. expect( pathsWithNoAttribute[ 0 ] ).to.deep.equal( [ 'div' ] );
  83. expect( pathsWithBoldAttribute.length ).to.equal( 2 );
  84. expect( pathsWithBoldAttribute[ 0 ] ).to.deep.equal( [ 'p' ] );
  85. expect( pathsWithBoldAttribute[ 1 ] ).to.deep.equal( [ 'header' ] );
  86. } );
  87. } );
  88. describe( '_hasMatchingPath', () => {
  89. it( 'should return true if there is at least one allowed path that matches query path', () => {
  90. item.addAllowed( 'div header' );
  91. item.addAllowed( 'image' );
  92. expect( item._hasMatchingPath( 'ALLOW', [ 'div', 'header' ] ) ).to.be.true;
  93. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div', 'header' ] ) ).to.be.true;
  94. expect( item._hasMatchingPath( 'ALLOW', [ 'div', 'header', 'span' ] ) ).to.be.true;
  95. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div', 'p', 'header', 'span' ] ) ).to.be.true;
  96. } );
  97. it( 'should return false if there are no allowed paths that match query path', () => {
  98. item.addAllowed( 'div p' );
  99. expect( item._hasMatchingPath( 'ALLOW', [ 'p' ] ) ).to.be.false;
  100. expect( item._hasMatchingPath( 'ALLOW', [ 'div' ] ) ).to.be.false;
  101. expect( item._hasMatchingPath( 'ALLOW', [ 'p', 'div' ] ) ).to.be.false;
  102. } );
  103. it( 'should return true if there is at least one disallowed path that matches query path', () => {
  104. item.addAllowed( 'div header' );
  105. item.addDisallowed( 'p header' );
  106. expect( item._hasMatchingPath( 'DISALLOW', [ 'html', 'div', 'p', 'header', 'span' ] ) ).to.be.true;
  107. } );
  108. it( 'should use only paths that are registered for given attribute', () => {
  109. item.addAllowed( 'div p' );
  110. item.addAllowed( 'div', 'bold' );
  111. item.addAllowed( 'header' );
  112. item.addDisallowed( 'header', 'bold' );
  113. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div', 'p' ] ) ).to.be.true;
  114. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div' ] ) ).to.be.false;
  115. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div' ], 'bold' ) ).to.be.true;
  116. expect( item._hasMatchingPath( 'DISALLOW', [ 'html', 'div', 'header' ] ) ).to.be.false;
  117. expect( item._hasMatchingPath( 'DISALLOW', [ 'html', 'div', 'p', 'header', 'span' ], 'bold' ) ).to.be.true;
  118. } );
  119. } );
  120. describe( 'toJSON', () => {
  121. it( 'should create proper JSON string', () => {
  122. let parsedItem = JSON.parse( JSON.stringify( item ) );
  123. expect( parsedItem._schema ).to.equal( '[treeModel.Schema]' );
  124. } );
  125. } );