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

Add insertInto option #135

Merged
merged 2 commits into from
Mar 17, 2017
Merged

Conversation

ahstro
Copy link
Contributor

@ahstro ahstro commented Jul 13, 2016

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.

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 using insertInto 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

@geekuillaume
Copy link

Is there any news on this PR ?
@ahstro you need to accept the CLA to merge this.

Thanks for your work !

@ahstro
Copy link
Contributor Author

ahstro commented Feb 14, 2017

@geekuillaume The "Details" link dosn't work, says "There is no CLA to sign for webpack/style-loader". Same is true when I change webpack in the URL to webpack-contrib.

@rustydb
Copy link

rustydb commented Feb 22, 2017

Can anyone sign this? Really am looking forward to this being merged in.

@ahstro
Copy link
Contributor Author

ahstro commented Feb 22, 2017

Signing the CLA worked now, so if there's nothing I need to change, this should be good to merge now.

@DanielBadan
Copy link

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getStyleTarget => getElement/getStyle

Copy link
Contributor

@bebraw bebraw left a 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?

@ekulabuhov
Copy link
Member

ekulabuhov commented Mar 6, 2017

This is very similar to #65. Whichever is merged first most likely to close the other too.

@ahstro
Copy link
Contributor Author

ahstro commented Mar 6, 2017

@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 <head> tag with the expected result. The solution I came up with is comparing the entire jsdom document source with a string gotten from a new method on utils that inserts the expected style output at some index in the jsdomHtml, but that might be a really bad way of doing things and I'm tired, so I'm not going to work more on that now. Hope you or someone approves or, preferably, has a better idea soon. :)

@bebraw
Copy link
Contributor

bebraw commented Mar 6, 2017

@ekulabuhov Any idea on what would be a good way to test the change?

@ekulabuhov
Copy link
Member

@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.

@ahstro
Copy link
Contributor Author

ahstro commented Mar 7, 2017

@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.

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Mar 13, 2017

For v1.0.0 I'm currently drafting a few refactors for discussion (will open a detailed issue later :D) and one of the things I would like to change is e.g

Merge insertAt/insertTo/... => selector: 'head' || 'body' || '#el' || '.el' (use querySelector + fallback, default selector is 'head') like in this PR

README.md Outdated
@@ -47,9 +47,12 @@ Note: Behavior is undefined when `unuse`/`unref` is called more often than `use`

### Options

#### `insertInto`
Copy link
Member

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

Copy link
Contributor Author

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)
Copy link
Member

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)
}

Copy link
Contributor Author

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.

Copy link
Member

@michael-ciniawsky michael-ciniawsky Mar 13, 2017

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

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Mar 13, 2017

Also please take a look into #126 adding it to useable aswell 🙃

@ekulabuhov
Copy link
Member

@michael-ciniawsky did you mean #126 in your last comment? 😀

I like the idea of merging insertAt and insertTo 👍

@ahstro
Copy link
Contributor Author

ahstro commented Mar 13, 2017

I do like the idea of merging them, but if I understood it correctly, that's out of scope for this PR, right?

@ahstro
Copy link
Contributor Author

ahstro commented Mar 13, 2017

@michael-ciniawsky No problem :) Did you see the comment I left on your line comment regarding the fallback?

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a 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 💯

Copy link
Contributor

@bebraw bebraw left a 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.

@michael-ciniawsky
Copy link
Member

Closes #44

Copy link
Member

@joshwiens joshwiens left a 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

@michael-ciniawsky
Copy link
Member

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.
@ahstro
Copy link
Contributor Author

ahstro commented Mar 17, 2017

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.

@ahstro
Copy link
Contributor Author

ahstro commented Mar 17, 2017

Changed default parameter syntax because Travis complained

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahstro Thx

@jinyang1994
Copy link

How to use this feature? I want insert into multiple iframe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants