Class: CssCompare::CSS::Component::Supports

Inherits:
Base
  • Object
show all
Includes:
CssCompare::CSS::Component
Defined in:
lib/css_compare/css/component/supports.rb

Overview

Represents the @support CSS rule.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CssCompare::CSS::Component

#media_node, #root_node

Methods inherited from Base

#equals?

Constructor Details

#initialize(node, query_list = []) ⇒ Supports

Returns a new instance of Supports.

Parameters:

  • node (Sass::Tree::SupportsNode)
  • query_list (Array<String>) (defaults to: [])

    the query list of the parent node (the conditions under which this node is evaluated).



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/css_compare/css/component/supports.rb', line 29

def initialize(node, query_list = [])
  @name = node.name
  @rules = {}
  condition = node.condition.to_css.gsub(/\s*!important\s*/, '')
  unless query_list.empty?
    media_node = media_node([Engine::GLOBAL_QUERY], node.children, node.options)
    node = root_node(media_node, node.options)
  end
  rules = CssCompare::CSS::Engine.new(node).evaluate(nil, query_list)
  @rules[condition] = rules
end

Instance Attribute Details

#nameString (readonly)

The name of the @support directive. Can be browser-prefixed.

Returns:

  • (String)


14
15
16
# File 'lib/css_compare/css/component/supports.rb', line 14

def name
  @name
end

#rulesHash{String => CssCompare::CSS::Engine}

The assigned rules grouped by the @supports’ conditions.

stylesheet. Why not to create a new engine for it?

Returns:



23
24
25
# File 'lib/css_compare/css/component/supports.rb', line 23

def rules
  @rules
end

Instance Method Details

#==(other) ⇒ Boolean

Checks, whether two @supports rules are equal.

They are only equal, if all of their rules are equal.

Parameters:

  • other (Supports)

    the supports rule to compare this to.

Returns:

  • (Boolean)


49
50
51
# File 'lib/css_compare/css/component/supports.rb', line 49

def ==(other)
  super(@rules, other.rules)
end

#deep_copy(name = @name) ⇒ Supports

Returns a deep copy of this object.

Returns:



73
74
75
76
77
78
79
# File 'lib/css_compare/css/component/supports.rb', line 73

def deep_copy(name = @name)
  copy = dup
  copy.name = name
  copy.rules = {}
  @rules.each { |k, v| copy.rules[k] = v.deep_copy }
  copy
end

#merge(other) ⇒ Void

Merges this @supports rule with another one.

Parameters:

Returns:

  • (Void)


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/css_compare/css/component/supports.rb', line 57

def merge(other)
  other.rules.each do |cond, engine|
    if @rules[cond]
      merge_selectors(engine.selectors, cond)
      merge_keyframes(engine.keyframes, cond)
      merge_namespaces(engine.namespaces, cond)
      merge_supports(engine.supports, cond)
    else
      @rules[cond] = engine
    end
  end
end

#to_jsonHash

Creates the JSON representation of this object.

Returns:

  • (Hash)


84
85
86
87
88
89
90
# File 'lib/css_compare/css/component/supports.rb', line 84

def to_json
  json = { :name => @name.to_sym, :rules => {} }
  @rules.inject(json[:rules]) do |result, (k, v)|
    result.update(k => v.to_json)
  end
  json
end