Simple Way to Validate CSS Colors
function isColor(col) {
const cache = isColor[col]
if (cache != null) {
console.log('- reading cache')
return cache
}
isColor.el.style = ''
isColor.el.style.color = col
return isColor[col] = !!isColor.el.style.color
}
isColor.el = document.createElement('div')
console.log('is "red" a color?', isColor('red'))
console.log('from the cache: ', isColor('red'))
console.log('is "rgbx(1, 2, 3)" a color?', isColor('rgbx(1, 2, 3)'))
console.log('is "#0f0" a color?', isColor('#0f0'))
console.log('is "hsl(192, 50%, 50%)" a color?', isColor('hsl(192, 50%, 50%)'))
console.log('from the cache: ', isColor('hsl(192, 50%, 50%)'))
console.log('is "lab(2000.1337% -8.6911 -159.131231 / .987189732)" ?',
isColor('lab(2000.1337% -8.6911 -159.131231 / .987189732)'))
console.log('is "snippetZone" ?', isColor('snippetZone'))
I find this technique is usually good enough to validate colors…
Mutation Observer
// Select the node that will be observed for mutations
const targetNode = document.getElementById('some-id');
// Options for the observer (which mutations to observe)
const config = { attributes: true, childList: true, subtree: true };
// Callback function to execute when mutations are observed
const callback = function(mutationsList, observer) {
// Use traditional 'for loops' for IE 11 (goodbye IE11!!!!)
for(const mutation of mutationsList) {
if (mutation.type === 'childList') {
console.log('A child node has been added or removed.');
}
else if (mutation.type === 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
}
};
// Create an observer instance linked to the callback function
const observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);
// Later, you can stop observing
observer.disconnect();
This is pure gold if you haven’t used it… (from MDN)
W3Schools Output Tag
<!-- from w3schools.com -->
<!DOCTYPE html>
<html>
<body>
<h1>The output element</h1>
<form oninput="x.value=parseInt(a.value)+parseInt(b.value)">
<input type="range" id="a" value="50">
+<input type="number" id="b" value="25">
=<output name="x" for="a b"></output>
</form>
<p><strong>Note:</strong> The output element is not supported in Edge 12 (or earlier).</p>
</body>
</html>
I like w3Schools.
This code has some problems… but… for a cool little snippet to play with – I think that’s ok. SnippetZone certainly has tons of things like this…