From 7b8561a55cb61cbe663c1ea90cedde45b0ba5668 Mon Sep 17 00:00:00 2001 From: catarak Date: Mon, 11 Jul 2016 17:00:30 -0400 Subject: [PATCH] add srcdoc polyfill to static --- client/modules/IDE/components/PreviewFrame.js | 1 - index.html | 1 + static/srcdoc-polyfill.js | 82 +++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 static/srcdoc-polyfill.js diff --git a/client/modules/IDE/components/PreviewFrame.js b/client/modules/IDE/components/PreviewFrame.js index a4e46e87..36d70ceb 100644 --- a/client/modules/IDE/components/PreviewFrame.js +++ b/client/modules/IDE/components/PreviewFrame.js @@ -1,7 +1,6 @@ import React, { PropTypes } from 'react'; import ReactDOM from 'react-dom'; import escapeStringRegexp from 'escape-string-regexp'; -import 'srcdoc-polyfill'; // sandbox="allow-scripts allow-pointer-lock allow-same-origin allow-popups allow-modals allow-forms" diff --git a/index.html b/index.html index de2e4106..3153c672 100644 --- a/index.html +++ b/index.html @@ -9,5 +9,6 @@
+ diff --git a/static/srcdoc-polyfill.js b/static/srcdoc-polyfill.js new file mode 100644 index 00000000..83fc788b --- /dev/null +++ b/static/srcdoc-polyfill.js @@ -0,0 +1,82 @@ +(function(root, factory) { + // `root` does not resolve to the global window object in a Browserified + // bundle, so a direct reference to that object is used instead. + var _srcDoc = window.srcDoc; + + if (typeof define === "function" && define.amd) { + define(['exports'], function(exports) { + factory(exports, _srcDoc); + root.srcDoc = exports; + }); + } else if (typeof exports === "object") { + factory(exports, _srcDoc); + } else { + root.srcDoc = {}; + factory(root.srcDoc, _srcDoc); + } +})(this, function(exports, _srcDoc) { + var idx, iframes; + var isCompliant = !!("srcdoc" in document.createElement("iframe")); + var implementations = { + compliant: function( iframe, content ) { + + if (content) { + iframe.setAttribute("srcdoc", content); + } + }, + legacy: function( iframe, content ) { + + var jsUrl; + + if (!iframe || !iframe.getAttribute) { + return; + } + + if (!content) { + content = iframe.getAttribute("srcdoc"); + } else { + iframe.setAttribute("srcdoc", content); + } + + if (content) { + // The value returned by a script-targeted URL will be used as + // the iFrame's content. Create such a URL which returns the + // iFrame element's `srcdoc` attribute. + jsUrl = "javascript: window.frameElement.getAttribute('srcdoc');"; + + iframe.setAttribute("src", jsUrl); + + // Explicitly set the iFrame's window.location for + // compatability with IE9, which does not react to changes in + // the `src` attribute when it is a `javascript:` URL, for + // some reason + if (iframe.contentWindow) { + iframe.contentWindow.location = jsUrl; + } + } + } + }; + var srcDoc = exports; + // Assume the best + srcDoc.set = implementations.compliant; + srcDoc.noConflict = function() { + window.srcDoc = _srcDoc; + return srcDoc; + }; + + // If the browser supports srcdoc, no shimming is necessary + if (isCompliant) { + return; + } + + srcDoc.set = implementations.legacy; + + // Automatically shim any iframes already present in the document + iframes = document.getElementsByTagName("iframe"); + idx = iframes.length; + + while (idx--) { + srcDoc.set( iframes[idx] ); + } + +});