CSS as a Query Language

· web databases · Source ↗

TLDR

  • CSS selectors are structurally identical to Datalog rules: selector = body, declarations = head, making CSS a latent logic/query language.

Key Takeaways

  • CSS selectors already perform set intersection (combinators) and pattern matching on a fact base (the DOM), mirroring Datalog’s core mechanics.
  • Real CSS cannot express recursive relational queries, e.g. transitively propagating a data-theme class through arbitrary nesting depths.
  • CSSLog, the proposed extension, adds class mutation (class: +bar) and recursive rule firing, enabling fixpoint evaluation over DOM facts.
  • Fixpoint evaluation: the engine repeatedly applies all rules until no new facts are derived, the same naive evaluation loop used by Datalog engines.
  • SQL’s WITH RECURSIVE exists for exactly this gap; CSSLog proposes closing the same gap at the styling layer without a separate query language.

Hacker News Comment Review

  • Commenters split on whether this is genuinely useful or an elaborate curiosity; the recursive theming example convinced some but others couldn’t map it to real problems they face.
  • The CSS-as-XPath angle got traction: CSS selectors are already used outside browsers (pyastgrep queries Python ASTs with them), suggesting the query-language framing has practical precedent.
  • LLM reasoning ability over CSS versus Datalog syntax surfaced as a speculative but concrete angle: CSS’s familiarity may make it a better substrate for AI-generated queries than raw Datalog.

Notable Comments

  • @spookylukey: pyastgrep lets you run CSS selectors against Python ASTs today, e.g. Call > func > Name#main, validating the non-browser query use case.
  • @aisalwaysa: “LLMs suck at CSS now, very tempting to try this” – argues CSS familiarity might make CSSLog a better LLM query target than Datalog.

Original | Discuss on HN