{"id":13745,"library":"parserlib","title":"ParserLib CSS Parser","description":"ParserLib is a CSS3 SAX-inspired parser written in JavaScript, designed for event-driven parsing of CSS stylesheets. It supports standard CSS syntax and offers a degree of validation for property names and values, though it is not exhaustive. The library is currently at version 1.1.1, with its most recent update focused on build file regeneration and minor CSS3 feature additions like `turn` units and nested media query support. Unlike AST-based parsers, ParserLib emits events during the parsing process rather than constructing a syntax tree, requiring developers to implement listeners for desired functionalities. It is built to operate in various JavaScript environments, including Node.js, Rhino, and directly in HTML pages, making it versatile for different project setups.","status":"active","version":"1.1.1","language":"javascript","source_language":"en","source_url":"https://github.com/CSSLint/parser-lib","tags":["javascript","parser","css","css3","sax","style","stylesheet"],"install":[{"cmd":"npm install parserlib","lang":"bash","label":"npm"},{"cmd":"yarn add parserlib","lang":"bash","label":"yarn"},{"cmd":"pnpm add parserlib","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The library primarily supports CommonJS 'require' syntax. Direct ESM 'import' is not natively supported without a transpilation step.","wrong":"import parserlib from 'parserlib';","symbol":"parserlib","correct":"const parserlib = require('parserlib');"},{"note":"Access the Parser class through the `css` namespace of the main `parserlib` object. ESM imports are not directly supported.","wrong":"import { Parser } from 'parserlib/css';","symbol":"parserlib.css.Parser","correct":"const { Parser } = require('parserlib').css;"},{"note":"ParserLib is SAX-inspired and event-driven; there is no AST. Interact with the parsed CSS by adding listeners for specific events like 'property', 'rule', etc.","symbol":"parser.addListener","correct":"parser.addListener('property', (event) => console.log(event.property.text));"}],"quickstart":{"code":"const parserlib = require('parserlib');\n\nconst parser = new parserlib.css.Parser({\n  starHack: true, // Treat properties with a leading asterisk as if the asterisk wasn't there\n  underscoreHack: true // Treat properties with a leading underscore as if the underscore wasn't there\n});\n\nconst someCSSText = `\n  /* This is a sample CSS block */\n  @media screen and (min-width: 768px) {\n    .container {\n      width: 90%;\n      margin: 0 auto;\n    }\n  }\n\n  body {\n    font-family: Arial, sans-serif;\n    color: #333;\n  }\n\n  h1._title { /* underscore hack example */\n    font-size: 2em;\n    *color: #00f; /* star hack example, if 'starHack' is true */\n    text-decoration: underline;\n  }\n`;\n\ntry {\n  parser.parse(someCSSText);\n  console.log('CSS parsed successfully. Remember, this is an event-driven parser; attach listeners for detailed output.');\n} catch (e) {\n  console.error('Parsing error:', e.message);\n}","lang":"javascript","description":"Demonstrates initializing the CSS parser with 'starHack' and 'underscoreHack' options and parsing a sample CSS string. It highlights the event-driven nature where no AST is returned directly."},"warnings":[{"fix":"Review project-specific changelogs or GitHub releases for `v1.0.0` if available, and adapt code to the updated API. Implement comprehensive tests to identify regressions.","message":"Major version `1.0.0` was released without explicit breaking change documentation. Users upgrading from `0.x` versions should anticipate potential API changes and thoroughly test their applications.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Instead of expecting a return value with a parse tree, register event listeners for specific CSS constructs (e.g., `startstylesheet`, `property`, `rule`) to capture and process information as it's parsed. Example: `parser.addListener('property', (event) => { /* ... */ });`","message":"ParserLib is a SAX-inspired, event-driven parser and *does not* construct or return an Abstract Syntax Tree (AST). Developers must attach event listeners to process CSS rules and properties incrementally.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For strict CSS validation requirements, integrate additional dedicated CSS validation tools alongside ParserLib. Do not rely solely on ParserLib for comprehensive syntax or semantic checks.","message":"The parser's validation capabilities are not exhaustive; it is 'not guaranteed to thoroughly validate all possible CSS properties.'","severity":"gotcha","affected_versions":"All versions"},{"fix":"In Node.js environments, use `const parserlib = require('parserlib');`. For browser use, ensure the `parserlib.js` script is loaded via a `<script>` tag. For modern ESM projects, a bundler or manual CommonJS-to-ESM conversion might be necessary.","message":"The library predominantly supports CommonJS modules (`require()`). Direct ECMAScript Module (`import`) syntax is not natively supported without transpilation or a bundler.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For client-side parsing of substantial CSS, consider processing in Web Workers to offload the main thread, or pre-process CSS on the server-side.","message":"Parsing large CSS files directly in a browser environment can lead to unresponsiveness, as indicated in the documentation.","severity":"gotcha","affected_versions":"All versions (browser environments)"}],"env_vars":null,"search_vec":"'1.1.1':51 'addit':66 'ast':77 'ast-bas':76 'base':78 'build':59 'built':103 'construct':89 'css':2,22,27,127 'css3':7,64,128 'current':48 'degre':32 'design':15 'desir':99 'develop':94 'differ':122 'direct':114 'driven':19 'emit':81 'environ':109 'event':18,82 'event-driven':17 'exhaust':44 'featur':65 'file':60 'focus':57 'function':100 'html':116 'implement':96 'includ':110 'inspir':10 'javascript':14,108,125 'librari':46 'like':67 'listen':97 'make':118 'media':72 'minor':63 'name':37 'nest':71 'node.js':111 'offer':30 'oper':105 'page':117 'pars':20,85 'parser':3,11,79,126 'parserlib':1,4,80 'process':86 'project':123 'properti':36 'queri':73 'rather':87 'recent':55 'regener':61 'requir':93 'rhino':112 'sax':9,129 'sax-inspir':8 'setup':124 'standard':26 'style':130 'stylesheet':23,131 'support':25,74 'syntax':28,91 'though':40 'tree':92 'turn':68 'unit':69 'unlik':75 'updat':56 'valid':34 'valu':39 'various':107 'versatil':120 'version':50 'written':12","created_at":"2026-04-20T01:56:05.356638+00:00","updated_at":"2026-04-20T01:56:05.356638+00:00","problems":[{"fix":"Examine the CSS content at the reported line and column, and correct the syntax. Consider enabling `strict: false` option during parser initialization for basic error recovery, although it may not resolve all issues.","cause":"The input CSS string contains a malformed syntax that the parser cannot recover from.","error":"Syntax Error: Expected <token> at line X, column Y"},{"fix":"Ensure `require('parserlib')` successfully loads the module. Verify that the script loading order is correct in browser environments if using `<script>` tags, or that the bundler configuration is correct for CommonJS modules.","cause":"The 'parserlib' module or its 'css' property was not correctly loaded or is undefined.","error":"TypeError: parserlib.css.Parser is not a constructor"},{"fix":"For browser usage, include `parserlib.js` via a `<script>` tag. If using a module system in the browser, ensure your bundler (e.g., Webpack, Rollup) is configured to handle CommonJS modules.","cause":"Attempting to use Node.js-specific `require` syntax in a browser environment without a CommonJS module loader/bundler.","error":"ReferenceError: require is not defined (when using 'require' in browser)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.1.1","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/CSSLint/parser-lib","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/parserlib","openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization","data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}