|
|
@@ -33,7 +33,7 @@ describe( 'constructor', () => {
|
|
|
} );
|
|
|
|
|
|
it( 'should allow inline in block', () => {
|
|
|
- expect( schema.checkQuery( { name: '$inline', inside: [ '$block' ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: '$inline', inside: [ '$block' ] } ) ).to.be.true;
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
@@ -59,7 +59,7 @@ describe( 'registerItem', () => {
|
|
|
it( 'should make registered item inherit allows from base item', () => {
|
|
|
schema.registerItem( 'image', '$inline' );
|
|
|
|
|
|
- expect( schema.checkQuery( { name: 'image', inside: [ '$block' ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'image', inside: [ '$block' ] } ) ).to.be.true;
|
|
|
} );
|
|
|
|
|
|
it( 'should throw if item with given name has already been registered in schema', () => {
|
|
|
@@ -108,11 +108,11 @@ describe( 'allow', () => {
|
|
|
schema.registerItem( 'p', '$block' );
|
|
|
schema.registerItem( 'div', '$block' );
|
|
|
|
|
|
- expect( schema.checkQuery( { name: 'p', inside: [ 'div' ] } ) ).to.be.false;
|
|
|
+ expect( schema.check( { name: 'p', inside: [ 'div' ] } ) ).to.be.false;
|
|
|
|
|
|
schema.allow( { name: 'p', inside: 'div' } );
|
|
|
|
|
|
- expect( schema.checkQuery( { name: 'p', inside: [ 'div' ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'p', inside: [ 'div' ] } ) ).to.be.true;
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
@@ -121,91 +121,193 @@ describe( 'disallow', () => {
|
|
|
schema.registerItem( 'p', '$block' );
|
|
|
schema.registerItem( 'div', '$block' );
|
|
|
|
|
|
- schema.allow( { name: '$block', attribute: 'bold', inside: 'div' } );
|
|
|
+ schema.allow( { name: '$block', attributes: 'bold', inside: 'div' } );
|
|
|
|
|
|
- expect( schema.checkQuery( { name: 'p', attribute: 'bold', inside: [ 'div' ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'p', attributes: 'bold', inside: [ 'div' ] } ) ).to.be.true;
|
|
|
|
|
|
- schema.disallow( { name: 'p', attribute: 'bold', inside: 'div' } );
|
|
|
+ schema.disallow( { name: 'p', attributes: 'bold', inside: 'div' } );
|
|
|
|
|
|
- expect( schema.checkQuery( { name: 'p', attribute: 'bold', inside: [ 'div' ] } ) ).to.be.false;
|
|
|
+ expect( schema.check( { name: 'p', attributes: 'bold', inside: [ 'div' ] } ) ).to.be.false;
|
|
|
} );
|
|
|
} );
|
|
|
|
|
|
-describe( 'checkAtPosition', () => {
|
|
|
- let doc, root;
|
|
|
+describe( 'check', () => {
|
|
|
+ describe( 'string or array of strings as inside', () => {
|
|
|
+ it( 'should return false if given element is not registered in schema', () => {
|
|
|
+ expect( schema.check( { name: 'new', inside: [ 'div', 'header' ] } ) ).to.be.false;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should handle path given as string', () => {
|
|
|
+ expect( schema.check( { name: '$inline', inside: '$block $block $block' } ) ).to.be.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should handle attributes', () => {
|
|
|
+ schema.registerItem( 'p', '$block' );
|
|
|
+ schema.allow( { name: 'p', inside: '$block' } );
|
|
|
+
|
|
|
+ expect( schema.check( { name: 'p', inside: [ '$block' ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'p', inside: [ '$block' ], attributes: 'bold' } ) ).to.be.false;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should support required attributes', () => {
|
|
|
+ schema.registerItem( 'a', '$inline' );
|
|
|
+ schema.requireAttributes( 'a', [ 'name' ] );
|
|
|
+ schema.requireAttributes( 'a', [ 'href' ] );
|
|
|
+ schema.allow( { name: 'a', inside: '$block', attributes: [ 'name', 'href', 'title', 'target' ] } );
|
|
|
+
|
|
|
+ // Even though a is allowed in $block thanks to inheriting from $inline, we require href or name attribute.
|
|
|
+ expect( schema.check( { name: 'a', inside: '$block' } ) ).to.be.false;
|
|
|
+
|
|
|
+ // Even though a with title is allowed, we have to meet at least on required attributes set.
|
|
|
+ expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'title' ] } ) ).to.be.false;
|
|
|
+
|
|
|
+ expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name' ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'href' ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name', 'href' ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'a', inside: '$block', attributes: [ 'name', 'title', 'target' ] } ) ).to.be.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should not require attributes from parent schema items', () => {
|
|
|
+ schema.registerItem( 'parent' );
|
|
|
+ schema.registerItem( 'child', 'parent' );
|
|
|
+ schema.allow( { name: 'parent', inside: '$block' } );
|
|
|
+ schema.requireAttributes( 'parent', [ 'required' ] );
|
|
|
+
|
|
|
+ // Even though we require "required" attribute on parent, the requirement should not be inherited.
|
|
|
+ expect( schema.check( { name: 'child', inside: '$block' } ) ).to.be.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should support multiple attributes', () => {
|
|
|
+ // Let's take example case, where image item has to have a pair of "alt" and "src" attributes.
|
|
|
+ // Then it could have other attribute which is allowed on inline elements, i.e. "bold".
|
|
|
+ schema.registerItem( 'img', '$inline' );
|
|
|
+ schema.requireAttributes( 'img', [ 'alt', 'src' ] );
|
|
|
+ schema.allow( { name: '$inline', inside: '$block', attributes: 'bold' } );
|
|
|
+ schema.allow( { name: 'img', inside: '$block', attributes: [ 'alt', 'src' ] } );
|
|
|
+
|
|
|
+ // Image without any attributes is not allowed.
|
|
|
+ expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt' ] } ) ).to.be.false;
|
|
|
+
|
|
|
+ // Image can't have just alt or src.
|
|
|
+ expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt' ] } ) ).to.be.false;
|
|
|
+ expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'src' ] } ) ).to.be.false;
|
|
|
+
|
|
|
+ expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src' ] } ) ).to.be.true;
|
|
|
+
|
|
|
+ // Because of inherting from $inline, image can have bold
|
|
|
+ expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src', 'bold' ] } ) ).to.be.true;
|
|
|
+ // But it can't have only bold without alt or/and src.
|
|
|
+ expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'bold' ] } ) ).to.be.false;
|
|
|
+ expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'src', 'bold' ] } ) ).to.be.false;
|
|
|
+ expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'bold' ] } ) ).to.be.false;
|
|
|
+
|
|
|
+ // Even if image has src and alt, it can't have attributes that weren't allowed
|
|
|
+ expect( schema.check( { name: 'img', inside: '$block', attributes: [ 'alt', 'src', 'attr' ] } ) ).to.be.false;
|
|
|
+ } );
|
|
|
+ } );
|
|
|
|
|
|
- beforeEach( () => {
|
|
|
- doc = new Document();
|
|
|
- root = doc.createRoot( 'root', 'div' );
|
|
|
+ describe( 'array of elements as inside', () => {
|
|
|
+ beforeEach( () => {
|
|
|
+ schema.registerItem( 'div', '$block' );
|
|
|
+ schema.registerItem( 'header', '$block' );
|
|
|
+ schema.registerItem( 'p', '$block' );
|
|
|
+ schema.registerItem( 'img', '$inline' );
|
|
|
|
|
|
- root.insertChildren( 0, [
|
|
|
- new Element( 'div' ),
|
|
|
- new Element( 'header' ),
|
|
|
- new Element( 'p' )
|
|
|
- ] );
|
|
|
+ schema.allow( { name: '$block', inside: 'div' } );
|
|
|
+ schema.allow( { name: '$inline', attributes: 'bold', inside: '$block' } );
|
|
|
|
|
|
- schema.registerItem( 'div', '$block' );
|
|
|
- schema.registerItem( 'header', '$block' );
|
|
|
- schema.registerItem( 'p', '$block' );
|
|
|
+ schema.disallow( { name: '$inline', attributes: 'bold', inside: 'header' } );
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return true if given element is allowed by schema at given position', () => {
|
|
|
+ // P is block and block is allowed in DIV.
|
|
|
+ expect( schema.check( { name: 'p', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
|
|
|
+
|
|
|
+ // IMG is inline and inline is allowed in block.
|
|
|
+ expect( schema.check( { name: 'img', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'img', inside: [ new Element( 'p' ) ] } ) ).to.be.true;
|
|
|
|
|
|
- schema.allow( { name: '$block', inside: 'div' } );
|
|
|
- schema.allow( { name: '$inline', attribute: 'bold', inside: '$block' } );
|
|
|
+ // Inline is allowed in any block and is allowed with attribute bold.
|
|
|
+ expect( schema.check( { name: 'img', inside: [ new Element( 'div' ) ], attributes: [ 'bold' ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'img', inside: [ new Element( 'p' ) ], attributes: [ 'bold' ] } ) ).to.be.true;
|
|
|
|
|
|
- schema.disallow( { name: '$inline', attribute: 'bold', inside: 'header' } );
|
|
|
+ // Inline is allowed in header which is allowed in DIV.
|
|
|
+ expect( schema.check( { name: 'header', inside: [ new Element( 'div' ) ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'img', inside: [ new Element( 'header' ) ] } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: 'img', inside: [ new Element( 'div' ), new Element( 'header' ) ] } ) ).to.be.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return false if given element is not allowed by schema at given position', () => {
|
|
|
+ // P with attribute is not allowed.
|
|
|
+ expect( schema.check( { name: 'p', inside: [ new Element( 'div' ) ], attributes: 'bold' } ) ).to.be.false;
|
|
|
+
|
|
|
+ // Bold text is not allowed in header
|
|
|
+ expect( schema.check( { name: '$text', inside: [ new Element( 'header' ) ], attributes: 'bold' } ) ).to.be.false;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return false if given element is not registered in schema', () => {
|
|
|
+ expect( schema.check( { name: 'new', inside: [ new Element( 'div' ) ] } ) ).to.be.false;
|
|
|
+ } );
|
|
|
} );
|
|
|
|
|
|
- it( 'should return true if given element is allowed by schema at given position', () => {
|
|
|
- // Block should be allowed in root.
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0 ] ), '$block' ) ).to.be.true;
|
|
|
+ describe( 'position as inside', () => {
|
|
|
+ let doc, root;
|
|
|
|
|
|
- // P is block and block should be allowed in root.
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0 ] ), 'p' ) ).to.be.true;
|
|
|
+ beforeEach( () => {
|
|
|
+ doc = new Document();
|
|
|
+ root = doc.createRoot( 'root', 'div' );
|
|
|
|
|
|
- // P is allowed in DIV by the set rule.
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), 'p' ) ).to.be.true;
|
|
|
+ root.insertChildren( 0, [
|
|
|
+ new Element( 'div' ),
|
|
|
+ new Element( 'header' ),
|
|
|
+ new Element( 'p' )
|
|
|
+ ] );
|
|
|
|
|
|
- // Inline is allowed in any block and is allowed with attribute bold.
|
|
|
- // We do not check if it is allowed in header, because it is disallowed by the set rule.
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), '$inline' ) ).to.be.true;
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 2, 0 ] ), '$inline' ) ).to.be.true;
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), '$inline', 'bold' ) ).to.be.true;
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 2, 0 ] ), '$inline', 'bold' ) ).to.be.true;
|
|
|
+ schema.registerItem( 'div', '$block' );
|
|
|
+ schema.registerItem( 'header', '$block' );
|
|
|
+ schema.registerItem( 'p', '$block' );
|
|
|
|
|
|
- // Header is allowed in DIV.
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), 'header' ) ).to.be.true;
|
|
|
+ schema.allow( { name: '$block', inside: 'div' } );
|
|
|
+ schema.allow( { name: '$inline', attributes: 'bold', inside: '$block' } );
|
|
|
|
|
|
- // Inline is allowed in block and root is DIV, which is block.
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0 ] ), '$inline' ) ).to.be.true;
|
|
|
- } );
|
|
|
+ schema.disallow( { name: '$inline', attributes: 'bold', inside: 'header' } );
|
|
|
+ } );
|
|
|
|
|
|
- it( 'should return false if given element is not allowed by schema at given position', () => {
|
|
|
- // P with attribute is not allowed anywhere.
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0 ] ), 'p', 'bold' ) ).to.be.false;
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0, 0 ] ), 'p', 'bold' ) ).to.be.false;
|
|
|
+ it( 'should return true if given element is allowed by schema at given position', () => {
|
|
|
+ // Block should be allowed in root.
|
|
|
+ expect( schema.check( { name: '$block', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
|
|
|
|
|
|
- // Bold text is not allowed in header
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 1, 0 ] ), '$text', 'bold' ) ).to.be.false;
|
|
|
- } );
|
|
|
+ // P is block and block should be allowed in root.
|
|
|
+ expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
|
|
|
|
|
|
- it( 'should return false if given element is not registered in schema', () => {
|
|
|
- expect( schema.checkAtPosition( new Position( root, [ 0 ] ), 'new' ) ).to.be.false;
|
|
|
- } );
|
|
|
-} );
|
|
|
+ // P is allowed in DIV by the set rule.
|
|
|
+ expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
|
|
|
|
|
|
-describe( 'checkQuery', () => {
|
|
|
- it( 'should return false if given element is not registered in schema', () => {
|
|
|
- expect( schema.checkQuery( { name: 'new', inside: [ 'div', 'header' ] } ) ).to.be.false;
|
|
|
- } );
|
|
|
+ // Inline is allowed in any block and is allowed with attribute bold.
|
|
|
+ // We do not check if it is allowed in header, because it is disallowed by the set rule.
|
|
|
+ expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ) } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: '$inline', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.true;
|
|
|
+ expect( schema.check( { name: '$inline', inside: new Position( root, [ 2, 0 ] ), attributes: 'bold' } ) ).to.be.true;
|
|
|
|
|
|
- it( 'should handle path given as string', () => {
|
|
|
- expect( schema.checkQuery( { name: '$inline', inside: '$block $block $block' } ) ).to.be.true;
|
|
|
- } );
|
|
|
+ // Header is allowed in DIV.
|
|
|
+ expect( schema.check( { name: 'header', inside: new Position( root, [ 0, 0 ] ) } ) ).to.be.true;
|
|
|
|
|
|
- it( 'should handle attributes', () => {
|
|
|
- schema.registerItem( 'p', '$block' );
|
|
|
- schema.allow( { name: 'p', inside: '$block' } );
|
|
|
+ // Inline is allowed in block and root is DIV, which is block.
|
|
|
+ expect( schema.check( { name: '$inline', inside: new Position( root, [ 0 ] ) } ) ).to.be.true;
|
|
|
+ } );
|
|
|
+
|
|
|
+ it( 'should return false if given element is not allowed by schema at given position', () => {
|
|
|
+ // P with attribute is not allowed anywhere.
|
|
|
+ expect( schema.check( { name: 'p', inside: new Position( root, [ 0 ] ), attributes: 'bold' } ) ).to.be.false;
|
|
|
+ expect( schema.check( { name: 'p', inside: new Position( root, [ 0, 0 ] ), attributes: 'bold' } ) ).to.be.false;
|
|
|
+
|
|
|
+ // Bold text is not allowed in header
|
|
|
+ expect( schema.check( { name: '$text', inside: new Position( root, [ 1, 0 ] ), attributes: 'bold' } ) ).to.be.false;
|
|
|
+ } );
|
|
|
|
|
|
- expect( schema.checkQuery( { name: 'p', inside: '$block' } ) ).to.be.true;
|
|
|
- expect( schema.checkQuery( { name: 'p', attribute: 'bold', inside: '$block' } ) ).to.be.false;
|
|
|
+ it( 'should return false if given element is not registered in schema', () => {
|
|
|
+ expect( schema.check( { name: 'new', inside: new Position( root, [ 0 ] ) } ) ).to.be.false;
|
|
|
+ } );
|
|
|
} );
|
|
|
} );
|