|
|
@@ -4,11 +4,8 @@
|
|
|
*/
|
|
|
|
|
|
import Document from '/ckeditor5/engine/model/document.js';
|
|
|
-import Range from '/ckeditor5/engine/model/range.js';
|
|
|
-import Position from '/ckeditor5/engine/model/position.js';
|
|
|
-import Text from '/ckeditor5/engine/model/text.js';
|
|
|
-import Element from '/ckeditor5/engine/model/element.js';
|
|
|
import isAttributeAllowedInSelection from '/ckeditor5/core/command/helpers/isattributeallowedinselection.js';
|
|
|
+import { setData } from '/tests/engine/_utils/model.js';
|
|
|
|
|
|
describe( 'isAttributeAllowedInSelection', () => {
|
|
|
const attribute = 'bold';
|
|
|
@@ -28,30 +25,19 @@ describe( 'isAttributeAllowedInSelection', () => {
|
|
|
|
|
|
// Disallow bold on image.
|
|
|
document.schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } );
|
|
|
-
|
|
|
- root.insertChildren( 0, [
|
|
|
- new Element( 'p', [], [
|
|
|
- new Text( 'foo' ),
|
|
|
- new Element( 'img' ),
|
|
|
- new Element( 'img' ),
|
|
|
- new Text( 'bar' )
|
|
|
- ] ),
|
|
|
- new Element( 'h1' ),
|
|
|
- new Element( 'p' )
|
|
|
- ] );
|
|
|
} );
|
|
|
|
|
|
describe( 'when selection is collapsed', () => {
|
|
|
it( 'should return true if characters with the attribute can be placed at caret position', () => {
|
|
|
- document.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] );
|
|
|
+ setData( document, '<p>f[]oo</p>' );
|
|
|
expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
|
|
|
} );
|
|
|
|
|
|
it( 'should return false if characters with the attribute cannot be placed at caret position', () => {
|
|
|
- document.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] );
|
|
|
+ setData( document, '<h1>[]</h1>' );
|
|
|
expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.false;
|
|
|
|
|
|
- document.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 2 ] ) ) ] );
|
|
|
+ setData( document, '[]' );
|
|
|
expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.false;
|
|
|
} );
|
|
|
} );
|
|
|
@@ -59,29 +45,29 @@ describe( 'isAttributeAllowedInSelection', () => {
|
|
|
describe( 'when selection is not collapsed', () => {
|
|
|
it( 'should return true if there is at least one node in selection that can have the attribute', () => {
|
|
|
// Simple selection on a few characters.
|
|
|
- document.selection.setRanges( [ new Range( new Position( root, [ 0, 0 ] ), new Position( root, [ 0, 3 ] ) ) ] );
|
|
|
+ setData( document, '<p>[foo]</p>' );
|
|
|
expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
|
|
|
|
|
|
// Selection spans over characters but also include nodes that can't have attribute.
|
|
|
- document.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 6 ] ) ) ] );
|
|
|
+ setData( document, '<p>fo[o<img />b]ar</p>' );
|
|
|
expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
|
|
|
|
|
|
// Selection on whole root content. Characters in P can have an attribute so it's valid.
|
|
|
- document.selection.setRanges( [ new Range( new Position( root, [ 0 ] ), new Position( root, [ 3 ] ) ) ] );
|
|
|
+ setData( document, '[<p>foo<img />bar</p><h1></h1>]' );
|
|
|
expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
|
|
|
|
|
|
// Selection on empty P. P can have the attribute.
|
|
|
- document.selection.setRanges( [ new Range( new Position( root, [ 2 ] ), new Position( root, [ 3 ] ) ) ] );
|
|
|
+ setData( document, '[<p></p>]' );
|
|
|
expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.true;
|
|
|
} );
|
|
|
|
|
|
it( 'should return false if there are no nodes in selection that can have the attribute', () => {
|
|
|
// Selection on DIV which can't have bold text.
|
|
|
- document.selection.setRanges( [ new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) ) ] );
|
|
|
+ setData( document, '[<h1></h1>]' );
|
|
|
expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.false;
|
|
|
|
|
|
// Selection on two images which can't be bold.
|
|
|
- document.selection.setRanges( [ new Range( new Position( root, [ 0, 3 ] ), new Position( root, [ 0, 5 ] ) ) ] );
|
|
|
+ setData( document, '<p>foo[<img /><img />]bar</p>' );
|
|
|
expect( isAttributeAllowedInSelection( attribute, document.selection, document.schema ) ).to.be.false;
|
|
|
} );
|
|
|
} );
|