schemaitem.js 5.1 KB

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