Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 5, 2025

This PR implements a comprehensive CSS tokenizer and parser to support advanced CSS features and improve maintainability throughout the JSAR Runtime engine.

Implementation Overview

The implementation provides two main components:

CSS Tokenizer (css_tokenizer.hpp/cpp)

A complete CSS tokenizer that handles all token types defined in the CSS Syntax Module Level 3:

  • Basic tokens: identifiers, functions, strings, numbers, dimensions, percentages
  • CSS-specific tokens: at-keywords (@media, @keyframes), hash tokens (#id, #color)
  • Selector tokens: attribute selector operators (^=, $=, *=, |=, ~=)
  • Structural tokens: brackets, braces, parentheses, semicolons, colons, commas
  • Special tokens: CDO/CDC (<!-- -->), comments (filtered during tokenization)

CSS Parser (css_parser.hpp/cpp)

A general-purpose parser that can handle:

Complete Stylesheet Parsing:

  • StyleRule - Standard style rules with complex selectors
  • KeyframeRule - @keyframes animations
  • FontFaceRule - @font-face declarations
  • MediaRule - @media queries with nested rules
  • ImportRule - @import statements

Single Style Declaration Parsing:

  • Extracts property/value pairs from declaration strings
  • Supports complex function values (linear-gradient(), rgba(), transform())
  • Preserves important declarations (!important)
  • Perfect for inline styles and style attributes

Example Usage

#include <client/cssom/parsers/css_tokenizer.hpp>
#include <client/cssom/parsers/css_parser.hpp>

// Parse complete stylesheet
CSSParser parser(R"(
  @media screen and (max-width: 768px) {
    .container { width: 100%; }
  }
  body { 
    background: linear-gradient(to right, red, blue);
    transform: rotate(45deg) scale(1.2);
  }
)");

auto rules = parser.parseStylesheet();
// Returns vector of parsed CSS rules

// Parse style declaration
auto result = parser.parseStyleDeclaration("color: red; font-size: 16px;");
// Returns: {properties: {"color": "red", "font-size": "16px"}, valid: true}

Key Features

  • CSS Syntax Module Level 3 compliance for robust tokenization
  • Complex selector support including combinators, pseudo-classes, and attribute selectors
  • Function value preservation for gradients, transforms, and other CSS functions
  • Error recovery with graceful handling of malformed CSS
  • Memory-safe C++ implementation using RAII and smart pointers
  • Comprehensive test coverage with both unit and integration tests

Architecture

The implementation follows existing codebase patterns and integrates seamlessly with the current CSS processing infrastructure. It extends the existing CSSValueTokenizer approach to handle full CSS syntax while maintaining consistency with parsers like CSSFilterParser and CSSTransformParser.

This provides a solid foundation for implementing more advanced CSS features throughout the engine, including better stylesheet processing, enhanced style attribute handling, and support for modern CSS syntax.

Fixes #303.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • registry.npmmirror.com
    • Triggering command: npm ci (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits September 5, 2025 18:56
Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
…criteria met

Co-authored-by: yorkie <1935767+yorkie@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Add General CSS Tokenizer and Parser in cssom/parsers Add General CSS Tokenizer and Parser in cssom/parsers Sep 5, 2025
@Copilot Copilot AI requested a review from yorkie September 5, 2025 19:06
Copilot finished work on behalf of yorkie September 5, 2025 19:06
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.

Add General CSS Tokenizer and Parser in cssom/parsers
2 participants