Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 77d3e75

Browse files
cqrIgorMinar
authored andcommitted
fix(jqLite): clone wrapNode in jqlite/wrap
Change jqLite's implementation of wrap() to clone the wrapNode before wrapping the target element in it. Match jQuery's wrap() behavior and prevent accidentally attaching target element to the DOM as a side effect. Closes #3860 Closes #4194
1 parent 1bdca93 commit 77d3e75

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/jqLite.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ forEach({
842842
},
843843

844844
wrap: function(element, wrapNode) {
845-
wrapNode = jqLite(wrapNode)[0];
845+
wrapNode = jqLite(wrapNode).eq(0).clone()[0];
846846
var parent = element.parentNode;
847847
if (parent) {
848848
parent.replaceChild(wrapNode, element);

test/jqLiteSpec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,6 +1556,14 @@ describe('jqLite', function() {
15561556
text.wrap("<span>");
15571557
expect(text.parent().text()).toEqual('A<a>B</a>C');
15581558
});
1559+
it('should clone elements to be wrapped around target', function () {
1560+
var root = jqLite('<div class="sigil"></div>');
1561+
var span = jqLite('<span>A</span>');
1562+
1563+
span.wrap(root);
1564+
expect(root.children().length).toBe(0);
1565+
expect(span.parent().hasClass('sigil')).toBeTruthy();
1566+
});
15591567
});
15601568

15611569

0 commit comments

Comments
 (0)