-
-
Notifications
You must be signed in to change notification settings - Fork 72
Add a check for undefined val for parseColor #190
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
base: main
Are you sure you want to change the base?
Conversation
Can you explain why you are passing undefined to this function in the first place? |
I'm not - Stencil JS is. When running test for web components built with Stencil that line under some circumstances causes After looking both cssColor and Stencil, I feel cssStlyle is the right place to fix this. |
It's probably wrong to treat See the test case below. Note that when So the right approach might be to add a try-catch around try {
var res = resolveColor(val, {
format: 'specifiedValue',
});
if (res) {
return res;
}
} catch (e) {
// Return the previous value somehow
}
return undefined; But I'm not sure if we are caching values somewhere. Test case: <!DOCTYPE html>
<html>
<style>
.div {
background-color: red;
width: 50%;
height: 3rem;
}
</style>
<div id="div" class="div"></div>
<script>
const declaration = document.styleSheets[0].cssRules[0].style;
let declarationValue = declaration.getPropertyValue('background-color');
console.log(`
declarationValue: ${declarationValue}
`);
declaration.setProperty('background-color', 'green');
declarationValue = declaration.getPropertyValue('background-color');
console.log(`
declaration.setProperty('background-color', 'green');
declarationValue: ${declarationValue}
`);
declaration.setProperty('background-color', undefined);
declarationValue = declaration.getPropertyValue('background-color');
console.log(`
declaration.setProperty('background-color', undefined);
declarationValue: ${declarationValue}
`);
const div = document.getElementById('div');
let propertyValue = getComputedStyle(div).getPropertyValue('background-color');
let stylePropertyValue = div.style.getPropertyValue('background-color');
console.log(`
default value:
propertyValue: ${propertyValue}
stylePropertyValue: ${stylePropertyValue}, is empty string: ${stylePropertyValue === ''}
`);
div.style.setProperty('background-color', 'blue');
propertyValue = getComputedStyle(div).getPropertyValue('background-color');
stylePropertyValue = div.style.getPropertyValue('background-color');
console.log(`
div.style.setProperty('background-color', 'blue');
propertyValue: ${propertyValue}
stylePropertyValue: ${stylePropertyValue}
`);
div.style.setProperty('background-color', undefined);
propertyValue = getComputedStyle(div).getPropertyValue('background-color');
stylePropertyValue = div.style.getPropertyValue('background-color');
console.log(`
div.style.setProperty('background-color', undefined);
propertyValue: ${propertyValue}
stylePropertyValue: ${stylePropertyValue}
`);
div.style.setProperty('background-color', '');
propertyValue = getComputedStyle(div).getPropertyValue('background-color');
stylePropertyValue = div.style.getPropertyValue('background-color');
console.log(`
div.style.setProperty('background-color', '');
propertyValue: ${propertyValue}
stylePropertyValue: ${stylePropertyValue}, is empty string: ${stylePropertyValue === ''}
`);
div.style.backgroundColor = 'rebeccapurple';
propertyValue = getComputedStyle(div).getPropertyValue('background-color');
stylePropertyValue = div.style.getPropertyValue('background-color');
console.log(`
div.style.backgroundColor = 'rebeccapurple';
propertyValue: ${propertyValue}
stylePropertyValue: ${stylePropertyValue}
`);
div.style.backgroundColor = undefined;
propertyValue = getComputedStyle(div).getPropertyValue('background-color');
stylePropertyValue = div.style.getPropertyValue('background-color');
console.log(`
div.style.backgroundColor = undefined;
propertyValue: ${propertyValue}
stylePropertyValue: ${stylePropertyValue}
`);
div.style.backgroundColor = null;
propertyValue = getComputedStyle(div).getPropertyValue('background-color');
stylePropertyValue = div.style.getPropertyValue('background-color');
console.log(`
div.style.backgroundColor = null;
propertyValue: ${propertyValue}
stylePropertyValue: ${stylePropertyValue}, is empty string: ${stylePropertyValue === ''}
`);
</script>
</html> Results in browser: declarationValue: red
declaration.setProperty('background-color', 'green');
declarationValue: green
declaration.setProperty('background-color', undefined);
declarationValue: green
default value:
propertyValue: rgb(0, 128, 0)
stylePropertyValue: , is empty string: true
div.style.setProperty('background-color', 'blue');
propertyValue: rgb(0, 0, 255)
stylePropertyValue: blue
div.style.setProperty('background-color', undefined);
propertyValue: rgb(0, 0, 255)
stylePropertyValue: blue
div.style.setProperty('background-color', '');
propertyValue: rgb(0, 128, 0)
stylePropertyValue: , is empty string: true
div.style.backgroundColor = 'rebeccapurple';
propertyValue: rgb(102, 51, 153)
stylePropertyValue: rebeccapurple
div.style.backgroundColor = undefined;
propertyValue: rgb(102, 51, 153)
stylePropertyValue: rebeccapurple
div.style.backgroundColor = null;
propertyValue: rgb(0, 128, 0)
stylePropertyValue: , is empty string: true |
I've looked into it a bit deeper and it seems there's nothing wrong with returning undefined early. |
I guess I am not excited to merge this until we can get a test in the upstream jsdom repository that this solves. I also wonder if this is just one instance of #129. |
Test summary: #196 (comment) |
Fixes issue #189