Invision Power Services: [Tool] CSS Specificity Calculator - Invision Power Services

Jump to content

Web Design and Coding

Our community chat forum areas are for off-topic discussion only. Please do not post topics about IPS or its products and services here. We provide other forum areas for IPS discussion.

Do you need technical support on IPS products or services?

You can obtain support via the client area, or you can try to obtain peer-to-peer support at IPS Resources.

Did you find a bug in one of our products?

If you believe you've found a bug please post it to the bug tracker.

Have a suggestion or feedback?

Use the company feedback forum or appropriate product feedback forum. You can also submit a ticket in your client area if it's a private matter.
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[Tool] CSS Specificity Calculator Rate Topic: -----

#1 User is offline   Stephen Icon

  • Meet Jay
  • PipPipPipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 18,141
  • Joined: 22-March 02
  • Location:London, England

Posted 29 June 2005 - 01:04 PM

I made a CSS specificity calculator today because I was bored (its fairly pointless but so what ;)). Just thought I'd post the URL so people can play around with it

http://www.rebelinbl...specificity.php

Find any problems just let me know.

For those who don't know CSS specificity is how the browser knows which rules to apply when multiple rules conflict, the selector with the higher score gets used
Posted Image Posted Image
0

#2 User is offline   Michael K. Icon

  • Advanced Member
  • PipPipPipPip
  • View blog
  • Group: Members
  • Posts: 424
  • Joined: 31-August 03

Posted 29 June 2005 - 01:23 PM

Ooh.. nice, not seeing any problems so far. :)
Just a question, but how do you measure specificity? Happen to have a reference or something?
Michael
"When life gives you lemons, find someone with a papercut."
0

#3 User is offline   Stephen Icon

  • Meet Jay
  • PipPipPipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 18,141
  • Joined: 22-March 02
  • Location:London, England

Posted 29 June 2005 - 01:32 PM

The way it works is with an "abcd" formula (the way the W3C explains it, just replace the result of a, b, c, d below)

a) 1 if the selector is a "style" attribute otherwise 0
b) count the number of ID attributes in the selector
c) count the number of other attributes (classes and [attribute]) and pseudo-clases (:hover etc) in the selector
d) count the number of elements and pseudo-elements (:before etc) in the selector

The way I do it

Start at 0, add 1000 for style attribute, add 100 for each ID, add 10 for each attribute/pseudo-class, add 1 for each element name/pseudo-element


So "body #content .data img:hover" would be 121 (or 0121)

1 for body, 100 for #content, 10 for .data, 1 for img and 10 for :hover

So they both work out the same

You can see more info at http://www.w3.org/TR...tml#specificity
Posted Image Posted Image
0

#4 User is offline   _ Icon

  • Needs Serious Help
  • PipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 1,655
  • Joined: 09-December 03

Posted 29 June 2005 - 04:01 PM

Might want to set 'Properties' as 'position: fixed'...
Posted Image
(colours are based on your IP address and almost* unique)
(* meaning that two bytes are left unused, and there's always the chance of a collision)
0

#5 User is offline   Stephen Icon

  • Meet Jay
  • PipPipPipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 18,141
  • Joined: 22-March 02
  • Location:London, England

Posted 29 June 2005 - 04:04 PM

nah thats OK was just a quick knock up for something I needed today whilst at work, will sort out the html itself when I get home from work
Posted Image Posted Image
0

#6 User is offline   Michael K. Icon

  • Advanced Member
  • PipPipPipPip
  • View blog
  • Group: Members
  • Posts: 424
  • Joined: 31-August 03

Posted 29 June 2005 - 04:12 PM

View PostStephen, on Jun 29 2005, 06:32 AM, said:

The way it works is with an "abcd" formula (the way the W3C explains it, just replace the result of a, b, c, d below)

[..snip..]

You can see more info at http://www.w3.org/TR...tml#specificity

Ah, alright. Thanks. :)
And yeah, all it needs now (if you get bored again) is a little touching up really. Who ever said a generator couldn't look pretty? :P
Michael
"When life gives you lemons, find someone with a papercut."
0

#7 User is offline   Stephen Icon

  • Meet Jay
  • PipPipPipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 18,141
  • Joined: 22-March 02
  • Location:London, England

Posted 29 June 2005 - 04:18 PM

Found an error html>body #item returns 100, it must be more than that but I'm not sure what it would be, I assume 102
Posted Image Posted Image
0

#8 User is offline   ReloadedElement Icon

  • IPB Newbie
  • Pip
  • View blog
  • Group: Members
  • Posts: 3
  • Joined: 29-June 05
  • Location:United States

Posted 29 June 2005 - 05:48 PM

Thats a nice script you got there. Sure it will come in handy.
0

#9 User is offline   Brendon Koz Icon

  • Needs Hobby
  • PipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 4,741
  • Joined: 06-September 03
  • Location:Saratoga Springs, NY, USA

Posted 30 June 2005 - 04:40 PM

Aww, no source? :)
Posted Image
mysiteonline.org™
They say, "Practice makes perfect," yet they also say, "Nobody's perfect"... I don't get it.
0

#10 User is offline   Stephen Icon

  • Meet Jay
  • PipPipPipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 18,141
  • Joined: 22-March 02
  • Location:London, England

Posted 30 June 2005 - 05:13 PM

will add a view source option later, but trust me its ugly ;) Literally knocked it up because I wanted a quick way to find out the values of my CSS files without having to manually do it
Posted Image Posted Image
0

#11 User is offline   Stephen Icon

  • Meet Jay
  • PipPipPipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 18,141
  • Joined: 22-March 02
  • Location:London, England

Posted 30 June 2005 - 07:31 PM

<html>
<head>
	<title>CSS Specificity Calculator</title>
	<script type="text/javascript">
		function show(data) {
			window.document.getElementById('code').innerHTML = data;
		}

		function floatdetails() {
			var posY = 0;
			var netscape = (navigator.appName.indexOf("Netscape") != -1);
			
			function move(id) {
				var divpos = document.getElementById ? document.getElementById(id): document.all ? document.all[id] : document.layers[id];
			
				divpos.sP = function(y) {
					this.style.top = y + "px";
				};
				
				divpos.y = posY;
				return divpos;
			}
			
			window.floatdiv = function() {
				var pY = netscape ? pageYOffset : document.body.scrollTop;
				pos.y += (pY + posY - pos.y) / 8;
				pos.sP(pos.y);
				setTimeout("floatdiv()");
			}

			pos = move("code");
			floatdiv();
		}
</script>
</head>
<body>
<div align="center">
<h1>CSS specificity calculator</h1>
<?php

/*
 *
 * CSS specificity calculator
 * Written by Stephen Ball
 * [url="http://www.rebelinblue.com"]http://www.rebelinblue.com[/url]
 * --------------------------
 * If you improve on this script
 * please do let me know
 *
 */

function cleanup(&$data, $value) {
	$data = trim($data);
}

// Pseudo classes
$classes  = array(":link", ":visited", ":hover", ":active", ":focus", ":target", ":lang", ":enabled", 
                  ":disabled", ":checked", ":indeterminate", ":root", ":nth-child", ":nth-last-child",
                  ":nth-of-type", ":nth-last-of-type", ":first-child", ":last-child", ":first-of-type", 
                  ":last-of-type", ":only-child", ":only-of-type", ":empty", ":contains", ":not");
// Pseudo elements
$elements = array(":before", ":after", ":first-line", ":first-letter", ":selection");

// HTML tags
$tags     = array("a", "abbr", "acronym", "address", "applet", "area", "b", "base", "basefont", "bgsound",
                  "bdo", "big", "blink", "blockquote", "body", "br", "button", "caption", "center", "cite",
                  "code", "col", "colgroup", "comment", "dd", "del", "dfn", "dir", "div", "dl", "dt", "em",
                  "embed", "fieldset", "font", "form", "frame", "frameset", "h", "h1", "h2", "h3", "h4", "h5",
                  "h6", "head", "hr", "html", "i", "iframe", "img", "input", "ins", "isindex", "kbd", "label",
                  "legend", "li", "link", "listing", "map", "marquee", "menu", "meta", "multicol", "nextid",
                  "nobr", "noframes", "noscript", "object", "ol", "optgroup", "option", "p", "param", "plaintext",
                  "pre", "q", "s", "samp", "script", "select", "server", "small", "sound", "spacer", "span",
                  "strike", "strong", "style", "sub", "sup", "table", "tbody", "td", "textarea", "textflow",
                  "tfoot", "th", "thead", "title", "tr", "tt", "u", "ul", "var", "wbr", "xmp");

// Check if form has been submitted
if (isset($_POST["css"])) {
    // Strip slashes
	$_POST["css"] = get_magic_quotes_gpc() ? stripslashes($_POST["css"]) : $_POST["css"];

	$data = preg_replace("/\/\\*[\\s\\S]*?\\*\//", "", $_POST["css"]);  // Strip comments
	$data = preg_replace("/\{(.*?)\}/s", ", ", $data);				    // Strip content and replace with delimiter
	$data = str_replace(" ,", ",", $data);								// Strip spaces after selectors

	$tmp = explode(",", $data);                                         // Get rid of selector groupings
	array_walk($tmp, 'cleanup');                                        // Trim whitespace from end of selectors

	?>
	<table border="1" width="760" style="margin: auto;">
	<tr>
	<td>
	<table>
		<tr align="middle">
			<th width="40%">Selector</th>
			<th width="5%">Score</th>
			<th width="5%">Properties</th>
		</tr>
	<?php

    // Loop through the selector array
	foreach ($tmp as $var) {
		$score = 0;                                                     // Set the score to 0
		if (!empty($var)) {
			$score += substr_count($var, '#') * 100;	                // Count the IDs and multiple by 100 (i.e. #itemID)
			$score += substr_count($var, '.') * 10;		                // Count the classes and multiple by 10 (i.e. .classname)
			$score += substr_count($var, '[') * 10;		                // Count the attributes and multiple by 10 (i.e. input[type=text])
	
			foreach ($classes as $test) {				                // Count the pseudo-classes and multiple by 10
				$score += substr_count($var, $test) * 10;
			}

			foreach ($elements as $test) {				                // Count the pseudo-elements and multiple by 10
				$score += substr_count($var, $test);
			}

			$cleaned = str_replace(":", " ", $var);                     // Remove the pseudo selector
			$cleaned = str_replace("+", " ", $cleaned);                 // Remove the adjacent sibling selector
			$cleaned = str_replace(">", " ", $cleaned);                 // Remove the child selector
			$cleaned = str_replace("*", " ", $cleaned);                 // Remove the universal selector
			$cleaned = str_replace(".", " ", $cleaned);                 // Remove the class selector
			$cleaned = str_replace("#", " ", $cleaned);                 // Remove the ID selector

			$cleaned = explode(" ", $cleaned);                          // Explode on space
			foreach ($cleaned as $rubbish) {                            // Count the type selectors (i.e. HTML tags)
				if (in_array(strtolower($rubbish), $tags)) {
					$score++;
				}
			}

            // Now to find the actually properties
			$code = substr($_POST["css"], strpos($_POST["css"], $var)); // Find the position of the current selector
			$first = strpos($code, "{") + 1;                            // Get the position of the first {
			$last = strpos($code, "}");                                 // Get the position of the first }
			
            $code = substr($code, $first, $last - $first);              // Get the content between the { and }
			$code = preg_replace("/\/\\*[\\s\\S]*?\\*\//", "", $code);	// Strip any comments from the properties

            // Now make the data safe for javascript
			$code = trim($code);                                        // Trim the properties
			$code = str_replace("\\", "\\\\", $code);                   // Escape any slashes
			$code = str_replace("'", "\\'", $code);                     // Escape any apostrophes
			$code = str_replace("\r\n", "<br>", $code);                 // Convert new lines to HTML break (Can't use nl2br as it doesn't remove the newlines)
			$code = str_replace("\n", "<br>", $code);
   			$code = str_replace("\r", "<br>", $code);

			$score = str_pad($score, 3, '0', STR_PAD_LEFT);             // Pad the final score to 3 characters
			$defined = substr_count($code, ":");                        // Count the number of properties the selector defines

			?>
			<tr>
				<td onmouseover="show('<?=$code;?>');" onmouseout="show('');" style="cursor: pointer;"><?=$var;?></td>
				<td align="center"><?=$score;?></td>
				<td align="center"><?=$defined;?></td>
			</tr>
			<?php
		}
	}

	?>
				</table>
			</td>
			<td width='50%' valign='top' style="padding: 5px;">
				<center><strong>Property data</strong></center>
				<div id='code' style='position: relative;'></div>
				<script type='text/javascript'>floatdetails();</script>
			</td>
		</tr>
	</table>
	<p>Note: Inline styles using the HTML &quot;styles&quot; attribute have a score of 1000.</p>
	<p><a href="specificity.php">Start over</a></p>
	<?php
}
else {
	?>
	<p>Paste your CSS into the field below, the specificity of your selectors will be calculated for you.</p>
	<h2>Current limitations</h2>
	<ul>
		<li>Your CSS <b>must</b> <a href="http://jigsaw.w3.org/css-validator/" title="Ensure the file is valid CSS">be valid</a> in order for the calculator to work</li>
		<li>Currently only HTML elements are counted in the score, custom tags such as XML are not</li>
	</ul>
	<form method="post" action="specificity.php">
		<textarea name="css" rows="25" cols="90"></textarea>
		<br /><input type="submit" value="Calculate" />
	</form>
	<?
}
?>
</div>
</body>
</html>


I've over commented it so you can see whats going on, have fun ;)
Posted Image Posted Image
0

#12 User is offline   Brendon Koz Icon

  • Needs Hobby
  • PipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 4,741
  • Joined: 06-September 03
  • Location:Saratoga Springs, NY, USA

Posted 30 June 2005 - 11:50 PM

:( Half the fun is in figuring out what others do and how they do it. I guess that's the debugger in me from college when I was helping to look over classmates' C++ programs that had well over 144 compiler errors...then you fix 10 or so errors and it would run further (not knowing it stopped as I didn't read ALL errors) and you'd get 500+ errors the next time around. ;)

Thanks though, much appreciated! This will help me out while I continue to (ever so slowly) learn CSS.
Posted Image
mysiteonline.org™
They say, "Practice makes perfect," yet they also say, "Nobody's perfect"... I don't get it.
0

#13 User is offline   Stephen Icon

  • Meet Jay
  • PipPipPipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 18,141
  • Joined: 22-March 02
  • Location:London, England

Posted 01 July 2005 - 12:08 AM

Ah yes, know whats thats like :D
Posted Image Posted Image
0

#14 User is offline   elj Icon

  • Captain Boycott ^_^
  • PipPipPipPipPipPipPip
  • View blog
  • Group: Members
  • Posts: 4,359
  • Joined: 19-October 03
  • Location:West Yorkshire, UK

Posted 01 July 2005 - 10:28 AM

Nice. :)
The sky isn't evil. Try looking up.
-- Joss Whedon


UK Web Development
0

#15 User is offline   Cool Dude 2k Icon

  • IPB Member
  • PipPip
  • View blog
  • Group: Members
  • Posts: 56
  • Joined: 07-April 04
  • Location:Look behind yourself!!

Post icon  Posted 01 July 2005 - 12:43 PM

Looks awesome Very nice job you did on it Stephen. :cool: ^_^
Posted Image Posted Image KolibriOS 和樹- Казуки
Posted Image
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users