Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insertInto and insertAt collaboration bug fixed #325

Merged
merged 1 commit into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/addStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ var isOldIE = memoize(function () {
return window && document && document.all && !window.atob;
});

var getTarget = function (target) {
var getTarget = function (target, parent) {
if (parent){
return parent.querySelector(target);
}
return document.querySelector(target);
};

var getElement = (function (fn) {
var memo = {};

return function(target) {
return function(target, parent) {
// If passing function in options, then use it for resolve "head" element.
// Useful for Shadow Root style i.e
// {
Expand All @@ -40,7 +43,7 @@ var getElement = (function (fn) {
return target();
}
if (typeof memo[target] === "undefined") {
var styleTarget = getTarget.call(this, target);
var styleTarget = getTarget.call(this, target, parent);
// Special case to return head of iframe instead of iframe itself
if (window.HTMLIFrameElement && styleTarget instanceof window.HTMLIFrameElement) {
try {
Expand Down Expand Up @@ -181,7 +184,7 @@ function insertStyleElement (options, style) {
} else if (options.insertAt === "bottom") {
target.appendChild(style);
} else if (typeof options.insertAt === "object" && options.insertAt.before) {
var nextSibling = getElement(options.insertInto + " " + options.insertAt.before);
var nextSibling = getElement(options.insertAt.before, target);
target.insertBefore(style, nextSibling);
} else {
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");
Expand Down
26 changes: 25 additions & 1 deletion test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("basic tests", function() {
rootDir = path.resolve(__dirname + "/../") + "/",
jsdomHtml = [
"<html>",
"<head>",
"<head id='head'>",
existingStyle,
"</head>",
"<body>",
Expand Down Expand Up @@ -158,6 +158,30 @@ describe("basic tests", function() {
}, selector);
});

it("insert at before with insert into custom element by function", function(done) {
const selector = "#head";
styleLoaderOptions.insertInto = () => document.querySelector("#head");

styleLoaderOptions.insertAt = {
before: "#existing-style"
};

let expected = requiredCss;

runCompilerTest(expected, done, function() {
let head = this.document.querySelector(selector);
let existingStyleIndex;
for (let i = 0; i < head.children.length; i++){
let html = `<style id="existing-style">${head.children[i].innerHTML}</style>`;
if(html === existingStyle){
existingStyleIndex = i;
break;
}
}
return head.children[existingStyleIndex - 1].innerHTML;
}, selector);
}); // it insert at before with insert into

it("singleton (true)", function(done) {
// Setup
styleLoaderOptions.singleton = true;
Expand Down