From f50cf9032c8f01277d221089b8e88d544dc7f7ec Mon Sep 17 00:00:00 2001 From: chrisrhoden Date: Sun, 29 Sep 2013 16:19:07 -0400 Subject: [PATCH] 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 --- src/jqLite.js | 2 +- test/jqLiteSpec.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/jqLite.js b/src/jqLite.js index 0db4c25103f4..08f67b2260a0 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -882,7 +882,7 @@ forEach({ }, wrap: function(element, wrapNode) { - wrapNode = jqLite(wrapNode)[0]; + wrapNode = jqLite(wrapNode).eq(0).clone()[0]; var parent = element.parentNode; if (parent) { parent.replaceChild(wrapNode, element); diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 38ec858f03e7..c6ae68326e1a 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -1542,6 +1542,15 @@ describe('jqLite', function() { text.wrap(""); expect(text.parent().text()).toEqual('ABC'); }); + it('should clone elements to be wrapped around target', function () { + var root = jqLite('
'); + var span = jqLite('A'); + jqLite(document.body).append(root); + + span.wrap(root); + expect(root.text()).toBe(''); + expect(span.parent().hasClass('sigil')).toBeTruthy(); + }); });