Harry Harry - 1 year ago 59
CSS Question

javascript validate css

I'm allowing user-input css.

What is a light and performant way to validate that css on the client side?

I'm looking for a javascript solution. I don't want to send the css off to some remote server or anything.

Allowing user css


Edit: NVM on this question. I started using a server-side validator:

Answer Source
function getDefinedCss(s){
    if(!document.styleSheets) return '';
    if(typeof s== 'string') s= RegExp('\\b'+s+'\\b','i'); // IE capitalizes html selectors 

    var A, S, DS= document.styleSheets, n= DS.length, SA= [];
        S= DS[--n];
        A= (S.rules)? S.rules: S.cssRules;
        for(var i= 0, L= A.length; i<L; i++){
                tem= A[i].selectorText? [A[i].selectorText, A[i].style.cssText]: [A[i]+''];
                if(s.test(tem[0])) SA[SA.length]= tem;
    return SA.join('\n\n');

// Then you check the class if exists by calling getDefinedCss('myclassname')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download