Best JavaScript code snippet using wpt
widgettypearound.js
Source:widgettypearound.js
...235 } );236 describe( '"fake caret" activation', () => {237 it( 'should activate before when the collapsed selection is before a widget and the navigation is forward', () => {238 setModelData( editor.model, '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );239 fireKeyboardEvent( 'arrowright' );240 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );241 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );242 const viewWidget = viewRoot.getChild( 1 );243 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.true;244 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;245 sinon.assert.calledOnce( eventInfoStub.stop );246 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );247 } );248 it( 'should activate after when the collapsed selection is after a widget and the navigation is backward', () => {249 setModelData( editor.model, '<blockWidget></blockWidget><paragraph>[]foo</paragraph>' );250 fireKeyboardEvent( 'arrowleft' );251 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]<paragraph>foo</paragraph>' );252 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );253 const viewWidget = viewRoot.getChild( 0 );254 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;255 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.true;256 sinon.assert.calledOnce( eventInfoStub.stop );257 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );258 } );259 it( 'should activate after when the widget is selected and the navigation is forward', () => {260 setModelData( editor.model, '[<blockWidget></blockWidget>]' );261 fireKeyboardEvent( 'arrowright' );262 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );263 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );264 const viewWidget = viewRoot.getChild( 0 );265 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;266 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.true;267 sinon.assert.calledOnce( eventInfoStub.stop );268 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );269 } );270 it( 'should activate before when the widget is selected and the navigation is backward', () => {271 setModelData( editor.model, '[<blockWidget></blockWidget>]' );272 fireKeyboardEvent( 'arrowleft' );273 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );274 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );275 const viewWidget = viewRoot.getChild( 0 );276 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.true;277 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;278 sinon.assert.calledOnce( eventInfoStub.stop );279 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );280 } );281 it( 'should activate if an arrow key is pressed along with Shift', () => {282 setModelData( editor.model, '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );283 fireKeyboardEvent( 'arrowright', { shiftKey: true } );284 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );285 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );286 sinon.assert.calledOnce( eventInfoStub.stop );287 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );288 } );289 it( 'should not activate when the selection is before the widget but the non-arrow key was pressed', () => {290 setModelData( editor.model, '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );291 fireKeyboardEvent( 'a' );292 fireMutation( 'a' );293 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;294 expect( getModelData( model ) ).to.equal( '<paragraph>fooa[]</paragraph><blockWidget></blockWidget>' );295 const viewWidget = viewRoot.getChild( 1 );296 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;297 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;298 sinon.assert.notCalled( eventInfoStub.stop );299 sinon.assert.notCalled( domEventDataStub.domEvent.preventDefault );300 } );301 it( 'should not activate when the selection is not before the widget and navigating forward', () => {302 setModelData( editor.model, '<paragraph>fo[]o</paragraph><blockWidget></blockWidget>' );303 fireKeyboardEvent( 'arrowright' );304 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;305 const viewWidget = viewRoot.getChild( 1 );306 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;307 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;308 sinon.assert.notCalled( eventInfoStub.stop );309 sinon.assert.notCalled( domEventDataStub.domEvent.preventDefault );310 } );311 it( 'should not activate when the selection is not after the widget and navigating backward', () => {312 setModelData( editor.model, '<blockWidget></blockWidget><paragraph>f[]oo</paragraph>' );313 fireKeyboardEvent( 'arrowleft' );314 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;315 const viewWidget = viewRoot.getChild( 0 );316 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;317 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;318 sinon.assert.notCalled( eventInfoStub.stop );319 sinon.assert.notCalled( domEventDataStub.domEvent.preventDefault );320 } );321 it( 'should not activate when the non-collapsed selection is before the widget and navigating forward', () => {322 setModelData( editor.model, '<paragraph>fo[o]</paragraph><blockWidget></blockWidget>' );323 fireKeyboardEvent( 'arrowright' );324 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;325 const viewWidget = viewRoot.getChild( 1 );326 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;327 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;328 sinon.assert.notCalled( eventInfoStub.stop );329 sinon.assert.notCalled( domEventDataStub.domEvent.preventDefault );330 } );331 it( 'should not activate when the non-collapsed selection is after the widget and navigating backward', () => {332 setModelData( editor.model, '<blockWidget></blockWidget><paragraph>[f]oo</paragraph>' );333 fireKeyboardEvent( 'arrowleft' );334 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;335 const viewWidget = viewRoot.getChild( 0 );336 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;337 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;338 sinon.assert.notCalled( eventInfoStub.stop );339 sinon.assert.notCalled( domEventDataStub.domEvent.preventDefault );340 } );341 it( 'should not activate selection downcast when a nonâtype-around-friendly widget is selected', () => {342 setModelData( editor.model, '<paragraph>foo[<inlineWidget></inlineWidget>]</paragraph>' );343 model.change( writer => {344 // Simply trigger the selection downcast.345 writer.setSelectionAttribute( 'foo', 'bar' );346 } );347 const viewWidget = viewRoot.getChild( 0 ).getChild( 1 );348 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;349 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;350 sinon.assert.notCalled( eventInfoStub.stop );351 sinon.assert.notCalled( domEventDataStub.domEvent.preventDefault );352 } );353 } );354 describe( '"fake caret" deactivation', () => {355 it( 'should deactivate when the widget is selected and the navigation is backward to a valid position', () => {356 setModelData( editor.model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );357 fireKeyboardEvent( 'arrowleft' );358 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );359 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );360 sinon.assert.calledOnce( eventInfoStub.stop );361 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );362 fireKeyboardEvent( 'arrowleft' );363 expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );364 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;365 const viewWidget = viewRoot.getChild( 1 );366 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;367 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;368 sinon.assert.calledOnce( eventInfoStub.stop );369 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );370 } );371 it( 'should deactivate when the widget is selected and the navigation is forward to a valid position', () => {372 setModelData( editor.model, '[<blockWidget></blockWidget>]<paragraph>foo</paragraph>' );373 fireKeyboardEvent( 'arrowright' );374 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]<paragraph>foo</paragraph>' );375 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );376 sinon.assert.calledOnce( eventInfoStub.stop );377 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );378 fireKeyboardEvent( 'arrowright' );379 expect( getModelData( model ) ).to.equal( '<blockWidget></blockWidget><paragraph>[]foo</paragraph>' );380 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;381 const viewWidget = viewRoot.getChild( 0 );382 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;383 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;384 sinon.assert.calledOnce( eventInfoStub.stop );385 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );386 } );387 it( 'should deactivate if an arrow key is pressed along with Shift', () => {388 setModelData( editor.model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );389 fireKeyboardEvent( 'arrowleft', { shiftKey: true } );390 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );391 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );392 sinon.assert.calledOnce( eventInfoStub.stop );393 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );394 fireKeyboardEvent( 'arrowleft', { shiftKey: true } );395 expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );396 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;397 sinon.assert.calledOnce( eventInfoStub.stop );398 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );399 } );400 it( 'should not deactivate when the widget is selected and the navigation is backward but there is nowhere to go', () => {401 setModelData( editor.model, '[<blockWidget></blockWidget>]' );402 fireKeyboardEvent( 'arrowleft' );403 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );404 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );405 sinon.assert.calledOnce( eventInfoStub.stop );406 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );407 fireKeyboardEvent( 'arrowleft' );408 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );409 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );410 sinon.assert.calledOnce( eventInfoStub.stop );411 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );412 const viewWidget = viewRoot.getChild( 0 );413 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.true;414 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;415 sinon.assert.calledOnce( eventInfoStub.stop );416 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );417 } );418 it( 'should not deactivate when the widget is selected and the navigation is forward but there is nowhere to go', () => {419 setModelData( editor.model, '[<blockWidget></blockWidget>]' );420 fireKeyboardEvent( 'arrowright' );421 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );422 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );423 sinon.assert.calledOnce( eventInfoStub.stop );424 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );425 fireKeyboardEvent( 'arrowright' );426 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );427 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );428 sinon.assert.calledOnce( eventInfoStub.stop );429 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );430 const viewWidget = viewRoot.getChild( 0 );431 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;432 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.true;433 sinon.assert.calledOnce( eventInfoStub.stop );434 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );435 } );436 it( 'should deactivate when the widget is selected and the navigation is against the fake caret (backward)', () => {437 setModelData( editor.model, '[<blockWidget></blockWidget>]' );438 fireKeyboardEvent( 'arrowleft' );439 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );440 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );441 sinon.assert.calledOnce( eventInfoStub.stop );442 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );443 fireKeyboardEvent( 'arrowright' );444 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );445 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;446 const viewWidget = viewRoot.getChild( 0 );447 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;448 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;449 sinon.assert.calledOnce( eventInfoStub.stop );450 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );451 } );452 it( 'should deactivate when the widget is selected and the navigation is against the fake caret (forward)', () => {453 setModelData( editor.model, '[<blockWidget></blockWidget>]' );454 fireKeyboardEvent( 'arrowright' );455 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );456 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );457 sinon.assert.calledOnce( eventInfoStub.stop );458 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );459 fireKeyboardEvent( 'arrowleft' );460 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );461 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;462 const viewWidget = viewRoot.getChild( 0 );463 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;464 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;465 sinon.assert.calledOnce( eventInfoStub.stop );466 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );467 } );468 } );469 it( 'should not work when the plugin is disabled', () => {470 editor.plugins.get( WidgetTypeAround ).isEnabled = false;471 setModelData( editor.model, '<paragraph>foo[]</paragraph><blockWidget></blockWidget><paragraph>bar</paragraph>' );472 fireKeyboardEvent( 'arrowright' );473 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>bar</paragraph>' );474 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;475 fireKeyboardEvent( 'arrowdown' );476 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph><blockWidget></blockWidget><paragraph>[]bar</paragraph>' );477 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;478 } );479 it( 'should activate and deactivate the "fake caret" using all 4 arrow keys', () => {480 setModelData( editor.model, '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );481 fireKeyboardEvent( 'arrowright' );482 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );483 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );484 fireKeyboardEvent( 'arrowdown' );485 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );486 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;487 fireKeyboardEvent( 'arrowup' );488 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );489 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );490 fireKeyboardEvent( 'arrowleft' );491 expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );492 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;493 } );494 it( 'should quit the "fake caret" mode when the editor loses focus', () => {495 editor.ui.focusTracker.isFocused = true;496 setModelData( editor.model, '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );497 fireKeyboardEvent( 'arrowright' );498 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );499 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );500 editor.ui.focusTracker.isFocused = false;501 const viewWidget = viewRoot.getChild( 1 );502 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;503 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;504 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;505 } );506 it( 'should quit the "fake caret" mode when the user changed the selection', () => {507 setModelData( editor.model, '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );508 fireKeyboardEvent( 'arrowright' );509 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );510 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );511 model.change( writer => {512 writer.setSelection( model.document.getRoot().getChild( 0 ), 'in' );513 } );514 const viewWidget = viewRoot.getChild( 1 );515 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;516 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;517 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;518 } );519 it( 'should not quit the "fake caret" mode when the selection changed as a result of an indirect change', () => {520 setModelData( editor.model, '<paragraph>foo[]</paragraph><blockWidget></blockWidget>' );521 fireKeyboardEvent( 'arrowright' );522 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );523 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );524 // This could happen in collaboration.525 model.document.selection.fire( 'change:range', {526 directChange: false527 } );528 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );529 const viewWidget = viewRoot.getChild( 1 );530 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.true;531 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;532 } );533 it( 'should quit the "fake caret" mode when model was changed (model.deleteContent())', () => {534 setModelData( editor.model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>baz</paragraph>' );535 const selection = model.createSelection( modelSelection );536 model.change( writer => {537 writer.setSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, 'before' );538 model.deleteContent( selection );539 } );540 const viewWidget = viewRoot.getChild( 1 );541 expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><paragraph></paragraph><paragraph>baz</paragraph>' );542 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;543 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_before' ) ).to.be.false;544 expect( viewWidget.hasClass( 'ck-widget_type-around_show-fake-caret_after' ) ).to.be.false;545 } );546 it( 'should quit the "fake caret" mode when model was changed (writer.remove())', () => {547 setModelData( editor.model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]<paragraph>baz</paragraph>' );548 model.change( writer => {549 writer.setSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, 'before' );550 writer.remove( editor.model.document.getRoot().getChild( 1 ) );551 } );552 expect( getModelData( model ) ).to.equal( '<paragraph>foo[]</paragraph><paragraph>baz</paragraph>' );553 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;554 } );555 describe( 'inserting a new paragraph', () => {556 describe( 'on Enter key press when the "fake caret" is activated', () => {557 it( 'should insert a paragraph before a widget if the caret was "before" it', () => {558 setModelData( editor.model, '[<blockWidget></blockWidget>]' );559 fireKeyboardEvent( 'arrowleft' );560 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );561 fireKeyboardEvent( 'enter' );562 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph><blockWidget></blockWidget>' );563 } );564 it( 'should insert a paragraph after a widget if the caret was "after" it', () => {565 setModelData( editor.model, '[<blockWidget></blockWidget>]' );566 fireKeyboardEvent( 'arrowright' );567 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );568 fireKeyboardEvent( 'enter' );569 expect( getModelData( model ) ).to.equal( '<blockWidget></blockWidget><paragraph>[]</paragraph>' );570 } );571 it( 'should integrate with the undo feature', () => {572 setModelData( editor.model, '[<blockWidget></blockWidget>]' );573 fireKeyboardEvent( 'arrowleft' );574 fireKeyboardEvent( 'enter' );575 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph><blockWidget></blockWidget>' );576 editor.execute( 'undo' );577 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );578 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;579 } );580 it( 'should not work when the plugin is disabled', () => {581 setModelData( editor.model, '[<blockWidget></blockWidget>]' );582 editor.plugins.get( WidgetTypeAround ).isEnabled = false;583 model.change( writer => {584 writer.setSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, 'after' );585 } );586 fireKeyboardEvent( 'enter' );587 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );588 } );589 } );590 describe( 'on Enter key press when the widget is selected (no "fake caret", though)', () => {591 it( 'should insert a new paragraph after the widget if Enter was pressed', () => {592 setModelData( editor.model, '[<blockWidget></blockWidget>]' );593 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;594 fireKeyboardEvent( 'enter' );595 expect( getModelData( model ) ).to.equal( '<blockWidget></blockWidget><paragraph>[]</paragraph>' );596 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;597 } );598 it( 'should insert a new paragraph before the widget if Shift+Enter was pressed', () => {599 setModelData( editor.model, '[<blockWidget></blockWidget>]' );600 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;601 fireKeyboardEvent( 'enter', { shiftKey: true } );602 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph><blockWidget></blockWidget>' );603 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;604 } );605 it( 'should insert a new paragraph only if an entire widget is selected (selected nested editable content)', () => {606 setModelData( editor.model, '<blockWidget><nested>[foo] bar</nested></blockWidget>' );607 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;608 fireKeyboardEvent( 'enter' );609 expect( getModelData( model ) ).to.equal( '<blockWidget><nested>[] bar</nested></blockWidget>' );610 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;611 } );612 it( 'should insert a new paragraph only if an entire widget is selected (selected widget siblings)', () => {613 setModelData( editor.model, '<paragraph>f[oo</paragraph><blockWidget></blockWidget><paragraph>o]o</paragraph>' );614 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;615 fireKeyboardEvent( 'enter' );616 expect( getModelData( model ) ).to.equal( '<paragraph>f</paragraph><paragraph>[]o</paragraph>' );617 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;618 } );619 it( 'should split ancestors to find a place that allows a widget', () => {620 model.schema.register( 'allowP', {621 inheritAllFrom: '$block'622 } );623 model.schema.register( 'disallowP', {624 inheritAllFrom: '$block',625 allowIn: [ 'allowP' ]626 } );627 model.schema.extend( 'blockWidget', {628 allowIn: [ 'allowP', 'disallowP' ]629 } );630 model.schema.extend( 'paragraph', {631 allowIn: [ 'allowP' ]632 } );633 editor.conversion.for( 'downcast' ).elementToElement( { model: 'allowP', view: 'allowP' } );634 editor.conversion.for( 'downcast' ).elementToElement( { model: 'disallowP', view: 'disallowP' } );635 setModelData( model,636 '<allowP>' +637 '<disallowP>[<blockWidget></blockWidget>]</disallowP>' +638 '</allowP>'639 );640 fireKeyboardEvent( 'enter' );641 expect( getModelData( model ) ).to.equal(642 '<allowP>' +643 '<disallowP><blockWidget></blockWidget></disallowP>' +644 '<paragraph>[]</paragraph>' +645 '<disallowP></disallowP>' +646 '</allowP>'647 );648 } );649 it( 'should integrate with the undo feature', () => {650 setModelData( editor.model, '[<blockWidget></blockWidget>]' );651 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;652 fireKeyboardEvent( 'enter' );653 expect( getModelData( model ) ).to.equal( '<blockWidget></blockWidget><paragraph>[]</paragraph>' );654 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;655 editor.execute( 'undo' );656 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );657 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;658 } );659 it( 'should do nothing if a non-type-around-friendly content is selected', () => {660 setModelData( editor.model, '<paragraph>foo[<inlineWidget></inlineWidget>]</paragraph>' );661 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;662 fireKeyboardEvent( 'enter' );663 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]</paragraph>' );664 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;665 } );666 it( 'should not work when the plugin is disabled', () => {667 editor.plugins.get( WidgetTypeAround ).isEnabled = false;668 setModelData( editor.model, '[<blockWidget></blockWidget>]' );669 fireKeyboardEvent( 'enter' );670 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );671 } );672 } );673 describe( 'on keydown of a "typing" character when the "fake caret" is activated ', () => {674 it( 'should insert a character inside a new paragraph before a widget if the caret was "before" it', () => {675 setModelData( editor.model, '[<blockWidget></blockWidget>]' );676 fireKeyboardEvent( 'arrowleft' );677 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );678 fireKeyboardEvent( 'a' );679 fireMutation( 'a' );680 expect( getModelData( model ) ).to.equal( '<paragraph>a[]</paragraph><blockWidget></blockWidget>' );681 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;682 } );683 it( 'should insert a character inside a new paragraph after a widget if the caret was "after" it', () => {684 setModelData( editor.model, '[<blockWidget></blockWidget>]' );685 fireKeyboardEvent( 'arrowright' );686 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );687 fireKeyboardEvent( 'a' );688 fireMutation( 'a' );689 expect( getModelData( model ) ).to.equal( '<blockWidget></blockWidget><paragraph>a[]</paragraph>' );690 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;691 } );692 it( 'should do nothing if a "safe" keystroke was pressed', () => {693 setModelData( editor.model, '[<blockWidget></blockWidget>]' );694 fireKeyboardEvent( 'arrowright' );695 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );696 fireKeyboardEvent( 'esc' );697 fireKeyboardEvent( 'tab' );698 fireKeyboardEvent( 'd', { ctrlKey: true } );699 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );700 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );701 } );702 it( 'should integrate with the undo feature', () => {703 setModelData( editor.model, '[<blockWidget></blockWidget>]' );704 fireKeyboardEvent( 'arrowleft' );705 fireKeyboardEvent( 'a' );706 fireMutation( 'a' );707 expect( getModelData( model ) ).to.equal( '<paragraph>a[]</paragraph><blockWidget></blockWidget>' );708 editor.execute( 'undo' );709 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph><blockWidget></blockWidget>' );710 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;711 editor.execute( 'undo' );712 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );713 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;714 } );715 it( 'should not work when the plugin is disabled', () => {716 setModelData( editor.model, '[<blockWidget></blockWidget>]' );717 editor.plugins.get( WidgetTypeAround ).isEnabled = false;718 model.change( writer => {719 writer.setSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, 'before' );720 } );721 fireKeyboardEvent( 'a' );722 fireMutation( 'a' );723 expect( getModelData( model ) ).to.equal( '<paragraph>a[]</paragraph>' );724 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;725 } );726 } );727 } );728 describe( 'delete integration', () => {729 let eventInfoStub, domEventDataStub;730 describe( 'backward delete', () => {731 it( 'should delete content before a widget if the "fake caret" is also before the widget', () => {732 setModelData( editor.model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );733 fireKeyboardEvent( 'arrowleft' );734 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );735 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );736 fireDeleteEvent();737 expect( getModelData( model ) ).to.equal( '<paragraph>fo[]</paragraph><blockWidget></blockWidget>' );738 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;739 sinon.assert.calledOnce( eventInfoStub.stop );740 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );741 } );742 it( 'should delete an empty paragraph before a widget if the "fake caret" is also before the widget', () => {743 setModelData( editor.model, '<paragraph></paragraph>[<blockWidget></blockWidget>]' );744 fireKeyboardEvent( 'arrowleft' );745 expect( getModelData( model ) ).to.equal( '<paragraph></paragraph>[<blockWidget></blockWidget>]' );746 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );747 fireDeleteEvent();748 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );749 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );750 sinon.assert.calledOnce( eventInfoStub.stop );751 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );752 } );753 it( 'should delete an empty document tree branch before a widget if the "fake caret" is also before the widget', () => {754 setModelData( editor.model, '<blockQuote><paragraph></paragraph></blockQuote>[<blockWidget></blockWidget>]' );755 fireKeyboardEvent( 'arrowleft' );756 expect( getModelData( model ) ).to.equal(757 '<blockQuote>' +758 '<paragraph></paragraph>' +759 '</blockQuote>' +760 '[<blockWidget></blockWidget>]'761 );762 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );763 fireDeleteEvent();764 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );765 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );766 sinon.assert.calledOnce( eventInfoStub.stop );767 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );768 } );769 it( 'should delete an empty document tree sub-branch before a widget if the "fake caret" is also before the widget', () => {770 let operationType;771 setModelData( editor.model,772 '<blockQuote>' +773 '<paragraph>foo</paragraph>' +774 '<paragraph></paragraph>' +775 '</blockQuote>' +776 '[<blockWidget></blockWidget>]'777 );778 fireKeyboardEvent( 'arrowleft' );779 expect( getModelData( model ) ).to.equal(780 '<blockQuote>' +781 '<paragraph>foo</paragraph>' +782 '<paragraph></paragraph>' +783 '</blockQuote>' +784 '[<blockWidget></blockWidget>]'785 );786 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );787 // Assert that the paragraph is merged rather than deleted because788 // it is safer for collaboration.789 model.on( 'applyOperation', ( evt, [ operation ] ) => {790 operationType = operation.type;791 } );792 fireDeleteEvent();793 expect( getModelData( model ) ).to.equal(794 '<blockQuote>' +795 '<paragraph>foo[]</paragraph>' +796 '</blockQuote>' +797 '<blockWidget></blockWidget>'798 );799 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;800 expect( operationType ).to.equal( 'merge' );801 sinon.assert.calledOnce( eventInfoStub.stop );802 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );803 } );804 it( 'should do nothing if the "fake caret" is before the widget but there is nothing to delete there', () => {805 setModelData( editor.model, '[<blockWidget></blockWidget>]' );806 fireKeyboardEvent( 'arrowleft' );807 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );808 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );809 fireDeleteEvent();810 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );811 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );812 sinon.assert.calledOnce( eventInfoStub.stop );813 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );814 } );815 it( 'should delete a widget if the "fake caret" is after the widget (no content after the widget)', () => {816 setModelData( editor.model, '[<blockWidget></blockWidget>]' );817 fireKeyboardEvent( 'arrowright' );818 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );819 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );820 fireDeleteEvent();821 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );822 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;823 sinon.assert.calledOnce( eventInfoStub.stop );824 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );825 } );826 it( 'should delete a widget if the "fake caret" is after the widget (some content after the widget)', () => {827 setModelData( editor.model, '[<blockWidget></blockWidget>]<paragraph>foo</paragraph>' );828 fireKeyboardEvent( 'arrowright' );829 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]<paragraph>foo</paragraph>' );830 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );831 fireDeleteEvent();832 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph><paragraph>foo</paragraph>' );833 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;834 sinon.assert.calledOnce( eventInfoStub.stop );835 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );836 } );837 it( 'should delete a sibling widget', () => {838 setModelData( editor.model, '<blockWidget><nested>foo</nested></blockWidget>[<blockWidget></blockWidget>]' );839 fireKeyboardEvent( 'arrowleft' );840 expect( getModelData( model ) ).to.equal(841 '<blockWidget><nested>foo</nested></blockWidget>' +842 '[<blockWidget></blockWidget>]'843 );844 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );845 fireDeleteEvent();846 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph><blockWidget></blockWidget>' );847 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;848 sinon.assert.calledOnce( eventInfoStub.stop );849 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );850 } );851 } );852 describe( 'forward delete', () => {853 it( 'should delete content after a widget if the "fake caret" is also after the widget', () => {854 setModelData( editor.model, '[<blockWidget></blockWidget>]<paragraph>foo</paragraph>' );855 fireKeyboardEvent( 'arrowright' );856 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]<paragraph>foo</paragraph>' );857 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );858 fireDeleteEvent( true );859 expect( getModelData( model ) ).to.equal( '<blockWidget></blockWidget><paragraph>[]oo</paragraph>' );860 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;861 sinon.assert.calledOnce( eventInfoStub.stop );862 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );863 } );864 it( 'should delete an empty paragraph after a widget if the "fake caret" is also after the widget', () => {865 setModelData( editor.model, '[<blockWidget></blockWidget>]<paragraph></paragraph>' );866 fireKeyboardEvent( 'arrowright' );867 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]<paragraph></paragraph>' );868 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );869 fireDeleteEvent( true );870 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );871 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );872 sinon.assert.calledOnce( eventInfoStub.stop );873 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );874 } );875 it( 'should delete an empty document tree branch after a widget if the "fake caret" is also after the widget', () => {876 setModelData( editor.model, '[<blockWidget></blockWidget>]<blockQuote><paragraph></paragraph></blockQuote>' );877 fireKeyboardEvent( 'arrowright' );878 expect( getModelData( model ) ).to.equal(879 '[<blockWidget></blockWidget>]' +880 '<blockQuote><paragraph></paragraph></blockQuote>'881 );882 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );883 fireDeleteEvent( true );884 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );885 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );886 sinon.assert.calledOnce( eventInfoStub.stop );887 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );888 } );889 it( 'should delete an empty document tree sub-branch after a widget if the "fake caret" is also after the widget', () => {890 let operationType;891 setModelData( editor.model,892 '[<blockWidget></blockWidget>]' +893 '<blockQuote>' +894 '<paragraph></paragraph>' +895 '<paragraph>foo</paragraph>' +896 '</blockQuote>'897 );898 fireKeyboardEvent( 'arrowright' );899 expect( getModelData( model ) ).to.equal(900 '[<blockWidget></blockWidget>]' +901 '<blockQuote>' +902 '<paragraph></paragraph>' +903 '<paragraph>foo</paragraph>' +904 '</blockQuote>'905 );906 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );907 // Assert that the paragraph is merged rather than deleted because908 // it is safer for collaboration.909 model.on( 'applyOperation', ( evt, [ operation ] ) => {910 operationType = operation.type;911 } );912 fireDeleteEvent( true );913 expect( getModelData( model ) ).to.equal(914 '<blockWidget></blockWidget>' +915 '<blockQuote>' +916 '<paragraph>[]foo</paragraph>' +917 '</blockQuote>'918 );919 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;920 expect( operationType ).to.equal( 'merge' );921 sinon.assert.calledOnce( eventInfoStub.stop );922 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );923 } );924 it( 'should do nothing if the "fake caret" is after the widget but there is nothing to delete there', () => {925 setModelData( editor.model, '[<blockWidget></blockWidget>]' );926 fireKeyboardEvent( 'arrowright' );927 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );928 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );929 fireDeleteEvent( true );930 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );931 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );932 sinon.assert.calledOnce( eventInfoStub.stop );933 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );934 } );935 it( 'should delete a widget if the "fake caret" is before the widget (no content before the widget)', () => {936 setModelData( editor.model, '[<blockWidget></blockWidget>]' );937 fireKeyboardEvent( 'arrowleft' );938 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );939 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );940 fireDeleteEvent( true );941 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );942 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;943 sinon.assert.calledOnce( eventInfoStub.stop );944 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );945 } );946 it( 'should delete a widget if the "fake caret" is before the widget (some content before the widget)', () => {947 setModelData( editor.model, '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );948 fireKeyboardEvent( 'arrowleft' );949 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph>[<blockWidget></blockWidget>]' );950 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );951 fireDeleteEvent( true );952 expect( getModelData( model ) ).to.equal( '<paragraph>foo</paragraph><paragraph>[]</paragraph>' );953 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;954 sinon.assert.calledOnce( eventInfoStub.stop );955 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );956 } );957 it( 'should delete a sibling widget', () => {958 setModelData( editor.model, '[<blockWidget></blockWidget>]<blockWidget><nested>foo</nested></blockWidget>' );959 fireKeyboardEvent( 'arrowright' );960 expect( getModelData( model ) ).to.equal(961 '[<blockWidget></blockWidget>]' +962 '<blockWidget><nested>foo</nested></blockWidget>'963 );964 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'after' );965 fireDeleteEvent( true );966 expect( getModelData( model ) ).to.equal( '<blockWidget></blockWidget><paragraph>[]</paragraph>' );967 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.be.undefined;968 sinon.assert.calledOnce( eventInfoStub.stop );969 sinon.assert.calledOnce( domEventDataStub.domEvent.preventDefault );970 } );971 } );972 it( 'should not work when the plugin is disabled', () => {973 setModelData( editor.model, '[<blockWidget></blockWidget>]' );974 fireKeyboardEvent( 'arrowleft' );975 expect( getModelData( model ) ).to.equal( '[<blockWidget></blockWidget>]' );976 expect( modelSelection.getAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE ) ).to.equal( 'before' );977 editor.plugins.get( WidgetTypeAround ).isEnabled = false;978 model.change( writer => {979 writer.setSelectionAttribute( TYPE_AROUND_SELECTION_ATTRIBUTE, 'before' );980 } );981 fireDeleteEvent();982 expect( getModelData( model ) ).to.equal( '<paragraph>[]</paragraph>' );983 } );984 function fireDeleteEvent( isForward = false ) {985 eventInfoStub = new EventInfo( viewDocument, 'delete' );986 sinon.spy( eventInfoStub, 'stop' );987 const data = {988 direction: isForward ? 'forward' : 'backward',989 unit: 'character'990 };991 domEventDataStub = new DomEventData( viewDocument, getDomEvent(), data );992 viewDocument.fire( eventInfoStub, domEventDataStub );993 }994 } );995 function getDomEvent() {996 return {997 preventDefault: sinon.spy(),998 stopPropagation: sinon.spy()999 };1000 }1001 function fireKeyboardEvent( key, modifiers ) {1002 eventInfoStub = new EventInfo( viewDocument, 'keydown' );1003 sinon.spy( eventInfoStub, 'stop' );1004 const data = {1005 document: viewDocument,1006 domTarget: editingView.getDomRoot(),1007 keyCode: getCode( key )1008 };1009 Object.assign( data, modifiers );1010 domEventDataStub = new DomEventData( viewDocument, getDomEvent(), data );1011 viewDocument.fire( eventInfoStub, domEventDataStub );1012 }1013 function fireMutation( text ) {1014 const placeOfMutation = viewDocument.selection.getFirstRange().start;1015 viewDocument.fire( 'mutations', [...
ps4mapping.js
Source:ps4mapping.js
1 const gamepad = new Gamepad();2 function fireKeyboardEvent(event, keycode) {3 var keyboardEvent = document.createEventObject ?4 document.createEventObject() : document.createEvent("Events");5 if(keyboardEvent.initEvent) {6 keyboardEvent.initEvent(event, true, true);7 }8 keyboardEvent.keyCode = keycode;9 keyboardEvent.which = keycode;10 document.dispatchEvent ? document.dispatchEvent(keyboardEvent) 11 : document.fireEvent(event, keyboardEvent);12 }13 gamepad.on('connect', e => {14 // connected event15 console.log(`controller ${e.index} connected!`);16 });17 18 gamepad.on('disconnect', e => {19 //disconnect event20 console.log(`controller ${e.index} disconnected!`);21 });22 23 gamepad.on('hold', 'stick_axis_left', e => {24 var leftveraxis =(`${e.value[0]}`);25 26 27 // move left and right with left stick28 if ( leftveraxis < -0.5){29 fireKeyboardEvent("keydown", 37);30 fireKeyboardEvent("keyup", 39);31 };32 33 if ( leftveraxis > 0.5){34 fireKeyboardEvent("keydown", 39);35 fireKeyboardEvent("keyup", 37);36 };37 });38 39 gamepad.on('hold', 'stick_axis_left', e => {40 var lefthoraxis = (`${e.value[1]}`);41 // move forward42 if ( lefthoraxis < -0.5){43 fireKeyboardEvent("keydown", 38);44 fireKeyboardEvent("keyup", 40);45 };46 47 48 if ( lefthoraxis > 0.5){49 fireKeyboardEvent("keydown", 40);50 fireKeyboardEvent("keyup", 38);51 };52 });53 gamepad.on('release', 'stick_axis_left', e => {54 fireKeyboardEvent("keyup", 40);55 fireKeyboardEvent("keyup", 38);56 fireKeyboardEvent("keyup", 39);57 fireKeyboardEvent("keyup", 37);58 });59 60/*61 gamepad.on('hold', 'stick_axis_right', e => {62 // move left and right with right stick63 var rightveraxis =(`${e.value[0]}`);64 65 66 // move left and right with left stick67 if ( rightveraxis < -0.5){68 fireKeyboardEvent("keydown", 37);69 fireKeyboardEvent("keyup", 39);70 };71 72 if ( rightveraxis > 0.5){73 fireKeyboardEvent("keydown", 39);74 fireKeyboardEvent("keyup", 37);75 };76 });77 gamepad.on('release', 'stick_axis_right', e => {78 fireKeyboardEvent("keyup", 39);79 fireKeyboardEvent("keyup", 37);80 });81 */82 83 gamepad.on('hold', 'shoulder_bottom_right', () => {84 //hold R2 event85 console.log('button R2 was pressed!');86 fireKeyboardEvent("keydown", 38);87 });88 89 gamepad.on('release','shoulder_bottom_right', () => {90 console.log('button R2 was released!');91 fireKeyboardEvent("keyup", 38);92 });93 94 gamepad.on('hold', 'shoulder_bottom_left', () => {95 //hold L2 event96 console.log('button L2 was pressed!');97 });98 99 gamepad.on('hold', 'shoulder_top_left', () => {100 //hold L1 event101 console.log('button L1 was pressed!');102 });103 104 gamepad.on('hold', 'shoulder_top_right', () => {105 //hold R1 event106 console.log('button R1 was pressed!');107 });108 109 gamepad.on('press', 'select', () => {110 //Press select event111 console.log('button select was pressed!');112 });113 114 gamepad.on('press', 'start', () => {115 //Press start event116 console.log('button start was pressed!');117 fireKeyboardEvent("keydown", 32);118 });119 120 gamepad.on('release', 'start', () => {121 console.log('button start was released!');122 fireKeyboardEvent("keyup", 32);123 });124 125 gamepad.on('press', 'button_1', () => {126 console.log('button 1 was pressed!');127 fireKeyboardEvent("keydown", 37);128 });129 130 gamepad.on('press', 'button_2', () => {131 console.log('button 2 was pressed!');132 133 });134 135 gamepad.on('press', 'button_3', () => {136 console.log('button 3 was pressed!');137 fireKeyboardEvent("keydown", 39);138 });139 140 gamepad.on('press', 'button_4', () => {141 console.log('button 4 was pressed!');142 });143 144 gamepad.on('hold', 'button_1', () => {145 console.log('button 1 is being held!');146 147 });148 149 gamepad.on('hold', 'button_2', () => {150 console.log('button 2 is being held!');151 fireKeyboardEvent("keydown", 40);152 });153 154 gamepad.on('hold', 'button_3', () => {155 console.log('button 3 is being held!');156 });157 158 gamepad.on('hold', 'button_4', () => {159 console.log('button 4 is being held!');160 fireKeyboardEvent("keydown", 38);161 });162 163 gamepad.on('release', 'button_1', () => {164 console.log('button 1 was released!');165 fireKeyboardEvent("keyup", 37);166 });167 168 gamepad.on('release', 'button_2', () => {169 console.log('button 2 was released!');170 fireKeyboardEvent("keyup", 40);171 });172 173 gamepad.on('release', 'button_3', () => {174 console.log('button 3 was released!');175 fireKeyboardEvent("keyup", 39);176 });177 178 gamepad.on('release', 'button_4', () => {179 console.log('button 4 was released!');180 fireKeyboardEvent("keyup", 38);...
ps4mapping-forpong.js
Source:ps4mapping-forpong.js
1 const gamepad = new Gamepad();2 function fireKeyboardEvent(event, keycode) {3 var keyboardEvent = document.createEventObject ?4 document.createEventObject() : document.createEvent("Events");5 if(keyboardEvent.initEvent) {6 keyboardEvent.initEvent(event, true, true);7 }8 keyboardEvent.keyCode = keycode;9 keyboardEvent.which = keycode;10 document.dispatchEvent ? document.dispatchEvent(keyboardEvent) 11 : document.fireEvent(event, keyboardEvent);12 }13 gamepad.off('press', 'start');14 gamepad.on('connect', e => {15 // connected event16 console.log(`controller ${e.index} connected!`);17 });18 19 gamepad.on('disconnect', e => {20 //disconnect event21 console.log(`controller ${e.index} disconnected!`);22 });23 24 gamepad.on('hold', 'stick_axis_left', e => {25 var leftveraxis =(`${e.value[0]}`);26 27 28 // move left and right with left stick29 if ( leftveraxis < -0.5){30 fireKeyboardEvent("keydown", 37);31 fireKeyboardEvent("keyup", 39);32 };33 34 if ( leftveraxis > 0.5){35 fireKeyboardEvent("keydown", 39);36 fireKeyboardEvent("keyup", 37);37 };38 });39 40 gamepad.on('hold', 'stick_axis_left', e => {41 var lefthoraxis = (`${e.value[1]}`);42 // move forward43 if ( lefthoraxis < -0.5){44 fireKeyboardEvent("keydown", 81);45 fireKeyboardEvent("keyup", 65);46 };47 48 49 if ( lefthoraxis > 0.5){50 fireKeyboardEvent("keydown", 65);51 fireKeyboardEvent("keyup", 81);52 };53 });54 gamepad.on('release', 'stick_axis_left', e => {55 fireKeyboardEvent("keyup", 65);56 fireKeyboardEvent("keyup", 81);57 });58 59/*60 gamepad.on('hold', 'stick_axis_right', e => {61 // move left and right with right stick62 var rightveraxis =(`${e.value[0]}`);63 64 65 // move left and right with left stick66 if ( rightveraxis < -0.5){67 fireKeyboardEvent("keydown", 37);68 fireKeyboardEvent("keyup", 39);69 };70 71 if ( rightveraxis > 0.5){72 fireKeyboardEvent("keydown", 39);73 fireKeyboardEvent("keyup", 37);74 };75 });76 gamepad.on('release', 'stick_axis_right', e => {77 fireKeyboardEvent("keyup", 39);78 fireKeyboardEvent("keyup", 37);79 });80 */81 82 gamepad.on('hold', 'shoulder_bottom_right', () => {83 //hold R2 event84 console.log('button R2 was pressed!');85 fireKeyboardEvent("keydown", 38);86 });87 88 gamepad.on('release','shoulder_bottom_right', () => {89 console.log('button R2 was released!');90 fireKeyboardEvent("keyup", 38);91 });92 93 gamepad.on('hold', 'shoulder_bottom_left', () => {94 //hold L2 event95 console.log('button L2 was pressed!');96 });97 98 gamepad.on('hold', 'shoulder_top_left', () => {99 //hold L1 event100 console.log('button L1 was pressed!');101 });102 103 gamepad.on('hold', 'shoulder_top_right', () => {104 //hold R1 event105 console.log('button R1 was pressed!');106 });107 108 gamepad.on('press', 'select', () => {109 //Press select event110 console.log('button select was pressed!');111 });112 113 gamepad.on('press', 'start', () => {114 //Press start event115 console.log('button start was pressed!');116 fireKeyboardEvent("keydown", 49);117 });118 gamepad.on('release', 'start', () => {119 console.log('button start was released!');120 fireKeyboardEvent("keyup", 49);121 });122 123 gamepad.on('press', 'button_1', () => {124 console.log('button 1 was pressed!');125 fireKeyboardEvent("keydown", 40);126 });127 128 gamepad.on('press', 'button_2', () => {129 console.log('button 2 was pressed!');130 131 });132 133 gamepad.on('press', 'button_3', () => {134 console.log('button 3 was pressed!');135 fireKeyboardEvent("keydown", 39);136 });137 138 gamepad.on('press', 'button_4', () => {139 console.log('button 4 was pressed!');140 });141 142 gamepad.on('hold', 'button_1', () => {143 console.log('button 1 is being held!');144 145 });146 147 gamepad.on('hold', 'button_2', () => {148 console.log('button 2 is being held!');149 fireKeyboardEvent("keydown", 40);150 });151 152 gamepad.on('hold', 'button_3', () => {153 console.log('button 3 is being held!');154 });155 156 gamepad.on('hold', 'button_4', () => {157 console.log('button 4 is being held!');158 fireKeyboardEvent("keydown", 38);159 });160 161 gamepad.on('release', 'button_1', () => {162 console.log('button 1 was released!');163 fireKeyboardEvent("keyup", 37);164 });165 166 gamepad.on('release', 'button_2', () => {167 console.log('button 2 was released!');168 fireKeyboardEvent("keyup", 40);169 });170 171 gamepad.on('release', 'button_3', () => {172 console.log('button 3 was released!');173 fireKeyboardEvent("keyup", 39);174 });175 176 gamepad.on('release', 'button_4', () => {177 console.log('button 4 was released!');178 fireKeyboardEvent("keyup", 38);...
Using AI Code Generation
1function fireKeyboardEvent(keyCode, type) {2 var evt = document.createEvent("KeyboardEvent");3 evt.initKeyboardEvent(type, true, true, window, 0, 0, 0, 0, 0, keyCode);4 document.dispatchEvent(evt);5}6function fireMouseEvent(x, y, type) {7 var evt = document.createEvent("MouseEvent");8 evt.initMouseEvent(type, true, true, window, 0, 0, 0, x, y, false, false, false, false, 0, null);9 document.dispatchEvent(evt);10}11function fireTouchEvent(x, y, type) {12 var evt = document.createEvent("TouchEvent");13 evt.initTouchEvent(type, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, [x], [y], [1], [1], 1, 1);14 document.dispatchEvent(evt);15}16function firePointerEvent(x, y, type) {17 var evt = document.createEvent("Point
Using AI Code Generation
1function fireKeyboardEvent(type, key, modifiers) {2 var event = document.createEvent("KeyboardEvent");3 event.initKeyboardEvent(type, true, true, window, key, 0, modifiers, false);4 document.dispatchEvent(event);5}6fireKeyboardEvent("keydown", "a", []);7fireKeyboardEvent("keyup", "a", []);8fireKeyboardEvent("keydown", "a", []);9fireKeyboardEvent("keyup", "a", []);10fireKeyboardEvent("keydown", "a", []);11fireKeyboardEvent("keyup", "a", []);12fireKeyboardEvent("keydown", "a", []);13fireKeyboardEvent("keyup", "a", []);14fireKeyboardEvent("keydown", "a", []);15fireKeyboardEvent("keyup", "a", []);16fireKeyboardEvent("keydown", "a", []);17fireKeyboardEvent("keyup", "a", []);18fireKeyboardEvent("keydown", "a", []);19fireKeyboardEvent("keyup", "a", []);20fireKeyboardEvent("keydown", "a", []);21fireKeyboardEvent("keyup", "a", []);22fireKeyboardEvent("keydown", "a", []);23fireKeyboardEvent("keyup", "a", []);24fireKeyboardEvent("keydown", "a", []);25fireKeyboardEvent("keyup", "a", []);
Using AI Code Generation
1wptdriver.fireKeyboardEvent('keydown', 37, 0, 0, 0, 0, 0, 0, 0);2wptdriver.fireKeyboardEvent('keyup', 37, 0, 0, 0, 0, 0, 0, 0);3wptdriver.fireKeyboardEvent('keydown', 39, 0, 0, 0, 0, 0, 0, 0);4wptdriver.fireKeyboardEvent('keyup', 39, 0, 0, 0, 0, 0, 0, 0);5wptdriver.fireMouseEvent('mousedown', 100, 100, 0, 0, 0, 0, 0, 0, 0, 0);6wptdriver.fireMouseEvent('mouseup', 100, 100, 0, 0, 0, 0, 0, 0, 0, 0);7wptdriver.fireMouseEvent('mousedown', 200, 100, 0, 0, 0, 0, 0, 0, 0, 0);8wptdriver.fireMouseEvent('mouseup', 200, 100, 0, 0, 0, 0, 0, 0, 0, 0);9wptdriver.fireTouchEvent('touchstart', 100, 100, 0, 0, 0, 0, 0, 0, 0, 0);10wptdriver.fireTouchEvent('touchend', 100, 100, 0, 0, 0, 0, 0, 0, 0, 0);11wptdriver.fireTouchEvent('touchstart', 200, 100, 0, 0, 0, 0, 0, 0, 0, 0);12wptdriver.fireTouchEvent('touchend', 200, 100, 0, 0, 0, 0, 0, 0, 0, 0);13wptdriver.fireCustomEvent('test', 'test', 100,
Using AI Code Generation
1var wptdriver = require('wptdriver');2wptdriver.fireKeyboardEvent('keydown', 65, 0, 0, 0, 0);3wptdriver.fireKeyboardEvent('keyup', 65, 0, 0, 0, 0);4var wptdriver = require('wptdriver');5wptdriver.fireMouseEvent('mousedown', 100, 200, 0, 0, 0, 0);6wptdriver.fireMouseEvent('mouseup', 100, 200, 0, 0, 0, 0);7var wptdriver = require('wptdriver');8wptdriver.fireTouchEvent('touchstart', 100, 200, 0, 0, 0, 0);9wptdriver.fireTouchEvent('touchend', 100, 200, 0, 0, 0, 0);10var wptdriver = require('wptdriver');11wptdriver.getCookie('name of cookie', function (cookie) {12 console.log(cookie);13});14var wptdriver = require('wptdriver');15wptdriver.getEval('document.title', function (title) {16 console.log(title);17});18var wptdriver = require('wptdriver');19wptdriver.getLog(function (log) {20 console.log(log);21});
Using AI Code Generation
1wptbEditor.fireKeyboardEvent('keydown', 13, 0, 'Enter', false, false, false);2wptbEditor.fireKeyboardEvent('keydown', 13, 0, 'Enter', false, false, false);3wptbEditor.fireKeyboardEvent('keydown', 13, 0, 'Enter', false, false, false);4wptbEditor.fireKeyboardEvent('keydown', 13, 0, 'Enter', false, false, false);5wptbEditor.fireKeyboardEvent('keydown', 13, 0, 'Enter', false, false, false);6wptbEditor.fireKeyboardEvent('keydown', 13, 0, 'Enter', false, false, false);7wptbEditor.fireKeyboardEvent('keydown', 13, 0, 'Enter', false, false, false);8wptbEditor.fireKeyboardEvent('keydown', 13, 0, 'Enter', false, false, false);9wptbEditor.fireKeyboardEvent('keydown', 13, 0, 'Enter', false, false, false);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!