(function() {
var firstLetterRules = [];
var regex = /::?first-letter/i;
//Chrome only
var matches = document.createElement("div").webkitMatchesSelector;
//1. Get a list of all CSS blocks
var loadRules = function() {
var stylesheets = document.styleSheets;
for (var i = 0; i < stylesheets.length; i++) {
var stylesheet = stylesheets[i];
var rules = stylesheet.cssRules;
for (var k = 0; k < rules.length; k++) {
var rule = rules[k];
//2. Filter out CSS blocks which do not contain `:first-letter` in the block's selector
if (regex.test(rule.selectorText)) {
firstLetterRules.push(rule);
}
}
}
};
window.hasFirstLetterStyle = function(element) {
/*3. Iterate over the list of remaining CSS blocks, and run matchesSelector()with the target element as a receiver and the current CSS block's selector as the argument.*/
for( var i = 0, len = firstLetterRules.length; i < len; ++i ) {
var rule = firstLetterRules[i],
text = rule.selectorText.replace( regex, "" );
if( !text.trim() ) text = "*";
//If `matchesSelector()` returns `true`, then the current CSS block's rules affect the target element's first letter.
if( matches.call( element, text ) ) {
return true;
}
}
return false;
};
loadRules();
})();
$('p').each(function(){
if( hasFirstLetterStyle( this ) ) {
console.log( this, true );
}
else {
console.log( this, false );
}
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.