{"id":13724442,"url":"https://github.com/WICG/css-parser-api","last_synced_at":"2025-05-07T18:32:21.067Z","repository":{"id":48541809,"uuid":"58818824","full_name":"WICG/css-parser-api","owner":"WICG","description":"This is the repo where the CSS Houdini parser API will be worked on","archived":false,"fork":false,"pushed_at":"2021-07-20T22:51:03.000Z","size":56,"stargazers_count":71,"open_issues_count":9,"forks_count":14,"subscribers_count":22,"default_branch":"main","last_synced_at":"2024-08-04T01:25:08.980Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WICG.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-14T16:45:00.000Z","updated_at":"2024-08-02T02:20:23.000Z","dependencies_parsed_at":"2022-09-23T13:53:46.155Z","dependency_job_id":null,"html_url":"https://github.com/WICG/css-parser-api","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WICG%2Fcss-parser-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WICG%2Fcss-parser-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WICG%2Fcss-parser-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WICG%2Fcss-parser-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WICG","download_url":"https://codeload.github.com/WICG/css-parser-api/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224636539,"owners_count":17344565,"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-08-03T01:01:57.249Z","updated_at":"2024-11-14T14:30:56.826Z","avatar_url":"https://github.com/WICG.png","language":"HTML","funding_links":[],"categories":["HTML"],"sub_categories":[],"readme":"# Overview\nThe goal of this specification is to allow authors access to the engine's parser.\nThere are two over-arching use cases:\n1. Pass the parser a string receive and receive an object\nto work with instead of building custom parsing in JS.\n2. Extend what the parser understands for fully polyfilling\n\nDesires for these APIs:\n* Build on top of the [TypedOM](https://drafts.css-houdini.org/css-typed-om/) and make modifications to that\n  specification where necessary.\n* Be able to request varying levels of error handling by the parser\n\n## Example: Parsing out a ruleset\n\n```\n   var background = window.cssParse.rule(\"background: green\");\n   console.log(background.styleMap.get(\"background\").value) // \"green\"\n\n   var styles = window.cssParse.ruleSet(\".foo { background: green; margin: 5px; }\");\n   console.log(styles.length) // 5\n   console.log(styles[0].styleMap.get(\"margin-top\").value) // 5\n   console.log(styles[0].styleMap.get(\"margin-top\").type) // \"px\"\n```\n\n## Example: Parsing out a stylesheet\n\n```\n\tconst style = fetch(\"style.css\")\n\t\t.then(response=\u003eCSS.parseStylesheet(response.body));\n\tstyle.then(console.log);\n\n    /* example of the object once we have it more refined */\n    );\n```\n\nUse Cases\n=========\n\n1. Extending CSS in a more intrusive way than our hooks allow - most of the stylesheet is valid CSS, but you want to intercede on the unrecognized things and fix it up into valid CSS.  (Probably don't want to do this - it collides with the custom property/function/at-rule case, but without the friendliness to future language extensions.)\n2. Custom properties that are more complex than we allow you to specify - something that takes \"`\u003clength\u003e || \u003ccolor\u003e`\", for example. Right now your only choice is to give it the grammar `\"*\"` which just gives you a string that you have to parse yourself.  Similar with custom functions (`--conic-gradient(...)`) and custom at-rules (`@--svg`) - these will *rarely* fit in our limited set of allowed grammars.\n3. CSS-like languages that want to rely on CSS's generic syntax, like CAS or that one mapping language I forget the name of. These have nothing to do with CSS and will likely result in something other than a stylesheet: CAS turns into a series of querySelector() and setAttribute() calls; the mapping thing turns into some data structures specific to that application.\n4. Extending HTML/SVG attributes that use a CSS-like syntax, like `\u003cimg sizes\u003e`. If you wanted to add support for the `h` descriptor to `sizes`, you currently have to write your own full-feature CSS parser. (`sizes` is pretty complex; you shouldn't skimp.) Better to let the engine parse it as \"generic CSS\", then you can recognize the parts you need and do image selection yourself.\n\nExample of the problem\n======================\n\nHere is an example of some JS code that is wanting to parse out various CSS\ntypes and also seperate out the values from their units.\n\n```\n\n\t\tfunction parseValues(value,propertyName) {\n\t\t\t// Trim value on the edges\n\t\t\tvalue = value.trim();\n\n\t\t\t// Normalize letter-casing\n\t\t\tvalue = value.toLowerCase();\n\n\t\t\t// Map colors to a standard value (eg: white, blue, yellow)\n\t\t\tif (isKeywordColor(value)) { return \"\u003ccolor-keyword\u003e\"; }\n\n\t\t\tvalue = value.replace(/[#][0-9a-fA-F]+/g, '#xxyyzz');\n\n\t\t\t// Escapce identifiers containing numbers\n\t\t\tvar numbers = ['ZERO','ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE'];\n\n\t\t\tvalue = value.replace(\n\t\t\t\t/([_a-z][-_a-z]|[_a-df-z])[0-9]+[-_a-z0-9]*/g,\n\t\t\t\ts=\u003enumbers.reduce(\n\t\t\t\t\t(m,nstr,nint)=\u003em.replace(RegExp(nint,'g'),nstr),\n\t\t\t\t\ts\n\t\t\t\t)\n\t\t\t);\n\n\t\t\t// Remove any digits eg: 55px -\u003e px, 1.5 -\u003e 0.0, 1 -\u003e 0\n\t\t\tvalue = value.replace(/(?:[+]|[-]|)(?:(?:[0-9]+)(?:[.][0-9]+|)|(?:[.][0-9]+))(?:[e](?:[+]|[-]|)(?:[0-9]+))?(%|e[a-z]+|[a-df-z][a-z]*)/g, \"$1\");\n\t\t\tvalue = value.replace(/(?:[+]|[-]|)(?:[0-9]+)(?:[.][0-9]+)(?:[e](?:[+]|[-]|)(?:[0-9]+))?/g, \" \u003cfloat\u003e \");\n\t\t\tvalue = value.replace(/(?:[+]|[-]|)(?:[.][0-9]+)(?:[e](?:[+]|[-]|)(?:[0-9]+))?/g, \" \u003cfloat\u003e \");\n\t\t\tvalue = value.replace(/(?:[+]|[-]|)(?:[0-9]+)(?:[e](?:[+]|[-]|)(?:[0-9]+))/g, \" \u003cfloat\u003e \");\n\t\t\tvalue = value.replace(/(?:[+]|[-]|)(?:[0-9]+)/g, \" \u003cint\u003e \");\n\n\t\t\t// Unescapce identifiers containing numbers\n\t\t\tvalue = numbers.reduce(\n\t\t\t\t(m,nstr,nint)=\u003em.replace(RegExp(nstr,'g'),nint),\n\t\t\t\tvalue\n\t\t\t)\n\n\t\t\t// Remove quotes\n\t\t\tvalue = value.replace(/('|‘|’|\")/g, \"\");\n\n\t\t\t//\n\t\t\tswitch(propertyName) {\n\t\t\t\tcase 'counter-increment':\n\t\t\t\tcase 'counter-reset':\n\t\t\t\t\t// Anonymize the user identifier\n\t\t\t\t\tvalue = value.replace(/[-_a-zA-Z0-9]+/g,' \u003ccustom-ident\u003e ');\n\t\t\t\t\tbreak;\n\t\t\t\tcase 'grid':\n\t\t\t\tcase 'grid-template':\n\t\t\t\tcase 'grid-template-rows':\n\t\t\t\tcase 'grid-template-columns':\n\t\t\t\tcase 'grid-template-areas':\n\t\t\t\t\t// Anonymize line names\n\t\t\t\t\tvalue = value.replace(/\\[[-_a-zA-Z0-9 ]+\\]/g,' \u003cline-names\u003e ');\n\t\t\t\t\tbreak;\n\t\t\t\tcase '--var':\n\t\t\t\t\t// Replace (...), {...} and [...]\n\t\t\t\t\tvalue = value.replace(/[(](?:[^()]+|[(](?:[^()]+|[(](?:[^()]+|[(](?:[^()]+|[(](?:[^()]*)[)])*[)])*[)])*[)])*[)]/g, \" \u003cparentheses-block\u003e \");\n\t\t\t\t\tvalue = value.replace(/[(](?:[^()]+|[(](?:[^()]+|[(](?:[^()]+|[(](?:[^()]+|[(](?:[^()]*)[)])*[)])*[)])*[)])*[)]/g, \" \u003cparentheses-block\u003e \");\n\t\t\t\t\tvalue = value.replace(/\\[(?:[^()]+|\\[(?:[^()]+|\\[(?:[^()]+|\\[(?:[^()]+|\\[(?:[^()]*)\\])*\\])*\\])*\\])*\\]/g, \" \u003ccurly-brackets-block\u003e \");\n\t\t\t\t\tvalue = value.replace(/\\[(?:[^()]+|\\[(?:[^()]+|\\[(?:[^()]+|\\[(?:[^()]+|\\[(?:[^()]*)\\])*\\])*\\])*\\])*\\]/g, \" \u003ccurly-brackets-block\u003e \");\n\t\t\t\t\tvalue = value.replace(/\\{(?:[^()]+|\\{(?:[^()]+|\\{(?:[^()]+|\\{(?:[^()]+|\\{(?:[^()]*)\\})*\\})*\\})*\\})*\\}/g, \" \u003csquare-brackets-block\u003e \");\n\t\t\t\t\tvalue = value.replace(/\\{(?:[^()]+|\\{(?:[^()]+|\\{(?:[^()]+|\\{(?:[^()]+|\\{(?:[^()]*)\\})*\\})*\\})*\\})*\\}/g, \" \u003csquare-brackets-block\u003e \");\n\t\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\treturn value.trim();\n\t\t}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWICG%2Fcss-parser-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FWICG%2Fcss-parser-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FWICG%2Fcss-parser-api/lists"}