This repository was archived by the owner on Feb 26, 2024. It is now read-only.
File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -61,8 +61,16 @@ export function patchProperty(obj, prop) {
6161 }
6262
6363 if ( typeof fn === 'function' ) {
64- this [ _prop ] = fn ;
65- this . addEventListener ( eventName , fn , false ) ;
64+ var wrapFn = function ( event ) {
65+ var result ;
66+ result = fn . apply ( this , arguments ) ;
67+
68+ if ( result != undefined && ! result )
69+ event . preventDefault ( ) ;
70+ } ;
71+
72+ this [ _prop ] = wrapFn ;
73+ this . addEventListener ( eventName , wrapFn , false ) ;
6674 } else {
6775 this [ _prop ] = null ;
6876 }
Original file line number Diff line number Diff line change @@ -266,4 +266,33 @@ describe('element', function () {
266266 } ) ;
267267 } ) ;
268268
269+ describe ( 'onEvent default behavior' , function ( ) {
270+ var checkbox ;
271+ beforeEach ( function ( ) {
272+ checkbox = document . createElement ( 'input' ) ;
273+ checkbox . type = "checkbox" ;
274+ document . body . appendChild ( checkbox ) ;
275+ } ) ;
276+
277+ afterEach ( function ( ) {
278+ document . body . removeChild ( checkbox ) ;
279+ } ) ;
280+
281+ it ( 'should be possible to prevent default behavior by returning false' , function ( ) {
282+ checkbox . onclick = function ( ) {
283+ return false ;
284+ } ;
285+
286+ checkbox . click ( ) ;
287+ expect ( checkbox . checked ) . toBe ( false ) ;
288+ } ) ;
289+
290+ it ( 'should have no effect on default behavior when not returning anything' , function ( ) {
291+ checkbox . onclick = function ( ) { } ;
292+
293+ checkbox . click ( ) ;
294+ expect ( checkbox . checked ) . toBe ( true ) ;
295+ } ) ;
296+ } ) ;
297+
269298} ) ;
You can’t perform that action at this time.
0 commit comments