Skip to content
This repository has been archived by the owner on Dec 29, 2017. It is now read-only.

Support Polyfills on Dynamically Inserted DOM Elements #22

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 53 additions & 2 deletions dist/polyfill.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*!
* Polyfill.js - v0.1.0
*
* Copyright (c) 2015 Philip Walton <http://philipwalton.com>
* Copyright (c) 2016 Philip Walton <http://philipwalton.com>
* Released under the MIT license
*
* Date: 2015-06-21
* Date: 2016-09-02
*/
;(function(window, document, undefined){

Expand Down Expand Up @@ -1010,6 +1010,7 @@ function Polyfill(options) {
this._buildMediaQueryMap()
this._reportInitialMatches()
this._addMediaListeners()
this._addMutationObserver()
}


Expand Down Expand Up @@ -1060,6 +1061,9 @@ Polyfill.prototype.getCurrentMatches = function() {
*/
Polyfill.prototype.destroy = function() {
if (this._undoUnmatched) {
if (typeof this.mutationObserver != 'undefined') {
this.mutationObserver.disconnect()
}
this._undoUnmatched(this.getCurrentMatches())
EventManager.removeListeners(this)
}
Expand Down Expand Up @@ -1281,6 +1285,53 @@ Polyfill.prototype._addMediaListeners = function() {
)
}

Polyfill.prototype._addMutationObserver = function() {
this._defer(
function() {
if (typeof MutationObserver === 'undefined') { return false }
return this._filteredRules && this._doMatched
},
function() {
var instance = this
, rule
, selector
, selectors = []
, i = 0
, j = 0

while (rule = this._filteredRules[i++]) {
while (selector = rule.selectors[j++]) {
selectors.push(selector)
}
}

this.mutationObserver = new MutationObserver(function(mutations, observer) {
var addedNode
, mutation
, k = 0
, l = 0
, m = 0
while (mutation = mutations[k++]) {
if (mutation.type != 'childList') { continue }
while (selector = selectors[l++]) {
while (addedNode = mutation.addedNodes[m++]) {
if (addedNode.nodeType != Node.ELEMENT_NODE) { continue }
if (addedNode.querySelector(selector)) {
instance._doMatched(instance.getCurrentMatches())
return
}
}
m = 0
}
l = 0
}
})

this.mutationObserver.observe(document, { childList: true, subtree: true })
}
)
}

Polyfill.modules = {
DownloadManager: DownloadManager,
StyleManager: StyleManager,
Expand Down