Class: Cataract::Rule
- Inherits:
-
Object
- Object
- Cataract::Rule
- Defined in:
- lib/cataract/rule.rb
Overview
Represents a CSS rule with a selector and declarations.
Rule is a C struct defined as: ‘Struct.new(:id, :selector, :declarations, :specificity)`
Rules are created by the parser and stored in Stylesheet objects. Each rule contains:
-
An ID (position in the stylesheet)
-
A CSS selector string
-
An array of Declaration structs
-
A specificity value (calculated lazily)
Media query information is stored separately in Stylesheet’s media_index.
Instance Attribute Summary collapse
-
#declarations ⇒ Array<Declaration>
Array of CSS property declarations.
-
#id ⇒ Integer
The rule’s position in the stylesheet (0-indexed).
-
#selector ⇒ String
The CSS selector (e.g., “body”, “.class”, “#id”).
-
#specificity ⇒ Integer
Get the CSS specificity value for this rule’s selector.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compare rules by their attributes rather than object identity.
-
#at_rule? ⇒ Boolean
Check if this is an at-rule.
-
#at_rule_type?(_type) ⇒ Boolean
Check if this is a specific at-rule type.
-
#has_important?(property = nil) ⇒ Boolean
Check if this rule has any !important declarations, optionally for a specific property.
-
#has_property?(property, value = nil) ⇒ Boolean
Check if this rule has a declaration with the specified property and optional value.
-
#selector? ⇒ Boolean
Check if this is a selector-based rule (vs an at-rule like @keyframes).
Instance Attribute Details
#declarations ⇒ Array<Declaration>
Array of CSS property declarations
28 29 30 |
# File 'lib/cataract/rule.rb', line 28 def declarations @declarations end |
#id ⇒ Integer
The rule’s position in the stylesheet (0-indexed)
28 29 30 |
# File 'lib/cataract/rule.rb', line 28 def id @id end |
#selector ⇒ String
The CSS selector (e.g., “body”, “.class”, “#id”)
28 29 30 |
# File 'lib/cataract/rule.rb', line 28 def selector @selector end |
#specificity ⇒ Integer
Get the CSS specificity value for this rule’s selector.
Specificity is calculated lazily on first access and then cached. The calculation follows the CSS specification:
-
Inline styles: not applicable to parsed stylesheets
-
IDs: count of #id selectors
-
Classes/attributes/pseudo-classes: count of .class, [attr], :pseudo
-
Elements/pseudo-elements: count of element, ::pseudo
28 29 30 |
# File 'lib/cataract/rule.rb', line 28 def specificity @specificity end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Compare rules by their attributes rather than object identity.
Two rules are equal if they have the same id, selector, declarations, and specificity.
121 122 123 124 125 126 127 128 |
# File 'lib/cataract/rule.rb', line 121 def ==(other) return false unless other.is_a?(Rule) id == other.id && selector == other.selector && declarations == other.declarations && specificity == other.specificity end |
#at_rule? ⇒ Boolean
Check if this is an at-rule.
66 67 68 |
# File 'lib/cataract/rule.rb', line 66 def at_rule? false end |
#at_rule_type?(_type) ⇒ Boolean
Check if this is a specific at-rule type.
74 75 76 |
# File 'lib/cataract/rule.rb', line 74 def at_rule_type?(_type) false end |
#has_important?(property = nil) ⇒ Boolean
Check if this rule has any !important declarations, optionally for a specific property.
107 108 109 110 111 112 113 |
# File 'lib/cataract/rule.rb', line 107 def has_important?(property = nil) if property declarations.any? { |d| d.property == property && d.important } else declarations.any?(&:important) end end |
#has_property?(property, value = nil) ⇒ Boolean
Check if this rule has a declaration with the specified property and optional value.
89 90 91 92 93 94 95 |
# File 'lib/cataract/rule.rb', line 89 def has_property?(property, value = nil) declarations.any? do |decl| property_matches = decl.property == property value_matches = value.nil? || decl.value == value property_matches && value_matches end end |
#selector? ⇒ Boolean
Check if this is a selector-based rule (vs an at-rule like @keyframes).
59 60 61 |
# File 'lib/cataract/rule.rb', line 59 def selector? true end |