Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it( 'do nothing when another key is pressed', () => {
// fooxxx{}bar
const p = new ViewContainerElement( 'p', null, [ foo, ui, bar ] );
viewRoot._appendChild( p );
view.change( writer => {
writer.setSelection( [ ViewRange._createFromParentsAndOffsets( bar, 0, bar, 0 ) ] );
} );
renderAndFireKeydownEvent( { keyCode: keyCodes.arrowleft } );
testUtils.checkAssertions(
() => check( 'bar', 0 ),
// Safari renders selection at the end of the text node.
() => check( 'xxx', 3 )
);
} );
emitter.listenTo( view.document, 'keydown', ( evt, data ) => {
// This implementation works only for collapsed selection.
if ( !modelSelection.isCollapsed ) {
return;
}
// When user tries to expand the selection or jump over the whole word or to the beginning/end then
// two-steps movement is not necessary.
if ( data.shiftKey || data.altKey || data.ctrlKey ) {
return;
}
const arrowRightPressed = data.keyCode == keyCodes.arrowright;
const arrowLeftPressed = data.keyCode == keyCodes.arrowleft;
// When neither left or right arrow has been pressed then do noting.
if ( !arrowRightPressed && !arrowLeftPressed ) {
return;
}
const position = modelSelection.getFirstPosition();
const contentDirection = locale.contentLanguageDirection;
let isMovementHandled;
if ( ( contentDirection === 'ltr' && arrowRightPressed ) || ( contentDirection === 'rtl' && arrowLeftPressed ) ) {
isMovementHandled = twoStepCaretHandler.handleForwardMovement( position, data );
} else {
isMovementHandled = twoStepCaretHandler.handleBackwardMovement( position, data );
}
'[]foo',
keyCodes.arrowright,
'[]foo'
);
test(
'should move selection forward from selected object - down arrow',
'[]foo',
keyCodes.arrowdown,
'[]foo'
);
test(
'should move selection backward from selected object - left arrow',
'foo[]',
keyCodes.arrowleft,
'foo[]'
);
test(
'should move selection backward from selected object - up arrow',
'foo[]',
keyCodes.arrowup,
'foo[]'
);
test(
'should move selection to next widget - right arrow',
'[]',
keyCodes.arrowright,
'[]'
);
it( 'should do nothing when alt key is pressed', () => {
setData( model, '<$text c="true">foo<$text a="true" b="true">b[]ar' );
fireKeyDownEvent( {
keyCode: keyCodes.arrowleft,
altKey: true
} );
expect( selection.isGravityOverridden ).to.be.false;
} );
'foo[]',
keyCodes.arrowright,
'foo[]'
);
test(
'should move selection to object element - down arrow',
'foo[]',
keyCodes.arrowdown,
'foo[]'
);
test(
'should move selection to object element - left arrow',
'[]foo',
keyCodes.arrowleft,
'[]foo'
);
test(
'should move selection to object element - up arrow',
'[]foo',
keyCodes.arrowup,
'[]foo'
);
test(
'do nothing on non objects - right arrow',
'foo[]bar',
keyCodes.arrowright,
'foo[]bar'
);
it( 'intercepts the arrow* events and overrides the default toolbar behavior', () => {
const keyEvtData = {
stopPropagation: sinon.spy()
};
keyEvtData.keyCode = keyCodes.arrowdown;
view.keystrokes.press( keyEvtData );
sinon.assert.calledOnce( keyEvtData.stopPropagation );
keyEvtData.keyCode = keyCodes.arrowup;
view.keystrokes.press( keyEvtData );
sinon.assert.calledTwice( keyEvtData.stopPropagation );
keyEvtData.keyCode = keyCodes.arrowleft;
view.keystrokes.press( keyEvtData );
sinon.assert.calledThrice( keyEvtData.stopPropagation );
keyEvtData.keyCode = keyCodes.arrowright;
view.keystrokes.press( keyEvtData );
sinon.assert.callCount( keyEvtData.stopPropagation, 4 );
} );
it( 'should do nothing if is disabled', () => {
observer.disable();
return checkEventPrevention( keyCodes.arrowleft, false );
} );
const viewB = writer.document.selection.getFirstPosition().parent;
const viewTextX = parse( 'x' );
viewB._appendChild( viewTextX );
writer.setSelection( viewTextX, 1 );
const domB = view.getDomRoot( 'main' ).querySelector( 'b' );
domB.childNodes[ 0 ].data += 'x';
const domRange = document.createRange();
domSelection.removeAllRanges();
domRange.setStart( domB.childNodes[ 0 ], INLINE_FILLER_LENGTH + 1 );
domRange.collapse( true );
domSelection.addRange( domRange );
} );
viewDocument.fire( 'keydown', { keyCode: keyCodes.arrowleft, domTarget: view.domRoots.get( 'main' ) } );
expect( startsWithFiller( domSelection.anchorNode ) ).to.be.true;
expect( domSelection.anchorOffset ).to.equal( INLINE_FILLER_LENGTH + 1 );
expect( domSelection.isCollapsed ).to.be.true;
} );
} );
function jumpOverInlineFiller( evt, data ) {
if ( data.keyCode == keyCodes.arrowleft ) {
const domSelection = data.domTarget.ownerDocument.defaultView.getSelection();
if ( domSelection.rangeCount == 1 && domSelection.getRangeAt( 0 ).collapsed ) {
const domParent = domSelection.getRangeAt( 0 ).startContainer;
const domOffset = domSelection.getRangeAt( 0 ).startOffset;
if ( startsWithFiller( domParent ) && domOffset <= INLINE_FILLER_LENGTH ) {
domSelection.collapse( domParent, 0 );
}
}
}
}