-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject element polyfills where used using Babel
- Loading branch information
Showing
5 changed files
with
100 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,2 @@ | ||
// Caution before editing - For latest builds, this module is replaced with emptiness and thus not imported (see build-scripts/bundle.js) | ||
import "lit/polyfill-support"; | ||
|
||
// Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md | ||
(function (arr) { | ||
arr.forEach((item) => { | ||
if (Object.prototype.hasOwnProperty.call(item, "append")) { | ||
return; | ||
} | ||
Object.defineProperty(item, "append", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function append(...argArr) { | ||
const docFrag = document.createDocumentFragment(); | ||
|
||
argArr.forEach((argItem) => { | ||
const isNode = argItem instanceof Node; | ||
docFrag.appendChild( | ||
isNode ? argItem : document.createTextNode(String(argItem)) | ||
); | ||
}); | ||
|
||
this.appendChild(docFrag); | ||
}, | ||
}); | ||
}); | ||
})([Element.prototype, Document.prototype, DocumentFragment.prototype]); | ||
|
||
// Source: https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttributeNames | ||
if (Element.prototype.getAttributeNames === undefined) { | ||
Element.prototype.getAttributeNames = function () { | ||
const attributes = this.attributes; | ||
const length = attributes.length; | ||
const result = new Array(length); | ||
for (let i = 0; i < length; i++) { | ||
result[i] = attributes[i].name; | ||
} | ||
return result; | ||
}; | ||
} | ||
|
||
// Source: https://gist.github.com/rebelchris/365f26f95d7e9f432f64f21886d9b9ef | ||
if (!Element.prototype.toggleAttribute) { | ||
Element.prototype.toggleAttribute = function (name, force) { | ||
if (force !== undefined) { | ||
force = !!force; | ||
} | ||
|
||
if (this.hasAttribute(name)) { | ||
if (force) { | ||
return true; | ||
} | ||
|
||
this.removeAttribute(name); | ||
return false; | ||
} | ||
if (force === false) { | ||
return false; | ||
} | ||
|
||
this.setAttribute(name, ""); | ||
return true; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md | ||
(function (arr) { | ||
arr.forEach((item) => { | ||
if (Object.prototype.hasOwnProperty.call(item, "append")) { | ||
return; | ||
} | ||
Object.defineProperty(item, "append", { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function append(...argArr) { | ||
const docFrag = document.createDocumentFragment(); | ||
|
||
argArr.forEach((argItem) => { | ||
const isNode = argItem instanceof Node; | ||
docFrag.appendChild( | ||
isNode ? argItem : document.createTextNode(String(argItem)) | ||
); | ||
}); | ||
|
||
this.appendChild(docFrag); | ||
}, | ||
}); | ||
}); | ||
})([Element.prototype, Document.prototype, DocumentFragment.prototype]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Source: https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttributeNames | ||
if (Element.prototype.getAttributeNames === undefined) { | ||
Element.prototype.getAttributeNames = function () { | ||
const attributes = this.attributes; | ||
const length = attributes.length; | ||
const result = new Array(length); | ||
for (let i = 0; i < length; i++) { | ||
result[i] = attributes[i].name; | ||
} | ||
return result; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Source: https://gist.github.com/rebelchris/365f26f95d7e9f432f64f21886d9b9ef | ||
if (!Element.prototype.toggleAttribute) { | ||
Element.prototype.toggleAttribute = function (name, force) { | ||
if (force !== undefined) { | ||
force = !!force; | ||
} | ||
|
||
if (this.hasAttribute(name)) { | ||
if (force) { | ||
return true; | ||
} | ||
|
||
this.removeAttribute(name); | ||
return false; | ||
} | ||
if (force === false) { | ||
return false; | ||
} | ||
|
||
this.setAttribute(name, ""); | ||
return true; | ||
}; | ||
} |