Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
jquery
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
夏彬
jquery
Commits
37c3d087
Commit
37c3d087
authored
May 20, 2015
by
Oleg Gaidarenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Event: improve originalEvent hack
Ref 6df669f0fb87cd9975a18bf6bbe3c3548afa4fee Ref gh-2336
parent
e6a492d3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
141 additions
and
6 deletions
+141
-6
event.js
src/event.js
+12
-6
event.js
test/unit/event.js
+129
-0
No files found.
src/event.js
View file @
37c3d087
...
@@ -648,17 +648,22 @@ jQuery.event = {
...
@@ -648,17 +648,22 @@ jQuery.event = {
}
}
},
},
// Piggyback on a donor event to simulate a different one
simulate
:
function
(
type
,
elem
,
event
,
bubble
)
{
simulate
:
function
(
type
,
elem
,
event
,
bubble
)
{
// Piggyback on a donor event to simulate a different one.
// Fake originalEvent to avoid donor's stopPropagation, but if the
// simulated event prevents default then we do the same on the donor.
var
e
=
jQuery
.
extend
(
var
e
=
jQuery
.
extend
(
new
jQuery
.
Event
(),
new
jQuery
.
Event
(),
event
,
event
,
{
{
type
:
type
,
type
:
type
,
isSimulated
:
true
,
isSimulated
:
true
originalEvent
:
{}
// Previously, `originalEvent: {}` was set here, so stopPropagation call
// would not be triggered on donor event, since in our own
// jQuery.event.stopPropagation function we had a check for existence of
// originalEvent.stopPropagation method, so, consequently it would be a noop.
//
// Guard for simulated events was moved to jQuery.event.stopPropagation function
// since `originalEvent` should point to the original event for the
// constancy with other events and for more focused logic
}
}
);
);
if
(
bubble
)
{
if
(
bubble
)
{
...
@@ -763,7 +768,8 @@ jQuery.Event.prototype = {
...
@@ -763,7 +768,8 @@ jQuery.Event.prototype = {
var
e
=
this
.
originalEvent
;
var
e
=
this
.
originalEvent
;
this
.
isPropagationStopped
=
returnTrue
;
this
.
isPropagationStopped
=
returnTrue
;
if
(
!
e
)
{
if
(
!
e
||
this
.
isSimulated
)
{
return
;
return
;
}
}
// If stopPropagation exists, run it on the original event
// If stopPropagation exists, run it on the original event
...
...
test/unit/event.js
View file @
37c3d087
...
@@ -2686,6 +2686,135 @@ test( "Inline event result is returned (#13993)", function() {
...
@@ -2686,6 +2686,135 @@ test( "Inline event result is returned (#13993)", function() {
equal
(
result
,
42
,
"inline handler returned value"
);
equal
(
result
,
42
,
"inline handler returned value"
);
});
});
test
(
"preventDefault() on focusin does not throw exception"
,
function
(
assert
)
{
expect
(
1
);
var
done
=
assert
.
async
(),
input
=
jQuery
(
"<input/>"
).
appendTo
(
"#form"
);
input
.
on
(
"focusin"
,
function
(
event
)
{
var
exceptionCaught
;
try
{
event
.
preventDefault
();
}
catch
(
theException
)
{
exceptionCaught
=
theException
;
}
assert
.
strictEqual
(
exceptionCaught
,
undefined
,
"Preventing default on focusin throws no exception"
);
done
();
}
).
trigger
(
"focus"
);
}
);
test
(
"Donor event interference"
,
function
(
assert
)
{
assert
.
expect
(
10
);
var
html
=
"<div id='donor-outer'>"
+
"<form id='donor-form'>"
+
"<input id='donor-input' type='radio' />"
+
"</form>"
+
"</div>"
;
jQuery
(
"#qunit-fixture"
).
append
(
html
);
jQuery
(
"#donor-outer"
).
on
(
"click"
,
function
(
event
)
{
assert
.
ok
(
true
,
"click bubbled to outer div"
);
assert
.
equal
(
typeof
event
.
originalEvent
,
"object"
,
"make sure originalEvent exist"
);
assert
.
equal
(
event
.
type
,
"click"
,
"make sure event type is correct"
);
}
);
jQuery
(
"#donor-input"
).
on
(
"click"
,
function
(
event
)
{
assert
.
ok
(
true
,
"got a click event from the input"
);
assert
.
ok
(
!
event
.
isPropagationStopped
(),
"propagation says it's not stopped"
);
assert
.
equal
(
event
.
type
,
"click"
,
"make sure event type is correct"
);
assert
.
equal
(
typeof
event
.
originalEvent
,
"object"
,
"make sure originalEvent exist"
);
}
);
jQuery
(
"#donor-input"
).
on
(
"change"
,
function
(
event
)
{
assert
.
equal
(
typeof
event
.
originalEvent
,
"object"
,
"make sure originalEvent exist"
);
assert
.
equal
(
event
.
type
,
"change"
,
"make sure event type is correct"
);
assert
.
ok
(
true
,
"got a change event from the input"
);
event
.
stopPropagation
();
}
);
jQuery
(
"#donor-input"
)[
0
].
click
();
}
);
test
(
"originalEvent property for IE8"
,
function
(
assert
)
{
if
(
!
(
/msie 8
\.
0/i
.
test
(
window
.
navigator
.
userAgent
))
)
{
assert
.
expect
(
1
);
assert
.
ok
(
true
,
"Assertions should run only in IE"
);
return
;
}
assert
.
expect
(
12
);
var
html
=
"<div id='donor-outer'>"
+
"<form id='donor-form'>"
+
"<input id='donor-input' type='radio' />"
+
"</form>"
+
"</div>"
;
jQuery
(
"#qunit-fixture"
).
append
(
html
);
jQuery
(
"#donor-outer"
).
on
(
"change"
,
function
(
event
)
{
assert
.
ok
(
true
,
"click bubbled to outer div"
);
assert
.
equal
(
event
.
originalEvent
.
type
,
"click"
,
"make sure simulated event is a click"
);
assert
.
equal
(
event
.
type
,
"change"
,
"make sure event type is correct"
);
}
);
jQuery
(
"#donor-outer"
).
on
(
"click"
,
function
(
event
)
{
assert
.
ok
(
true
,
"click bubbled to outer div"
);
assert
.
equal
(
event
.
originalEvent
.
type
,
"click"
,
"make sure originalEvent exist"
);
}
);
jQuery
(
"#donor-input"
).
on
(
"click"
,
function
(
event
)
{
assert
.
ok
(
true
,
"got a click event from the input"
);
assert
.
ok
(
!
event
.
isPropagationStopped
(),
"propagation says it's not stopped"
);
assert
.
equal
(
event
.
originalEvent
.
type
,
"click"
,
"make sure originalEvent exist"
);
assert
.
equal
(
event
.
type
,
"click"
,
"make sure event type is correct"
);
}
);
jQuery
(
"#donor-input"
).
on
(
"change"
,
function
(
event
)
{
assert
.
equal
(
event
.
originalEvent
.
type
,
"click"
,
"make sure originalEvent exist"
);
assert
.
equal
(
event
.
type
,
"change"
,
"make sure event type is correct"
);
assert
.
ok
(
true
,
"got a change event from the input"
);
}
);
jQuery
(
"#donor-input"
)[
0
].
click
();
}
);
test
(
"originalEvent property for Chrome, Safari and FF of simulated event"
,
function
(
assert
)
{
var
userAgent
=
window
.
navigator
.
userAgent
;
if
(
!
(
/chrome/i
.
test
(
userAgent
)
||
/firefox/i
.
test
(
userAgent
)
||
/safari/i
.
test
(
userAgent
)
)
)
{
assert
.
expect
(
1
);
assert
.
ok
(
true
,
"Assertions should run only in Chrome, Safari and FF"
);
return
;
}
assert
.
expect
(
4
);
var
html
=
"<div id='donor-outer'>"
+
"<form id='donor-form'>"
+
"<input id='donor-input' type='radio' />"
+
"</form>"
+
"</div>"
;
jQuery
(
"#qunit-fixture"
).
append
(
html
);
jQuery
(
"#donor-outer"
).
on
(
"focusin"
,
function
(
event
)
{
assert
.
ok
(
true
,
"focusin bubbled to outer div"
);
assert
.
equal
(
event
.
originalEvent
.
type
,
"focus"
,
"make sure originalEvent type is correct"
);
assert
.
equal
(
event
.
type
,
"focusin"
,
"make sure type is correct"
);
}
);
jQuery
(
"#donor-input"
).
on
(
"focus"
,
function
()
{
assert
.
ok
(
true
,
"got a focus event from the input"
);
}
);
jQuery
(
"#donor-input"
).
trigger
(
"focus"
);
}
);
// This tests are unreliable in Firefox
// This tests are unreliable in Firefox
if
(
!
(
/firefox/i
.
test
(
window
.
navigator
.
userAgent
))
)
{
if
(
!
(
/firefox/i
.
test
(
window
.
navigator
.
userAgent
))
)
{
test
(
"Check order of focusin/focusout events"
,
2
,
function
()
{
test
(
"Check order of focusin/focusout events"
,
2
,
function
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment