schemaitem.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. import CKEditorError from '/ckeditor5/core/ckeditorerror.js';
  9. let schema, item;
  10. beforeEach( () => {
  11. schema = new Schema();
  12. schema.registerItem( 'p', 'block' );
  13. schema.registerItem( 'header', 'block' );
  14. schema.registerItem( 'div', 'block' );
  15. schema.registerItem( 'html', 'block' );
  16. schema.registerItem( 'span', 'inline' );
  17. schema.registerItem( 'image', 'inline' );
  18. item = new SchemaItem( schema );
  19. } );
  20. describe( 'constructor', () => {
  21. it( 'should create empty schema item', () => {
  22. let item = new SchemaItem( schema );
  23. expect( item._disallowed ).to.deep.equal( [] );
  24. expect( item._allowed ).to.deep.equal( [] );
  25. } );
  26. it( 'should throw if no schema was passed', () => {
  27. expect( () => {
  28. new SchemaItem();
  29. } ).to.throw( CKEditorError, /schema-item-no-schema/ );
  30. } );
  31. } );
  32. describe( 'addAllowed', () => {
  33. it( 'should add paths to the item as copies of passed array', () => {
  34. let path1 = [ 'div', 'header' ];
  35. let path2 = [ 'p' ];
  36. item.addAllowed( path1 );
  37. item.addAllowed( path2 );
  38. let paths = item._getPaths( 'ALLOW' );
  39. expect( paths.length ).to.equal( 2 );
  40. expect( paths[ 0 ] ).not.to.equal( path1 );
  41. expect( paths[ 1 ] ).not.to.equal( path2 );
  42. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  43. expect( paths[ 1 ] ).to.deep.equal( [ 'p' ] );
  44. } );
  45. it( 'should accept paths as string with element names separated with space', () => {
  46. item.addAllowed( 'div header' );
  47. let paths = item._getPaths( 'ALLOW' );
  48. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  49. } );
  50. it( 'should group paths by attribute', () => {
  51. item.addAllowed( 'p', 'bold' );
  52. item.addAllowed( 'div' );
  53. item.addAllowed( 'header', 'bold' );
  54. let pathsWithNoAttribute = item._getPaths( 'ALLOW' );
  55. let pathsWithBoldAttribute = item._getPaths( 'ALLOW', 'bold' );
  56. expect( pathsWithNoAttribute.length ).to.equal( 1 );
  57. expect( pathsWithNoAttribute[ 0 ] ).to.deep.equal( [ 'div' ] );
  58. expect( pathsWithBoldAttribute.length ).to.equal( 2 );
  59. expect( pathsWithBoldAttribute[ 0 ] ).to.deep.equal( [ 'p' ] );
  60. expect( pathsWithBoldAttribute[ 1 ] ).to.deep.equal( [ 'header' ] );
  61. } );
  62. } );
  63. describe( 'addDisallowed', () => {
  64. it( 'should add paths to the item as copies of passed array', () => {
  65. let path1 = [ 'div', 'header' ];
  66. let path2 = [ 'p' ];
  67. item.addDisallowed( path1 );
  68. item.addDisallowed( path2 );
  69. let paths = item._getPaths( 'DISALLOW' );
  70. expect( paths.length ).to.equal( 2 );
  71. expect( paths[ 0 ] ).not.to.equal( path1 );
  72. expect( paths[ 1 ] ).not.to.equal( path2 );
  73. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  74. expect( paths[ 1 ] ).to.deep.equal( [ 'p' ] );
  75. } );
  76. it( 'should accept paths as string with element names separated with space', () => {
  77. item.addDisallowed( 'div header' );
  78. let paths = item._getPaths( 'DISALLOW' );
  79. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  80. } );
  81. it( 'should group paths by attribute', () => {
  82. item.addDisallowed( 'p', 'bold' );
  83. item.addDisallowed( 'div' );
  84. item.addDisallowed( 'header', 'bold' );
  85. let pathsWithNoAttribute = item._getPaths( 'DISALLOW' );
  86. let pathsWithBoldAttribute = item._getPaths( 'DISALLOW', 'bold' );
  87. expect( pathsWithNoAttribute.length ).to.equal( 1 );
  88. expect( pathsWithNoAttribute[ 0 ] ).to.deep.equal( [ 'div' ] );
  89. expect( pathsWithBoldAttribute.length ).to.equal( 2 );
  90. expect( pathsWithBoldAttribute[ 0 ] ).to.deep.equal( [ 'p' ] );
  91. expect( pathsWithBoldAttribute[ 1 ] ).to.deep.equal( [ 'header' ] );
  92. } );
  93. } );
  94. describe( '_hasMatchingPath', () => {
  95. it( 'should return true if there is at least one allowed path that matches query path', () => {
  96. item.addAllowed( 'div header' );
  97. item.addAllowed( 'image' );
  98. expect( item._hasMatchingPath( 'ALLOW', [ 'div', 'header' ] ) ).to.be.true;
  99. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div', 'header' ] ) ).to.be.true;
  100. expect( item._hasMatchingPath( 'ALLOW', [ 'div', 'header', 'span' ] ) ).to.be.true;
  101. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div', 'p', 'header', 'span' ] ) ).to.be.true;
  102. } );
  103. it( 'should return false if there are no allowed paths that match query path', () => {
  104. item.addAllowed( 'div p' );
  105. expect( item._hasMatchingPath( 'ALLOW', [ 'p' ] ) ).to.be.false;
  106. expect( item._hasMatchingPath( 'ALLOW', [ 'div' ] ) ).to.be.false;
  107. expect( item._hasMatchingPath( 'ALLOW', [ 'p', 'div' ] ) ).to.be.false;
  108. } );
  109. it( 'should return true if there is at least one disallowed path that matches query path', () => {
  110. item.addAllowed( 'div header' );
  111. item.addDisallowed( 'p header' );
  112. expect( item._hasMatchingPath( 'DISALLOW', [ 'html', 'div', 'p', 'header', 'span' ] ) ).to.be.true;
  113. } );
  114. it( 'should use only paths that are registered for given attribute', () => {
  115. item.addAllowed( 'div p' );
  116. item.addAllowed( 'div', 'bold' );
  117. item.addAllowed( 'header' );
  118. item.addDisallowed( 'header', 'bold' );
  119. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div', 'p' ] ) ).to.be.true;
  120. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div' ] ) ).to.be.false;
  121. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div' ], 'bold' ) ).to.be.true;
  122. expect( item._hasMatchingPath( 'DISALLOW', [ 'html', 'div', 'header' ] ) ).to.be.false;
  123. expect( item._hasMatchingPath( 'DISALLOW', [ 'html', 'div', 'p', 'header', 'span' ], 'bold' ) ).to.be.true;
  124. } );
  125. } );