From 3db0ee355f04c575669849d13558408269f513ac Mon Sep 17 00:00:00 2001 From: vrichard Date: Wed, 19 Apr 2017 11:46:39 +0200 Subject: [PATCH] add global instead of window to partially support nodejs --- package.json | 2 +- url-polyfill.js | 38 +++++++++++++++----------------------- url-polyfill.min.js | 2 +- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/package.json b/package.json index 729f31a..4c66ae0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "url-polyfill", - "version": "1.0.3", + "version": "1.0.4", "description": "Polyfill URL and URLSearchParams", "main": "url-polyfill.js", "scripts": { diff --git a/url-polyfill.js b/url-polyfill.js index e18686c..635b24b 100644 --- a/url-polyfill.js +++ b/url-polyfill.js @@ -1,15 +1,14 @@ +var g = (typeof global !== 'undefined') ? global : + (typeof window !== 'undefined') ? window : + (typeof self !== 'undefined') ? self : this; -(function() { +(function(global) { /** * Polyfill URLSearchParams * * Inspired from : https://github.com/WebReflection/url-search-params/blob/master/src/url-search-params.js */ - var checkIfURLSearchParamsIsSupported = function() { - return ('URLSearchParams' in window); - }; - var checkIfIteratorIsSupported = function() { try { return !!Symbol.iterator; @@ -38,7 +37,6 @@ return iterator; }; - var polyfillURLSearchParams= function() { var URLSearchParams = function(searchString) { @@ -67,7 +65,6 @@ var proto = URLSearchParams.prototype; - proto.append = function(name, value) { if(name in this._entries) { this._entries[name].push(value.toString()); @@ -139,20 +136,18 @@ return searchString; }; - window.URLSearchParams = URLSearchParams; + global.URLSearchParams = URLSearchParams; }; - if(!checkIfURLSearchParamsIsSupported()) { + if(!('URLSearchParams' in global)) { polyfillURLSearchParams(); } - // console.log(new URLSearchParams('a=b&c=d')); - // HTMLAnchorElement -})(); +})(g); -(function() { +(function(global) { /** * Polyfill URL * @@ -171,7 +166,7 @@ var polyfillURL = function() { - var _URL = window.URL; + var _URL = global.URL; var URL = function(url, base) { if(typeof url !== 'string') throw new TypeError('Failed to construct \'URL\': Invalid URL'); @@ -286,7 +281,7 @@ return _URL.revokeObjectURL.apply(_URL, arguments); }; - window.URL = URL; + global.URL = URL; }; @@ -294,24 +289,21 @@ polyfillURL(); } - if(!('origin' in window.location)) { + if((global.location !== void 0) && !('origin' in global.location)) { var getOrigin = function() { - return window.location.protocol + '//' + window.location.hostname + (window.location.port ? (':' + window.location.port) : ''); + return global.location.protocol + '//' + global.location.hostname + (global.location.port ? (':' + global.location.port) : ''); }; try { - Object.defineProperty(window.location, 'origin', { + Object.defineProperty(global.location, 'origin', { get: getOrigin, enumerable: true }); } catch(e) { setInterval(function() { - window.location.origin = getOrigin(); + global.location.origin = getOrigin(); }, 100); } } - - // console.log(new URL('https://www.yahoo.com/?fr=yset_ie_syc_oracle&type=orcl_hpset')); - -})(); +})(g); diff --git a/url-polyfill.min.js b/url-polyfill.min.js index 14554f0..7692b07 100644 --- a/url-polyfill.min.js +++ b/url-polyfill.min.js @@ -1 +1 @@ -(function(){var t=function(){return"URLSearchParams"in window};var e=function(){try{return!!Symbol.iterator}catch(t){return false}};var n=e();var r=function(t){var e={next:function(){var e=t.shift();return{done:e===void 0,value:e}}};if(n){e[Symbol.iterator]=function(){return e}}return e};var i=function(){var t=function(e){Object.defineProperty(this,"_entries",{value:{}});if(typeof e==="string"){if(e!==""){e=e.replace(/^\?/,"");var n=e.split("&");var r;for(var i=0;i1?decodeURIComponent(r[1]):"")}}}else if(e instanceof t){var o=this;e.forEach(function(t,e){o.append(t,e)})}};var e=t.prototype;e.append=function(t,e){if(t in this._entries){this._entries[t].push(e.toString())}else{this._entries[t]=[e.toString()]}};e.delete=function(t){delete this._entries[t]};e.get=function(t){return t in this._entries?this._entries[t][0]:null};e.getAll=function(t){return t in this._entries?this._entries[t].slice(0):[]};e.has=function(t){return t in this._entries};e.set=function(t,e){this._entries[t]=[e.toString()]};e.forEach=function(t,e){var n;for(var r in this._entries){if(this._entries.hasOwnProperty(r)){n=this._entries[r];for(var i=0;i0)t+="&";t+=encodeURIComponent(n)+"="+encodeURIComponent(e)});return t};window.URLSearchParams=t};if(!t()){i()}})();(function(){var t=function(){try{var t=new URL("b","http://a");t.pathname="c%20d";return t.href==="http://a/c%20d"&&t.searchParams}catch(t){return false}};var e=function(){var t=window.URL;var e=function(t,e){if(typeof t!=="string")throw new TypeError("Failed to construct 'URL': Invalid URL");var n=document.implementation.createHTMLDocument("");if(e){var r=n.createElement("base");r.href=e;n.head.appendChild(r)}var i=n.createElement("a");i.href=t;n.body.appendChild(i);if(i.protocol===":"||!/:/.test(i.href)){throw new TypeError("Invalid URL")}Object.defineProperty(this,"_anchorElement",{value:i})};var n=e.prototype;var r=function(t){Object.defineProperty(n,t,{get:function(){return this._anchorElement[t]},set:function(e){this._anchorElement[t]=e},enumerable:true})};["hash","host","hostname","href","port","protocol","search"].forEach(function(t){r(t)});Object.defineProperties(n,{toString:{get:function(){var t=this;return function(){return t.href}}},pathname:{get:function(){return this._anchorElement.pathname.replace(/(^\/?)/,"/")},set:function(t){this._anchorElement.pathname=t},enumerable:true},origin:{get:function(){return this._anchorElement.protocol+"//"+this._anchorElement.hostname+(this._anchorElement.port?":"+this._anchorElement.port:"")},enumerable:true},password:{get:function(){return""},set:function(t){},enumerable:true},username:{get:function(){return""},set:function(t){},enumerable:true},searchParams:{get:function(){var t=new URLSearchParams(this.search);var e=this;["append","delete","set"].forEach(function(n){var r=t[n];t[n]=function(){r.apply(t,arguments);e.search=t.toString()}});return t},enumerable:true}});e.createObjectURL=function(e){return t.createObjectURL.apply(t,arguments)};e.revokeObjectURL=function(e){return t.revokeObjectURL.apply(t,arguments)};window.URL=e};if(!t()){e()}if(!("origin"in window.location)){var n=function(){return window.location.protocol+"//"+window.location.hostname+(window.location.port?":"+window.location.port:"")};try{Object.defineProperty(window.location,"origin",{get:n,enumerable:true})}catch(t){setInterval(function(){window.location.origin=n()},100)}}})(); \ No newline at end of file +var g=typeof global!=="undefined"?global:typeof window!=="undefined"?window:typeof self!=="undefined"?self:this;(function(e){var t=function(){try{return!!Symbol.iterator}catch(e){return false}};var n=t();var r=function(e){var t={next:function(){var t=e.shift();return{done:t===void 0,value:t}}};if(n){t[Symbol.iterator]=function(){return t}}return t};var i=function(){var t=function(e){Object.defineProperty(this,"_entries",{value:{}});if(typeof e==="string"){if(e!==""){e=e.replace(/^\?/,"");var n=e.split("&");var r;for(var i=0;i1?decodeURIComponent(r[1]):"")}}}else if(e instanceof t){var o=this;e.forEach(function(e,t){o.append(e,t)})}};var i=t.prototype;i.append=function(e,t){if(e in this._entries){this._entries[e].push(t.toString())}else{this._entries[e]=[t.toString()]}};i.delete=function(e){delete this._entries[e]};i.get=function(e){return e in this._entries?this._entries[e][0]:null};i.getAll=function(e){return e in this._entries?this._entries[e].slice(0):[]};i.has=function(e){return e in this._entries};i.set=function(e,t){this._entries[e]=[t.toString()]};i.forEach=function(e,t){var n;for(var r in this._entries){if(this._entries.hasOwnProperty(r)){n=this._entries[r];for(var i=0;i0)e+="&";e+=encodeURIComponent(n)+"="+encodeURIComponent(t)});return e};e.URLSearchParams=t};if(!("URLSearchParams"in e)){i()}})(g);(function(e){var t=function(){try{var e=new URL("b","http://a");e.pathname="c%20d";return e.href==="http://a/c%20d"&&e.searchParams}catch(e){return false}};var n=function(){var t=e.URL;var n=function(e,t){if(typeof e!=="string")throw new TypeError("Failed to construct 'URL': Invalid URL");var n=document.implementation.createHTMLDocument("");if(t){var r=n.createElement("base");r.href=t;n.head.appendChild(r)}var i=n.createElement("a");i.href=e;n.body.appendChild(i);if(i.protocol===":"||!/:/.test(i.href)){throw new TypeError("Invalid URL")}Object.defineProperty(this,"_anchorElement",{value:i})};var r=n.prototype;var i=function(e){Object.defineProperty(r,e,{get:function(){return this._anchorElement[e]},set:function(t){this._anchorElement[e]=t},enumerable:true})};["hash","host","hostname","href","port","protocol","search"].forEach(function(e){i(e)});Object.defineProperties(r,{toString:{get:function(){var e=this;return function(){return e.href}}},pathname:{get:function(){return this._anchorElement.pathname.replace(/(^\/?)/,"/")},set:function(e){this._anchorElement.pathname=e},enumerable:true},origin:{get:function(){return this._anchorElement.protocol+"//"+this._anchorElement.hostname+(this._anchorElement.port?":"+this._anchorElement.port:"")},enumerable:true},password:{get:function(){return""},set:function(e){},enumerable:true},username:{get:function(){return""},set:function(e){},enumerable:true},searchParams:{get:function(){var e=new URLSearchParams(this.search);var t=this;["append","delete","set"].forEach(function(n){var r=e[n];e[n]=function(){r.apply(e,arguments);t.search=e.toString()}});return e},enumerable:true}});n.createObjectURL=function(e){return t.createObjectURL.apply(t,arguments)};n.revokeObjectURL=function(e){return t.revokeObjectURL.apply(t,arguments)};e.URL=n};if(!t()){n()}if(e.location!==void 0&&!("origin"in e.location)){var r=function(){return e.location.protocol+"//"+e.location.hostname+(e.location.port?":"+e.location.port:"")};try{Object.defineProperty(e.location,"origin",{get:r,enumerable:true})}catch(t){setInterval(function(){e.location.origin=r()},100)}}})(g); \ No newline at end of file