Skip to content

Commit

Permalink
fix(injectBaseStylesheet): address style injection issue
Browse files Browse the repository at this point in the history
Fix a bug where getElementByTagName() was attempting to access an attr, .innerHTML, that was not defined.

Instead, create and inject a new style tag as a child to the head.
  • Loading branch information
luqven committed Feb 24, 2021
1 parent 74d8cb4 commit d0352f4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Easily add \"zoom on hover\" functionality to your site's images. Lightweight, no-dependency JavaScript.",
"contributors": [
"Frederick Fogerty <frederick.fogerty@gmail.com> (https://github.com/frederickfogerty)",
"Sherwin Heydarbeygi <sherwin@imgix.com> (https://github.com/sherwinski)"
"Sherwin Heydarbeygi <sherwin@imgix.com> (https://github.com/sherwinski)",
"Luis Ball <lball@imgix.com> (https://github.com/luqven)"
],
"main": "lib/Drift.js",
"module": "es/Drift.js",
Expand Down
9 changes: 5 additions & 4 deletions src/js/injectBaseStylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ export default function injectBaseStylesheet() {
return;
}

// create the injected styles
const styleEl = document.createElement("style");
styleEl.type = "text/css";
styleEl.classList = "drift-base-styles";

styleEl.textContent = RULES;
const head = document.head;

// prepend them to the document head's first child
const head = document.head;
const allHeadElements = head.getElementsByTagName("*");
allHeadElements.innerHTML = styleEl + allHeadElements.innerHTML;
const firstItem = allHeadElements.item(0);
firstItem.outerHTML = styleEl.outerHTML + firstItem.outerHTML;
}

0 comments on commit d0352f4

Please sign in to comment.