Class: Cataract::AtRule

Inherits:
Object
  • Object
show all
Defined in:
lib/cataract/at_rule.rb

Overview

Represents a CSS at-rule like @keyframes, @font-face, @supports, etc.

AtRule is a C struct defined as: ‘Struct.new(:id, :selector, :content, :specificity)`

At-rules define CSS resources or control structures rather than selecting elements. Unlike regular rules, they don’t have CSS specificity and are filtered out when using ‘select(&:selector?)`.

The content field varies by at-rule type:

  • ‘@keyframes`: Array of Rule (keyframe percentage blocks like “0%”, “100%”)

  • ‘@font-face`: Array of Declaration (font property declarations)

  • ‘@supports`: Array of Rule (conditional rules)

Examples:

Parse @keyframes

css = "@keyframes fade { 0% { opacity: 0; } 100% { opacity: 1; } }"
sheet = Cataract.parse_css(css)
at_rule = sheet.rules.first
at_rule.selector #=> "@keyframes fade"
at_rule.content #=> [Rule, Rule] (two keyframe blocks)

Parse @font-face

css = "@font-face { font-family: 'MyFont'; src: url('font.woff'); }"
sheet = Cataract.parse_css(css)
at_rule = sheet.rules.first
at_rule.selector #=> "@font-face"
at_rule.content #=> [Declaration, Declaration]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentArray<Rule>, Array<Declaration>

Nested rules or declarations

Returns:

  • (Array<Rule>, Array<Declaration>)

    the current value of content



35
36
37
# File 'lib/cataract/at_rule.rb', line 35

def content
  @content
end

#idInteger

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

Returns:

  • (Integer)

    the current value of id



35
36
37
# File 'lib/cataract/at_rule.rb', line 35

def id
  @id
end

#selectorString

The at-rule identifier (e.g., “@keyframes fade”, “@font-face”)

Returns:

  • (String)

    the current value of selector



35
36
37
# File 'lib/cataract/at_rule.rb', line 35

def selector
  @selector
end

#specificitynil

Always nil for at-rules (they don’t have CSS specificity)

Returns:

  • (nil)

    the current value of specificity



35
36
37
# File 'lib/cataract/at_rule.rb', line 35

def specificity
  @specificity
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare at-rules by their attributes rather than object identity.

Two at-rules are equal if they have the same id, selector, and content.

Parameters:

  • other (Object)

    Object to compare with

Returns:

  • (Boolean)

    true if at-rules have same attributes



88
89
90
91
92
93
94
# File 'lib/cataract/at_rule.rb', line 88

def ==(other)
  return false unless other.is_a?(AtRule)

  id == other.id &&
    selector == other.selector &&
    content == other.content
end

#at_rule?Boolean

Check if this is an at-rule.

Returns:

  • (Boolean)

    Always returns true for AtRule objects



46
47
48
# File 'lib/cataract/at_rule.rb', line 46

def at_rule?
  true
end

#at_rule_type?(type) ⇒ Boolean

Check if this is a specific at-rule type.

Examples:

Check for @keyframes

at_rule.at_rule_type?(:keyframes) #=> true if selector is "@keyframes ..."

Check for @font-face

at_rule.at_rule_type?(:font_face) #=> true if selector is "@font-face"

Parameters:

  • type (Symbol)

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

Returns:

  • (Boolean)

    true if at-rule matches the type



60
61
62
63
# File 'lib/cataract/at_rule.rb', line 60

def at_rule_type?(type)
  type_str = "@#{type.to_s.tr('_', '-')}"
  selector.start_with?(type_str)
end

#has_important?(_property = nil) ⇒ Boolean

Check if this at-rule has any !important declarations.

Parameters:

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

    Optional property name

Returns:

  • (Boolean)

    Always returns false for AtRule objects



78
79
80
# File 'lib/cataract/at_rule.rb', line 78

def has_important?(_property = nil)
  false
end

#has_property?(_property, _value = nil) ⇒ Boolean

Check if this at-rule has a declaration with the specified property.

Parameters:

  • _property (String)

    CSS property name

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

    Optional value to match

Returns:

  • (Boolean)

    Always returns false for AtRule objects



70
71
72
# File 'lib/cataract/at_rule.rb', line 70

def has_property?(_property, _value = nil)
  false
end

#selector?Boolean

Check if this is a selector-based rule (vs an at-rule like @keyframes).

Returns:

  • (Boolean)

    Always returns false for AtRule objects



39
40
41
# File 'lib/cataract/at_rule.rb', line 39

def selector?
  false
end