Registry / serialization / parserlib

parserlib

JSON →
library0.1.1jsnpmunverified

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.

npm install parserlib
INSTALL
IMPORT
SIG · PARSERLIB
P
parserlib
serializationjavascriptv0.1.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parserlib
const parserlib = require('parserlib');
import parserlib from 'parserlib';
The library primarily supports CommonJS 'require' syntax. Direct ESM 'import' is not natively supported without a transpilation step.
parserlib.css.Parser
const { Parser } = require('parserlib').css;
import { Parser } from 'parserlib/css';
Access the Parser class through the `css` namespace of the main `parserlib` object. ESM imports are not directly supported.
parser.addListener
parser.addListener('property', (event) => console.log(event.property.text));
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.

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.

const parserlib = require('parserlib'); const parser = new parserlib.css.Parser({ starHack: true, // Treat properties with a leading asterisk as if the asterisk wasn't there underscoreHack: true // Treat properties with a leading underscore as if the underscore wasn't there }); const someCSSText = ` /* This is a sample CSS block */ @media screen and (min-width: 768px) { .container { width: 90%; margin: 0 auto; } } body { font-family: Arial, sans-serif; color: #333; } h1._title { /* underscore hack example */ font-size: 2em; *color: #00f; /* star hack example, if 'starHack' is true */ text-decoration: underline; } `; try { parser.parse(someCSSText); console.log('CSS parsed successfully. Remember, this is an event-driven parser; attach listeners for detailed output.'); } catch (e) { console.error('Parsing error:', e.message); }
Debug
Known issues
breakingMajor 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.
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.
affects: >=1.0.0
gotchaParserLib 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.
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) => { /* ... */ });`
affects: All versions
gotchaThe parser's validation capabilities are not exhaustive; it is 'not guaranteed to thoroughly validate all possible CSS properties.'
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.
affects: All versions
gotchaThe library predominantly supports CommonJS modules (`require()`). Direct ECMAScript Module (`import`) syntax is not natively supported without transpilation or a bundler.
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.
affects: All versions
gotchaParsing large CSS files directly in a browser environment can lead to unresponsiveness, as indicated in the documentation.
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.
affects: All versions (browser environments)
Errors
Common errors & fixes
Syntax Error: Expected <token> at line X, column Y
The input CSS string contains a malformed syntax that the parser cannot recover from.
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.
TypeError: parserlib.css.Parser is not a constructor
The 'parserlib' module or its 'css' property was not correctly loaded or is undefined.
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.
ReferenceError: require is not defined (when using 'require' in browser)
Attempting to use Node.js-specific `require` syntax in a browser environment without a CommonJS module loader/bundler.
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.
Upgrade
Version history
0.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources