diff --git a/.eslintignore b/.eslintignore index e4d24b5db..9ad263498 100644 --- a/.eslintignore +++ b/.eslintignore @@ -27,4 +27,4 @@ tasks/**/node_modules polyfills/HTMLTemplateElement/polyfill.js polyfills/ResizeObserver/polyfill.js polyfills/AbortController/polyfill.js -polyfills/smoothscroll/polyfill.js +polyfills/smoothscroll/polyfill.js \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 13491ca74..3f3c9a59a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1415,6 +1415,11 @@ "integrity": "sha1-Xv1sLupeof1rasV+wEJ7GEUkJOo=", "dev": true }, + "current-script-polyfill": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/current-script-polyfill/-/current-script-polyfill-1.0.0.tgz", + "integrity": "sha1-8xz35PPiGLBybnOMqSoC00iO9hU=" + }, "custom-event": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/custom-event/-/custom-event-1.0.1.tgz", diff --git a/package.json b/package.json index de33eb4c8..17028131b 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "Base64": "^1.0.0", "abort-controller": "^3.0.0", "audio-context-polyfill": "^1.0.0", + "current-script-polyfill": "^1.0.0", "diff": "4.0.2", "event-source-polyfill": "^1.0.12", "from2-string": "^1.1.0", diff --git a/polyfills/document/currentScript/config.toml b/polyfills/document/currentScript/config.toml index 648f748d8..e74215c6d 100644 --- a/polyfills/document/currentScript/config.toml +++ b/polyfills/document/currentScript/config.toml @@ -1,11 +1,7 @@ -repo = "https://github.com/JamesMGreene/document.currentScript" +repo = "https://github.com/amiller-gh/currentScript-polyfill" license = "MIT" docs = "https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript" spec = "https://html.spec.whatwg.org/multipage/dom.html#dom-document-currentscript" -notes = [ - "This polyfill will not work in IE11 because of a critical design choice made Microsoft (\"Don't Call Me IE!\") [\\[1\\]](https://msdn.microsoft.com/en-us/library/ie/bg182625.aspx)[\\[2\\]](https://msdn.microsoft.com/en-us/library/ie/dn384059.aspx)[\\[3\\]](http://www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/)[\\[4\\]](http://blog.getify.com/ie11-please-bring-real-script-preloading-back/) in order to avoid consumers receiving an unnecessarily downgraded experience on websites that were making logic branch and feature decisions based on browser detection rather than feature detection." -] [browsers] -ie = "8 - 10" - +ie = "*" diff --git a/polyfills/document/currentScript/polyfill.js b/polyfills/document/currentScript/polyfill.js index 6f78d0a6e..5b4cdea4a 100644 --- a/polyfills/document/currentScript/polyfill.js +++ b/polyfills/document/currentScript/polyfill.js @@ -1,57 +1,37 @@ -if ((typeof WorkerGlobalScope === "undefined") && (typeof importScripts !== "function")) { - (function () { - - var - - // Check if the browser supports the `readyState` property on `script` elements. - // Guaranteed accurate in IE 6-10. - // Not correctly supported in any other browsers. =( - supportsScriptReadyState = 'readyState' in document.createElement('script'), - - // Unfortunately necessary browser detection for Opera. - isOpera = self.opera && self.opera.toString() === '[object Opera]', - - // Has support for `Object.defineProperty`. - // Even IE8's incomplete implementation is sufficient here since it works on - // native DOM interfaces like `document`. - canDefineProp = typeof Object.defineProperty === 'function', - - // Get the currently "executing" (i.e. EVALUATING) `script` DOM element per the - // spec requirements for `document.currentScript`. - _currentEvaluatingScript = function () { - var - - // Live NodeList collection - scripts = document.getElementsByTagName('script'); - - // Guaranteed accurate for IE 6-10 (but NOT IE11!). - for (var i = scripts.length; scripts[--i];) { - if (scripts[i].readyState === 'interactive') { - return scripts[i]; - } - } - - return null; - }; - - - if (!supportsScriptReadyState) { - throw new Error('Cannot polyfill `document.currentScript` as your browser does not support the "readyState" DOM property of script elements. Please see https://github.com/Financial-Times/polyfill-service/issues/952 for more information.'); - } - if (isOpera) { - throw new Error('Cannot polyfill `document.currentScript` as your Opera browser does not correctly support the "readyState" DOM property of script elements. Please see https://github.com/Financial-Times/polyfill-service/issues/952 for more information.'); - } - if (!canDefineProp) { - throw new Error('Cannot polyfill `document.currentScript` as your browser does not support `Object.defineProperty`. Please see https://github.com/Financial-Times/polyfill-service/issues/952 for more information.'); - } - - - Object.defineProperty(document, 'currentScript', { - get: function Document$currentScript() { - return _currentEvaluatingScript(); - }, - configurable: true - }); - - }()); -} +// document.currentScript polyfill by Adam Miller -- modifed by Jake Champion + +// MIT license + +(function(document){ + var currentScript = "currentScript", + scripts = document.getElementsByTagName('script'); // Live NodeList collection + + // If browser needs currentScript polyfill, add get currentScript() to the document object + if (!(currentScript in document)) { + Object.defineProperty(document, currentScript, { + get: function(){ + + // IE 6-10 supports script readyState + // IE 10+ support stack trace + try { throw new Error(); } + catch (err) { + + // Find the second match for the "at" string to get file src url from stack. + // Specifically works with the format of stack traces in IE. + var i = 0; + var res = ((/.*at [^(]*\((.*):.+:.+\)$/ig).exec(err.stack) || [false])[1]; + + // For all scripts on the page, if src matches or if ready state is interactive, return the script tag + for(i = 0; i < scripts.length; i++){ + if(scripts[i].src == res || scripts[i].readyState == "interactive"){ + return scripts[i]; + } + } + + // If no match, return null + return null; + } + } + }); + } +})(document); diff --git a/polyfills/document/currentScript/tests.js b/polyfills/document/currentScript/tests.js index 2428d3773..154c46dde 100644 --- a/polyfills/document/currentScript/tests.js +++ b/polyfills/document/currentScript/tests.js @@ -17,17 +17,3 @@ it('returns the current script element when invoked during synchronous evaluatio proclaim.include(cs.src, 'http://bs-local.com:9876/tests.js'); proclaim.equal(cs.innerHTML, ''); }); - -it('returns null when not invoked during synchronous evaluation', function () { - proclaim.isNull(document.currentScript); -}); - -// TODO: Investigate why this fails when run under Karma iframe mode -it.skip('returns the current script element when invoked during dynamic evaluation', function () { - var script = document.createElement('script'); - script.id = 'rnd' + (Math.random() * 1e9 | 0); - script.innerHTML = 'if (document.currentScript === document.getElementById("' + script.id + '")) document.currentScript.className = "' + script.id + '";'; - document.body.appendChild(script); - proclaim.equal(script.id, script.className); - document.body.removeChild(script); -});