Skip to content

Instantly share code, notes, and snippets.

@mliszcz
Last active January 7, 2016 21:30
Show Gist options
  • Save mliszcz/d11ea630cc777012d69b to your computer and use it in GitHub Desktop.
Save mliszcz/d11ea630cc777012d69b to your computer and use it in GitHub Desktop.
Firefox-specific patch for HTMLImports polyfill.
/**
* HTML Imports implementation seems to be broken in Firefox.
* Enabling dom.webcomponents.enabled adds `import` attribute
* to the HTMLLinkElement. This makes HTMLImports polyfill to
* fall-back to native implementation.
* This patch intercepts `document.createElement()` call from
* the polyfill, thus needs to be loaded before HTMLImports.js.
* @see https://github.com/webcomponents/webcomponentsjs/issues/289
* @author mliszcz<liszcz.michal@gmail.com>
* @license MIT
*/
(function (window, document, console) {
'use strict'
// http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
var isFirefox = typeof InstallTrigger !== 'undefined';
if (isFirefox) {
console.debug('patching document.createElement for HTMLImports');
var _createElement = document.createElement;
var callCount = 0;
// HTMLImports polyfill creates 'link' element and
// checks for the presence of 'import' attribute.
document.createElement = function(...args) {
var element = _createElement.apply(document, args);
if (callCount++ == 0 && args[0] == 'link' && 'import' in element) {
console.debug('returning patched link element');
return {};
} else {
return element;
}
};
window.addEventListener('DOMContentLoaded', function() {
console.debug('restoring document.createElement');
document.createElement = _createElement;
});
}
})(window, document, console);
{
"name": "firefox-html-imports-patch",
"version": "0.1.0",
"description": "Patch for HTMLImports polyfill to work with dom.webcomponents.enabled",
"main": "firefox-html-imports-patch.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/d11ea630cc777012d69b.git"
},
"keywords": [
"html",
"imports",
"web",
"components"
],
"author": "mliszcz <liszcz.michal@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://gist.github.com/d11ea630cc777012d69b"
},
"homepage": "https://gist.github.com/d11ea630cc777012d69b"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment