Checking css selector

Is there a way to programmatically check if the javascript / jquery selector is correct?

like .classbut .class,not

programmatically in javascript or any backend language (besides going through jQuery source code)

therefore in pseudo code

 def selectorErrors(selector)

     // do nasty stuff to the selector
     if valid?
      return nil
    else
      return errors
    end
end
+2
source share
3 answers

That should confirm 99% of what you can throw in jQuery with much less overhead

function validateQuery(str) {
    try {
        document.querySelector(str);
        return true;
    }
    catch(e) {
        // something here to check WHY it invalid
    }
    return false;
}

edit: ok, this will not tell you what is wrong with him, but he (quickly) checks the correctness - at least you only need to check WHY it is invalid, not IF: p

+2
source
existOrValid('#a')

function existOrValid(a){

    if (/^[#,.]{0,1}[A-Za-z][0-9A-Za-z\-\._:]*$/.test(a)) {
    alert('valid');
}

}

, id,

0

$('.a.b')

If you want an intersection, just write a selector together without spaces between them. So, for an element with identifier a with classes b and c, you should write:

$('#a.b.c')

-4
source

Source: https://habr.com/ru/post/1624738/


All Articles