Skip to content

Commit 34a0d61

Browse files
committed
fix(injectBaseStylesheet): address style injection issue
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.
1 parent 839379d commit 34a0d61

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Easily add \"zoom on hover\" functionality to your site's images. Lightweight, no-dependency JavaScript.",
55
"contributors": [
66
"Frederick Fogerty <[email protected]> (https://github.com/frederickfogerty)",
7-
"Sherwin Heydarbeygi <[email protected]> (https://github.com/sherwinski)"
7+
"Sherwin Heydarbeygi <[email protected]> (https://github.com/sherwinski)",
8+
"Luis Ball <[email protected]> (https://github.com/luqven)"
89
],
910
"main": "lib/Drift.js",
1011
"module": "es/Drift.js",

src/js/injectBaseStylesheet.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,14 @@ export default function injectBaseStylesheet() {
5555
return;
5656
}
5757

58+
// create the injected styles
5859
const styleEl = document.createElement("style");
59-
styleEl.type = "text/css";
6060
styleEl.classList = "drift-base-styles";
61-
6261
styleEl.textContent = RULES;
63-
const head = document.head;
6462

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

0 commit comments

Comments
 (0)