GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add semantic validation for CSS property names and value types by Copilot · Pull Request #99 · HtmlUnit/htmlunit-cssparser

Conversation

Copilot AI commented Dec 22, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

The CSS parser performs syntactic parsing but lacks semantic validation, silently accepting typos like colr: red and invalid combinations like color: 10px.

Changes

Core Validation (CSSValidator)

  • Property name validation with Levenshtein distance-based typo suggestions (distance ≤ 2)
  • Value type checking against property requirements (color, length, percentage, etc.)
  • Automatic allowlisting for custom properties (--*), vendor prefixes (-webkit-, -moz-, -ms-, -o-), and global keywords (inherit, initial, unset, revert, revert-layer)

Integration

  • Modified AbstractCSSParser.handleProperty() to invoke validator when enabled
  • Added setSemanticValidationEnabled(boolean) configuration API
  • Validation disabled by default (opt-in for backward compatibility)

Data

  • css-properties.txt resource file with 232 known CSS properties
  • ValidationWarning class with INFO/WARNING/ERROR severity levels

Usage

CSSOMParser parser = new CSSOMParser();
parser.setSemanticValidationEnabled(true);
parser.setErrorHandler(new MyErrorHandler());
parser.parseStyleSheet(inputSource, null);

Parsing .example { colr: red; } now reports:

ERROR: Unknown CSS property 'colr'. Did you mean 'color'?

Test Coverage

  • 14 unit tests for validator logic (property matching, Levenshtein distance, special cases)
  • 8 integration tests with actual CSS parsing scenarios

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:

  • commons.apache.org
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.11/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.11/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.11 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.11/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/htmlunit-cssparser/htmlunit-cssparser org.codehaus.plexus.classworlds.launcher.Launcher clean verify -DskipTests=false (dns block)
  • junit.org
    • Triggering command: /usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.11/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.11/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.11 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.11/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/htmlunit-cssparser/htmlunit-cssparser org.codehaus.plexus.classworlds.launcher.Launcher clean verify -DskipTests=false (dns block)

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

  • Configure Actions setup steps to set up my environment, which run before the firewall is enabled
  • Add the appropriate URLs or hosts to the custom allowlist in this repository's Copilot coding agent settings (admins only)
Original prompt

Add Semantic Validation to CSS Parser

Problem

The current CSS parser performs syntactic parsing but lacks semantic validation. This means:

  • Unknown CSS properties are accepted without warnings (e.g., colr: red instead of color: red)
  • Invalid property-value combinations pass validation (e.g., color: 10px)
  • Type mismatches are not detected (e.g., width: red)
  • No helpful suggestions when users make typos
  • Browser compatibility issues are not flagged

Current behavior:

.example {
    colr: red;           /* Typo - silently accepted */
    color: 10px;         /* Wrong value type - accepted */
    background: inherit inherit;  /* Duplicate values - accepted */
    widht: 100%;         /* Typo - accepted */
}

All of the above parse successfully without any warnings, leading to CSS that won't work as expected.

Solution

Implement a comprehensive semantic validation layer that validates:

  1. Property names against known CSS properties
  2. Value types match property requirements
  3. Property-value compatibility
  4. Common typos with suggestions
  5. Deprecated properties/values (optional)

Implementation Plan

1. Create CSSValidator Class

package org.htmlunit.cssparser.parser.validator;

import java.util.*;
import org.htmlunit.cssparser.parser.*;

/**
 * Validates CSS properties and values for semantic correctness.
 * Provides suggestions for common mistakes and typos.
 */
public class CSSValidator {
    
    private final Set<String> knownProperties;
    private final Map<String, Set<ValueType>> propertyValueTypes;
    private final boolean strict;
    
    public enum ValueType {
        COLOR,
        LENGTH,
        PERCENTAGE,
        NUMBER,
        INTEGER,
        URL,
        STRING,
        IDENTIFIER,
        TIME,
        ANGLE,
        FREQUENCY,
        RESOLUTION,
        FUNCTION,
        KEYWORD
    }
    
    public CSSValidator() {
        this(false);
    }
    
    public CSSValidator(boolean strict) {
        this.strict = strict;
        this.knownProperties = loadKnownProperties();
        this.propertyValueTypes = loadPropertyValueTypes();
    }
    
    /**
     * Validates a CSS property declaration.
     * 
     * @param property The property name
     * @param value The lexical unit value
     * @param locator The locator for error reporting
     * @throws CSSParseException if validation fails in strict mode
     * @return List of validation warnings (empty if valid)
     */
    public List<ValidationWarning> validateProperty(String property, LexicalUnit value, Locator locator) {
        List<ValidationWarning> warnings = new ArrayList<>();
        
        // Validate property name
        if (!isKnownProperty(property)) {
            String suggestion = findClosestPropertyMatch(property);
            String message = "Unknown CSS property '" + property + "'.";
            if (suggestion != null) {
                message += " Did you mean '" + suggestion + "'?";
            }
            
            ValidationWarning warning = new ValidationWarning(
                message, 
                locator, 
                ValidationWarning.Level.ERROR
            );
            warnings.add(warning);
            
            if (strict) {
                throw new CSSParseException(message, locator);
            }
            return warnings; // Don't validate value if property is unknown
        }
        
        // Validate value type
        if (value != null && !isValidValueForProperty(property, value)) {
            Set<ValueType> expectedTypes = propertyValueTypes.get(property.toLowerCase(Locale.ROOT));
            String message = "Invalid value type for property '" + property + "'. " +
                           "Expected: " + formatExpectedTypes(expectedTypes) +
                           ", got: " + getValueType(value);
            
            ValidationWarning warning = new ValidationWarning(
                message,
                locator,
                ValidationWarning.Level.WARNING
            );
            warnings.add(warning);
        }
        
        // Validate specific property rules
        warnings.addAll(validatePropertySpecificRules(property, value, locator));
        
        return warnings;
    }
    
    /**
     * Checks if a property name is a known CSS property.
     */
    public boolean isKnownProperty(String property) {
        if (property == null) {
            return false;
        }
        
        // Custom properties (CSS variables) are always valid
        if (property.startsWith("--")) {
            return true;
        }
        
        // Vendor prefixes are allowed
        if (property.startsWith("-webkit-") || 
            property.startsWith("-moz-") || 
            property.startsWith("-ms-") || 
            property.startsWith("-o-")) {
            return true;
        }
        
        return knownProperties.contains(property.toLowerCase(Locale.ROOT));...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

*This pull request was created from Copilot chat.*
>

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Initial plan
4779bd8
Copilot AI assigned Copilot and rbri Dec 22, 2025
Copilot started work on behalf of rbri December 22, 2025 13:14
View session
Copilot AI and others added 3 commits December 22, 2025 13:24
@rbri
Add semantic validation feature with CSSValidator and ValidationWarni…
7d3fc30
…ng classes

Co-authored-by: rbri <[email protected]>
@rbri
Fix checkstyle violations in validator classes
c64650b
Co-authored-by: rbri <[email protected]>
@rbri
Fix import statements to use short form List instead of fully qualified
3666894
Co-authored-by: rbri <[email protected]>
Copilot AI changed the title [WIP] Add semantic validation to CSS parser Add semantic validation for CSS property names and value types Dec 22, 2025
Copilot AI requested a review from rbri December 22, 2025 13:33
Copilot finished work on behalf of rbri December 22, 2025 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@rbri rbri Awaiting requested review from rbri

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@rbri rbri

Copilot code review Copilot

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@rbri
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.