LoginSignup
6
4

More than 5 years have passed since last update.

CSSのパースにはJSCSSP

Last updated at Posted at 2012-05-03
  • 多少invalidでもパースしきってくれる
  • background: #000 というCSSも background-color: #000 のように統一してくれる

nodeモジュールやCPANモジュールも漁ったがこれが一番だった。

var cssText = 'body {background: #000 url(image/hoge.gif) no-repeat fixed right bottom;}';

var parser = new CSSParser();
var parsed = parser.parse(cssText, false, false);

JSON.stringify( parsed.cssRules[0].declarations[0], null, "  " );

/*
  "{
    "type": 1000,
    "property": "background-color",
    "values": [
      {
        "value": "#000",
        "type": 1,
        "name": null,
        "parentRule": null,
        "parentStyleSheet": null
      }
    ],
    "valueText": "#000",
    "priority": false,
    "parsedCssText": "background-color: #000;",
    "parentStyleSheet": null,
    "parentRule": null
  }"
*/

公式:JSCSSP, a CSS parser in JavaScript

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4