Harry Harry - 10 months ago 33
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:

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')