Class: Cataract::Rule

Inherits:
Object
  • Object
show all
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.

Examples:

Access rule properties

sheet = Cataract.parse_css("body { color: red; font-size: 14px; }")
rule = sheet.rules.first
rule.selector #=> "body"
rule.specificity #=> 1
rule.declarations.length #=> 2

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#declarationsArray<Declaration>

Array of CSS property declarations

Returns:

  • (Array<Declaration>)

    the current value of declarations



28
29
30
# File 'lib/cataract/rule.rb', line 28

def declarations
  @declarations
end

#idInteger

The rule’s position in the stylesheet (0-indexed)

Returns:

  • (Integer)

    the current value of id



28
29
30
# File 'lib/cataract/rule.rb', line 28

def id
  @id
end

#selectorString

The CSS selector (e.g., “body”, “.class”, “#id”)

Returns:

  • (String)

    the current value of selector



28
29
30
# File 'lib/cataract/rule.rb', line 28

def selector
  @selector
end

#specificityInteger

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

Examples:

Get specificity

rule = Cataract.parse_css("#header .nav a").rules.first
rule.specificity #=> 111 (1 ID + 1 class + 1 element)

Returns:

  • (Integer)

    CSS specificity value



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.

Parameters:

  • other (Object)

    Object to compare with

Returns:

  • (Boolean)

    true if rules have same attributes



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.

Returns:

  • (Boolean)

    Always returns false for Rule objects



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.

Parameters:

  • _type (Symbol)

    At-rule type (e.g., :keyframes, :font_face)

Returns:

  • (Boolean)

    Always returns false for Rule objects



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.

Examples:

Check for any !important

rule.has_important? #=> true

Check for color !important

rule.has_important?('color') #=> true

Parameters:

  • property (String, nil) (defaults to: nil)

    Optional property name to match

Returns:

  • (Boolean)

    true if rule has matching !important declaration



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.

Examples:

Check for color property

rule.has_property?('color') #=> true

Check for specific property value

rule.has_property?('color', 'red') #=> true

Parameters:

  • property (String)

    CSS property name to match

  • value (String, nil) (defaults to: nil)

    Optional value to match

Returns:

  • (Boolean)

    true if rule has matching declaration



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).

Returns:

  • (Boolean)

    Always returns true for Rule objects



59
60
61
# File 'lib/cataract/rule.rb', line 59

def selector?
  true
end