-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add prepend polyfill.
- Loading branch information
Dan Ziv
committed
Sep 12, 2018
1 parent
5cf4bf9
commit ec8dea2
Showing
2 changed files
with
38 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import './performance-now'; | ||
import './prepend'; |
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,37 @@ | ||
// @flow | ||
import PolyfillManager from './polyfill-manager'; | ||
import getLogger from '../utils/logger'; | ||
|
||
export default class PrependPolyfill { | ||
static id: string = 'prepend'; | ||
static _logger: any = getLogger('PrependPolyfill'); | ||
|
||
static install(): void { | ||
[Element.prototype, Document.prototype, DocumentFragment.prototype].forEach(function(item) { | ||
if (item.hasOwnProperty('prepend')) { | ||
PrependPolyfill._logger.debug('No need to install polyfill on item', item); | ||
return; | ||
} | ||
PrependPolyfill._logger.debug('Installing polyfill on item', item); | ||
// $FlowFixMe | ||
Object.defineProperty(item, 'prepend', { | ||
configurable: true, | ||
enumerable: true, | ||
writable: true, | ||
value: function prepend() { | ||
var argArr = Array.prototype.slice.call(arguments), | ||
docFrag = document.createDocumentFragment(); | ||
|
||
argArr.forEach(function(argItem) { | ||
var isNode = argItem instanceof Node; | ||
docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem))); | ||
}); | ||
|
||
this.insertBefore(docFrag, this.firstChild); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
PolyfillManager.register(PrependPolyfill); |