Skip to content

fix: parse CSS rule declarations for RUR metric in figma2code - #51

Open
Jason59000 wants to merge 1 commit into
CGCL-codes:mainfrom
Jason59000:fix/figma2code-rur-tinycss2-parsing
Open

fix: parse CSS rule declarations for RUR metric in figma2code#51
Jason59000 wants to merge 1 commit into
CGCL-codes:mainfrom
Jason59000:fix/figma2code-rur-tinycss2-parsing

Conversation

@Jason59000

Copy link
Copy Markdown

Problem

extract_declarations() in quality.py (line 170-173) filters QualifiedRule.content for Declaration instances, but tinycss2.parse_stylesheet() returns raw component-value tokens (WhitespaceToken, IdentToken, DimensionToken, etc.), not Declaration objects. As a result, CSS declarations inside <style> blocks and external stylesheets contribute zero declarations to the RUR (Relative Unit Share) metric — only inline style="" attributes are counted.

This is consistent with the RUR values in Table 2: all 10 models score between 1.05% and 4.81%, which corresponds to residual inline style usage rather than actual CSS analysis.

Fix

One-line change: call parse_declaration_list() (already imported) on the rule content before filtering for Declaration instances.

Reproduction

import tinycss2
from tinycss2.ast import Declaration

css = 'body { width: 100%; height: 50vh; padding: 1rem; }'
sheet = tinycss2.parse_stylesheet(css)
rule = sheet[0]

# Before fix: 0 declarations found
buggy = [x for x in rule.content if isinstance(x, Declaration)]
print(len(buggy))  # 0

# After fix: 3 declarations found
fixed = tinycss2.parse_declaration_list(rule.content)
print(len([x for x in fixed if isinstance(x, Declaration)]))  # 3

See also: tinycss2 docs — Common Use Cases

extract_declarations() filters QualifiedRule.content for Declaration
instances, but tinycss2.parse_stylesheet() returns raw component-value
tokens, not Declaration objects. This causes all CSS in <style> blocks
to contribute zero declarations to the RUR metric.

Add parse_declaration_list() call (already imported) to convert tokens
to Declaration objects before filtering.
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