tree-sitter-css-strict
v0.1.3
Published
Strict CSS grammar for tree-sitter
Maintainers
Readme
tree-sitter-css-strict
A tree-sitter grammar for strict CSS, for standalone stylesheets. It is a maintained fork of tree-sitter/tree-sitter-css that tracks upstream CSS syntax but treats // as invalid outside strings and URLs, as browsers do. The upstream grammar accepts JavaScript-style line comments for injected CSS, which can hide broken stylesheets in a standalone file. This grammar also accepts valid unquoted relative URLs such as url(../fonts/a.eot); protocol-relative URLs such as url(//cdn.example.com/a.png), double slashes in strings, and /* block comments */ remain valid.
Installation
Rust:
cargo add tree-sitter tree-sitter-css-strictNode:
npm install tree-sitter-css-strictA GitHub Packages copy is also published as @alfpricot/tree-sitter-css-strict.
Requirements
- Rust 1.85 or later (
rust-versioninCargo.toml) for the crate. - A C compiler, because both bindings compile the generated parser (
src/parser.c) andsrc/scanner.c; the Node binding falls back to compiling when no prebuilt binary matches the platform. - Node 24 for development (
.nvmrc); thetree-sitterpeer dependency is^0.25.0and is optional.
Usage
Rust:
let mut parser = tree_sitter::Parser::new();
let language = tree_sitter_css_strict::LANGUAGE;
parser
.set_language(&language.into())
.expect("Error loading strict CSS parser");
let tree = parser.parse("a { color: red; }", None).unwrap();
assert!(!tree.root_node().has_error());Node:
import Parser from 'tree-sitter';
import CSS from 'tree-sitter-css-strict';
const parser = new Parser();
parser.setLanguage(CSS);The Rust crate exports HIGHLIGHTS_QUERY; the Node binding exposes it as CSS.HIGHLIGHTS_QUERY. See ARCHITECTURE.md for how the grammar, bindings, and queries fit together.
How to contribute
Branch from dev, run tree-sitter generate after every grammar.js change, and commit the regenerated parser files with it. Run tree-sitter test, cargo test, npm run lint, and npm run format:check before opening a pull request. CONTRIBUTING.md has the exact commands.
