class CssCompare::CSS::Component::Keyframes

Represents an @keyframes declaration

@see www.w3.org/TR/css3-animations/#keyframes

Attributes

name[R]

The name of the keyframes.

@return [String] the name

rules[R]

The rules of the keyframe grouped by @media query conditions.

Rules’ example structure:

{
  'all' => {
    '0%'   => {KeyframeSelector},
    '100%' => {KeyframeSelector}
  },
  '(max-width: 600px)' => {
    '0%'   => {KeyframeSelector},
    '50%'  => {KeyframeSelector},
    '100%' => {KeyframeSelector}
  }
}

@return [Hash{String => Hash{String => KeyframeSelector}}]

Public Class Methods

new(node, conditions) click to toggle source
# File lib/css_compare/css/component/keyframes.rb, line 32
def initialize(node, conditions)
  @name = node.value[1]
  process_conditions(conditions, process_rules(node.children))
end

Public Instance Methods

==(other) click to toggle source

Checks, whether two @keyframes are equal.

Two @keyframes are only equal, if they both have equal keyframe selectors under each and every condition. If a condition or frame is missing from one or another, the @keyframes are not equal.

@param [Keyframes] other the @keyframe to compare this

with.

@param [Boolean]

Calls superclass method CssCompare::CSS::Component::Base#==
# File lib/css_compare/css/component/keyframes.rb, line 47
def ==(other)
  conditions = @rules.keys + other.rules.keys
  conditions.uniq!
  conditions.all? do |condition|
    return false unless @rules[condition] && other.rules[condition]
    super(@rules[condition], other.rules[condition])
  end
end
deep_copy(name = @name) click to toggle source
# File lib/css_compare/css/component/keyframes.rb, line 72
def deep_copy(name = @name)
  copy = dup
  copy.name = name
  copy.rules = @rules.inject({}) do |result, (k, v)|
    result.update(k => v.deep_copy)
  end
end
merge(keyframes) click to toggle source

Merges this selector with another one.

The new declaration of the keyframe under the same condition rewrites the previous one. No deep_copy needs to be made and the value can be passed by reference.

@param [Keyframes] keyframes the keyframes to

extend this one.

@return [Void]

# File lib/css_compare/css/component/keyframes.rb, line 66
def merge(keyframes)
  keyframes.rules.each do |condition, selector|
    @rules[condition] = selector
  end
end
process_conditions(conditions, keyframe_rules) click to toggle source

Assigns the processed rules to the passed conditions By reference. No deep copy needed, as the {KeyframeSelector}s won’t be altered or merged with another {KeyframeSelector}, since this feature is missing at @keyframe directives.

@return [Hash{String => Hash}] @see ‘@rules`

# File lib/css_compare/css/component/keyframes.rb, line 101
def process_conditions(conditions, keyframe_rules)
  @rules = conditions.inject({}) do |kf, condition|
    kf.update(condition => keyframe_rules)
  end
end
process_rules(rule_nodes) click to toggle source

Processes the keyframe rules and creates their internal representation.

@return [Hash{String => KeyframeSelector}]

# File lib/css_compare/css/component/keyframes.rb, line 111
def process_rules(rule_nodes)
  rule_nodes.each_with_object({}) do |node, rules|
    if node.is_a?(Sass::Tree::KeyframeRuleNode)
      rule = Component::KeyframesSelector.new(node)
      rules.update(rule.value => rule)
    end
    rules
  end
end
to_json() click to toggle source

Creates the JSON representation of this keyframes.

@return [Hash]

# File lib/css_compare/css/component/keyframes.rb, line 83
def to_json
  json = { :name => @name.to_sym, :rules => {} }
  @rules.each_with_object(json[:rules]) do |(cond, rules), frames|
    rules.each_with_object(frames[cond.to_sym] = {}) do |(value, rule), result|
      result.update(value.to_sym => rule.to_json)
    end
    frames
  end
  json
end