Qwrap In selector, the selector is converted into a filter function. The implementation method is as follows:
Convert the selector string to the function string, and then generate a new function (s.
Remove the pseudo-class Code , Simplified as follows:
<SCRIPT> var selector = {/** CSS selector attribute operator */_ operators: {// the following expression: AA indicates the ATTR value, VV indicates the Compare value '': 'A', // istrue | hasvalue' = ': 'aa = "VV"', // equal '! = ': 'Aa! = "VV" ', // unequal '~ = ': 'Aa & ("" + AA + ""). indexof ("VV")>-1 ', // onepart' | = ': 'aa & (AA + "-"). indexof ("VV-") = 0', // firstpart '^ =': 'aa & aa. indexof ("VV") = 0', // beginwith '$ =': 'aa & aa. lastindexof ("VV") = AA. length-"VV ". length', // endwith '* =': 'aa & aa. indexof (v)>-1 '// contains},/** common element attributes */_ attrgetters: function () {var o = {'class': 'El. classname', 'for': 'el.html for ', 'href': 'El. getattribute ("href", 2) '}; var attrs = 'Nam E, ID, classname, value, selected, checked, disabled, type, tagname, readonly '. split (','); For (VAR I = 0, A; A = attrs [I]; I ++) O [a] = "El. "+ A; return O;} (),/** converts a selector string into a filter function. * @ method selector2filter * @ Param {string} sselector: Filter selector. This selector has no Relational operators (",> + ~ ") * @ Returns {function}: returns the filter function. * @ Example: var fun = selector2filter ("input. AAA "); alert (fun); */selector2filter: function (sselector) {var S = sselector, Reg = // \ [\ s *((?: [\ W \ u00c0-\ Uffff-] | \.) +) \ s *(? :( \ S? =) \ S * (['"] *) (. *?) \ 3 |) \ s * \]/g, // select expression parsing for attribute, thanks jquerysfun = []; S = S. replace (/\ :( [\ W \-] +) (\ ([^)] + )\))? /G, function (a, B, c, d, e) {pseudo dos. push ([B, d]); Return "" ;}) // pseudo class. replace (/^ \ */g, function (a) {// arbitrary tagname scaling statement sfun. push ('El. nodetype = 1 '); Return ''}). replace (/^ ([\ W \-] +)/g, function (a) {// tagname scaling statement sfun. push ('El. tagname = "'+. touppercase () + '"'); Return ''}). replace (/([\ [(]. *) | # (\ W +) | \. (\ W +)/g, function (A, B, C, D) {// ID scaling method // classname scaling method return B | C & '[ID = "' + C + '"]' | D & '[classname ~ = "'+ D +'"] ';}). replace (Reg, function (a, B, c, d, e) {// [Foo] [Foo = ""] [Foo ~ = ""] And other var attrgetter = selector. _ attrgetters [B] | 'El. getattribute ("'+ B +'") '; sfun. push (selector. _ OPERATORS [c | '']. replace (/AA/g, attrgetter ). replace (/VV/g, E | ''); Return'';}); If (S. length) {Throw "unsupported selector: \ n" + sselector + "\ n" + S;} If (sfun. length) {sfun = 'Return ('+ sfun. join (") & (") + ');'; alert (sfun); return new function ("El", sfun);} return function (EL) {return true ;}}}; selector. selector2filter ('div. AAA. bbb [readonly] '); </SCRIPT>
This is actually a very useful function. I don't understand why sizzle is not provided by the way.