Skip to content

Commit

Permalink
Add two polyfills for nextElementSibling and previousElementSibling
Browse files Browse the repository at this point in the history
I've added two new files polyfill files although they didn't come directly
from polyfill.io they were based off a PR that was merged in the library
but not included in the new polyfill-library repo.
I've added comments pointing to the original pull request for the
detection and for the polyfill.
  • Loading branch information
aliuk2012 committed May 17, 2019
1 parent d6f0f66 commit b5cf231
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/components/tabs/tabs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import '../../vendor/polyfills/Function/prototype/bind'
import '../../vendor/polyfills/Element/prototype/classList'
import '../../vendor/polyfills/Element/prototype/nextElementSibling'
import '../../vendor/polyfills/Element/prototype/previousElementSibling'
import '../../vendor/polyfills/Event' // addEventListener and event.target normaliziation
import { nodeListForEach } from '../../common'

Expand Down Expand Up @@ -205,7 +207,7 @@ Tabs.prototype.onTabKeydown = function (e) {

Tabs.prototype.activateNextTab = function () {
var currentTab = this.getCurrentTab()
var nextTabListItem = currentTab.parentNode.nextSibling
var nextTabListItem = currentTab.parentNode.nextElementSibling
if (nextTabListItem) {
var nextTab = nextTabListItem.querySelector('.govuk-tabs__tab')
}
Expand All @@ -219,7 +221,7 @@ Tabs.prototype.activateNextTab = function () {

Tabs.prototype.activatePreviousTab = function () {
var currentTab = this.getCurrentTab()
var previousTabListItem = currentTab.parentNode.previousSibling
var previousTabListItem = currentTab.parentNode.previousElementSibling
if (previousTabListItem) {
var previousTab = previousTabListItem.querySelector('.govuk-tabs__tab')
}
Expand Down
44 changes: 44 additions & 0 deletions src/vendor/polyfills/Element/prototype/nextElementSibling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import '../../Object/defineProperty'
import '../../Element'

(function(undefined) {

// Detection from https://github.com/Financial-Times/polyfill-service/pull/1062/files#diff-b09a5d2acf3314b46a6c8f8d0c31b85c
var detect = (
'Element' in this && "nextElementSibling" in document.documentElement
)

if (detect) return


(function (global) {
var dpSupport = true;
var defineGetter = function (object, name, fn, configurable) {
if (Object.defineProperty)
Object.defineProperty(object, name, {
configurable: false === dpSupport ? true : !!configurable,
get: fn
});

else object.__defineGetter__(name, fn);
};
/** Ensure the browser allows Object.defineProperty to be used on native JavaScript objects. */
try {
defineGetter({}, "support");
}
catch (e) {
dpSupport = false;
}

// Polyfill from https://github.com/Financial-Times/polyfill-service/pull/1062/files#diff-404b69b4750d18dea4174930a49170fd
Object.defineProperty(Element.prototype, "nextElementSibling", {
get: function(){
var el = this.nextSibling;
while (el && el.nodeType !== 1) { el = el.nextSibling; }
return (el.nodeType === 1) ? el : null;
}
});

}(this));

}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});
43 changes: 43 additions & 0 deletions src/vendor/polyfills/Element/prototype/previousElementSibling.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import '../../Object/defineProperty'
import '../../Element'

(function(undefined) {

// Detection from https://github.com/Financial-Times/polyfill-service/pull/1062/files#diff-a162235fbc9c0dd40d4032265f44942e
var detect = (
'Element' in this && 'previousElementSibling' in document.documentElement
)

if (detect) return

(function (global) {
var dpSupport = true;
var defineGetter = function (object, name, fn, configurable) {
if (Object.defineProperty)
Object.defineProperty(object, name, {
configurable: false === dpSupport ? true : !!configurable,
get: fn
});

else object.__defineGetter__(name, fn);
};
/** Ensure the browser allows Object.defineProperty to be used on native JavaScript objects. */
try {
defineGetter({}, "support");
}
catch (e) {
dpSupport = false;
}

// Polyfill from https://github.com/Financial-Times/polyfill-service/pull/1062/files#diff-b45a1197b842728cb76b624b6ba7d739
Object.defineProperty(Element.prototype, 'previousElementSibling', {
get: function(){
var el = this.previousSibling;
while (el && el.nodeType !== 1) { el = el.previousSibling; }
return (el.nodeType === 1) ? el : null;
}
});

}(this));

}).call('object' === typeof window && window || 'object' === typeof self && self || 'object' === typeof global && global || {});

0 comments on commit b5cf231

Please sign in to comment.