Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:13:42+00:00 2026-06-04T02:13:42+00:00

I have parsed a given CSS file/string into a JSON object like so: {

  • 0

I have parsed a given CSS file/string into a JSON object like so:

{
    "#header": {
        "color": "#000000"
    },
    "#header h1": {
        "color": "#000"
    },
    "h1": {
        "color": "#4fb6e5"
    }
}

What I want to do now is re-order them based on Specificty. In this case, the #header h1 should come after the h1 in the JSON object as this is how they’ll be applied in the browser.

How can I do this? Are there any existing libraries for this? Or any useful libraries to help with this?

I can use both Javascript/jQuery or PHP to do this. I’m looking for implementation advice and hopefully this has already been done!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:13:43+00:00Added an answer on June 4, 2026 at 2:13 am

    Short answers:

    Are there any existing libraries for this?

    No, not one I’m aware of that does this “out-of-the-box” for you.

    Or any useful libraries to help with this?

    Yes, there is json_decode and uksort:

    $specificity = function($selector) {
    
        /*
         * @link http://www.w3.org/TR/selectors/#specificity
         *
         * 9. Calculating a selector's specificity
         *
         * A selector's specificity is calculated as follows:
         * 
         *  * count the number of ID selectors in the selector (= a)
         *  * count the number of class selectors, attributes selectors, and 
         *    pseudo-classes in the selector (= b)
         *  * count the number of type selectors and pseudo-elements in the 
         *    selector (= c)
         *  * ignore the universal selector
         *
         * Selectors inside the negation pseudo-class are counted like any other, 
         * but the negation itself does not count as a pseudo-class.
         *
         * Concatenating the three numbers a-b-c (in a number system with a large 
         * base) gives the specificity.
         */
        ...
        return (int) $result;
    }
    
    $compare = function($a, $b) use ($specificity) {
        return $specificity($a) - $specificity($b)
    };
    
    $array = json_decode('{"yours" : "json"}', true);
    
    uksort($array, $compare);
    
    echo json_encode((object) $array);
    

    As this code example shows, it only explains how to calculate the specificity in a comment and it does not contain the code. That’s just because I don’t have that code at hand, but I have put in there the specification how this is done (at least for CSS 3).

    If you’re looking for a CSS selector parser, I know about XDOM (because I wrote it). It’s available on github: https://github.com/hakre/XDOM – It’s a 100% CSS 3 compatible CSS selector parser.

    To my best knowledge that is most of what you get as of today in terms of ready-made solutions. All other CSS selector parsers I know of are not compatible with the CSS 3 standard in full because they don’t follow the W3C specification. Which might be good for you: If you don’t need strict CSS3 compatbility, you might find some other code-chunks that suit your needs already.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have parsed XML file into objects, in which each object has a 1:1
Given that I have access to an IField field (parsed from another Java file),
Given a string as a form of input (parsed from an input file) which
I have a thousands of data parsed from huge XML to be inserted into
I have a url which have a response in Json lon=-0.1275&pg=0 I parsed the
I have parse json file which contain more than hundred location name, there latitude
-- I just parsed a big file and I created a list containing 42.000
anyone can help me implement this Given that i have a txt file and
I have a csv file where each row defines a room in a given
I know that it's faster to do the following: var $header = $(#header); $header.css({color:#ff0000});

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.