-
-
Notifications
You must be signed in to change notification settings - Fork 470
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
Add insertInto option #135
Conversation
Is there any news on this PR ? Thanks for your work ! |
@geekuillaume The "Details" link dosn't work, says "There is no CLA to sign for webpack/style-loader". Same is true when I change |
Can anyone sign this? Really am looking forward to this being merged in. |
Signing the CLA worked now, so if there's nothing I need to change, this should be good to merge now. |
So is there anyone reviewing PRs? |
addStyles.js
Outdated
@@ -13,8 +13,16 @@ var stylesInDom = {}, | |||
isOldIE = memoize(function() { | |||
return /msie [6-9]\b/.test(window.navigator.userAgent.toLowerCase()); | |||
}), | |||
getHeadElement = memoize(function () { | |||
return document.head || document.getElementsByTagName("head")[0]; | |||
getStyleTarget = (function(fn) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getStyleTarget
=> getElement/getStyle
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to add tests for this?
This is very similar to #65. Whichever is merged first most likely to close the other too. |
@bebraw I'm looking at testing, but the existing test utils seem to be built around this feature not existing, by comparing only the contents of the |
@ekulabuhov Any idea on what would be a good way to test the change? |
@ahstro I think you might need to replace that document.head reference with the querySelector. Add path as the last parameter to runCompilerTest and default it to head. Come to think about it, I'm not sure if querySelector can query Shadow DOM. Ping me if you have any troubles adding this. |
@ekulabuhov We're using my fork for work to query a ShadowDOM, so it works, you just need some fancy query. I don't have access to the computer where I started work on testing yesterday, so I can't fix it right now, but I'll try to get to it later today. |
For Merge |
README.md
Outdated
@@ -47,9 +47,12 @@ Note: Behavior is undefined when `unuse`/`unref` is called more often than `use` | |||
|
|||
### Options | |||
|
|||
#### `insertInto` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please put it directly below the insertAt
section
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On it :)
return memo[selector] | ||
}; | ||
})(function (styleTarget) { | ||
return document.querySelector(styleTarget) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a fallback here, I guess 😛 ?
Pseudo Fallback
if (!document.querySelector) {
if (selector.match('#')) return document.getElementById(selector)
if (selector.match('.')) return document.getElementByClassName(selector)
return document.getElementByTagName(selector)
} else {
return document.querySelector(selector)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really? According to Can I Use, the support for querySelector in newer browsers is better than that of getElementsByClassName, the latter not having any support for IE8 while the former has partial support.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forget what I said then :D
Also please take a look into #126 adding it to useable aswell 🙃 |
@michael-ciniawsky did you mean #126 in your last comment? 😀 I like the idea of merging |
I do like the idea of merging them, but if I understood it correctly, that's out of scope for this PR, right? |
@michael-ciniawsky No problem :) Did you see the comment I left on your line comment regarding the fallback? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ahstro Finally a test would be 💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding tests would be ideal.
Closes #44 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes to the feature & it's implementation.
It won't merge until it's properly tested
Closes #131 |
Add another option - called `insertInto` - which allows specifying a CSS selector - e.g. `#styles` - to be used as an argument to `document.querySelector` to find an element into which to insert the generated `<style>`-tags. Will default to the `<head>`-tag if undefined.
Okay, added tests :) I kept it a separate commit so I can easily change anything if it's not satisfactory; let me know if I should squash them. |
Changed default parameter syntax because Travis complained |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ahstro Thx
How to use this feature? I want insert into multiple iframe. |
Add another option called
insertInto
- which allows specifying a CSS selector - e.g.#styles
- to be used as an argument todocument.querySelector
to find an element into which to insert the generated<style>
-tags. Will default to the<head>
-tag if undefined.This was created because I wanted to insert
<style>
tags into a ShadowRoot (like in #131), but this is not a specific solution for that, but does solve it by allowing people to first create a ShadowRoot and then target it usinginsertInto
and the appropriate selector.Not att all sure if this is wanted, but it exists, so there ya go :)
Also, I replaced the
getHeadElement
function with a custom memoized function rather than add the key feature to the existing memoizing function, not sure if that was the better choice, but it felt like it.Closes #131