Converts a selector string to a filter function.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.