dbj* :rx() Sizzle Pseudo Selector
QUnit usage from http://jsfiddle.net/elijahmanor/fqJmu/light/
by dbjdbj
HTML
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
<h1 id="qunit-header"></h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">
<div id="testbed" class="testbed">
<input value="whatever" />
</div>
</div>
CSS
body { margin: 10px; padding: 0; background-color:grey; }
.testbed { position:absolute; display:none; }
JavaScript
/*
Inspired by : http://james.padolsey.com/javascript/regex-selector-for-jquery/
which was not working because if was returning true on non existent attributes,
properties or css props
Also added prop label, so that http://api.jquery.com/prop/ can be used
usage:
// select anything that has css property 'right' which is a number
// that is '10px' or '24%' or '1em', but not 'default', 'auto' etc
var $see_mee_in_debugger_ = $(":rx(css:right,\\d+)");
*/
(function (jQuery,undefined) {
jQuery.expr[':']["rx"] = function (elem, index, match) {
var matchParams = match[3].split(','),
validLabels = /^(data|css|prop):/,
attr = {
method: matchParams[0].match(validLabels) ?
matchParams[0].split(':')[0] : 'attr',
property: matchParams.shift().replace(validLabels, '')
},
regexFlags = 'ig',
regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g, ''), regexFlags);
/* 2012 MAR 06 DBJ replaced
return regex.test(jQuery(elem)[attr.method](attr.property));
with this: */
var value_ = jQuery(elem)[attr.method](attr.property);
return !!value_ && regex.test(value_);
}
}($ || jQuery))
var $fixture = $( "#qunit-fixture" );
test("should find one 'testbed' div for display:'none'",
function (){
var $d = $("div:rx(css:display,none)") ;
ok($d[0]) ;
ok($d.length == 1,"exactly one 'testbed' div was found")
});
test("should find 2 div's where 'left' is 'auto', since this is default value for the css property 'left'",
function () {
var $d = $("div:rx(css:left,auto)") ;
ok($d.length == 2, "excatly 2 were found" )
});
test(" should find one 'input' where property 'value' is 'whatever'",
function (){
var $d =...