From 54b80cc5273adc0016163e2011cbc4b0f56826fa Mon Sep 17 00:00:00 2001 From: Nick Colley Date: Mon, 24 Feb 2020 16:31:18 +0000 Subject: [PATCH 1/4] Add Element.prototype.nextElementSibling polyfill --- .../prototype/nextElementSibling/config.toml | 25 +++++++++++ .../prototype/nextElementSibling/detect.js | 1 + .../prototype/nextElementSibling/polyfill.js | 7 +++ .../prototype/nextElementSibling/tests.js | 45 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 polyfills/Element/prototype/nextElementSibling/config.toml create mode 100644 polyfills/Element/prototype/nextElementSibling/detect.js create mode 100644 polyfills/Element/prototype/nextElementSibling/polyfill.js create mode 100644 polyfills/Element/prototype/nextElementSibling/tests.js diff --git a/polyfills/Element/prototype/nextElementSibling/config.toml b/polyfills/Element/prototype/nextElementSibling/config.toml new file mode 100644 index 000000000..c4757d391 --- /dev/null +++ b/polyfills/Element/prototype/nextElementSibling/config.toml @@ -0,0 +1,25 @@ +aliases = [ ] +dependencies = [ + "Element", + "Object.defineProperty" +] +license = "MIT" +docs = "https://developer.mozilla.org/en-US/docs/Web/API/NonDocumentTypeChildNode/nextElementSibling" + +[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" diff --git a/polyfills/Element/prototype/nextElementSibling/detect.js b/polyfills/Element/prototype/nextElementSibling/detect.js new file mode 100644 index 000000000..4e7263ef7 --- /dev/null +++ b/polyfills/Element/prototype/nextElementSibling/detect.js @@ -0,0 +1 @@ +'document' in self && "nextElementSibling" in document.documentElement diff --git a/polyfills/Element/prototype/nextElementSibling/polyfill.js b/polyfills/Element/prototype/nextElementSibling/polyfill.js new file mode 100644 index 000000000..0bcb6397e --- /dev/null +++ b/polyfills/Element/prototype/nextElementSibling/polyfill.js @@ -0,0 +1,7 @@ +Object.defineProperty(Element.prototype, "nextElementSibling", { + get: function(){ + var el = this.nextSibling; + while (el && el.nodeType !== 1) { el = el.nextSibling; } + return el; + } +}); diff --git a/polyfills/Element/prototype/nextElementSibling/tests.js b/polyfills/Element/prototype/nextElementSibling/tests.js new file mode 100644 index 000000000..cb8eff7d3 --- /dev/null +++ b/polyfills/Element/prototype/nextElementSibling/tests.js @@ -0,0 +1,45 @@ +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); +}); From ddda1875f5e0b2debb6f3eefbbd4729b3af53a0f Mon Sep 17 00:00:00 2001 From: Nick Colley Date: Mon, 24 Feb 2020 16:31:42 +0000 Subject: [PATCH 2/4] Add Element.prototype.previousElementSibling polyfill --- .../previousElementSibling/config.toml | 25 +++++++++++ .../previousElementSibling/detect.js | 1 + .../previousElementSibling/polyfill.js | 7 +++ .../prototype/previousElementSibling/tests.js | 45 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 polyfills/Element/prototype/previousElementSibling/config.toml create mode 100644 polyfills/Element/prototype/previousElementSibling/detect.js create mode 100644 polyfills/Element/prototype/previousElementSibling/polyfill.js create mode 100644 polyfills/Element/prototype/previousElementSibling/tests.js diff --git a/polyfills/Element/prototype/previousElementSibling/config.toml b/polyfills/Element/prototype/previousElementSibling/config.toml new file mode 100644 index 000000000..0094cd67c --- /dev/null +++ b/polyfills/Element/prototype/previousElementSibling/config.toml @@ -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" diff --git a/polyfills/Element/prototype/previousElementSibling/detect.js b/polyfills/Element/prototype/previousElementSibling/detect.js new file mode 100644 index 000000000..a0cc375c4 --- /dev/null +++ b/polyfills/Element/prototype/previousElementSibling/detect.js @@ -0,0 +1 @@ +'document' in self && "previousElementSibling" in document.documentElement diff --git a/polyfills/Element/prototype/previousElementSibling/polyfill.js b/polyfills/Element/prototype/previousElementSibling/polyfill.js new file mode 100644 index 000000000..efd20a14f --- /dev/null +++ b/polyfills/Element/prototype/previousElementSibling/polyfill.js @@ -0,0 +1,7 @@ +Object.defineProperty(Element.prototype, 'previousElementSibling', { + get: function(){ + var el = this.previousSibling; + while (el && el.nodeType !== 1) { el = el.previousSibling; } + return el; + } +}); diff --git a/polyfills/Element/prototype/previousElementSibling/tests.js b/polyfills/Element/prototype/previousElementSibling/tests.js new file mode 100644 index 000000000..cb8eff7d3 --- /dev/null +++ b/polyfills/Element/prototype/previousElementSibling/tests.js @@ -0,0 +1,45 @@ +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); +}); From bd901522210374a132b3733046da980c1056e53d Mon Sep 17 00:00:00 2001 From: Jake Champion Date: Mon, 24 Feb 2020 19:59:33 +0000 Subject: [PATCH 3/4] fix linting issues --- polyfills/Element/prototype/nextElementSibling/tests.js | 7 +++++-- .../Element/prototype/previousElementSibling/tests.js | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/polyfills/Element/prototype/nextElementSibling/tests.js b/polyfills/Element/prototype/nextElementSibling/tests.js index cb8eff7d3..81521ca09 100644 --- a/polyfills/Element/prototype/nextElementSibling/tests.js +++ b/polyfills/Element/prototype/nextElementSibling/tests.js @@ -1,3 +1,6 @@ +/* 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'); @@ -8,7 +11,7 @@ it("should return null if the node is the only child of its parent node", functi it("should return null if the node only has text sibling", function () { var parent = document.createElement('div'), - p = document.createElement('p'); + p = document.createElement('p'), text = document.createTextNode('Hi there, how are you doing today?'); parent.appendChild(p); parent.appendChild(text); @@ -18,7 +21,7 @@ it("should return null if the node only has text sibling", function () { it("should return null if the node only has comment sibling", function () { var parent = document.createElement('div'), - p = document.createElement('p'); + p = document.createElement('p'), comment = document.createComment('This is a comment in the document.'); parent.appendChild(p); parent.appendChild(comment); diff --git a/polyfills/Element/prototype/previousElementSibling/tests.js b/polyfills/Element/prototype/previousElementSibling/tests.js index cb8eff7d3..81521ca09 100644 --- a/polyfills/Element/prototype/previousElementSibling/tests.js +++ b/polyfills/Element/prototype/previousElementSibling/tests.js @@ -1,3 +1,6 @@ +/* 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'); @@ -8,7 +11,7 @@ it("should return null if the node is the only child of its parent node", functi it("should return null if the node only has text sibling", function () { var parent = document.createElement('div'), - p = document.createElement('p'); + p = document.createElement('p'), text = document.createTextNode('Hi there, how are you doing today?'); parent.appendChild(p); parent.appendChild(text); @@ -18,7 +21,7 @@ it("should return null if the node only has text sibling", function () { it("should return null if the node only has comment sibling", function () { var parent = document.createElement('div'), - p = document.createElement('p'); + p = document.createElement('p'), comment = document.createComment('This is a comment in the document.'); parent.appendChild(p); parent.appendChild(comment); From fbc2e56f7023f94dfea75ce116b98ad6629a37fe Mon Sep 17 00:00:00 2001 From: Jake Champion Date: Tue, 25 Feb 2020 00:48:46 +0000 Subject: [PATCH 4/4] use the original previousElementSibling tests --- .../prototype/previousElementSibling/tests.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/polyfills/Element/prototype/previousElementSibling/tests.js b/polyfills/Element/prototype/previousElementSibling/tests.js index 81521ca09..24fee6c85 100644 --- a/polyfills/Element/prototype/previousElementSibling/tests.js +++ b/polyfills/Element/prototype/previousElementSibling/tests.js @@ -6,27 +6,27 @@ it("should return null if the node is the only child of its parent node", functi p = document.createElement('p'); parent.appendChild(p); - proclaim.strictEqual(p.nextElementSibling, null); + proclaim.strictEqual(p.previousElementSibling, 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); + parent.appendChild(p); - proclaim.strictEqual(p.nextElementSibling, null); + proclaim.strictEqual(p.previousElementSibling, 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); + parent.appendChild(p); - proclaim.strictEqual(p.nextElementSibling, null); + proclaim.strictEqual(p.previousElementSibling, null); }); it("should return the first child element", function () { @@ -38,11 +38,11 @@ it("should return the first child element", function () { 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(text); + parent.appendChild(comment); parent.appendChild(p3); - proclaim.strictEqual(h2.nextElementSibling, p1); + proclaim.strictEqual(p3.previousElementSibling, p2); });