Closed
Bug 26528
Opened 26 years ago
Closed 23 years ago
cloneNode() doesn't copy event handlers
Categories
(Core :: DOM: Core & HTML, defect, P2)
Core
DOM: Core & HTML
Tracking
()
VERIFIED
FIXED
mozilla1.0
People
(Reporter: waterson, Assigned: fabian)
References
Details
(Keywords: dom1, testcase, Whiteboard: [HAVE FIX])
Attachments
(2 files)
380 bytes,
text/html
|
Details | |
1.89 KB,
patch
|
brendan
:
review+
jst
:
superreview+
asa
:
approval+
|
Details | Diff | Splinter Review |
Not sure if this is a bug or not, but I expected that cloneNode() would copy
the event handlers. See attached test case. Assuming this is a bug, here's
how to reproduce:
1. Click "Sheep". Note the message printed to the console.
2. Click "Clone It". A new "Sheep" button will be created via cloneNode().
3. Click the second "Sheep" button.
Expected behavior: a message is printed to the console.
Actual behavior: no message is printed.
Reporter | ||
Comment 1•26 years ago
|
||
Comment 2•26 years ago
|
||
In fact, they are copied but has the cloned node isn't yet in the DOM, the event
handlers are not compiled and therefore wont work.
Comment 3•25 years ago
|
||
There are 2 issues here:
1) Should event handlers be cloned along with cloneNode()? The DOM WG says no,
except HTML event handler attributes.
2) Should event handler attributes be compiled when an orphaned element is added
to a document (currently we only compile them if the attribute is set while the
element is attached to the document). The answer seems to be yes, but one can
create cases where this might not do what you expect.
Comment 4•25 years ago
|
||
Marking M16 for now. Ducarroz implied that he is working around this issue, but
would like it fixed eventually.
Status: NEW → ASSIGNED
Target Milestone: M16
M16 has been out for a while now, these bugs target milestones need to be
updated.
Comment 6•25 years ago
|
||
Marking nsbeta3... Developers on the DOM newsgroup have complained about this
functionality and the fix is reasonably straightforward.
Keywords: nsbeta3
Target Milestone: M16 → M18
Comment 7•25 years ago
|
||
correctness of DOM1 support. Fixed believed to be easy and low risk. Recomment
nsbeta3+.
Keywords: correctness
Comment 8•25 years ago
|
||
Since this isn't known to break existing content we (I and Nisheeth) are
minusing this for now, please speak up if you think this really should be fixed
for beta3.
Whiteboard: [nsbeta3-]
Comment 10•25 years ago
|
||
Actually I think this is not a bug. a quote from
http://www.w3.org/TR/2000/PR-DOM-Level-2-Events-20000927/events.html
"When a Node is copied using the cloneNode method the EventListeners attached to
the source Node are not attached to the copied Node. If the user wishes the same
EventListeners to be added to the newly created copy the user must add them
manually."
Reporter | ||
Comment 11•25 years ago
|
||
vidur, I'm guessing that the citation is a direct result of you clearing up this
issue with the WG? Please feel free to INVALID this bug...
Comment 12•25 years ago
|
||
It may be 'correct' not to copy the event handlers. But it isn't what other
browsers do (IE 5) so is going to cause compatability problems. If we added
an additional mechanism to copy event handlers, then we would probably be on
safe ground.
I'm currently using a large and nasty js hack to manually copy them. It isn't
very pretty and I suspect it will stop working when other 'features' of the
Gecho attribute/property duality a fixed up. Note that it isn't possible to
simple assign an event handler (using dest.onclick=src.onclick) as the function
context is wrong. My current workaround is as follows
function ffactory(body) // compile in context of current object
{
return new Function(body)
}
//copy onclick event handler from src to dest
dest.newFn = ffactory;
dest.onclick = dest.newFn(src.getAttribute("onclick"));
This 'feature' is compounded by the fact there is no way to (simply) enumerate
all the event handlers on an element, so using the above in a loop is messy.
Hope the above helps anyone who has spent as long a me trying to get round this
feature.
![]() |
||
Comment 13•25 years ago
|
||
*** Bug 60700 has been marked as a duplicate of this bug. ***
Comment 14•25 years ago
|
||
At the URL listed above, or more directly:
http://www.w3.org/TR/2000/PR-DOM-Level-2-Events-20000927/events.html#Events-registration-html40
it states:
"In HTML 4.0, event listeners were specified as attributes of an element. As
such, registration of a second event listener of the same type would replace the
first listener. The DOM Event Model allows registration of multiple event
listeners on a single EventTarget. To achieve this, event listeners are no
longer stored as attribute values.
In order to achieve compatibility with HTML 4.0, implementors may view the
setting of attributes which represent event handlers as the creation and
registration of an EventListener on the EventTarget."
While it says "implementors may" rather than something stronger, I'd suggest
that this would be a very useful thing for implementors to do.
Perhaps my attachment to bug 60700 gives a clearer demonstration of where this
may be useful.
Comment 15•25 years ago
|
||
Passing this one along to Johnny. At this point, I question whether we can
change our behavior without breaking some number of existing scripts (scripts
that manually copy the event handlers after cloning). Removing the Target
Milestone so Johnny can make a decision about validity and fix timeframe.
Assignee: vidur → jst
Status: ASSIGNED → NEW
Target Milestone: M18 → ---
Updated•25 years ago
|
Component: DOM Level 1 → DOM Core
![]() |
||
Comment 17•24 years ago
|
||
*** Bug 71938 has been marked as a duplicate of this bug. ***
Comment 19•24 years ago
|
||
cloneNode's innerHTML also cannot be accessed.
var foo = document.getElementById("foo");
var bar = foo.cloneNode(true);
alert (bar.innerHTML);
extended the existing testcase.
Comment 20•24 years ago
|
||
Accepting and targetting for mozilla0.9.8
Status: NEW → ASSIGNED
Target Milestone: --- → mozilla0.9.8
Assignee | ||
Comment 21•24 years ago
|
||
When fixing this we should make sure we don't break anything in the tree that
uses cloneNode(), and we should probably figure out whether this change is for
better or for worse, like Vidur said in post #15
Updated•24 years ago
|
Target Milestone: mozilla0.9.8 → mozilla0.9.9
Comment 22•24 years ago
|
||
*** Bug 121474 has been marked as a duplicate of this bug. ***
Updated•24 years ago
|
Assignee | ||
Comment 24•24 years ago
|
||
This also fixes bug 71937 for free. The new CloneNode() is slower but does the
right thing, and CloneNode() is barely used.
Comment 25•24 years ago
|
||
good news - i use CloneNode() very often! :-)
Comment 26•24 years ago
|
||
More to do ==> slower execution, no surprise there. I doubt the slowdown will be
noticeable at all, except in extreme edge casaes.
Updated•23 years ago
|
Whiteboard: [nsbeta3-] → [HAVE FIX]
Updated•23 years ago
|
Giving to Fabian since he has been working on the fix. If you can't do the
testing and get reviews in time for 1.0 please let us know.
Assignee: jst → hidday
Status: ASSIGNED → NEW
Assignee | ||
Comment 28•23 years ago
|
||
Sure, this just needs reviews... testing is already done.
Status: NEW → ASSIGNED
Comment 29•23 years ago
|
||
Comment on attachment 70948 [details] [diff] [review]
Proposed fix.
sr=jst
Attachment #70948 -
Flags: superreview+
Comment 30•23 years ago
|
||
Attachment #70948 -
Flags: review+
Comment 31•23 years ago
|
||
Comment on attachment 70948 [details] [diff] [review]
Proposed fix.
a=asa (on behalf of drivers) for checkin to the 1.0 trunk
Attachment #70948 -
Flags: approval+
Comment 32•23 years ago
|
||
So this fix clones attribute event listeners, or all event listeners for an
element? It is a bit unclear to me what is being fixed since there was some
disagreement about the spec. ... and if it does clone all event listeners, is
there a way to not clone event listeners?
Comment 33•23 years ago
|
||
This makes cloneNode() clone event handlers that are specified as attributes in
HTML, not dynamically set element.on* event handlers.
Comment 34•23 years ago
|
||
And also not those added with addEventListener, right? I ask because of the
comments in comment #14
Comment 35•23 years ago
|
||
Correct, event handlers (listeners) added with addEventListener() will not be
cloned either.
Assignee | ||
Comment 36•23 years ago
|
||
Checking in nsGenericHTMLElement.cpp;
/cvsroot/mozilla/content/html/content/src/nsGenericHTMLElement.cpp,v <--
nsGenericHTMLElement.cpp
new revision: 1.338; previous revision: 1.337
done
Marking FIXED, thanks for the reviews!
Status: ASSIGNED → RESOLVED
Closed: 23 years ago
Resolution: --- → FIXED
![]() |
||
Comment 38•23 years ago
|
||
*** Bug 136561 has been marked as a duplicate of this bug. ***
Assignee | ||
Comment 39•23 years ago
|
||
*** Bug 138130 has been marked as a duplicate of this bug. ***
Component: DOM: Core → DOM: Core & HTML
QA Contact: stummala → general
Comment hidden (collapsed) |
You need to log in
before you can comment on or make changes to this bug.
Description
•