Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nextElementSibling and previousElementSibling polyfills #484

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions polyfills/Element/prototype/nextElementSibling/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
aliases = [ ]
JakeChampion marked this conversation as resolved.
Show resolved Hide resolved
dependencies = [
"Element",
"Object.defineProperty"
]
license = "MIT"
docs = "https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/nextElementSibling"

[browsers]
JakeChampion marked this conversation as resolved.
Show resolved Hide resolved
# android = "all versions natively support this feature"
# bb = "Not on MDN"
chrome = "<4"
edge = "<12"
# edge_mob = "Not on MDN"
firefox = "<3.5"
# ios_chr = "Not on MDN"
# ios_saf = "all versions natively support this feature"
ie = "<9"
# ie_mob = "Not on MDN"
opera = "<10"
# op_mini = "Not on MDN"
op_mob = "<10.1"
safari = "<4"
firefox_mob = "<4"
# samsung_mob = "all versions natively support this feature"
1 change: 1 addition & 0 deletions polyfills/Element/prototype/nextElementSibling/detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'document' in self && "nextElementSibling" in document.documentElement
JakeChampion marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions polyfills/Element/prototype/nextElementSibling/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Object.defineProperty(Element.prototype, "nextElementSibling", {
JakeChampion marked this conversation as resolved.
Show resolved Hide resolved
get: function(){
var el = this.nextSibling;
while (el && el.nodeType !== 1) { el = el.nextSibling; }
return el;
JakeChampion marked this conversation as resolved.
Show resolved Hide resolved
}
});
48 changes: 48 additions & 0 deletions polyfills/Element/prototype/nextElementSibling/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-env mocha, browser */
/* global proclaim */

it("should return null if the node is the only child of its parent node", function () {
JakeChampion marked this conversation as resolved.
Show resolved Hide resolved
var parent = document.createElement('div'),
p = document.createElement('p');
parent.appendChild(p);

proclaim.strictEqual(p.nextElementSibling, null);
});

it("should return null if the node only has text sibling", function () {
var parent = document.createElement('div'),
p = document.createElement('p'),
text = document.createTextNode('Hi there, how are you doing today?');
parent.appendChild(p);
parent.appendChild(text);

proclaim.strictEqual(p.nextElementSibling, null);
});

it("should return null if the node only has comment sibling", function () {
var parent = document.createElement('div'),
p = document.createElement('p'),
comment = document.createComment('This is a comment in the document.');
parent.appendChild(p);
parent.appendChild(comment);

proclaim.strictEqual(p.nextElementSibling, null);
});

it("should return the first child element", function () {
var parent = document.createElement('div'),
h2 = document.createElement('h2'),
p1 = document.createElement('p'),
p2 = document.createElement('p'),
p3 = document.createElement('p'),
text = document.createTextNode('Hi there, how are you doing today?'),
comment = document.createComment('This is a comment in the document.');
parent.appendChild(h2);
parent.appendChild(text);
parent.appendChild(comment);
parent.appendChild(p1);
parent.appendChild(p2);
parent.appendChild(p3);

proclaim.strictEqual(h2.nextElementSibling, p1);
});
25 changes: 25 additions & 0 deletions polyfills/Element/prototype/previousElementSibling/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
aliases = [ ]
dependencies = [
"Element",
"Object.defineProperty"
]
license = "MIT"
docs = "https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/previousElementSibling"

[browsers]
# android = "all versions natively support this feature"
# bb = "Not on MDN"
chrome = "<4"
edge = "<12"
# edge_mob = "Not on MDN"
firefox = "<3.5"
# ios_chr = "Not on MDN"
# ios_saf = "all versions natively support this feature"
ie = "<9"
# ie_mob = "Not on MDN"
opera = "<10"
# op_mini = "Not on MDN"
op_mob = "<10.1"
safari = "<4"
firefox_mob = "<4"
# samsung_mob = "all versions natively support this feature"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
'document' in self && "previousElementSibling" in document.documentElement
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Object.defineProperty(Element.prototype, 'previousElementSibling', {
JakeChampion marked this conversation as resolved.
Show resolved Hide resolved
get: function(){
var el = this.previousSibling;
while (el && el.nodeType !== 1) { el = el.previousSibling; }
return el;
}
});
48 changes: 48 additions & 0 deletions polyfills/Element/prototype/previousElementSibling/tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-env mocha, browser */
/* global proclaim */

it("should return null if the node is the only child of its parent node", function () {
var parent = document.createElement('div'),
p = document.createElement('p');
parent.appendChild(p);

proclaim.strictEqual(p.nextElementSibling, null);
});

it("should return null if the node only has text sibling", function () {
var parent = document.createElement('div'),
p = document.createElement('p'),
text = document.createTextNode('Hi there, how are you doing today?');
parent.appendChild(p);
parent.appendChild(text);

proclaim.strictEqual(p.nextElementSibling, null);
});

it("should return null if the node only has comment sibling", function () {
var parent = document.createElement('div'),
p = document.createElement('p'),
comment = document.createComment('This is a comment in the document.');
parent.appendChild(p);
parent.appendChild(comment);

proclaim.strictEqual(p.nextElementSibling, null);
});

it("should return the first child element", function () {
var parent = document.createElement('div'),
h2 = document.createElement('h2'),
p1 = document.createElement('p'),
p2 = document.createElement('p'),
p3 = document.createElement('p'),
text = document.createTextNode('Hi there, how are you doing today?'),
comment = document.createComment('This is a comment in the document.');
parent.appendChild(h2);
parent.appendChild(text);
parent.appendChild(comment);
parent.appendChild(p1);
parent.appendChild(p2);
parent.appendChild(p3);

proclaim.strictEqual(h2.nextElementSibling, p1);
});