{"id":21995336,"url":"https://github.com/polymerlabs/shady-css-parser","last_synced_at":"2025-04-24T04:17:34.102Z","repository":{"id":66263329,"uuid":"41405911","full_name":"PolymerLabs/shady-css-parser","owner":"PolymerLabs","description":"A fast, small and flexible CSS parser.","archived":false,"fork":false,"pushed_at":"2024-02-04T20:57:34.000Z","size":225,"stargazers_count":28,"open_issues_count":8,"forks_count":6,"subscribers_count":31,"default_branch":"master","last_synced_at":"2025-04-24T04:17:08.320Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PolymerLabs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-08-26T05:18:33.000Z","updated_at":"2024-07-30T08:37:33.000Z","dependencies_parsed_at":"2024-06-18T15:04:19.960Z","dependency_job_id":"373dcf43-8f6c-4335-aabd-ec65aed59874","html_url":"https://github.com/PolymerLabs/shady-css-parser","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PolymerLabs%2Fshady-css-parser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PolymerLabs%2Fshady-css-parser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PolymerLabs%2Fshady-css-parser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PolymerLabs%2Fshady-css-parser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PolymerLabs","download_url":"https://codeload.github.com/PolymerLabs/shady-css-parser/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250560057,"owners_count":21450173,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-29T21:14:17.325Z","updated_at":"2025-04-24T04:17:34.078Z","avatar_url":"https://github.com/PolymerLabs.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Shady CSS Parser\n\nThe motivation for Shady CSS Parser is to provide a fast, small and flexible\nCSS parser suitable for facilitating runtime parsing and transformation of CSS.\nThe Polymer library and the Polymer Designer tool are both example cases where\nfast and flexible CSS parsing and transformation is a critical feature.\n\n### Goals\n\n -  Feasibility of being used in conjunction with Polymer or Polymer\nDesigner.\n -  Parse CSS loosely and flexibly. This parser is not spec-compliant, however\n it will parse all spec-compliant CSS.\n -  Parse CSS quickly and efficiently. This parser is a suitable tool to aide in\n the design and implementation of runtime transformations.\n -  Graceful error recovery. Malformed CSS will be parsed by this\nparser as closely as possible to the way a browser would parse it.\n\n### Installing\n\nWith `node` and `npm` installed, run the following command:\n\n```sh\nnpm install shady-css-parser\n```\n\n### Building\n\nRun the following commands from the project root:\n\n```sh\nnpm run build\n```\n\nThis will create a `dist` directory containing distributable artifacts.\n\n### Usage\n\n#### Basic parsing\n\n```js\nimport * as shadyCss from 'shady-css-parser';\nconst css = 'body { color: red; }';\nconst parser = new shadyCss.Parser();\nconst ast = parser.parse(css);\n```\n\n#### Custom parsing\n\n```js\n/* Step 1: Inherit from NodeFactory */\nclass CustomNodeFactory extends shadyCss.NodeFactory {\n\n  /*\n   * Step 2: Implement a custom node factory method. Here we override the\n   *   default factory for Expression nodes\n   */\n  expression(text) {\n    if (/^darken\\(/.test(text)) {\n      return {\n        type: 'darkenExpression',\n        color: text.replace(/^darken\\(/, '').replace(/\\)$/, ''),\n      };\n    } else {\n      return super.expression.apply(this, arguments);\n    }\n  }\n}\n\nconst css = 'body { color: darken(red); }';\n/* Step 3: Instantiate a Parser with an instance of the specialized\n * CustomNodeFactory */\nconst parser = new shadyCss.Parser(new CustomNodeFactory());\nconst ast = parser.parse(css);\n```\n\n#### Basic stringification\n\n```js\nconst stringifier = new shadyCss.Stringifier();\nstringifier.stringify(ast);\n```\n\nNote: the built-in Parser and Stringifier discard most insignficiant whitespace\nfrom parsed CSS.\n\n#### Custom stringification\n\n```js\n/* Step 1: Inherit from Stringifier. */\nclass CustomStringifier extends shadyCss.Stringifier {\n\n  /**\n  * Step 2: Implement a stringification method named after the type of the node\n  * you are interested in stringifying. In this case, we are implementing\n  * stringification for the Darken Expression nodes we implemented parsing for\n  * above.\n  */\n  darkenExpression(darkenExpression) {\n    // For the sake of brevity, please assume that the darken function returns\n    // a darker version of the color parameter:\n    return darken(darkenExpression.color);\n  }\n}\n\n/* Step 3: Use the custom stringifer: */\nconst stringifier = new CustomStringifier();\nconst css = stringifier.stringify(ast);\n```\n\n### Example ASTs\n\n#### Custom property declaration\n\n```css\n.container {\n  --nog: blue;\n}\n```\n```js\n{\n  \"type\": 1, /* stylesheet */\n  \"rules\": [\n    {\n      \"type\": 4, /* ruleset */\n      \"selector\": \".container\",\n      \"rulelist\": {\n        \"type\": 7, /* rulelist */\n        \"rules\": [\n          {\n            \"type\": 6, /* declaration */\n            \"name\": \"--nog\",\n            \"value\": {\n              \"type\": 5, /* expression */\n              \"text\": \"blue\"\n            }\n          }\n        ]\n      }\n    }\n  ]\n}\n```\n\n#### Mixin declaration\n\n```css\nruleset {\n  --mixin-name: {\n    /* rules */\n  };\n}\n```\n```js\n{\n  \"type\": 1, /* stylesheet */\n  \"rules\": [\n    {\n      \"type\": 4, /* ruleset */\n      \"selector\": \"ruleset\",\n      \"rulelist\": {\n        \"type\": 7, /* rulelist */\n        \"rules\": [\n          {\n            \"type\": 6, /* declaration */\n            \"name\": \"--mixin-name\",\n            \"value\": {\n              \"type\": 7, /* rulelist */\n              \"rules\": [\n                {\n                  \"type\": 2, /* comment */\n                  \"value\": \"\\/* rules *\\/\"\n                }\n              ]\n            }\n          }\n        ]\n      }\n    }\n  ]\n}\n```\n\n#### Mixin application\n\n```css\n.title {\n  @apply(--my-toolbar-title-theme);\n}\n```\n```js\n{\n  \"type\": 1, /* stylesheet */\n  \"rules\": [\n    {\n      \"type\": 4, /* ruleset */\n      \"selector\": \".title\",\n      \"rulelist\": {\n        \"type\": 7, /* rulelist */\n        \"rules\": [\n          {\n            \"type\": 3, /* at rule */\n            \"name\": \"apply\",\n            \"parameters\": \"(--my-toolbar-title-theme)\",\n            \"rulelist\": null\n          }\n        ]\n      }\n    }\n  ]\n}\n```\n\n#### Pathological comments\n\n```css\n/* unclosed\n@fiz {\n  --huk: {\n    /* buz */\n    baz: lur;\n  };\n}\n```\n```js\n{\n  \"type\": 1, /* stylesheet */\n  \"rules\": [\n    {\n      \"type\": 2, /* comment */\n      \"value\": \"\\/* unclosed\\n@fiz {\\n  --huk: {\\n    \\/* buz *\\/\"\n    },\n    {\n      \"type\": 6, /* declaration */\n      \"name\": \"baz\",\n      \"value\": {\n        \"type\": 5, /* expression */\n        \"text\": \"lur\"\n      }\n    },\n    {\n      \"type\": 8, /* discarded */\n      \"text\": \"};\\n\"\n    },\n    {\n      \"type\": 8, /* discarded */\n      \"text\": \"}\"\n    }\n  ]\n}\n```\n\n### Example stringification\n\n#### Basic ruleset\n\n```css\n/* before */\nbody {\n  margin: 0;\n  padding: 0px\n}\n```\n```css\n/* after */\nbody{margin:0;padding:0px;}\n```\n\n#### At rules\n\n```css\n/* before */\n@import url('foo.css');\n\n@font-face {\n  font-family: foo;\n}\n\n@charset 'foo';\n```\n```css\n/* after */\n@import url('foo.css');@font-face{font-family:foo;}@charset 'foo';\n```\n\n#### Custom properties\n\n```css\n/* before */\n:root {\n  --qux: vim;\n  --foo: {\n    bar: baz;\n  };\n}\n\n#target {\n  gak: var(--qux);\n  @apply(--foo);\n}\n```\n```css\n/* after */\n:root{--qux:vim;--foo:{bar:baz;};}#target{gak:var(--qux);@apply (--foo);}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolymerlabs%2Fshady-css-parser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpolymerlabs%2Fshady-css-parser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpolymerlabs%2Fshady-css-parser/lists"}