Reading List
The most recent articles from a list of feeds I subscribe to.
Create complex RegExps more easily
When I was writing my linear-gradient() to -webkit-gradient() converter, I knew in advance that I would have to use a quite large regular expression to validate and parse the input. Such a regex would be incredibly hard to read and fix potential issues, so I tried to find a way to cut the process down in reusable parts.
Turns out JavaScript regular expression objects have a .source property that can be used in the RegExp constructor to create a new RegExp out of another one. So I wrote a new function that takes a string with identifiers for regexp replacements in and replaces them with the corresponding sub-regexps, taken from an object literal as a second argument:
/**
* Create complex regexps in an easy-to-read way
* @param str {String} Final regex with for replacements
* @param replacements {Object} Object with the replacements
* @param flags {String} Just like the flags argument in the RegExp constructor
*/
RegExp.create = function(str, replacements, flags) {
for(var id in replacements) {
var replacement = replacements\[id\],
idRegExp = RegExp(' + id + ', 'gi');
if(replacement.source) {
replacement = replacement.source.replace(/^\\^|\\$$/g, '');
}
// Don't add extra parentheses if they already exist
str = str.replace(RegExp('\\\\(' + idRegExp.source + '\\\\)', 'gi'), '(' + replacement + ')');
str = str.replace(idRegExp, '(?:' + replacement + ')');
}
return RegExp(str, flags);
};
If you don’t like adding a function to the RegExp object, you can name it however you want. Here’s how I used it for my linear-gradient() parser:
self.regex = {};
self.regex.number = /^-?\[0-9\]\*\\.?\[0-9\]+$/;
self.regex.keyword = /^(?:top\\s+|bottom\\s+)?(?:right|left)|(?:right\\s+|left\\s+)?(?:top|bottom)$/;
self.regex.direction = RegExp.create('^(?:|deg|0)$', {
keyword: self.regex.keyword,
number: self.regex.number
});
self.regex.color = RegExp.create('(?:||)', {
keyword: /^(?:red|tan|grey|gray|lime|navy|blue|teal|aqua|cyan|gold|peru|pink|plum|snow|\[a-z\]{5,20})$/,
func: RegExp.create('^(?:rgb|hsl)a?\\\\((?:\\\\s\*%?\\\\s\*,?\\\\s\*){3,4}\\\\)$', {
number: self.regex.number
}),
hex: /^#(?:\[0-9a-f\]{1,2}){3}$/
});
self.regex.percentage = RegExp.create('^(?:%|0)$', {
number: self.regex.number
});
self.regex.length = RegExp.create('|0', {
number: self.regex.number,
unit: /%|px|mm|cm|in|em|rem|en|ex|ch|vm|vw|vh/
});
self.regex.colorStop = RegExp.create('\\\\s\*?', {
color: self.regex.color,
length: self.regex.length
}, 'g');
self.regex.linearGradient = RegExp.create('^linear-gradient\\\\(\\\\s\*(?:()\\\\s\*,)?\\\\s\*(\\\\s\*(?:,\\\\s\*\\\\s\*)+)\\\\)$', {
direction: self.regex.direction,
colorStop: self.regex.colorStop
}, 'i');
(self in this case was a local variable, not the window object)
Convert standard gradient syntax to -webkit-gradient and others
I hate -webkit-gradient() with a passion. Its syntax is cumbersome and it’s really limited: No angle support, no <length>s in color stop positions, no implied color stop positions, no elliptical gradients… So, I was really happy, when Webkit implemented the standard syntax this January. However, we’re still stuck with the horrid -webkit-gradient() for quite a while, since older Webkit browsers that don’t support it are widely used at this time.
Today, I decided to finally spare myself the hassle of converting my standard gradient syntax to -webkit-gradient() by hand. Tasks like that shouldn’t be handled by a human. So, I coded a little script to do the chore. Hope it helps you too: View demo
It currently only supports linear gradients, but I plan to add radial ones in the future. Also, when I get around to cleaning up the code a bit, I’ll add it on Github.
(Hope I didn’t leave in any very stupid bug, it’s really late here and I’m half asleep.)
Convert standard gradient syntax to -webkit-gradient and others
I hate -webkit-gradient() with a passion. Its syntax is cumbersome and it’s really limited: No angle support, no <length>s in color stop positions, no implied color stop positions, no elliptical gradients… So, I was really happy, when Webkit implemented the standard syntax this January. However, we’re still stuck with the horrid -webkit-gradient() for quite a while, since older Webkit browsers that don’t support it are widely used at this time.
Today, I decided to finally spare myself the hassle of converting my standard gradient syntax to -webkit-gradient() by hand. Tasks like that shouldn’t be handled by a human. So, I coded a little script to do the chore. Hope it helps you too: View demo
It currently only supports linear gradients, but I plan to add radial ones in the future. Also, when I get around to cleaning up the code a bit, I’ll add it on Github.
(Hope I didn’t leave in any very stupid bug, it’s really late here and I’m half asleep.)
Beveled corners & negative border-radius with CSS3 gradients
Just found out how to do beveled corners and simulate negative border radius without images, by utilizing CSS gradients once again. It’s amazing how many CSS problems can be solved with gradients alone. Read the text in the dabblet below to find out how (or just check the code):
It also falls back to a solid color background if CSS gradients are not supported. It will work on Firefox 3.6+, Chrome, Safari, Opera 11.10+ and IE10+.
PS: For my twitter friends, I had already written this when the robbers came and I was about to post it. I might have been really calm, but not as much as making CSS experiments the same day I was robbed and threatened by a gun :P
Beveled corners & negative border-radius with CSS3 gradients
Just found out how to do beveled corners and simulate negative border radius without images, by utilizing CSS gradients once again. It’s amazing how many CSS problems can be solved with gradients alone. Read the text in the dabblet below to find out how (or just check the code):
It also falls back to a solid color background if CSS gradients are not supported. It will work on Firefox 3.6+, Chrome, Safari, Opera 11.10+ and IE10+.
PS: For my twitter friends, I had already written this when the robbers came and I was about to post it. I might have been really calm, but not as much as making CSS experiments the same day I was robbed and threatened by a gun :P