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 support for custom tag attribute #155

Closed
wants to merge 0 commits into from

Conversation

felixmosh
Copy link
Contributor

@felixmosh felixmosh commented Dec 10, 2016

What kind of change does this PR introduce?
feature, allows to add additional attributes on the style / link element that the loader creates

Did you add tests for your changes?
No, tests
If relevant, did you update the README?
Update README
Summary

This PR allows to add id attribute or any custom attribute, in my case i have runtime css style processor that relies on custom attribute on the style tag that it should process.

Does this PR introduce a breaking change?
no breaking changes

Other information

Fixes #147

@jsf-clabot
Copy link

jsf-clabot commented Dec 10, 2016

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 4 committers have signed the CLA.

✅ d3viant0ne
❌ felixmosh
❌ blainekasten
❌ TheLarkInn

@felixmosh
Copy link
Contributor Author

felixmosh commented Dec 11, 2016

@SpaceK33z can you review it?

@felixmosh
Copy link
Contributor Author

someone is alive here?

addStyles.js Outdated
@@ -26,6 +26,8 @@ module.exports = function(list, options) {
}

options = options || {};
options.tagAttrs = typeof options.tagAttrs === "object"? options.tagAttrs : {};
Copy link
Member

Choose a reason for hiding this comment

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

spacing

@felixmosh
Copy link
Contributor Author

Done, thanx

@sokra
Copy link
Member

sokra commented Dec 23, 2016

the spacing should be tabs instead of spaces

@felixmosh
Copy link
Contributor Author

Fixed the spacing

README.md Outdated
@@ -61,6 +61,10 @@ By default, the style-loader appends `<style>` elements to the end of the `<head

If defined, the style-loader will re-use a single `<style>` element, instead of adding/removing individual elements for each required module. **Note:** this option is on by default in IE9, which has strict limitations on the number of style tags allowed on a page. You can enable or disable it with the singleton query parameter (`?singleton` or `?-singleton`).

#### `tagAttrs`

If defined, style-loader will attach given attributes with there values on `<style>` / `<link>` element.
Copy link
Contributor

Choose a reason for hiding this comment

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

there -> their

Copy link
Contributor

@SpaceK33z SpaceK33z Dec 23, 2016

Choose a reason for hiding this comment

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

Can you also add what this option expects? Is it an array or object? And what do I exactly need to define in that? A simple example would already help a lot.

@felixmosh
Copy link
Contributor Author

All done...

@gruberjl
Copy link

gruberjl commented Jan 9, 2017

This is exactly what I needed, thanks.

FYI: The docs say require('style-loader?{tagAttrs:{id: 'style-tag-id'}}!style.scss');. I think style-tag-id should be wrapped in double quotes. It did work what I used the options syntax in Webpack 2.

{
    loader: 'style-loader',
    options: {
        singleton: true,
        tagAttrs: {'amp-custom': ''}
    }
}

@felixmosh
Copy link
Contributor Author

felixmosh commented Jan 9, 2017

@gruberjl, you are right, fixed it.
Thanx.

This is exactly what i needed also, i`m waiting for the owner of the proj to approve it :\

@felixmosh
Copy link
Contributor Author

Someone?

@joshwiens
Copy link
Member

joshwiens commented Jan 19, 2017

@felixmosh - Give me a minute, I'll see if I can find someone though everyone is still pretty busy with the 2.2.0 final release of Webpack.

addStyles.js Outdated
linkElement.rel = "stylesheet";
options.tagAttrs.rel = "stylesheet";

attachTagAttrs(styleElement, options.tagAttrs);
insertStyleElement(options, linkElement);
return linkElement;
}

Copy link
Member

Choose a reason for hiding this comment

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

It might not hurt to throw a quick // document this function in there given it's an optional feature.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, i didn`t understood, what does it means "to throw a quick // document this function"

@dsebastien
Copy link

This will be very nice to have as it should allow specifying a nonce to be used with a content security policy (CSP).

README.md Outdated
@@ -61,7 +72,17 @@ By default, the style-loader appends `<style>` elements to the end of the `<head

If defined, the style-loader will re-use a single `<style>` element, instead of adding/removing individual elements for each required module. **Note:** this option is on by default in IE9, which has strict limitations on the number of style tags allowed on a page. You can enable or disable it with the singleton query parameter (`?singleton` or `?-singleton`).

## Recommended configuration
#### `tagAttrs`
Copy link
Member

Choose a reason for hiding this comment

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

tagAttrs => Attrs/Attributes

addStyles.js Outdated
@@ -26,6 +26,8 @@ module.exports = function(list, options) {
}

options = options || {};
options.tagAttrs = typeof options.tagAttrs === "object" ? options.tagAttrs : {};
Copy link
Member

Choose a reason for hiding this comment

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

tagAttrs => attrs/attributes

addStyles.js Outdated
insertStyleElement(options, linkElement);
return linkElement;
}

function attachTagAttrs(element, attrs) {
for(var key in attrs) {
Copy link
Member

Choose a reason for hiding this comment

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

options.attrs.forEach((attr) => element.setAttribute(attr, attrs[attr]));

Copy link
Contributor Author

@felixmosh felixmosh Mar 6, 2017

Choose a reason for hiding this comment

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

options.attrs is not an array but an key-value pair (object).
So forEach won't work.
I will change it to
Object.keys(options.attrs).forEach...

And because of the fact that this code gonna run on the browser, i prefer to omit the arrow func.

@michael-ciniawsky
Copy link
Member

Related #119

@joshwiens
Copy link
Member

So I hate to tell you this but it looks like there was an issue updating this PR around Jan 9th ( oops on a rebase maybe ).

Point being, the CLA assistant is currently showing 4 contributors to this PR which isn't going to work.

Simplest ( and probably only ) way to fix this is to copy code changes to a new branch, Open a new PR, reference this one & then close it. We will finish this and get it merged as soon as we have a pull request that doesn't have the CLA issues.

@felixmosh
Copy link
Contributor Author

So should i create a PR from scratch?

@joshwiens
Copy link
Member

To prevent issues, it just needs to be a new branch. Everything ( all the changes from this PR ) can be committed in one hash.

We will reference this PR from the new one, so none of the review comments will be lost.

@michael-ciniawsky michael-ciniawsky removed this from Feature in Dashboard Mar 6, 2017
@madjam002
Copy link

@dsebastien The way that custom tag attributes have been implemented here is not suitable for implementing CSP nonce attributes. A nonce by definition is a secure random string that can only be used once [1], and by hard coding a nonce in your webpack config goes against this. By using the same nonce over and over again, malicious entities know what the nonce will be every time which defeats the point of CSP in the first place.

[1] https://en.wikipedia.org/wiki/Cryptographic_nonce

@felixmosh
Copy link
Contributor Author

@madjam002, looks like the custom attribute feature is not suitable for that job, pls don't use it for that.

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.

load css with media=all attribute
9 participants