Add setting CSS property with important priority#189
Add setting CSS property with important priority#189maximzasorin wants to merge 5 commits intowebpro:masterfrom
Conversation
|
@webpro Please comment on PR. |
|
@maximzasorin My sincere apologies, I've totally missed it! Greenkeeper was polluting this space with PRs. Looking into it right now. |
webpro
left a comment
There was a problem hiding this comment.
I like that we're using getPropertyValue now 👍
| } | ||
| if(styleProps[prop] || styleProps[prop] === 0) { | ||
| element.style[prop] = styleProps[prop]; | ||
| element.style.setProperty(prop, styleProps[prop], important); |
There was a problem hiding this comment.
Let's optimize by doing something like this (not tested):
const important = /!important$/.test(styleProps[prop]) ? 'important' : undefined;
element.style.setProperty(prop, styleProps[prop].replace(/!important$/, ''), important);
Then the logic above it isn't executed if we're removing a property.
|
Btw, why don't we use this API: Makes implementation faster and simpler. How does jQuery do this? |
What will the API look like when setting multiple properties?
As far as I know jQuery does not support important properties via css method. From docs:
But they have an issue: jquery/jquery#3713 |
|
@webpro Added fix. |
| const important = /!important$/.test(styleProps[prop]) ? 'important' : undefined; | ||
| if(typeof styleProps[prop] === 'string') { | ||
| styleProps[prop] = styleProps[prop].replace(/\s*!important$/, ''); | ||
| } |
There was a problem hiding this comment.
Sorry for being picky, how about this (one line and slightly better readable?):
styleProps[prop] = important ? styleProps[prop].replace(/\s*!important$/, '') : styleProps[prop];
| assert(actualValue[0] === expected[0]); | ||
| assert(actualPriority[0] === 'important'); | ||
| assert(actualValue[1] === expected[1]); | ||
| assert(actualPriority[1] === ''); |
There was a problem hiding this comment.
Thanks for adding the tests, super useful! I know I have this actual/expected style everywhere, but could you please consider this for conciseness here:
assert(element[0].style.getPropertyValue('font-weight') === expected[0]);
assert(element[0].style.getPropertyPriority('font-weight') === 'important');
assert(element[0].style.getPropertyValue('font-style') === expected[1]);
assert(element[0].style.getPropertyPriority('font-style') === '');
| var element = $('#testFragment')[0]; | ||
| var actualValue = element.style.getPropertyValue('padding-right'); | ||
| var actualPriority = element.style.getPropertyPriority('padding-right'); | ||
| console.log('actual/expected', actualValue, expected); |
|
When sending the tests to BrowserStack, the CSS tests fail in IE and Edge:
Without any useful error/trace ( |
I have none of this, but I debugged it in Edge on BrowserStack and found an error. It looks like Edge otherwise processes the value
So I replaced the default value for the important argument from |
|
I tested the code in IE 11 and found a problem in the work of the If we already have a property with priority = important: element[0].style.setProperty('font-size', '15px', 'important')And then try to set the same property without priority: element[0].style.setProperty('font-size', '20px', '')That in Chrome, Firefox, Safari and Edge we get the value=20px and priority='', but in IE we get the value=15px and priority='important'. It turns out that the The only way out I see is to remove the property before setting a new value, something like is: if (!important) {
element[0].style.removeProperty('font-size')
}
element[0].style.setProperty('font-size', '20px', '')What do you think about it? |
|
Apologies for my late response. First of all, thanks a lot for putting in all the efforts and test it in IE11! Overall, by now I think the required changes/maintenance to support a bad practice (usage of |
|
Thanks for the comment. |
Hello, I added the ability to set properties with an important priority. No we can use it as follows:
In the tests, I did not use
getComputedStylebecause of an error in jsdom, for which inline styles are always in priority, see: jsdom/jsdom#2485#169