Skip to content

Parse Error Detection & Type Safety#25

Merged
jamescook merged 5 commits intomainfrom
errors-v2
Nov 25, 2025
Merged

Parse Error Detection & Type Safety#25
jamescook merged 5 commits intomainfrom
errors-v2

Conversation

@jamescook
Copy link
Owner

@jamescook jamescook commented Nov 25, 2025

Parse Error Detection & Type Safety

This PR adds comprehensive parse error detection to Cataract with minimal performance impact, along with type safety validation for the C extension.

Parse Error Detection

Adds optional raise_parse_errors option to Stylesheet.parse that validates CSS and raises ParseError exceptions for malformed input:

# Enable all error checking
Cataract::Stylesheet.parse(css, raise_parse_errors: true)

# Granular control over specific checks
Cataract::Stylesheet.parse(css, raise_parse_errors: {
  empty_values: true,               # color: ;
  malformed_declarations: true,     # color red (missing colon)
  invalid_selectors: true,          # empty or missing selectors
  invalid_selector_syntax: true,    # ..invalid, ##bad, ???
  malformed_at_rules: true,         # @media {}, @supports {}
  unclosed_blocks: true             # missing closing braces
})

ParseError Object

Errors include position tracking:

begin
  Cataract::Stylesheet.parse(css, raise_parse_errors: true)
rescue Cataract::ParseError => e
  e.message      # => "Empty value for property 'color' at line 3, column 12"
  e.line         # => 3
  e.column       # => 12
  e.error_type   # => :empty_value
end

Type Safety

Added comprehensive type validation for C extension constructor arguments:

  • Stylesheet.parse validates string input
  • Stylesheet.new validates options hash structure
  • Parser options validated (import_fetcher must be Proc, base_uri must be String, etc.)
  • Raises clear TypeError instead of segfaulting on invalid input

Selector Syntax Validation

The invalid_selector_syntax check uses a whitelist approach to validate CSS selector characters:

  • Valid: a-z A-Z 0-9 - _ . # [ ] : * > + ~ ( ) ' " = ^ $ | \ & % / ! and whitespace
  • Invalid sequences: .. (double dot), ## (double hash)
  • Invalid characters: ???, $$$, @@@, etc.
  • Supports: Pseudo-classes (:hover), pseudo-elements (::before), functions (:not())

Fuzzer Improvements

  • Added 22 invalid CSS patterns to fuzzer corpus for crash testing
  • Removed parse error tracking from fuzzer (now raises exceptions instead)

Implementation Details

  • Early exit optimization: checks only run when enabled
  • Both C and Pure Ruby implementations maintain parity
  • Error position calculated from original CSS string for accurate line/column numbers

@jamescook jamescook changed the title Errors v2 Parse Error Detection & Type Safety Nov 25, 2025
@jamescook jamescook marked this pull request as ready for review November 25, 2025 02:01
@jamescook jamescook merged commit 5afc38e into main Nov 25, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant