Ticket #4074 (closed bug: fixed)
.clone() doesn't respect ownerDocument
| Reported by: | friedcell | Owned by: | john |
|---|---|---|---|
| Priority: | major | Milestone: | 1.3.2 |
| Component: | core | Version: | 1.3.1 |
| Keywords: | clone iframe | Cc: | |
| Blocking: | Blocked by: |
Description
If you clone an element from a foreign document (not the one you're executing code in), the cloned node will not have the same ownerDocument as the original element. It will instead be owned by the document where the execution happens.
elem.ownerDocument === document > false var clone = $(elem).clone(); clone.ownerDocument === elem.ownerDocument > false (should be true) clone.ownerDocument === document > true (should be false)
There is a simple fix to this - the code in clone() shouldn't use document.createElement but this.ownerDocument and the call to clear should use this for context (currently uses nothing).
Change History
comment:2 Changed 4 years ago by john
- Status changed from new to closed
- Resolution set to fixed
This was fixed when the recent IE clone refactoring landed.
comment:3 Changed 4 years ago by doug
- Status changed from closed to reopened
- Resolution fixed deleted
Using this.ownerDocument for createElement is a partial fix. The call to clean() also needs the context.
That is,
return jQuery.clean([html.replace(/ jQuery\d+="(?:\d+|null)"/g, "").replace(/\s*/, "")], this.ownerDocument)[0];
I've seen the problem when using .wrap("<div></div>").
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
