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

Commit 77ed85b

Browse files
committed
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 Closes #15475
1 parent 0ddbcaf commit 77ed85b

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/jqLite.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -968,12 +968,15 @@ forEach({
968968

969969
after: function(element, newElement) {
970970
var index = element, parent = element.parentNode;
971-
newElement = new JQLite(newElement);
972971

973-
for (var i = 0, ii = newElement.length; i < ii; i++) {
974-
var node = newElement[i];
975-
parent.insertBefore(node, index.nextSibling);
976-
index = node;
972+
if (parent) {
973+
newElement = new JQLite(newElement);
974+
975+
for (var i = 0, ii = newElement.length; i < ii; i++) {
976+
var node = newElement[i];
977+
parent.insertBefore(node, index.nextSibling);
978+
index = node;
979+
}
977980
}
978981
},
979982

test/jqLiteSpec.js

+8
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,14 @@ describe('jqLite', function() {
19471947
span.after('abc');
19481948
expect(root.html().toLowerCase()).toEqual('<span></span>abc');
19491949
});
1950+
1951+
1952+
it('should not throw when the element has no parent', function() {
1953+
var span = jqLite('<span></span>');
1954+
expect(function() { span.after('abc'); }).not.toThrow();
1955+
expect(span.length).toBe(1);
1956+
expect(span[0].outerHTML).toBe('<span></span>');
1957+
});
19501958
});
19511959

19521960

0 commit comments

Comments
 (0)