utils.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import { getNodeSchemaName, removeDisallowedAttributes } from '../../src/model/utils';
  6. import Element from '../../src/model/element';
  7. import Text from '../../src/model/text';
  8. import Document from '../../src/model/document';
  9. import { stringify } from '../../src/dev-utils/model';
  10. describe( 'model utils', () => {
  11. describe( 'getNodeSchemaName()', () => {
  12. it( 'should return schema name for a given Element', () => {
  13. const element = new Element( 'paragraph' );
  14. expect( getNodeSchemaName( element ) ).to.equal( 'paragraph' );
  15. } );
  16. it( 'should return schema name for a Text', () => {
  17. const element = new Text();
  18. expect( getNodeSchemaName( element ) ).to.equal( '$text' );
  19. } );
  20. } );
  21. describe( 'removeDisallowedAttributes()', () => {
  22. let doc;
  23. beforeEach( () => {
  24. doc = new Document();
  25. doc.createRoot();
  26. const schema = doc.schema;
  27. schema.registerItem( 'paragraph', '$block' );
  28. schema.registerItem( 'el', '$inline' );
  29. schema.allow( { name: '$text', attributes: 'a', inside: 'paragraph' } );
  30. schema.allow( { name: '$text', attributes: 'c', inside: 'paragraph' } );
  31. schema.allow( { name: 'el', attributes: 'b' } );
  32. } );
  33. it( 'should remove disallowed by schema attributes from list of nodes', () => {
  34. const paragraph = new Element( 'paragraph' );
  35. const el = new Element( 'el', { a: 1, b: 1, c: 1 } );
  36. const foo = new Text( 'foo', { a: 1, b: 1 } );
  37. const bar = new Text( 'bar' );
  38. const biz = new Text( 'biz', { b: 1, c: 1 } );
  39. paragraph.appendChildren( [ el, foo, bar, biz ] );
  40. removeDisallowedAttributes( Array.from( paragraph.getChildren() ), [ paragraph ], doc.schema );
  41. expect( stringify( paragraph ) )
  42. .to.equal( '<paragraph><el b="1"></el><$text a="1">foo</$text>bar<$text c="1">biz</$text></paragraph>' );
  43. } );
  44. it( 'should remove disallowed by schema attributes from a single node', () => {
  45. const paragraph = new Element( 'paragraph' );
  46. const foo = new Text( 'foo', { a: 1, b: 1 } );
  47. removeDisallowedAttributes( foo, [ paragraph ], doc.schema );
  48. expect( stringify( foo ) ).to.equal( '<$text a="1">foo</$text>' );
  49. } );
  50. } );
  51. } );