-
Notifications
You must be signed in to change notification settings - Fork 0
/
shim.MutationObserver.js
1 lines (1 loc) · 3.79 KB
/
shim.MutationObserver.js
1
;window.MutationObserver=window.MutationObserver||function(t){"use strict";function e(t){this._watched=[],this._listener=t}function a(e){var a={type:null,target:null,addedNodes:[],removedNodes:[],previousSibling:null,nextSibling:null,attributeName:null,attributeNamespace:null,oldValue:null};for(var r in e)h(a,r)&&e[r]!==t&&(a[r]=e[r]);return a}e._period=30,e.prototype={observe:function(r,n){for(var o,l,h,f,p={attr:!!(n.attributes||n.attributeFilter||n.attributeOldValue),kids:!!n.childList,descendents:!!n.subtree,charData:!(!n.characterData&&!n.characterDataOldValue)},g=this._watched,v=0;v<g.length;v++)g[v].tar===r&&g.splice(v,1);n.attributeFilter&&(p.afilter=d(n.attributeFilter,function(t,e){return t[e]=!0,t},{})),g.push({tar:r,fn:(o=r,l=p,h=u(o,l),function(e){var r,n=e.length;l.charData&&3===o.nodeType&&o.nodeValue!==h.charData&&e.push(new a({type:"characterData",target:o,oldValue:h.charData})),l.attr&&h.attr&&i(e,o,h.attr,l.afilter),(l.kids||l.descendents)&&(r=function(e,r,n,u){var o;function l(t,r,n,o,l){for(var s,c,h,f=t.length-1,p=-~((f-l)/2);h=t.pop();)s=n[h.i],c=o[h.j],u.kids&&p&&Math.abs(h.i-h.j)>=f&&(e.push(a({type:"childList",target:r,addedNodes:[s],removedNodes:[s],nextSibling:s.nextSibling,previousSibling:s.previousSibling})),p--),u.attr&&c.attr&&i(e,s,c.attr,u.afilter),u.charData&&3===s.nodeType&&s.nodeValue!==c.charData&&e.push(a({type:"characterData",target:s,oldValue:c.charData})),u.descendents&&d(s,c)}function d(r,n){for(var h,f,p,g,v,b,m,y=r.childNodes,D=n.kids,N=y.length,V=D?D.length:0,_=0,k=0,S=0;k<N||S<V;)b=y[k],v=D[S],m=v&&v.node,b===m?(u.attr&&v.attr&&i(e,b,v.attr,u.afilter),u.charData&&v.charData!==t&&b.nodeValue!==v.charData&&e.push(a({type:"characterData",target:b,oldValue:v.charData})),f&&l(f,r,y,D,_),u.descendents&&(b.childNodes.length||v.kids&&v.kids.length)&&d(b,v),k++,S++):(o=!0,h||(h={},f=[]),b&&(h[p=s(b)]||(h[p]=!0,-1===(g=c(D,b,S,"node"))?u.kids&&(e.push(a({type:"childList",target:r,addedNodes:[b],nextSibling:b.nextSibling,previousSibling:b.previousSibling})),_++):f.push({i:k,j:g})),k++),m&&m!==y[k]&&(h[p=s(m)]||(h[p]=!0,-1===(g=c(y,m,k))?u.kids&&(e.push(a({type:"childList",target:n.node,removedNodes:[m],nextSibling:D[S+1],previousSibling:D[S-1]})),_--):f.push({i:g,j:S})),S++));f&&l(f,r,y,D,_)}return d(r,n),o}(e,o,h,l)),(r||e.length!==n)&&(h=u(o,l))})}),this._timeout||(f=this,function t(){var a=f.takeRecords();a.length&&f._listener(a,f),f._timeout=setTimeout(t,e._period)}())},takeRecords:function(){for(var t=[],e=this._watched,a=0;a<e.length;a++)e[a].fn(t);return t},disconnect:function(){this._watched=[],clearTimeout(this._timeout),this._timeout=null}};var r=document.createElement("i");r.style.top=0;var n=(r="null"!=r.attributes.style.value)?function(t,e){return e.value}:function(t,e){return"style"!==e.name?e.value:t.style.cssText};function i(t,e,r,i){for(var u,o,l={},s=e.attributes,d=s.length;d--;)o=(u=s[d]).name,i&&!h(i,o)||(n(e,u)!==r[o]&&t.push(a({type:"attributes",target:e,attributeName:o,oldValue:r[o],attributeNamespace:u.namespaceURI})),l[o]=!0);for(o in r)l[o]||t.push(a({target:e,type:"attributes",attributeName:o,oldValue:r[o]}))}function u(t,e){var a=!0;return function t(r){var i={node:r};return!e.charData||3!==r.nodeType&&8!==r.nodeType?(e.attr&&a&&1===r.nodeType&&(i.attr=d(r.attributes,function(t,a){return e.afilter&&!e.afilter[a.name]||(t[a.name]=n(r,a)),t},{})),a&&(e.kids||e.charData||e.attr&&e.descendents)&&(i.kids=function(t,e){for(var a=[],r=0;r<t.length;r++)a[r]=e(t[r],r,t);return a}(r.childNodes,t)),a=e.descendents):i.charData=r.nodeValue,i}(t)}var o=1,l="mo_id";function s(t){try{return t.id||(t[l]=t[l]||o++)}catch(e){try{return t.nodeValue}catch(t){return o++}}}function d(t,e,a){for(var r=0;r<t.length;r++)a=e(a,t[r],r,t);return a}function c(t,e,a,r){for(;a<t.length;a++)if((r?t[a][r]:t[a])===e)return a;return-1}function h(e,a){return e[a]!==t}return e}(void 0);