8
0

schemaitem.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import Schema from '/ckeditor5/core/treemodel/schema.js';
  8. import { SchemaItem as SchemaItem } from '/ckeditor5/core/treemodel/schema.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. } );
  27. describe( 'allow', () => {
  28. it( 'should add paths to the item as copies of passed array', () => {
  29. let path1 = [ 'div', 'header' ];
  30. let path2 = [ 'p' ];
  31. item.allow( path1 );
  32. item.allow( path2 );
  33. let paths = item._getPaths( 'ALLOW' );
  34. expect( paths.length ).to.equal( 2 );
  35. expect( paths[ 0 ] ).not.to.equal( path1 );
  36. expect( paths[ 1 ] ).not.to.equal( path2 );
  37. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  38. expect( paths[ 1 ] ).to.deep.equal( [ 'p' ] );
  39. } );
  40. it( 'should accept paths as string with element names separated with space', () => {
  41. item.allow( 'div header' );
  42. let paths = item._getPaths( 'ALLOW' );
  43. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  44. } );
  45. it( 'should group paths by attribute', () => {
  46. item.allow( 'p', 'bold' );
  47. item.allow( 'div' );
  48. item.allow( 'header', 'bold' );
  49. let pathsWithNoAttribute = item._getPaths( 'ALLOW' );
  50. let pathsWithBoldAttribute = item._getPaths( 'ALLOW', 'bold' );
  51. expect( pathsWithNoAttribute.length ).to.equal( 1 );
  52. expect( pathsWithNoAttribute[ 0 ] ).to.deep.equal( [ 'div' ] );
  53. expect( pathsWithBoldAttribute.length ).to.equal( 2 );
  54. expect( pathsWithBoldAttribute[ 0 ] ).to.deep.equal( [ 'p' ] );
  55. expect( pathsWithBoldAttribute[ 1 ] ).to.deep.equal( [ 'header' ] );
  56. } );
  57. } );
  58. describe( 'disallow', () => {
  59. it( 'should add paths to the item as copies of passed array', () => {
  60. let path1 = [ 'div', 'header' ];
  61. let path2 = [ 'p' ];
  62. item.disallow( path1 );
  63. item.disallow( path2 );
  64. let paths = item._getPaths( 'DISALLOW' );
  65. expect( paths.length ).to.equal( 2 );
  66. expect( paths[ 0 ] ).not.to.equal( path1 );
  67. expect( paths[ 1 ] ).not.to.equal( path2 );
  68. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  69. expect( paths[ 1 ] ).to.deep.equal( [ 'p' ] );
  70. } );
  71. it( 'should accept paths as string with element names separated with space', () => {
  72. item.disallow( 'div header' );
  73. let paths = item._getPaths( 'DISALLOW' );
  74. expect( paths[ 0 ] ).to.deep.equal( [ 'div', 'header' ] );
  75. } );
  76. it( 'should group paths by attribute', () => {
  77. item.disallow( 'p', 'bold' );
  78. item.disallow( 'div' );
  79. item.disallow( 'header', 'bold' );
  80. let pathsWithNoAttribute = item._getPaths( 'DISALLOW' );
  81. let pathsWithBoldAttribute = item._getPaths( 'DISALLOW', 'bold' );
  82. expect( pathsWithNoAttribute.length ).to.equal( 1 );
  83. expect( pathsWithNoAttribute[ 0 ] ).to.deep.equal( [ 'div' ] );
  84. expect( pathsWithBoldAttribute.length ).to.equal( 2 );
  85. expect( pathsWithBoldAttribute[ 0 ] ).to.deep.equal( [ 'p' ] );
  86. expect( pathsWithBoldAttribute[ 1 ] ).to.deep.equal( [ 'header' ] );
  87. } );
  88. } );
  89. describe( '_hasMatchingPath', () => {
  90. it( 'should return true if there is at least one allowed path that matches query path', () => {
  91. item.allow( 'div header' );
  92. item.allow( 'image' );
  93. expect( item._hasMatchingPath( 'ALLOW', [ 'div', 'header' ] ) ).to.be.true;
  94. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div', 'header' ] ) ).to.be.true;
  95. expect( item._hasMatchingPath( 'ALLOW', [ 'div', 'header', 'span' ] ) ).to.be.true;
  96. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div', 'p', 'header', 'span' ] ) ).to.be.true;
  97. } );
  98. it( 'should return false if there are no allowed paths that match query path', () => {
  99. item.allow( 'div p' );
  100. expect( item._hasMatchingPath( 'ALLOW', [ 'p' ] ) ).to.be.false;
  101. expect( item._hasMatchingPath( 'ALLOW', [ 'div' ] ) ).to.be.false;
  102. expect( item._hasMatchingPath( 'ALLOW', [ 'p', 'div' ] ) ).to.be.false;
  103. } );
  104. it( 'should return true if there is at least one disallowed path that matches query path', () => {
  105. item.allow( 'div header' );
  106. item.disallow( 'p header' );
  107. expect( item._hasMatchingPath( 'DISALLOW', [ 'html', 'div', 'p', 'header', 'span' ] ) ).to.be.true;
  108. } );
  109. it( 'should use only paths that are registered for given attribute', () => {
  110. item.allow( 'div p' );
  111. item.allow( 'div', 'bold' );
  112. item.allow( 'header' );
  113. item.disallow( 'header', 'bold' );
  114. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div', 'p' ] ) ).to.be.true;
  115. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div' ] ) ).to.be.false;
  116. expect( item._hasMatchingPath( 'ALLOW', [ 'html', 'div' ], 'bold' ) ).to.be.true;
  117. expect( item._hasMatchingPath( 'DISALLOW', [ 'html', 'div', 'header' ] ) ).to.be.false;
  118. expect( item._hasMatchingPath( 'DISALLOW', [ 'html', 'div', 'p', 'header', 'span' ], 'bold' ) ).to.be.true;
  119. } );
  120. } );
  121. describe( 'toJSON', () => {
  122. it( 'should create proper JSON string', () => {
  123. let parsedItem = JSON.parse( JSON.stringify( item ) );
  124. expect( parsedItem._schema ).to.equal( '[treeModel.Schema]' );
  125. } );
  126. } );