How to use prevent method in storybook-root

Best JavaScript code snippet using storybook-root

twostepcaretmovement.js

Source:twostepcaretmovement.js Github

copy

Full Screen

1/**2 * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.3 * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license4 */5/* global document */6import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';7import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin';8import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';9import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';10import TwoStepCaretMovement from '../src/twostepcaretmovement';11import Position from '@ckeditor/ckeditor5-engine/src/model/position';12import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';13import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';14import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';15import toArray from '@ckeditor/ckeditor5-utils/src/toarray';16import priorities from '@ckeditor/ckeditor5-utils/src/priorities';17import '@ckeditor/ckeditor5-core/tests/_utils/assertions/attribute';18describe( 'TwoStepCaretMovement()', () => {19 let editor, model, emitter, selection, view, plugin;20 let preventDefaultSpy, evtStopSpy;21 testUtils.createSinonSandbox();22 beforeEach( () => {23 emitter = Object.create( DomEmitterMixin );24 return VirtualTestEditor.create( { plugins: [ TwoStepCaretMovement ] } ).then( newEditor => {25 editor = newEditor;26 model = editor.model;27 selection = model.document.selection;28 view = editor.editing.view;29 plugin = editor.plugins.get( TwoStepCaretMovement );30 preventDefaultSpy = sinon.spy().named( 'preventDefault' );31 evtStopSpy = sinon.spy().named( 'evt.stop' );32 editor.model.schema.extend( '$text', {33 allowAttributes: [ 'a', 'b', 'c' ],34 allowIn: '$root'35 } );36 model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );37 editor.conversion.for( 'upcast' ).elementToAttribute( { view: 'a', model: 'a' } );38 editor.conversion.for( 'upcast' ).elementToAttribute( { view: 'b', model: 'b' } );39 editor.conversion.for( 'upcast' ).elementToAttribute( { view: 'c', model: 'c' } );40 editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );41 plugin.registerAttribute( 'a' );42 } );43 } );44 afterEach( () => {45 return editor.destroy();46 } );47 describe( 'moving right', () => {48 it( 'should do nothing for unrelated attribute (at the beginning)', () => {49 setData( model, '[]<$text c="true">foo</$text>' );50 testTwoStepCaretMovement( [51 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 0 },52 '→',53 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 1 }54 ] );55 } );56 it( 'should do nothing for unrelated attribute (at the end)', () => {57 setData( model, '<$text c="true">foo[]</$text>' );58 testTwoStepCaretMovement( [59 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },60 '→',61 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 }62 ] );63 } );64 it( 'should "enter" the text with attribute in two steps', () => {65 setData( model, '<$text c="true">foo[]</$text><$text a="true" b="true">bar</$text>' );66 testTwoStepCaretMovement( [67 // Gravity is not overridden, caret is at the beginning of the text but is "outside" of the text.68 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },69 '→',70 // Gravity is overridden, caret movement is blocked, selection at the beginning but "inside" the text.71 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },72 '→',73 // Caret movement was not blocked this time (still once) so everything works normally.74 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 }75 ] );76 } );77 it( 'should "leave" the text with attribute in two steps', () => {78 setData( model, '<$text a="true" b="true">bar[]</$text><$text c="true">foo</$text>' );79 testTwoStepCaretMovement( [80 // Gravity is not overridden, caret is at the end of the text but is "inside" of the text.81 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },82 '→',83 // Gravity is overridden, caret movement is blocked, selection at the end but "outside" the text.84 { selectionAttributes: [ 'c' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },85 '→',86 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 }87 ] );88 } );89 it( 'should use two-steps movement when between nodes with the same attribute but different value', () => {90 setData( model, '<$text a="1">bar[]</$text><$text a="2">foo</$text>' );91 testTwoStepCaretMovement( [92 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 3 },93 '→',94 // <$text a="1">bar</$text><$text a="2">[]foo</$text>95 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1, caretPosition: 3 },96 '→',97 // <$text a="1">bar</$text><$text a="2">f[]oo</$text>98 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 4 }99 ] );100 } );101 // https://github.com/ckeditor/ckeditor5/issues/937102 it( 'should not require two-steps between unrelated attributes inside the initial attribute', () => {103 setData( model, '<$text a="1">fo[]o</$text><$text a="1" b="2">bar</$text><$text a="1">baz</$text>' );104 testTwoStepCaretMovement( [105 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },106 '→',107 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },108 '→',109 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },110 '→',111 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },112 '→',113 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },114 '→',115 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 }116 ] );117 } );118 // https://github.com/ckeditor/ckeditor5-engine/issues/1301119 it( 'should handle passing through the only character in the block', () => {120 setData( model, '<$text a="1">[]x</$text>' );121 testTwoStepCaretMovement( [122 // <$text a="1">[]x</$text>123 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 0 },124 '→',125 // <$text a="1">x[]</$text>126 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 1 },127 '→',128 // <$text a="1">x</$text>[]129 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1, caretPosition: 1 },130 '→',131 // Stays at <$text a="1">x</$text>[]132 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1, caretPosition: 1 }133 ] );134 } );135 // https://github.com/ckeditor/ckeditor5-engine/issues/1301136 it( 'should handle passing through the only character in the block (no attribute in the initial selection)', () => {137 setData( model, '[]<$text a="1">x</$text>' );138 model.change( writer => writer.removeSelectionAttribute( 'a' ) );139 testTwoStepCaretMovement( [140 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },141 '→',142 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },143 '→',144 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },145 '→',146 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 2, evtStop: 2 },147 '→',148 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 2, evtStop: 2 }149 ] );150 } );151 // https://github.com/ckeditor/ckeditor5-engine/issues/1301152 it( 'should handle passing through the only-child with an attribute (multiple characters)', () => {153 setData( model, '[]<$text a="1">xyz</$text>' );154 testTwoStepCaretMovement( [155 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },156 '→',157 // <$text a="1">x{}yz</$text>158 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },159 '→',160 // <$text a="1">xy{}z</$text>161 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },162 '→',163 // <$text a="1">xyz{}</$text>164 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },165 '→',166 // <$text a="1">xyz</$text>{}167 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },168 '→',169 // <$text a="1">xyz</$text>{}170 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1 }171 ] );172 } );173 it( 'should handle leaving an attribute followed by another block', () => {174 setData( model, '<paragraph><$text a="1">foo[]</$text></paragraph><paragraph><$text b="1">bar</$text></paragraph>' );175 testTwoStepCaretMovement( [176 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },177 '→',178 // <paragraph><$text a="1">bar</$text>[]</paragraph><paragraph>foo</paragraph>179 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },180 '→',181 // <paragraph><$text a="1">bar</$text></paragraph><paragraph>f[]oo</paragraph>182 { selectionAttributes: [ 'b' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },183 '→',184 // <paragraph><$text a="1">bar</$text></paragraph><paragraph>fo[]o</paragraph>185 { selectionAttributes: [ 'b' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 }186 ] );187 } );188 } );189 describe( 'moving left', () => {190 it( 'should "enter" the text with attribute in two steps', () => {191 setData( model, '<$text>foo</$text><$text a="true" b="true">bar</$text><$text c="true">b[]iz</$text>' );192 testTwoStepCaretMovement( [193 // Gravity is not overridden, caret is a one character after the and of the text.194 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },195 '←',196 // Caret movement was not blocked but the gravity is overridden.197 { selectionAttributes: [ 'c' ], isGravityOverridden: true, preventDefault: 0, evtStop: 0 },198 '←',199 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },200 '←',201 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 }202 ] );203 } );204 it( 'should "leave" the text with attribute in two steps', () => {205 setData( model, '<$text c="true">foo</$text><$text a="true" b="true">b[]ar</$text>' );206 testTwoStepCaretMovement( [207 // Gravity is not overridden, caret is a one character after the beginning of the text.208 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },209 '←',210 // Caret movement was not blocked.211 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: true, preventDefault: 0, evtStop: 0 },212 '←',213 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },214 '←',215 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 }216 ] );217 } );218 it( 'should do nothing for unrelated attribute (at the beginning)', () => {219 setData( model, '<$text c="true">[]foo</$text>' );220 testTwoStepCaretMovement( [221 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },222 '←',223 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },224 '←',225 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 }226 ] );227 } );228 it( 'should do nothing for unrelated attribute (at the end)', () => {229 setData( model, '<$text c="true">foo</$text>[]' );230 testTwoStepCaretMovement( [231 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },232 '←',233 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },234 '←',235 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 }236 ] );237 } );238 it( 'should do nothing when caret is at the beginning of block element', () => {239 setData( model, '[]foo', { lastRangeBackward: true } );240 testTwoStepCaretMovement( [241 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },242 '←',243 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 }244 ] );245 } );246 it( 'should require two-steps movement when caret goes between text node with the same attribute but different value', () => {247 setData( model, '<$text a="2">foo</$text><$text a="1">b[]ar</$text>' );248 testTwoStepCaretMovement( [249 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 4 },250 '←',251 // <$text a="2">foo</$text><$text a="1">[]bar</$text>252 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 0, evtStop: 0, caretPosition: 3 },253 '←',254 // <$text a="2">foo[]</$text><$text a="1">bar</$text>255 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 3 }256 ] );257 expect( selection ).to.have.attribute( 'a', 2 );258 testTwoStepCaretMovement( [259 '←',260 // <$text a="2">fo[]o</$text><$text a="1">bar</$text>261 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 2 }262 ] );263 } );264 // https://github.com/ckeditor/ckeditor5/issues/937265 it( 'should not require two-steps between unrelated attributes inside the initial attribute', () => {266 setData( model, '<$text a="1">foo</$text><$text a="1" b="2">bar</$text><$text a="1">b[]az</$text>' );267 testTwoStepCaretMovement( [268 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },269 '←',270 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },271 '←',272 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },273 '←',274 { selectionAttributes: [ 'a', 'b' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },275 '←',276 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 }277 ] );278 } );279 // https://github.com/ckeditor/ckeditor5-engine/issues/1301280 it( 'should handle passing through the only-child with an attribute (single character)', () => {281 setData( model, '<$text a="1">x</$text>[]' );282 testTwoStepCaretMovement( [283 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 1 },284 '←',285 // <$text a="1">{}x</$text>286 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 0, evtStop: 0, caretPosition: 0 },287 '←',288 // {}<$text a="1">x</$text> (because it's a first-child)289 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 0 },290 '←',291 // {}<$text a="1">x</$text>292 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 0 }293 ] );294 } );295 // https://github.com/ckeditor/ckeditor5-engine/issues/1301296 it( 'should handle passing through the only character in the block (no attribute in the initial selection)', () => {297 setData( model, '<$text a="1">x</$text>[]' );298 model.change( writer => writer.removeSelectionAttribute( 'a' ) );299 testTwoStepCaretMovement( [300 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },301 '←',302 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },303 '←',304 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },305 '←',306 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 2, evtStop: 2 },307 '←',308 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 2, evtStop: 2 }309 ] );310 } );311 // https://github.com/ckeditor/ckeditor5-engine/issues/1301312 it( 'should handle passing through the only-child with an attribute (single character, text before)', () => {313 setData( model, 'abc<$text a="1">x</$text>[]' );314 testTwoStepCaretMovement( [315 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },316 '←',317 // abc<$text a="1">{}x</$text>318 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 0, evtStop: 0 },319 '←',320 // abc{}<$text a="1">x</$text> (because it's a first-child)321 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },322 '←',323 // abc{}<$text a="1">x</$text>324 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1 }325 ] );326 } );327 // https://github.com/ckeditor/ckeditor5-engine/issues/1301328 it( 'should handle passing through the only-child with an attribute (multiple characters)', () => {329 setData( model, '<$text a="1">xyz</$text>[]' );330 testTwoStepCaretMovement( [331 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },332 '←',333 // <$text a="1">xy{}z</$text>334 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },335 '←',336 // <$text a="1">x{}yz</$text>337 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },338 '←',339 // <$text a="1">{}xyz</$text>340 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 0, evtStop: 0 },341 '←',342 // {}<$text a="1">xyz</$text>343 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },344 '←',345 // {}<$text a="1">xyz</$text>346 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1 }347 ] );348 } );349 it( 'should handle leaving an attribute preceded by another block', () => {350 setData( model, '<paragraph><$text b="1">foo</$text></paragraph><paragraph><$text a="1">[]bar</$text></paragraph>' );351 testTwoStepCaretMovement( [352 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: [ 1, 0 ] },353 '←',354 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: [ 1, 0 ] },355 '←',356 { selectionAttributes: [ 'b' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: [ 0, 3 ] }357 ] );358 } );359 } );360 describe( 'moving and typing around the attribute', () => {361 it( 'should handle typing after the attribute', () => {362 setData( model, '<$text a="1">x[]</$text>' );363 testTwoStepCaretMovement( [364 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 1 },365 'y',366 // <$text a="1">xy[]</$text>367 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 2 },368 '→',369 // <$text a="1">xy</$text>[]370 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1, caretPosition: 2 },371 'z',372 // <$text a="1">xy</$text>z[]373 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 3 },374 '←',375 // <$text a="1">xy</$text>[]z376 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1, caretPosition: 2 },377 '←',378 // <$text a="1">xy[]</$text>z379 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 2, evtStop: 2, caretPosition: 2 },380 'w',381 // <$text a="1">xyw[]</$text>382 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 2, evtStop: 2, caretPosition: 3 }383 ] );384 } );385 it( 'should handle typing before the attribute', () => {386 setData( model, '<$text a="1">[]x</$text>' );387 testTwoStepCaretMovement( [388 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },389 '←',390 // []<$text a="1">x</$text>391 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },392 'z',393 // z[]<$text a="1">x</$text>394 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },395 'x',396 // zx[]<$text a="1">x</$text>397 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },398 '→',399 // zx<$text a="1">[]x</$text>400 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 2, evtStop: 2 },401 'a',402 // zx<$text a="1">a[]x</$text>403 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 2, evtStop: 2 }404 ] );405 } );406 // https://github.com/ckeditor/ckeditor5-engine/issues/1346407 // https://github.com/ckeditor/ckeditor5/issues/946408 it( 'should correctly re-renter the attribute', () => {409 setData( model, 'fo[]o <$text a="1">bar</$text>' );410 testTwoStepCaretMovement( [411 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },412 '→',413 // foo[] <$text a="1">bar</$text>414 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },415 '→',416 // foo []<$text a="1">bar</$text>417 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },418 '→',419 // foo <$text a="1">[]bar</$text>420 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },421 '→',422 // foo <$text a="1">b[]ar</$text>423 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },424 '←',425 // foo <$text a="1">[]bar</$text>426 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },427 '←',428 // foo []<$text a="1">bar</$text>429 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 2, evtStop: 2 },430 '←',431 // foo[] <$text a="1">bar</$text>432 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 2, evtStop: 2 },433 '←',434 // fo[]o <$text a="1">bar</$text>435 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 2, evtStop: 2 }436 ] );437 } );438 // https://github.com/ckeditor/ckeditor5/issues/922439 it( 'should not lose the new attribute when typing (after)', () => {440 setData( model, '<$text a="1">x[]</$text>' );441 testTwoStepCaretMovement( [442 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },443 '→',444 // <$text a="1">x</$text>[]445 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1 }446 ] );447 model.change( writer => {448 writer.setSelectionAttribute( 'b', 1 );449 } );450 // <$text a="1">x</$text><$text b="1">[]</$text>451 expect( selection ).to.have.property( 'isGravityOverridden', true );452 expect( getSelectionAttributesArray( selection ) ).to.have.members( [ 'b' ] );453 model.change( writer => {454 writer.insertText( 'yz', selection.getAttributes(), selection.getFirstPosition() );455 } );456 // <$text a="1">x</$text><$text b="1">yz[]</$text>457 expect( selection ).to.have.property( 'isGravityOverridden', false );458 expect( getSelectionAttributesArray( selection ) ).to.have.members( [ 'b' ] );459 } );460 // https://github.com/ckeditor/ckeditor5/issues/922461 it( 'should not lose the new attribute when typing (before)', () => {462 setData( model, '<$text a="1">[]x</$text>' );463 testTwoStepCaretMovement( [464 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },465 '←',466 // []<$text a="1">x</$text>467 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1 }468 ] );469 model.change( writer => {470 writer.setSelectionAttribute( 'b', 1 );471 } );472 // <$text b="1">[]</$text><$text a="1">x</$text>473 expect( selection ).to.have.property( 'isGravityOverridden', false );474 expect( getSelectionAttributesArray( selection ) ).to.have.members( [ 'b' ] );475 model.change( writer => {476 writer.insertText( 'yz', selection.getAttributes(), selection.getFirstPosition() );477 } );478 // <$text b="1">yz[]</$text><$text a="1">x</$text>479 expect( selection ).to.have.property( 'isGravityOverridden', false );480 expect( getSelectionAttributesArray( selection ) ).to.have.members( [ 'b' ] );481 } );482 } );483 describe( 'multiple attributes', () => {484 beforeEach( () => {485 plugin.registerAttribute( 'c' );486 } );487 it( 'should work with the two-step caret movement (moving right)', () => {488 setData( model, 'fo[]o<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>qux' );489 testTwoStepCaretMovement( [490 // fo[]o<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>qux491 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },492 '→',493 // foo[]<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>qux494 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },495 '→',496 // foo<$text a="true">[]foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>qux497 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },498 '→',499 '→',500 '→',501 // foo<$text a="true">foo[]</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>qux502 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },503 '→',504 // foo<$text a="true">foo</$text><$text a="true" c="true">[]bar</$text><$text c="true">baz</$text>qux505 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: true, preventDefault: 2, evtStop: 2 },506 '→',507 '→',508 '→',509 // foo<$text a="true">foo</$text><$text a="true" c="true">bar[]</$text><$text c="true">baz</$text>qux510 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: false, preventDefault: 2, evtStop: 2 },511 '→',512 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">[]baz</$text>qux513 { selectionAttributes: [ 'c' ], isGravityOverridden: true, preventDefault: 3, evtStop: 3 },514 '→',515 '→',516 '→',517 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz[]</$text>qux518 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 3, evtStop: 3 },519 '→',520 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>[]qux521 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 4, evtStop: 4 },522 '→',523 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>q[]ux524 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 4, evtStop: 4 }525 ] );526 } );527 it( 'should work with the two-step caret movement (moving left)', () => {528 setData( model, 'foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>q[]ux' );529 testTwoStepCaretMovement( [530 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>q[]ux531 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },532 '←',533 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>[]qux534 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 0, evtStop: 0 },535 '←',536 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz[]</$text>qux537 { selectionAttributes: [ 'c' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1 },538 '←',539 '←',540 '←',541 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">[]baz</$text>qux542 { selectionAttributes: [ 'c' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1 },543 '←',544 // foo<$text a="true">foo</$text><$text a="true" c="true">bar[]</$text><$text c="true">baz</$text>qux545 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: false, preventDefault: 2, evtStop: 2 },546 '←',547 '←',548 '←',549 // foo<$text a="true">foo</$text><$text a="true" c="true">[]bar</$text><$text c="true">baz</$text>qux550 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: true, preventDefault: 2, evtStop: 2 },551 '←',552 // foo<$text a="true">foo[]</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>qux553 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 3, evtStop: 3 },554 '←',555 '←',556 '←',557 // foo<$text a="true">[]foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>qux558 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 3, evtStop: 3 },559 '←',560 // foo[]<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>qux561 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 4, evtStop: 4 },562 '←',563 // fo[]o<$text a="true">foo</$text><$text a="true" c="true">bar</$text><$text c="true">baz</$text>qux564 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 4, evtStop: 4 }565 ] );566 } );567 describe( 'when two elements ends at the same position', () => {568 it( 'moving the caret in should take 2 steps', () => {569 setData( model, 'foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text>q[]ux' );570 testTwoStepCaretMovement( [571 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text>q[]ux572 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 10 },573 '←',574 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text>[]qux575 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 0, evtStop: 0, caretPosition: 9 },576 '←',577 // foo<$text a="true">foo</$text><$text a="true" c="true">bar[]</$text>qux578 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 9 },579 '←',580 // foo<$text a="true">foo</$text><$text a="true" c="true">ba[]r</$text>qux581 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 8 }582 ] );583 } );584 it( 'moving the caret out should take 2 steps', () => {585 setData( model, 'foo<$text a="true">foo</$text><$text a="true" c="true">ba[]r</$text>qux' );586 testTwoStepCaretMovement( [587 // foo<$text a="true">foo</$text><$text a="true" c="true">ba[]r</$text>qux588 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 8 },589 '→',590 // foo<$text a="true">foo</$text><$text a="true" c="true">bar[]</$text>qux591 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 9 },592 '→',593 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text>[]qux594 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1, caretPosition: 9 },595 '→',596 // foo<$text a="true">foo</$text><$text a="true" c="true">bar</$text>q[]ux597 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 10 }598 ] );599 } );600 } );601 describe( 'when two elements starts at the same position', () => {602 it( 'moving the caret in should take 2 steps', () => {603 setData( model, 'fo[]o<$text a="true" c="true">bar</$text><$text a="true">baz</$text>qux' );604 testTwoStepCaretMovement( [605 // fo[]o<$text a="true" c="true">bar</$text><$text a="true">baz</$text>qux606 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 2 },607 '→',608 // foo[]<$text a="true" c="true">bar</$text><$text a="true">baz</$text>qux609 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 3 },610 '→',611 // foo<$text a="true" c="true">[]bar</$text><$text a="true">baz</$text>qux612 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1, caretPosition: 3 },613 '→',614 // foo<$text a="true" c="true">b[]ar</$text><$text a="true">baz</$text>qux615 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 4 }616 ] );617 } );618 it( 'moving the caret out should take 2 steps', () => {619 setData( model, 'foo<$text a="true" c="true">b[]ar</$text><$text a="true">baz</$text>qux' );620 testTwoStepCaretMovement( [621 // foo<$text a="true" c="true">b[]ar</$text><$text a="true">baz</$text>qux622 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 4 },623 '←',624 // foo<$text a="true" c="true">[]bar</$text><$text a="true">baz</$text>qux625 { selectionAttributes: [ 'a', 'c' ], isGravityOverridden: true, preventDefault: 0, evtStop: 0, caretPosition: 3 },626 '←',627 // foo[]<$text a="true" c="true">bar</$text><$text a="true">baz</$text>qux628 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 3 },629 '←',630 // fo[]o<$text a="true" c="true">bar</$text><$text a="true">baz</$text>qux631 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 2 }632 ] );633 } );634 } );635 } );636 describe( 'mouse', () => {637 it( 'should not override gravity when selection is placed at the beginning of text', () => {638 setData( model, '<$text a="true">[]foo</$text>' );639 expect( selection ).to.have.property( 'isGravityOverridden', false );640 } );641 it( 'should not override gravity when selection is placed at the end of text', () => {642 setData( model, '<$text a="true">foo[]</$text>' );643 expect( selection ).to.have.property( 'isGravityOverridden', false );644 } );645 } );646 it( 'should listen with the higher priority than widget type around', () => {647 const highestPlusPrioritySpy = sinon.spy().named( 'highestPrioritySpy' );648 const highestPrioritySpy = sinon.spy().named( 'highestPrioritySpy' );649 const highPrioritySpy = sinon.spy().named( 'highPrioritySpy' );650 const normalPrioritySpy = sinon.spy().named( 'normalPrioritySpy' );651 setData( model, '<$text c="true">foo[]</$text><$text a="true" b="true">bar</$text>' );652 emitter.listenTo( view.document, 'arrowKey', highestPlusPrioritySpy, { context: '$text', priority: priorities.highest + 1 } );653 emitter.listenTo( view.document, 'arrowKey', highestPrioritySpy, { context: '$text', priority: 'highest' } );654 emitter.listenTo( view.document, 'arrowKey', highPrioritySpy, { context: '$text', priority: 'high' } );655 emitter.listenTo( view.document, 'arrowKey', normalPrioritySpy, { context: '$text', priority: 'normal' } );656 fireKeyDownEvent( {657 keyCode: keyCodes.arrowright,658 preventDefault: preventDefaultSpy659 } );660 expect( highestPlusPrioritySpy ).to.be.calledOnce;661 expect( preventDefaultSpy ).to.be.calledImmediatelyAfter( highestPlusPrioritySpy );662 expect( highestPrioritySpy ).not.to.be.called;663 expect( highPrioritySpy ).not.to.be.called;664 expect( normalPrioritySpy ).not.to.be.called;665 } );666 it( 'should do nothing when key other then arrow left and right is pressed', () => {667 setData( model, '<$text a="true">foo[]</$text>' );668 expect( () => {669 fireKeyDownEvent( { keyCode: keyCodes.arrowup } );670 } ).to.not.throw();671 } );672 it( 'should do nothing for non-collapsed selection', () => {673 setData( model, '<$text c="true">fo[o]</$text><$text a="true" b="true">bar</$text>' );674 fireKeyDownEvent( { keyCode: keyCodes.arrowright } );675 expect( selection ).to.have.property( 'isGravityOverridden', false );676 } );677 it( 'should do nothing when shift key is pressed', () => {678 setData( model, '<$text c="true">foo</$text><$text a="true" b="true">b[]ar</$text>' );679 fireKeyDownEvent( {680 keyCode: keyCodes.arrowleft,681 shiftKey: true682 } );683 expect( selection ).to.have.property( 'isGravityOverridden', false );684 } );685 it( 'should do nothing when alt key is pressed', () => {686 setData( model, '<$text c="true">foo</$text><$text a="true" b="true">b[]ar</$text>' );687 fireKeyDownEvent( {688 keyCode: keyCodes.arrowleft,689 altKey: true690 } );691 expect( selection ).to.have.property( 'isGravityOverridden', false );692 } );693 it( 'should do nothing when ctrl key is pressed', () => {694 setData( model, '<$text c="true">foo</$text><$text a="true" b="true">b[]ar</$text>' );695 fireKeyDownEvent( {696 keyCode: keyCodes.arrowleft,697 ctrlKey: true698 } );699 expect( selection ).to.have.property( 'isGravityOverridden', false );700 } );701 it( 'should do nothing when the not a direct selection change but at the attribute boundary', () => {702 setData( model, '<$text a="true">foo[]</$text>bar' );703 testTwoStepCaretMovement( [704 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0 },705 '→',706 { selectionAttributes: [], isGravityOverridden: true, preventDefault: 1, evtStop: 1 }707 ] );708 // Simulate an external text insertion BEFORE the user selection to trigger #change:range.709 model.enqueueChange( 'transparent', writer => {710 writer.insertText( 'x', selection.getFirstPosition().getShiftedBy( -2 ) );711 } );712 expect( selection ).to.have.property( 'isGravityOverridden', true );713 expect( getSelectionAttributesArray( selection ) ).to.have.members( [] );714 } );715 describe( 'right–to–left content', () => {716 it( 'should use the opposite helper methods (RTL content direction)', () => {717 // let model;718 return VirtualTestEditor719 .create( {720 plugins: [ TwoStepCaretMovement ],721 language: {722 content: 'he'723 }724 } )725 .then( newEditor => {726 model = newEditor.model;727 selection = model.document.selection;728 view = newEditor.editing.view;729 newEditor.model.schema.extend( '$text', {730 allowAttributes: [ 'a', 'b', 'c' ],731 allowIn: '$root'732 } );733 model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );734 newEditor.conversion.for( 'upcast' ).elementToAttribute( { view: 'a', model: 'a' } );735 newEditor.conversion.for( 'upcast' ).elementToAttribute( { view: 'b', model: 'b' } );736 newEditor.conversion.for( 'upcast' ).elementToAttribute( { view: 'c', model: 'c' } );737 newEditor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );738 newEditor.plugins.get( TwoStepCaretMovement ).registerAttribute( 'a' );739 return newEditor;740 } )741 .then( newEditor => {742 setData( model, '<$text>לזה[]</$text><$text a="true">שיוצג</$text>' );743 testTwoStepCaretMovement( [744 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 3 },745 '←',746 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 1, evtStop: 1, caretPosition: 3 }747 ], 'rtl' );748 preventDefaultSpy.resetHistory();749 evtStopSpy.resetHistory();750 setData( model, '<$text>לזה</$text><$text a="true">ש[]יוצג</$text>' );751 testTwoStepCaretMovement( [752 { selectionAttributes: [ 'a' ], isGravityOverridden: false, preventDefault: 0, evtStop: 0, caretPosition: 4 },753 '→',754 { selectionAttributes: [ 'a' ], isGravityOverridden: true, preventDefault: 0, evtStop: 0, caretPosition: 3 },755 '→',756 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 3 },757 '→',758 { selectionAttributes: [], isGravityOverridden: false, preventDefault: 1, evtStop: 1, caretPosition: 2 }759 ], 'rtl' );760 return newEditor.destroy();761 } );762 } );763 } );764 const keyMap = {765 '→': 'arrowright',766 '←': 'arrowleft'767 };768 function fireKeyDownEvent( options ) {769 const eventInfo = new EventInfo( view.document, 'keydown' );770 const eventData = new DomEventData( view.document, {771 target: document.body772 }, options );773 sinon.stub( eventInfo, 'stop' ).callsFake( evtStopSpy );774 view.document.fire( eventInfo, eventData );775 }776 function getSelectionAttributesArray( selection ) {777 return Array.from( selection.getAttributeKeys() );778 }779 function testTwoStepCaretMovement( scenario, rtl ) {780 for ( const step of scenario ) {781 if ( typeof step == 'string' ) {782 // An arrow key pressed. Fire the view event and update the model selection.783 if ( keyMap[ step ] ) {784 let preventDefaultCalled;785 fireKeyDownEvent( {786 keyCode: keyCodes[ keyMap[ step ] ],787 preventDefault: () => {788 preventDefaultSpy();789 preventDefaultCalled = true;790 }791 } );792 const position = selection.getFirstPosition();793 if ( !preventDefaultCalled ) {794 if ( step == '→' && !rtl || step == '←' && rtl ) {795 model.change( writer => {796 if ( position.isAtEnd ) {797 const nextBlock = position.parent.nextSibling;798 if ( nextBlock ) {799 writer.setSelection( Position._createAt( nextBlock, 0 ) );800 }801 } else {802 writer.setSelection( selection.getFirstPosition().getShiftedBy( 1 ) );803 }804 } );805 } else if ( step == '←' && !rtl || step == '→' && rtl ) {806 model.change( writer => {807 if ( position.isAtStart ) {808 const previousBlock = position.parent.previousSibling;809 if ( previousBlock ) {810 writer.setSelection( Position._createAt( previousBlock, 'end' ) );811 }812 } else {813 writer.setSelection( selection.getFirstPosition().getShiftedBy( -1 ) );814 }815 } );816 }817 }818 }819 // A regular key pressed. Type some text in the model.820 else {821 model.change( writer => {822 writer.insertText( step, selection.getAttributes(), selection.getFirstPosition() );823 } );824 }825 }826 // If not a key, then it's an assertion.827 else {828 const stepIndex = scenario.indexOf( step );829 const stepString = `in step #${ stepIndex }`;830 if ( step.caretPosition !== undefined ) {831 // Normalize position832 const caretPosition = toArray( step.caretPosition );833 expect( selection.getFirstPosition(), `in step #${ stepIndex }, selection's first position` )834 .to.have.deep.property( 'path', caretPosition );835 }836 expect( getSelectionAttributesArray( selection ), `${ stepString }, selection's attributes` )837 .to.have.members( step.selectionAttributes );838 expect( selection, `${ stepString }, selection's gravity` )839 .to.have.property( 'isGravityOverridden', step.isGravityOverridden );840 expect( preventDefaultSpy, stepString ).to.have.callCount( step.preventDefault );841 expect( evtStopSpy, stepString ).to.have.callCount( step.evtStop );842 }843 }844 }...

Full Screen

Full Screen

filedrophandler_test.js

Source:filedrophandler_test.js Github

copy

Full Screen

1// Copyright 2009 The Closure Library Authors. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS-IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14goog.provide('goog.events.FileDropHandlerTest');15goog.setTestOnly('goog.events.FileDropHandlerTest');16goog.require('goog.events');17goog.require('goog.events.BrowserEvent');18goog.require('goog.events.EventTarget');19goog.require('goog.events.EventType');20goog.require('goog.events.FileDropHandler');21goog.require('goog.testing.jsunit');22goog.require('goog.userAgent');23var textarea;24var doc;25var handler;26var dnd;27var files;28function setUp() {29 textarea = new goog.events.EventTarget();30 doc = new goog.events.EventTarget();31 textarea.ownerDocument = doc;32 handler = new goog.events.FileDropHandler(textarea);33 dnd = false;34 files = null;35 goog.events.listen(handler, goog.events.FileDropHandler.EventType.DROP,36 function(e) {37 dnd = true;38 files =39 e.getBrowserEvent().dataTransfer.files;40 });41}42function tearDown() {43 textarea.dispose();44 doc.dispose();45 handler.dispose();46}47function testOneFile() {48 var preventDefault = false;49 var expectedfiles = [{ fileName: 'file1.jpg' }];50 var dt = { types: ['Files'], files: expectedfiles };51 // Assert that default actions are prevented on dragenter.52 textarea.dispatchEvent(new goog.events.BrowserEvent({53 preventDefault: function() { preventDefault = true; },54 type: goog.events.EventType.DRAGENTER,55 dataTransfer: dt56 }));57 assertTrue(preventDefault);58 preventDefault = false;59 // Assert that default actions are prevented on dragover.60 textarea.dispatchEvent(new goog.events.BrowserEvent({61 preventDefault: function() { preventDefault = true; },62 type: goog.events.EventType.DRAGOVER,63 dataTransfer: dt64 }));65 assertTrue(preventDefault);66 preventDefault = false;67 // Assert that the drop effect is set to 'copy'.68 assertEquals('copy', dt.dropEffect);69 // Assert that default actions are prevented on drop.70 textarea.dispatchEvent(new goog.events.BrowserEvent({71 preventDefault: function() { preventDefault = true; },72 type: goog.events.EventType.DROP,73 dataTransfer: dt74 }));75 assertTrue(preventDefault);76 // Assert that DROP has been fired.77 assertTrue(dnd);78 assertEquals(1, files.length);79 assertEquals(expectedfiles[0].fileName, files[0].fileName);80}81function testMultipleFiles() {82 var preventDefault = false;83 var expectedfiles = [{ fileName: 'file1.jpg' }, { fileName: 'file2.jpg' }];84 var dt = { types: ['Files', 'text'], files: expectedfiles };85 // Assert that default actions are prevented on dragenter.86 textarea.dispatchEvent(new goog.events.BrowserEvent({87 preventDefault: function() { preventDefault = true; },88 type: goog.events.EventType.DRAGENTER,89 dataTransfer: dt90 }));91 assertTrue(preventDefault);92 preventDefault = false;93 // Assert that default actions are prevented on dragover.94 textarea.dispatchEvent(new goog.events.BrowserEvent({95 preventDefault: function() { preventDefault = true; },96 type: goog.events.EventType.DRAGOVER,97 dataTransfer: dt98 }));99 assertTrue(preventDefault);100 preventDefault = false;101 // Assert that the drop effect is set to 'copy'.102 assertEquals('copy', dt.dropEffect);103 // Assert that default actions are prevented on drop.104 textarea.dispatchEvent(new goog.events.BrowserEvent({105 preventDefault: function() { preventDefault = true; },106 type: goog.events.EventType.DROP,107 dataTransfer: dt108 }));109 assertTrue(preventDefault);110 // Assert that DROP has been fired.111 assertTrue(dnd);112 assertEquals(2, files.length);113 assertEquals(expectedfiles[0].fileName, files[0].fileName);114 assertEquals(expectedfiles[1].fileName, files[1].fileName);115}116function testNoFiles() {117 var preventDefault = false;118 var dt = { types: ['text'] };119 // Assert that default actions are not prevented on dragenter.120 textarea.dispatchEvent(new goog.events.BrowserEvent({121 preventDefault: function() { preventDefault = true; },122 type: goog.events.EventType.DRAGENTER,123 dataTransfer: dt124 }));125 assertFalse(preventDefault);126 preventDefault = false;127 // Assert that default actions are not prevented on dragover.128 textarea.dispatchEvent(new goog.events.BrowserEvent({129 preventDefault: function() { preventDefault = true; },130 type: goog.events.EventType.DRAGOVER,131 dataTransfer: dt132 }));133 assertFalse(preventDefault);134 preventDefault = false;135 // Assert that default actions are not prevented on drop.136 textarea.dispatchEvent(new goog.events.BrowserEvent({137 preventDefault: function() { preventDefault = true; },138 type: goog.events.EventType.DROP,139 dataTransfer: dt140 }));141 assertFalse(preventDefault);142 // Assert that DROP has not been fired.143 assertFalse(dnd);144 assertNull(files);145}146function testDragEnter() {147 var preventDefault = false;148 // Assert that default actions are prevented on dragenter.149 // In Chrome the dragenter event has an empty file list and the types is150 // set to 'Files'.151 textarea.dispatchEvent(new goog.events.BrowserEvent({152 preventDefault: function() { preventDefault = true; },153 type: goog.events.EventType.DRAGENTER,154 dataTransfer: { types: ['Files'], files: [] }155 }));156 assertTrue(preventDefault);157 preventDefault = false;158 // Assert that default actions are prevented on dragenter.159 // In Safari 4 the dragenter event has an empty file list and the types is160 // set to 'public.file-url'.161 textarea.dispatchEvent(new goog.events.BrowserEvent({162 preventDefault: function() { preventDefault = true; },163 type: goog.events.EventType.DRAGENTER,164 dataTransfer: { types: ['public.file-url'], files: [] }165 }));166 assertTrue(preventDefault);167 preventDefault = false;168 // Assert that default actions are not prevented on dragenter169 // when the drag contains no files.170 textarea.dispatchEvent(new goog.events.BrowserEvent({171 preventDefault: function() { preventDefault = true; },172 type: goog.events.EventType.DRAGENTER,173 dataTransfer: { types: ['text'], files: [] }174 }));175 assertFalse(preventDefault);176}177function testPreventDropOutside() {178 var preventDefault = false;179 var dt = { types: ['Files'], files: [{ fileName: 'file1.jpg' }] };180 // Assert that default actions are not prevented on dragenter on the181 // document outside the text area.182 doc.dispatchEvent(new goog.events.BrowserEvent({183 preventDefault: function() { preventDefault = true; },184 type: goog.events.EventType.DRAGENTER,185 dataTransfer: dt186 }));187 assertFalse(preventDefault);188 preventDefault = false;189 // Assert that default actions are not prevented on dragover on the190 // document outside the text area.191 doc.dispatchEvent(new goog.events.BrowserEvent({192 preventDefault: function() { preventDefault = true; },193 type: goog.events.EventType.DRAGOVER,194 dataTransfer: dt195 }));196 assertFalse(preventDefault);197 preventDefault = false;198 handler.dispose();199 // Create a new FileDropHandler that prevents drops outside the text area.200 handler = new goog.events.FileDropHandler(textarea, true);201 // Assert that default actions are now prevented on dragenter on the202 // document outside the text area.203 doc.dispatchEvent(new goog.events.BrowserEvent({204 preventDefault: function() { preventDefault = true; },205 type: goog.events.EventType.DRAGENTER,206 dataTransfer: dt207 }));208 assertTrue(preventDefault);209 preventDefault = false;210 // Assert that default actions are now prevented on dragover on the211 // document outside the text area.212 doc.dispatchEvent(new goog.events.BrowserEvent({213 preventDefault: function() { preventDefault = true; },214 type: goog.events.EventType.DRAGOVER,215 dataTransfer: dt216 }));217 assertTrue(preventDefault);218 preventDefault = false;219 // Assert also that the drop effect is set to 'none'.220 assertEquals('none', dt.dropEffect);221}222function testEffectAllowedExceptionIsCaught() {223 // This bug was only affecting IE10+.224 if (!goog.userAgent.IE || !goog.userAgent.isVersionOrHigher(10)) {225 return;226 }227 var preventDefault = false;228 var expectedfiles = [{fileName: 'file1.jpg'}];229 var dt = {types: ['Files'], files: expectedfiles};230 // We construct a mock DataTransfer object that define a setter will throw231 // SCRIPT65535 when attempt to set property effectAllowed to simulate IE Bug232 // #811625. See more: https://github.com/google/closure-library/issues/485.233 Object.defineProperty(dt, 'effectAllowed',234 {set: function(v) { throw new Error('SCRIPT65535'); }});235 // Assert that default actions are prevented on dragenter.236 textarea.dispatchEvent(new goog.events.BrowserEvent({237 preventDefault: function() { preventDefault = true; },238 type: goog.events.EventType.DRAGENTER,239 dataTransfer: dt240 }));241 assertTrue(preventDefault);242 preventDefault = false;243 // Assert that default actions are prevented on dragover.244 textarea.dispatchEvent(new goog.events.BrowserEvent({245 preventDefault: function() { preventDefault = true; },246 type: goog.events.EventType.DRAGOVER,247 dataTransfer: dt248 }));249 assertTrue(preventDefault);...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1import { addDecorator } from '@storybook/react';2import { withInfo } from '@storybook/addon-info';3import { withKnobs } from '@storybook/addon-knobs';4import { withA11y } from '@storybook/addon-a11y';5import { withTests } from '@storybook/addon-jest';6import { withPerformance } from 'storybook-addon-performance';7import results from '../.jest-test-results.json';8addDecorator(withInfo);9addDecorator(withKnobs);10addDecorator(withA11y);11addDecorator(withTests({ results }));12addDecorator(withPerformance);13const path = require('path');14module.exports = {15 module: {16 {17 loaders: [require.resolve('@storybook/source-loader')],18 },19 {20 loaders: [require.resolve('@storybook/addon-storysource/loader')],21 }22 },23 resolve: {24 alias: {25 '@': path.resolve(__dirname, '../src')26 }27 }28};29{30 ["@babel/preset-env", {31 "targets": {32 }33 }],34 "@babel/plugin-syntax-dynamic-import",35 "@babel/plugin-syntax-import-meta",36 ["@babel/plugin-proposal-decorators", { "legacy": true }],37 ["@babel/plugin-proposal-function-sent", { "loose": true }],38 ["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }],39 ["@babel/plugin-proposal-numeric-separator", { "loose": true }],40 ["@babel/plugin-proposal-optional-catch-binding", { "loose": true

Full Screen

Using AI Code Generation

copy

Full Screen

1import { storiesOf } from '@storybook/react';2import { action } from '@storybook/addon-actions';3import { linkTo } from '@storybook/addon-links';4import Button from './Button';5import Welcome from './Welcome';6storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);7storiesOf('Button', module)8 .add('with text', () => <Button onClick={action('clicked')}>Hello Button</Button>)9 .add('with some emoji', () => (10 <Button onClick={action('clicked')}>11 ));12import { storiesOf } from '@storybook/react';13import { action } from '@storybook/addon-actions';14import { linkTo } from '@storybook/addon-links';15import Button from './Button';16import Welcome from './Welcome';17storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);18storiesOf('Button', module)19 .add('with text', () => <Button onClick={action('clicked')}>Hello Button</Button>)20 .add('with some emoji', () => (21 <Button onClick={action('clicked')}>22 ));23import { storiesOf } from '@storybook/react';24import { action } from '@storybook/addon-actions';25import { linkTo } from '@storybook/addon-links';26import Button from './Button';27import Welcome from './Welcome';28storiesOf('Welcome', module).add('to Storybook', () => <Welcome showApp={linkTo('Button')} />);29storiesOf('Button', module)30 .add('with text', () => <Button onClick={action('clicked')}>Hello Button</Button>)31 .add('with some emoji', () => (32 <Button onClick={

Full Screen

Using AI Code Generation

copy

Full Screen

1import { prevent } from 'storybook-root-element';2prevent('body');3prevent('body', 'html');4prevent('body', 'html', 'div');5prevent('body', 'html', 'div', 'p');6prevent('body', 'html', 'div', 'p', 'a');7prevent('body', 'html', 'div', 'p', 'a', 'button');8prevent('body', 'html', 'div', 'p', 'a', 'button', 'input');9prevent('body', 'html', 'div', 'p', 'a', 'button', 'input', 'span');10prevent('body', 'html', 'div', 'p', 'a', 'button', 'input', 'span', 'h1');11prevent('body', 'html', 'div', 'p', 'a', 'button', 'input', 'span', 'h1', 'h2');12prevent('body', 'html', 'div', 'p', 'a', 'button', 'input', 'span', 'h1', 'h2', 'h3');

Full Screen

Using AI Code Generation

copy

Full Screen

1import { prevent } from 'storybook-root/prevent';2import { Link } from 'storybook-root/components/Link';3import { Button } from 'storybook-root/components/Button';4import { Form } from 'storybook-root/components/Form';5prevent();6export default {7};8export const LinkStory = () => {9 return (10 );11};12export const ButtonStory = () => {13 return (14 );15};16export const FormStory = () => {17 return (18 );19};20import { prevent } from 'storybook-root/prevent';21import { Link } from 'storybook-root/components/Link';22import { Button } from 'storybook-root/components/Button';23import { Form } from 'storybook-root/components/Form';24prevent();25export default {26};27export const LinkStory = () => {28 return (29 );30};31export const ButtonStory = () => {32 return (33 );34};35export const FormStory = () => {36 return (37 );38};39import { prevent } from 'storybook-root/prevent';40import { Link } from 'storybook-root/components/Link';41import { Button } from 'storybook-root/components/Button';42import { Form } from 'storybook-root/components/Form';

Full Screen

Using AI Code Generation

copy

Full Screen

1import { prevent } from 'storybook-root';2prevent('prevent me');3prevent('prevent me');4import React from 'react';5import { storiesOf } from '@storybook/react';6import { withKnobs, text } from '@storybook/addon-knobs';7import MyComponent from './MyComponent';8storiesOf('MyComponent', module)9 .addDecorator(withKnobs)10 .add('with knobs', () => (11 <MyComponent text={text('Text', 'Hello Storybook')} />12 ));13import React from 'react';14import { storiesOf } from '@storybook/react';15import { withNotes } from '@storybook/addon-notes';16import MyComponent from './MyComponent';17storiesOf('MyComponent', module)18 .add('with notes', withNotes('A very simple component')(() => (19 )));20import React from 'react';21import { storiesOf } from '@storybook/react';22import { action } from '@storybook/addon-actions';23import MyComponent from './MyComponent';24storiesOf('MyComponent', module)25 .add('with actions', () => (26 <MyComponent onClick={action('clicked')} />27 ));28import { configure, addParameters } from '@storybook/react';29addParameters({30 options: {31 theme: {32 },33 },34});35import { addParameters } from '@storybook/react';36addParameters({37 { name: 'twitter', value: '#00aced', default: true },38 { name: 'facebook', value: '#3b5998' },39});40import { addParameters } from

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run storybook-root automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful