From 2191b1a4442f6f295bef85bf1cf57099a20c222d Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Thu, 8 Dec 2016 11:12:45 +0200 Subject: [PATCH 1/3] fix(jqLite): silently ignore `after()` if element has no parent Previously, the element was always assumed to have a parent and an error was thrown when that was not the case. This commit makes jqLite consistent with jQuery, which silently ignores a call on elements that do not have a parent. Fixes #15331 Closes #15367 --- src/jqLite.js | 13 ++++++++----- test/jqLiteSpec.js | 6 ++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/jqLite.js b/src/jqLite.js index e882407be63f..1f61c80999d0 100644 --- a/src/jqLite.js +++ b/src/jqLite.js @@ -979,12 +979,15 @@ forEach({ after: function(element, newElement) { var index = element, parent = element.parentNode; - newElement = new JQLite(newElement); - for (var i = 0, ii = newElement.length; i < ii; i++) { - var node = newElement[i]; - parent.insertBefore(node, index.nextSibling); - index = node; + if (parent) { + newElement = new JQLite(newElement); + + for (var i = 0, ii = newElement.length; i < ii; i++) { + var node = newElement[i]; + parent.insertBefore(node, index.nextSibling); + index = node; + } } }, diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 886ca7c2cd42..89c77e2cd764 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -2182,6 +2182,12 @@ describe('jqLite', function() { span.after('abc'); expect(root.html().toLowerCase()).toEqual('abc'); }); + + + it('should not throw when the element has no parent', function() { + var span = jqLite(''); + expect(function() { span.after('abc'); }).not.toThrow(); + }); }); From 2f257dc911f7c8202229add6eca3bd34193968e7 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Thu, 8 Dec 2016 13:35:00 +0200 Subject: [PATCH 2/3] fixup! fix(jqLite): silently ignore `after()` if element has no parent --- test/jqLiteSpec.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index 89c77e2cd764..e88fcb606995 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -2187,6 +2187,7 @@ describe('jqLite', function() { it('should not throw when the element has no parent', function() { var span = jqLite(''); expect(function() { span.after('abc'); }).not.toThrow(); + expect(span).toJqEqual(jqLite('')); }); }); From c904a0595fdb33ed29a8a3353e424d868a8b6f41 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Thu, 8 Dec 2016 15:07:39 +0200 Subject: [PATCH 3/3] fixup! fix(jqLite): silently ignore `after()` if element has no parent --- test/jqLiteSpec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/jqLiteSpec.js b/test/jqLiteSpec.js index e88fcb606995..5333b029a142 100644 --- a/test/jqLiteSpec.js +++ b/test/jqLiteSpec.js @@ -2187,7 +2187,8 @@ describe('jqLite', function() { it('should not throw when the element has no parent', function() { var span = jqLite(''); expect(function() { span.after('abc'); }).not.toThrow(); - expect(span).toJqEqual(jqLite('')); + expect(span.length).toBe(1); + expect(span[0].outerHTML).toBe(''); }); });