Skip to content

Commit c7d8fec

Browse files
Vasikaranevilebottnawi
authored andcommitted
fix: insertInto and insertAt collaboration (#325)
1 parent e4fa68a commit c7d8fec

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

lib/addStyles.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ var isOldIE = memoize(function () {
2323
return window && document && document.all && !window.atob;
2424
});
2525

26-
var getTarget = function (target) {
26+
var getTarget = function (target, parent) {
27+
if (parent){
28+
return parent.querySelector(target);
29+
}
2730
return document.querySelector(target);
2831
};
2932

3033
var getElement = (function (fn) {
3134
var memo = {};
3235

33-
return function(target) {
36+
return function(target, parent) {
3437
// If passing function in options, then use it for resolve "head" element.
3538
// Useful for Shadow Root style i.e
3639
// {
@@ -40,7 +43,7 @@ var getElement = (function (fn) {
4043
return target();
4144
}
4245
if (typeof memo[target] === "undefined") {
43-
var styleTarget = getTarget.call(this, target);
46+
var styleTarget = getTarget.call(this, target, parent);
4447
// Special case to return head of iframe instead of iframe itself
4548
if (window.HTMLIFrameElement && styleTarget instanceof window.HTMLIFrameElement) {
4649
try {
@@ -181,7 +184,7 @@ function insertStyleElement (options, style) {
181184
} else if (options.insertAt === "bottom") {
182185
target.appendChild(style);
183186
} else if (typeof options.insertAt === "object" && options.insertAt.before) {
184-
var nextSibling = getElement(options.insertInto + " " + options.insertAt.before);
187+
var nextSibling = getElement(options.insertAt.before, target);
185188
target.insertBefore(style, nextSibling);
186189
} else {
187190
throw new Error("[Style Loader]\n\n Invalid value for parameter 'insertAt' ('options.insertAt') found.\n Must be 'top', 'bottom', or Object.\n (https://github.com/webpack-contrib/style-loader#insertat)\n");

test/basic.test.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("basic tests", function() {
2525
rootDir = path.resolve(__dirname + "/../") + "/",
2626
jsdomHtml = [
2727
"<html>",
28-
"<head>",
28+
"<head id='head'>",
2929
existingStyle,
3030
"</head>",
3131
"<body>",
@@ -158,6 +158,30 @@ describe("basic tests", function() {
158158
}, selector);
159159
});
160160

161+
it("insert at before with insert into custom element by function", function(done) {
162+
const selector = "#head";
163+
styleLoaderOptions.insertInto = () => document.querySelector("#head");
164+
165+
styleLoaderOptions.insertAt = {
166+
before: "#existing-style"
167+
};
168+
169+
let expected = requiredCss;
170+
171+
runCompilerTest(expected, done, function() {
172+
let head = this.document.querySelector(selector);
173+
let existingStyleIndex;
174+
for (let i = 0; i < head.children.length; i++){
175+
let html = `<style id="existing-style">${head.children[i].innerHTML}</style>`;
176+
if(html === existingStyle){
177+
existingStyleIndex = i;
178+
break;
179+
}
180+
}
181+
return head.children[existingStyleIndex - 1].innerHTML;
182+
}, selector);
183+
}); // it insert at before with insert into
184+
161185
it("singleton (true)", function(done) {
162186
// Setup
163187
styleLoaderOptions.singleton = true;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy