seamless-scroll-polyfill
Advanced tools
Comparing version 2.1.2 to 2.1.3-beta.1
@@ -0,1 +1,8 @@ | ||
## [2.1.3-beta.1](https://github.com/magic-akari/seamless-scroll-polyfill/compare/v2.1.2...v2.1.3-beta.1) (2021-09-20) | ||
### Bug Fixes | ||
* make sure Element#scroll === HTMLElement#scroll === SVGElement#scroll ([d7fd874](https://github.com/magic-akari/seamless-scroll-polyfill/commit/d7fd8742dd94ab41caec79575b459bdd6ed9e257)) | ||
## [2.1.2](https://github.com/magic-akari/seamless-scroll-polyfill/compare/v2.1.1...v2.1.2) (2021-09-13) | ||
@@ -2,0 +9,0 @@ |
@@ -65,3 +65,3 @@ (function (global, factory) { | ||
/* eslint-disable */ | ||
var getOriginalMethod = function (proto, method, fallback) { | ||
var backupMethod = function (proto, method, fallback) { | ||
var _a; | ||
@@ -84,5 +84,7 @@ var backup = "__seamless__$$" + method + "$$__backup__"; | ||
}; | ||
var modifyPrototypes = function (modification) { | ||
var modifyPrototypes = function (prop, func) { | ||
markPolyfill(func); | ||
[HTMLElement.prototype, SVGElement.prototype, Element.prototype].forEach(function (prototype) { | ||
modification(prototype); | ||
backupMethod(prototype, prop, HTMLElement.prototype[prop]); | ||
prototype[prop] = func; | ||
}); | ||
@@ -147,3 +149,5 @@ }; | ||
} | ||
var method = elementScrollXY.bind(element); | ||
var method = backupMethod(Object.getPrototypeOf(element), "scroll", | ||
// eslint-disable-next-line | ||
HTMLElement.prototype.scroll || elementScrollXY).bind(element); | ||
if (options.behavior !== "smooth") { | ||
@@ -610,14 +614,11 @@ method(targetX, targetY); | ||
}[scrollName]; | ||
patch(function (prototype) { | ||
prototype[scrollName] = function () { | ||
var args = arguments; | ||
if (arguments.length === 1) { | ||
scrollMethod(this, args[0], config); | ||
return; | ||
} | ||
var left = args[0]; | ||
var top = args[1]; | ||
scrollMethod(this, { left: left, top: top }); | ||
}; | ||
markPolyfill(prototype[scrollName]); | ||
patch(scrollName, function () { | ||
var args = arguments; | ||
if (arguments.length === 1) { | ||
scrollMethod(this, args[0], config); | ||
return; | ||
} | ||
var left = args[0]; | ||
var top = args[1]; | ||
scrollMethod(this, { left: left, top: top }); | ||
}); | ||
@@ -629,4 +630,6 @@ }; | ||
var elementScrollByPolyfill = createPolyfill("scrollBy", modifyPrototypes); | ||
var modifyWindow = function (modification) { | ||
modification(window); | ||
var modifyWindow = function (prop, func) { | ||
markPolyfill(func); | ||
backupMethod(window, prop); | ||
window[prop] = func; | ||
}; | ||
@@ -637,2 +640,8 @@ var windowScrollPolyfill = createPolyfill("scroll", modifyWindow); | ||
function elementScrollIntoViewBoolean(alignToTop) { | ||
elementScrollIntoView(this, { | ||
block: (alignToTop !== null && alignToTop !== void 0 ? alignToTop : true) ? "start" : "end", | ||
inline: "nearest", | ||
}); | ||
} | ||
var elementScrollIntoViewPolyfill = function (config) { | ||
@@ -642,15 +651,11 @@ if (isScrollBehaviorSupported()) { | ||
} | ||
var originalFunc = getOriginalMethod(window.HTMLElement.prototype, "scrollIntoView"); | ||
modifyPrototypes(function (prototype) { | ||
prototype.scrollIntoView = function scrollIntoView() { | ||
var args = arguments; | ||
var options = args[0]; | ||
if (args.length === 1 && isObject(options)) { | ||
elementScrollIntoView(this, options, config); | ||
return; | ||
} | ||
originalFunc.apply(this, args); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
markPolyfill(prototype.scrollIntoView); | ||
var originalFunc = backupMethod(window.HTMLElement.prototype, "scrollIntoView", elementScrollIntoViewBoolean); | ||
modifyPrototypes("scrollIntoView", function scrollIntoView() { | ||
var args = arguments; | ||
var options = args[0]; | ||
if (args.length === 1 && isObject(options)) { | ||
elementScrollIntoView(this, options, config); | ||
return; | ||
} | ||
originalFunc.apply(this, args); | ||
}); | ||
@@ -680,2 +685,3 @@ }; | ||
exports.elementScrollToPolyfill = elementScrollToPolyfill; | ||
exports.modifyWindow = modifyWindow; | ||
exports.polyfill = polyfill; | ||
@@ -682,0 +688,0 @@ exports.scroll = scroll; |
@@ -15,3 +15,3 @@ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).seamless={})}(this,(function(e){"use strict"; | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */var t=function(){return(t=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,arguments)};function n(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var o,r,l=n.call(e),i=[];try{for(;(void 0===t||t-- >0)&&!(o=l.next()).done;)i.push(o.value)}catch(e){r={error:e}}finally{try{o&&!o.done&&(n=l.return)&&n.call(l)}finally{if(r)throw r.error}}return i}var o=function(e){return void 0===e||"auto"===e||"instant"===e||"smooth"===e};function r(e,t){this.scrollLeft=e,this.scrollTop=t}var l=function(e,t,n){return void 0===n&&(n="cannot convert to dictionary."),"Failed to execute '"+e+"' on '"+t+"': "+n},i=function(e,t,n){return l(e,t,"The provided value '"+n+"' is not a valid enum value of type ScrollBehavior.")},c=function(e){var t=typeof e;return null!==e&&("object"===t||"function"===t)},u=function(){return"scrollBehavior"in window.document.documentElement.style},a=function(e){Object.defineProperty(e,"__isPolyfill",{value:!0})},f=function(e){[HTMLElement.prototype,SVGElement.prototype,Element.prototype].forEach((function(t){e(t)}))},s=function(e){return e.ownerDocument.scrollingElement||e.ownerDocument.documentElement},d=function(e){return.5*(1-Math.cos(Math.PI*e))},v=function(){var e,t,n;return null!==(n=null===(t=null===(e=window.performance)||void 0===e?void 0:e.now)||void 0===t?void 0:t.call(e))&&void 0!==n?n:window.Date.now()},w=function(e){var t=(v()-e.timeStamp)/(e.duration||500);if(t>1)return e.method(e.targetX,e.targetY),void e.callback();var n=(e.timingFunc||d)(t),o=e.startX+(e.targetX-e.startX)*n,r=e.startY+(e.targetY-e.startY)*n;e.method(o,r),e.rafId=window.requestAnimationFrame((function(){w(e)}))},m=function(e){return isFinite(e)?Number(e):0},h=function(e){return function(u,a,f){var d,h=n((d=u).window===d?[s(u.document.documentElement),"Window"]:[u,"Element"],2),p=h[0],y=h[1],g=null!=a?a:{};if(!c(g))throw new TypeError(l(e,y));if(!o(g.behavior))throw new TypeError(i(e,y,g.behavior));"scrollBy"===e&&(g.left=m(g.left)+p.scrollLeft,g.top=m(g.top)+p.scrollTop),function(e,n,o){var l,i;if(function(e){var t;return null!==(t=e.isConnected)&&void 0!==t?t:!(e.ownerDocument&&1&e.ownerDocument.compareDocumentPosition(e))}(e)){var c=e.scrollLeft,u=e.scrollTop,a=m(null!==(l=n.left)&&void 0!==l?l:c),f=m(null!==(i=n.top)&&void 0!==i?i:u);if(a!==c||f!==u){var s=r.bind(e);if("smooth"===n.behavior){var d=function(){window.removeEventListener("wheel",p),window.removeEventListener("touchmove",p)},h=t(t({},o),{timeStamp:v(),startX:c,startY:u,targetX:a,targetY:f,rafId:0,method:s,callback:d}),p=function(){window.cancelAnimationFrame(h.rafId),d()};window.addEventListener("wheel",p,{passive:!0,once:!0}),window.addEventListener("touchmove",p,{passive:!0,once:!0}),w(h)}else s(a,f)}}}(p,g,f)}},p=h("scroll"),y=h("scrollTo"),g=h("scrollBy"),b=p,T=y,E=g,S=p,P=y,V=g,B=function(e){switch(e){case"horizontal-tb":case"lr":case"lr-tb":case"rl":case"rl-tb":return 0;case"vertical-rl":case"tb":case"tb-rl":return 1;case"vertical-lr":case"tb-lr":return 2;case"sideways-rl":return 3;case"sideways-lr":return 4}return 0},L=function(e,t,o,r){var l,i=0;switch(t||(i^=2),e){case 0:i=i>>1|(1&i)<<1,o=(l=n([r,o],2))[0],r=l[1];break;case 1:case 3:i^=1;break;case 4:i^=2}return[i,o,r]},_=function(e){return 1==(1&L(B(e.writingMode),"rtl"!==e.direction,void 0,void 0)[0])},D=function(e,t,n,o,r,l,i){return 0!==e?e:r<t&&l>n||r>t&&l<n?null:r<=t&&i<=o||l>=n&&i>=o?2:l>n&&i<o||r<t&&i>o?3:null},I=function(e){return"visible"!==e&&"clip"!==e},M=function(e,t){return(e.clientHeight<e.scrollHeight||e.clientWidth<e.scrollWidth)&&(I(t.overflowY)||I(t.overflowX)||e===s(e))},W=function(e){var t=e.parentNode,n=e.parentElement;if(null===n&&null!==t){if(11===t.nodeType)return t.host;if(9===t.nodeType)return function(e){var t;try{return(null===(t=e.ownerDocument.defaultView)||void 0===t?void 0:t.frameElement)||null}catch(e){return null}}(e)}return n},k=function(e,t,n){return e<t?t:e>n?n:e},x=function(e,t,n){switch(e){case 1:return(t+n)/2;case 3:return n;case 2:case 0:return t}},H=function(e,t){var o,r,l,i=null===(o=e.ownerDocument.defaultView)||void 0===o?void 0:o.visualViewport,c=n(e===s(e)?[0,0,null!==(r=null==i?void 0:i.width)&&void 0!==r?r:e.clientWidth,null!==(l=null==i?void 0:i.height)&&void 0!==l?l:e.clientHeight]:[t.left,t.top,e.clientWidth,e.clientHeight],4),u=c[0],a=c[1],f=c[2],d=c[3],v=u+e.clientLeft,w=a+e.clientTop;return[w,v+f,w+d,v]},X=function(e,t){var o=[],r=e.ownerDocument,l=r.defaultView;if(!l)return o;for(var i=window.getComputedStyle(e),c="rtl"!==i.direction,u=n(function(e,t,o){var r=n(L(t,o,e.block||"start",e.inline||"nearest"),3),l=r[0];return[r[1],r[2]].map((function(e,t){switch(e){case"center":return 1;case"nearest":return 0;default:return"start"===e==!(l>>t&1)?2:3}}))}(t,B(i.writingMode||i.getPropertyValue("-webkit-writing-mode")||i.getPropertyValue("-ms-writing-mode")),c),2),a=u[0],f=u[1],s=n(function(e,t,n){var o,r=t.top,l=t.right,i=t.bottom,c=t.left,u=(o=e.ownerDocument,["scroll-margin","scroll-snap-margin"].filter((function(e){return e in o.documentElement.style}))[0]);if(!u)return[r,l,i,c];var a=function(e){var t=n.getPropertyValue(u+"-"+e);return parseInt(t,10)||0};return[r-a("top"),l+a("right"),i+a("bottom"),c-a("left")]}(e,e.getBoundingClientRect(),i),4),d=s[0],v=s[1],w=s[2],m=s[3],h=W(e);null!==h;h=W(h)){if(r!==h.ownerDocument){if(!(l=(r=h.ownerDocument).defaultView))break;var p=h.getBoundingClientRect(),y=p.left,g=p.top;d+=g,v+=y,w+=g,m+=y}var b=l.getComputedStyle(h);if("fixed"===b.position)break;if(M(h,b)){var T=h.getBoundingClientRect(),E=n(H(h,T),4),S=E[0],P=E[1],V=E[2],I=E[3],X=x(a,I,P),Y=x(f,S,V),j=D(a,I,P,h.clientWidth,m,v,v-m),C=D(f,S,V,h.clientHeight,d,w,w-d),F=(null===j?X:x(j,m,v))-X,O=(null===C?Y:x(C,d,w))-Y,$=_(b)?k(F,-h.scrollWidth+h.clientWidth-h.scrollLeft,-h.scrollLeft):k(F,-h.scrollLeft,h.scrollWidth-h.clientWidth-h.scrollLeft),R=k(O,-h.scrollTop,h.scrollHeight-h.clientHeight-h.scrollTop);o.push([h,{left:h.scrollLeft+$,top:h.scrollTop+R,behavior:t.behavior}]),d=Math.max(d-R,S),v=Math.min(v-$,P),w=Math.min(w-R,V),m=Math.max(m-$,I)}}return o},Y=function(e,t,r){var l=t||{};if(!o(l.behavior))throw new TypeError(i("scrollIntoView","Element",l.behavior));X(e,l).forEach((function(e){var t=n(e,2),o=t[0],l=t[1];b(o,l,r)}))},j=Y,C=function(e,t){return function(n){if(!u()){var o={scroll:p,scrollTo:y,scrollBy:g}[e];t((function(t){t[e]=function(){var e=arguments;if(1!==arguments.length){var t=e[0],r=e[1];o(this,{left:t,top:r})}else o(this,e[0],n)},a(t[e])}))}}},F=C("scroll",f),O=C("scrollTo",f),$=C("scrollBy",f),R=function(e){e(window)},A=C("scroll",R),N=C("scrollTo",R),q=C("scrollBy",R),z=function(e){if(!u()){var t=function(e,t,n){var o,r="__seamless__$$"+t+"$$__backup__";if(e[r]||(e[r]=e[t]||(e[t]=n)),null===(o=e[r])||void 0===o?void 0:o.__isPolyfill)throw new Error("unexpected_method");return e[r]}(window.HTMLElement.prototype,"scrollIntoView");f((function(n){n.scrollIntoView=function(){var n=arguments,o=n[0];1===n.length&&c(o)?j(this,o,e):t.apply(this,n)},a(n.scrollIntoView)}))}};e.elementScroll=b,e.elementScrollBy=E,e.elementScrollByPolyfill=$,e.elementScrollIntoView=j,e.elementScrollIntoViewPolyfill=z,e.elementScrollPolyfill=F,e.elementScrollTo=T,e.elementScrollToPolyfill=O,e.polyfill=function(e){u()||(F(e),O(e),$(e),z(e),A(e),N(e),q(e))},e.scroll=p,e.scrollBy=g,e.scrollIntoView=Y,e.scrollTo=y,e.windowScroll=S,e.windowScrollBy=V,e.windowScrollByPolyfill=q,e.windowScrollPolyfill=A,e.windowScrollTo=P,e.windowScrollToPolyfill=N,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
***************************************************************************** */var t=function(){return(t=Object.assign||function(e){for(var t,n=1,o=arguments.length;n<o;n++)for(var r in t=arguments[n])Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r]);return e}).apply(this,arguments)};function n(e,t){var n="function"==typeof Symbol&&e[Symbol.iterator];if(!n)return e;var o,r,l=n.call(e),i=[];try{for(;(void 0===t||t-- >0)&&!(o=l.next()).done;)i.push(o.value)}catch(e){r={error:e}}finally{try{o&&!o.done&&(n=l.return)&&n.call(l)}finally{if(r)throw r.error}}return i}var o=function(e){return void 0===e||"auto"===e||"instant"===e||"smooth"===e};function r(e,t){this.scrollLeft=e,this.scrollTop=t}var l=function(e,t,n){return void 0===n&&(n="cannot convert to dictionary."),"Failed to execute '"+e+"' on '"+t+"': "+n},i=function(e,t,n){return l(e,t,"The provided value '"+n+"' is not a valid enum value of type ScrollBehavior.")},c=function(e,t,n){var o,r="__seamless__$$"+t+"$$__backup__";if(e[r]||(e[r]=e[t]||(e[t]=n)),null===(o=e[r])||void 0===o?void 0:o.__isPolyfill)throw new Error("unexpected_method");return e[r]},u=function(e){var t=typeof e;return null!==e&&("object"===t||"function"===t)},a=function(){return"scrollBehavior"in window.document.documentElement.style},f=function(e){Object.defineProperty(e,"__isPolyfill",{value:!0})},s=function(e,t){f(t),[HTMLElement.prototype,SVGElement.prototype,Element.prototype].forEach((function(n){c(n,e,HTMLElement.prototype[e]),n[e]=t}))},d=function(e){return e.ownerDocument.scrollingElement||e.ownerDocument.documentElement},v=function(e){return.5*(1-Math.cos(Math.PI*e))},w=function(){var e,t,n;return null!==(n=null===(t=null===(e=window.performance)||void 0===e?void 0:e.now)||void 0===t?void 0:t.call(e))&&void 0!==n?n:window.Date.now()},m=function(e){var t=(w()-e.timeStamp)/(e.duration||500);if(t>1)return e.method(e.targetX,e.targetY),void e.callback();var n=(e.timingFunc||v)(t),o=e.startX+(e.targetX-e.startX)*n,r=e.startY+(e.targetY-e.startY)*n;e.method(o,r),e.rafId=window.requestAnimationFrame((function(){m(e)}))},h=function(e){return isFinite(e)?Number(e):0},p=function(e){return function(a,f,s){var v,p=n((v=a).window===v?[d(a.document.documentElement),"Window"]:[a,"Element"],2),y=p[0],g=p[1],b=null!=f?f:{};if(!u(b))throw new TypeError(l(e,g));if(!o(b.behavior))throw new TypeError(i(e,g,b.behavior));"scrollBy"===e&&(b.left=h(b.left)+y.scrollLeft,b.top=h(b.top)+y.scrollTop),function(e,n,o){var l,i;if(function(e){var t;return null!==(t=e.isConnected)&&void 0!==t?t:!(e.ownerDocument&&1&e.ownerDocument.compareDocumentPosition(e))}(e)){var u=e.scrollLeft,a=e.scrollTop,f=h(null!==(l=n.left)&&void 0!==l?l:u),s=h(null!==(i=n.top)&&void 0!==i?i:a);if(f!==u||s!==a){var d=c(Object.getPrototypeOf(e),"scroll",HTMLElement.prototype.scroll||r).bind(e);if("smooth"===n.behavior){var v=function(){window.removeEventListener("wheel",y),window.removeEventListener("touchmove",y)},p=t(t({},o),{timeStamp:w(),startX:u,startY:a,targetX:f,targetY:s,rafId:0,method:d,callback:v}),y=function(){window.cancelAnimationFrame(p.rafId),v()};window.addEventListener("wheel",y,{passive:!0,once:!0}),window.addEventListener("touchmove",y,{passive:!0,once:!0}),m(p)}else d(f,s)}}}(y,b,s)}},y=p("scroll"),g=p("scrollTo"),b=p("scrollBy"),T=y,E=g,S=b,P=y,L=g,B=b,V=function(e){switch(e){case"horizontal-tb":case"lr":case"lr-tb":case"rl":case"rl-tb":return 0;case"vertical-rl":case"tb":case"tb-rl":return 1;case"vertical-lr":case"tb-lr":return 2;case"sideways-rl":return 3;case"sideways-lr":return 4}return 0},_=function(e,t,o,r){var l,i=0;switch(t||(i^=2),e){case 0:i=i>>1|(1&i)<<1,o=(l=n([r,o],2))[0],r=l[1];break;case 1:case 3:i^=1;break;case 4:i^=2}return[i,o,r]},M=function(e){return 1==(1&_(V(e.writingMode),"rtl"!==e.direction,void 0,void 0)[0])},D=function(e,t,n,o,r,l,i){return 0!==e?e:r<t&&l>n||r>t&&l<n?null:r<=t&&i<=o||l>=n&&i>=o?2:l>n&&i<o||r<t&&i>o?3:null},H=function(e){return"visible"!==e&&"clip"!==e},I=function(e,t){return(e.clientHeight<e.scrollHeight||e.clientWidth<e.scrollWidth)&&(H(t.overflowY)||H(t.overflowX)||e===d(e))},W=function(e){var t=e.parentNode,n=e.parentElement;if(null===n&&null!==t){if(11===t.nodeType)return t.host;if(9===t.nodeType)return function(e){var t;try{return(null===(t=e.ownerDocument.defaultView)||void 0===t?void 0:t.frameElement)||null}catch(e){return null}}(e)}return n},k=function(e,t,n){return e<t?t:e>n?n:e},x=function(e,t,n){switch(e){case 1:return(t+n)/2;case 3:return n;case 2:case 0:return t}},j=function(e,t){var o,r,l,i=null===(o=e.ownerDocument.defaultView)||void 0===o?void 0:o.visualViewport,c=n(e===d(e)?[0,0,null!==(r=null==i?void 0:i.width)&&void 0!==r?r:e.clientWidth,null!==(l=null==i?void 0:i.height)&&void 0!==l?l:e.clientHeight]:[t.left,t.top,e.clientWidth,e.clientHeight],4),u=c[0],a=c[1],f=c[2],s=c[3],v=u+e.clientLeft,w=a+e.clientTop;return[w,v+f,w+s,v]},O=function(e,t){var o=[],r=e.ownerDocument,l=r.defaultView;if(!l)return o;for(var i=window.getComputedStyle(e),c="rtl"!==i.direction,u=n(function(e,t,o){var r=n(_(t,o,e.block||"start",e.inline||"nearest"),3),l=r[0];return[r[1],r[2]].map((function(e,t){switch(e){case"center":return 1;case"nearest":return 0;default:return"start"===e==!(l>>t&1)?2:3}}))}(t,V(i.writingMode||i.getPropertyValue("-webkit-writing-mode")||i.getPropertyValue("-ms-writing-mode")),c),2),a=u[0],f=u[1],s=n(function(e,t,n){var o,r=t.top,l=t.right,i=t.bottom,c=t.left,u=(o=e.ownerDocument,["scroll-margin","scroll-snap-margin"].filter((function(e){return e in o.documentElement.style}))[0]);if(!u)return[r,l,i,c];var a=function(e){var t=n.getPropertyValue(u+"-"+e);return parseInt(t,10)||0};return[r-a("top"),l+a("right"),i+a("bottom"),c-a("left")]}(e,e.getBoundingClientRect(),i),4),d=s[0],v=s[1],w=s[2],m=s[3],h=W(e);null!==h;h=W(h)){if(r!==h.ownerDocument){if(!(l=(r=h.ownerDocument).defaultView))break;var p=h.getBoundingClientRect(),y=p.left,g=p.top;d+=g,v+=y,w+=g,m+=y}var b=l.getComputedStyle(h);if("fixed"===b.position)break;if(I(h,b)){var T=h.getBoundingClientRect(),E=n(j(h,T),4),S=E[0],P=E[1],L=E[2],B=E[3],H=x(a,B,P),O=x(f,S,L),X=D(a,B,P,h.clientWidth,m,v,v-m),Y=D(f,S,L,h.clientHeight,d,w,w-d),C=(null===X?H:x(X,m,v))-H,F=(null===Y?O:x(Y,d,w))-O,$=M(b)?k(C,-h.scrollWidth+h.clientWidth-h.scrollLeft,-h.scrollLeft):k(C,-h.scrollLeft,h.scrollWidth-h.clientWidth-h.scrollLeft),R=k(F,-h.scrollTop,h.scrollHeight-h.clientHeight-h.scrollTop);o.push([h,{left:h.scrollLeft+$,top:h.scrollTop+R,behavior:t.behavior}]),d=Math.max(d-R,S),v=Math.min(v-$,P),w=Math.min(w-R,L),m=Math.max(m-$,B)}}return o},X=function(e,t,r){var l=t||{};if(!o(l.behavior))throw new TypeError(i("scrollIntoView","Element",l.behavior));O(e,l).forEach((function(e){var t=n(e,2),o=t[0],l=t[1];T(o,l,r)}))},Y=X,C=function(e,t){return function(n){if(!a()){var o={scroll:y,scrollTo:g,scrollBy:b}[e];t(e,(function(){var e=arguments;if(1!==arguments.length){var t=e[0],r=e[1];o(this,{left:t,top:r})}else o(this,e[0],n)}))}}},F=C("scroll",s),$=C("scrollTo",s),R=C("scrollBy",s),A=function(e,t){f(t),c(window,e),window[e]=t},N=C("scroll",A),q=C("scrollTo",A),z=C("scrollBy",A);function G(e){Y(this,{block:null==e||e?"start":"end",inline:"nearest"})}var J=function(e){if(!a()){var t=c(window.HTMLElement.prototype,"scrollIntoView",G);s("scrollIntoView",(function(){var n=arguments,o=n[0];1===n.length&&u(o)?Y(this,o,e):t.apply(this,n)}))}};e.elementScroll=T,e.elementScrollBy=S,e.elementScrollByPolyfill=R,e.elementScrollIntoView=Y,e.elementScrollIntoViewPolyfill=J,e.elementScrollPolyfill=F,e.elementScrollTo=E,e.elementScrollToPolyfill=$,e.modifyWindow=A,e.polyfill=function(e){a()||(F(e),$(e),R(e),J(e),N(e),q(e),z(e))},e.scroll=y,e.scrollBy=b,e.scrollIntoView=X,e.scrollTo=g,e.windowScroll=P,e.windowScrollBy=B,e.windowScrollByPolyfill=z,e.windowScrollPolyfill=N,e.windowScrollTo=L,e.windowScrollToPolyfill=q,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
//# sourceMappingURL=bundle.min.js.map |
@@ -5,12 +5,12 @@ export declare const checkBehavior: (behavior?: string | undefined) => behavior is ScrollBehavior | undefined; | ||
export declare const failedExecuteInvalidEnumValue: (method: string, object: string, value: string) => string; | ||
interface GetOriginalMethod { | ||
interface BackupMethod { | ||
<K extends keyof Element>(proto: Element, method: K, fallback?: unknown): Element[K]; | ||
<K extends keyof Window>(proto: Window, method: K, fallback?: unknown): Window[K]; | ||
} | ||
export declare const getOriginalMethod: GetOriginalMethod; | ||
export declare const backupMethod: BackupMethod; | ||
export declare const isObject: (value: unknown) => boolean; | ||
export declare const isScrollBehaviorSupported: () => boolean; | ||
export declare const markPolyfill: (method: Record<never, never>) => void; | ||
export declare const markPolyfill: (method: () => void) => void; | ||
declare type Prototype = typeof HTMLElement.prototype | typeof SVGElement.prototype | typeof Element.prototype; | ||
export declare const modifyPrototypes: (modification: (prototype: Prototype) => void) => void; | ||
export declare const modifyPrototypes: <T extends "scroll" | "scrollBy" | "scrollIntoView" | "scrollTo">(prop: T, func: Prototype[T]) => void; | ||
/** | ||
@@ -17,0 +17,0 @@ * - On Chrome and Firefox, document.scrollingElement will return the <html> element. |
@@ -11,3 +11,3 @@ export const checkBehavior = (behavior) => { | ||
/* eslint-disable */ | ||
export const getOriginalMethod = (proto, method, fallback) => { | ||
export const backupMethod = (proto, method, fallback) => { | ||
const backup = `__seamless__$$${method}$$__backup__`; | ||
@@ -29,5 +29,7 @@ proto[backup] ||= proto[method] ||= fallback; | ||
}; | ||
export const modifyPrototypes = (modification) => { | ||
export const modifyPrototypes = (prop, func) => { | ||
markPolyfill(func); | ||
[HTMLElement.prototype, SVGElement.prototype, Element.prototype].forEach((prototype) => { | ||
modification(prototype); | ||
backupMethod(prototype, prop, HTMLElement.prototype[prop]); | ||
prototype[prop] = func; | ||
}); | ||
@@ -34,0 +36,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { checkBehavior, elementScrollXY, failedExecute, failedExecuteInvalidEnumValue, isObject, scrollingElement, } from "./common.js"; | ||
import { backupMethod, checkBehavior, elementScrollXY, failedExecute, failedExecuteInvalidEnumValue, isObject, scrollingElement, } from "./common.js"; | ||
import { now, step } from "./scroll-step.js"; | ||
@@ -27,3 +27,5 @@ // https://drafts.csswg.org/cssom-view/#normalize-non-finite-values | ||
} | ||
const method = elementScrollXY.bind(element); | ||
const method = backupMethod(Object.getPrototypeOf(element), "scroll", | ||
// eslint-disable-next-line | ||
HTMLElement.prototype.scroll || elementScrollXY).bind(element); | ||
if (options.behavior !== "smooth") { | ||
@@ -30,0 +32,0 @@ method(targetX, targetY); |
@@ -5,2 +5,3 @@ import type { IScrollConfig } from "./scroll-step"; | ||
export declare const elementScrollByPolyfill: (config?: IScrollConfig | undefined) => void; | ||
export declare const modifyWindow: <T extends "scroll" | "scrollBy" | "scrollTo">(prop: T, func: (Window & typeof globalThis)[T]) => void; | ||
export declare const windowScrollPolyfill: (config?: IScrollConfig | undefined) => void; | ||
@@ -7,0 +8,0 @@ export declare const windowScrollToPolyfill: (config?: IScrollConfig | undefined) => void; |
@@ -1,2 +0,2 @@ | ||
import { isScrollBehaviorSupported, markPolyfill, modifyPrototypes } from "./common.js"; | ||
import { backupMethod, isScrollBehaviorSupported, markPolyfill, modifyPrototypes } from "./common.js"; | ||
import { scroll, scrollBy, scrollTo } from "./scroll.js"; | ||
@@ -12,14 +12,11 @@ const createPolyfill = (scrollName, patch) => (config) => { | ||
}[scrollName]; | ||
patch((prototype) => { | ||
prototype[scrollName] = function () { | ||
const args = arguments; | ||
if (arguments.length === 1) { | ||
scrollMethod(this, args[0], config); | ||
return; | ||
} | ||
const left = args[0]; | ||
const top = args[1]; | ||
scrollMethod(this, { left, top }); | ||
}; | ||
markPolyfill(prototype[scrollName]); | ||
patch(scrollName, function () { | ||
const args = arguments; | ||
if (arguments.length === 1) { | ||
scrollMethod(this, args[0], config); | ||
return; | ||
} | ||
const left = args[0]; | ||
const top = args[1]; | ||
scrollMethod(this, { left, top }); | ||
}); | ||
@@ -30,4 +27,6 @@ }; | ||
export const elementScrollByPolyfill = createPolyfill("scrollBy", modifyPrototypes); | ||
const modifyWindow = (modification) => { | ||
modification(window); | ||
export const modifyWindow = (prop, func) => { | ||
markPolyfill(func); | ||
backupMethod(window, prop); | ||
window[prop] = func; | ||
}; | ||
@@ -34,0 +33,0 @@ export const windowScrollPolyfill = createPolyfill("scroll", modifyWindow); |
@@ -1,3 +0,9 @@ | ||
import { getOriginalMethod, isObject, isScrollBehaviorSupported, markPolyfill, modifyPrototypes } from "./common.js"; | ||
import { backupMethod, isObject, isScrollBehaviorSupported, modifyPrototypes } from "./common.js"; | ||
import { elementScrollIntoView } from "./scrollIntoView.js"; | ||
function elementScrollIntoViewBoolean(alignToTop) { | ||
elementScrollIntoView(this, { | ||
block: alignToTop ?? true ? "start" : "end", | ||
inline: "nearest", | ||
}); | ||
} | ||
export const elementScrollIntoViewPolyfill = (config) => { | ||
@@ -7,17 +13,13 @@ if (isScrollBehaviorSupported()) { | ||
} | ||
const originalFunc = getOriginalMethod(window.HTMLElement.prototype, "scrollIntoView"); | ||
modifyPrototypes((prototype) => { | ||
prototype.scrollIntoView = function scrollIntoView() { | ||
const args = arguments; | ||
const options = args[0]; | ||
if (args.length === 1 && isObject(options)) { | ||
elementScrollIntoView(this, options, config); | ||
return; | ||
} | ||
originalFunc.apply(this, args); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
markPolyfill(prototype.scrollIntoView); | ||
const originalFunc = backupMethod(window.HTMLElement.prototype, "scrollIntoView", elementScrollIntoViewBoolean); | ||
modifyPrototypes("scrollIntoView", function scrollIntoView() { | ||
const args = arguments; | ||
const options = args[0]; | ||
if (args.length === 1 && isObject(options)) { | ||
elementScrollIntoView(this, options, config); | ||
return; | ||
} | ||
originalFunc.apply(this, args); | ||
}); | ||
}; | ||
//# sourceMappingURL=scrollIntoView.polyfill.js.map |
@@ -5,3 +5,3 @@ { | ||
"description": "Smooth Scroll behavior polyfill", | ||
"version": "2.1.2", | ||
"version": "2.1.3-beta.1", | ||
"author": { | ||
@@ -8,0 +8,0 @@ "name": "Dustan Kasten", |
@@ -42,3 +42,4 @@ [![Build Status](https://travis-ci.org/magic-akari/seamless-scroll-polyfill.svg?branch=master)](https://travis-ci.org/magic-akari/seamless-scroll-polyfill) | ||
```html | ||
<script src="https://cdn.jsdelivr.net/npm/seamless-scroll-polyfill@2.0.0/lib/bundle.min.js"></script> | ||
<!-- please replace the `latest` with specific version --> | ||
<script src="https://cdn.jsdelivr.net/npm/seamless-scroll-polyfill@latest/lib/bundle.min.js"></script> | ||
<script> | ||
@@ -45,0 +46,0 @@ // patch all methods |
@@ -16,3 +16,3 @@ export const checkBehavior = (behavior?: string): behavior is undefined | ScrollBehavior => { | ||
interface GetOriginalMethod { | ||
interface BackupMethod { | ||
<K extends keyof Element>(proto: Element, method: K, fallback?: unknown): Element[K]; | ||
@@ -23,3 +23,3 @@ <K extends keyof Window>(proto: Window, method: K, fallback?: unknown): Window[K]; | ||
/* eslint-disable */ | ||
export const getOriginalMethod: GetOriginalMethod = (proto: any, method: string, fallback?: unknown) => { | ||
export const backupMethod: BackupMethod = (proto: any, method: string, fallback?: unknown) => { | ||
const backup = `__seamless__$$${method}$$__backup__`; | ||
@@ -43,3 +43,3 @@ proto[backup] ||= proto[method] ||= fallback; | ||
export const markPolyfill = (method: Record<never, never>): void => { | ||
export const markPolyfill = (method: () => void): void => { | ||
Object.defineProperty(method, "__isPolyfill", { value: true }); | ||
@@ -50,5 +50,10 @@ }; | ||
export const modifyPrototypes = (modification: (prototype: Prototype) => void): void => { | ||
export const modifyPrototypes = <T extends "scroll" | "scrollTo" | "scrollBy" | "scrollIntoView">( | ||
prop: T, | ||
func: Prototype[T], | ||
): void => { | ||
markPolyfill(func); | ||
[HTMLElement.prototype, SVGElement.prototype, Element.prototype].forEach((prototype) => { | ||
modification(prototype); | ||
backupMethod(prototype, prop, HTMLElement.prototype[prop]); | ||
prototype[prop] = func; | ||
}); | ||
@@ -55,0 +60,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { isScrollBehaviorSupported, markPolyfill, modifyPrototypes } from "./common.js"; | ||
import { backupMethod, isScrollBehaviorSupported, markPolyfill, modifyPrototypes } from "./common.js"; | ||
import type { IScrollConfig } from "./scroll-step"; | ||
@@ -7,3 +7,3 @@ import { scroll, scrollBy, scrollTo } from "./scroll.js"; | ||
type Patch = (modification: (prototype: Record<ScrollName, (this: Element | typeof window) => void>) => void) => void; | ||
type Patch = <T extends ScrollName>(prop: T, func: (Element | typeof window)[T]) => void; | ||
@@ -23,16 +23,12 @@ const createPolyfill = | ||
patch((prototype) => { | ||
prototype[scrollName] = function (): void { | ||
const args = arguments; | ||
if (arguments.length === 1) { | ||
scrollMethod(this, args[0], config); | ||
return; | ||
} | ||
patch(scrollName, function (this: Element | typeof window): void { | ||
const args = arguments; | ||
if (arguments.length === 1) { | ||
scrollMethod(this, args[0], config); | ||
return; | ||
} | ||
const left = args[0] as number; | ||
const top = args[1] as number; | ||
scrollMethod(this, { left, top }); | ||
}; | ||
markPolyfill(prototype[scrollName]); | ||
const left = args[0] as number; | ||
const top = args[1] as number; | ||
scrollMethod(this, { left, top }); | ||
}); | ||
@@ -45,4 +41,6 @@ }; | ||
const modifyWindow: Patch = (modification) => { | ||
modification(window); | ||
export const modifyWindow = <T extends "scroll" | "scrollTo" | "scrollBy">(prop: T, func: typeof window[T]): void => { | ||
markPolyfill(func); | ||
backupMethod(window, prop); | ||
window[prop] = func; | ||
}; | ||
@@ -49,0 +47,0 @@ |
import { | ||
backupMethod, | ||
checkBehavior, | ||
@@ -44,3 +45,8 @@ elementScrollXY, | ||
const method = elementScrollXY.bind(element); | ||
const method = backupMethod( | ||
Object.getPrototypeOf(element), | ||
"scroll", | ||
// eslint-disable-next-line | ||
HTMLElement.prototype.scroll || elementScrollXY, | ||
).bind(element); | ||
@@ -47,0 +53,0 @@ if (options.behavior !== "smooth") { |
@@ -1,5 +0,12 @@ | ||
import { getOriginalMethod, isObject, isScrollBehaviorSupported, markPolyfill, modifyPrototypes } from "./common.js"; | ||
import { backupMethod, isObject, isScrollBehaviorSupported, modifyPrototypes } from "./common.js"; | ||
import type { IScrollConfig } from "./scroll-step"; | ||
import { elementScrollIntoView } from "./scrollIntoView.js"; | ||
function elementScrollIntoViewBoolean(this: Element, alignToTop?: unknown) { | ||
elementScrollIntoView(this, { | ||
block: alignToTop ?? true ? "start" : "end", | ||
inline: "nearest", | ||
}); | ||
} | ||
export const elementScrollIntoViewPolyfill = (config?: IScrollConfig): void => { | ||
@@ -10,20 +17,15 @@ if (isScrollBehaviorSupported()) { | ||
const originalFunc = getOriginalMethod(window.HTMLElement.prototype, "scrollIntoView"); | ||
const originalFunc = backupMethod(window.HTMLElement.prototype, "scrollIntoView", elementScrollIntoViewBoolean); | ||
modifyPrototypes((prototype) => { | ||
prototype.scrollIntoView = function scrollIntoView(): void { | ||
const args = arguments; | ||
const options = args[0] as unknown; | ||
modifyPrototypes("scrollIntoView", function scrollIntoView(this: Element): void { | ||
const args = arguments; | ||
const options = args[0] as unknown; | ||
if (args.length === 1 && isObject(options)) { | ||
elementScrollIntoView(this, options as ScrollIntoViewOptions, config); | ||
return; | ||
} | ||
if (args.length === 1 && isObject(options)) { | ||
elementScrollIntoView(this, options as ScrollIntoViewOptions, config); | ||
return; | ||
} | ||
originalFunc.apply(this, args as any); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||
markPolyfill(prototype.scrollIntoView); | ||
originalFunc.apply(this, args as any); | ||
}); | ||
}; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
2262
66
421690
63
1