Class: Cataract::AtRule

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

Overview

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:



32
33
34
# File 'lib/cataract/at_rule.rb', line 32

def content
  @content
end

#idInteger

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

Returns:

  • (Integer)

    the current value of id



32
33
34
# File 'lib/cataract/at_rule.rb', line 32

def id
  @id
end

#media_query_idInteger?

ID of MediaQuery if inside @media block, nil otherwise

Returns:

  • (Integer, nil)

    the current value of media_query_id



32
33
34
# File 'lib/cataract/at_rule.rb', line 32

def media_query_id
  @media_query_id
end

#selectorString

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

Returns:

  • (String)

    the current value of selector



32
33
34
# File 'lib/cataract/at_rule.rb', line 32

def selector
  @selector
end

#specificitynil

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

Returns:

  • (nil)

    the current value of specificity



32
33
34
# File 'lib/cataract/at_rule.rb', line 32

def specificity
  @specificity
end

Instance Method Details

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

Compare at-rules for logical equality based on CSS semantics.

Two at-rules are equal if they have the same selector and content. Internal implementation details (id) are not considered since they don't affect the CSS semantics.

Parameters:

  • other (Object)

    Object to compare with

Returns:

  • (Boolean)

    true if at-rules have same selector and content



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

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

  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



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

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



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

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



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

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



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

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



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

def selector?
  false
end