class CssCompare::CSS::Component::Property
Represents a CSS
property tied to a specific selector. It can have several values based on the conditions specified by the @media queries.
Constants
- COMPLEX_PROPERTIES
Attributes
@return [String] name of the property
Key-value pair of property values.
The key represents the condition set by a media query and the value is the actual value of the property.
@return [Hash{String=>Value}] name of the property
Public Class Methods
@note An optimization has been done here, as well.
If there are several conditions, all should be paired with the same value but not the same object, since the value can be overridden later in the process of the stylesheet evaluation. A clone of the value is paired with each of the conditions.
@param [Sass::Tree::PropNode] node the property node @param [Array<String>] conditions media query conditions
# File lib/css_compare/css/component/property.rb, line 28 def initialize(node, conditions) @name = node.resolved_name @values = {} value = Value.new(node) conditions.each { |c| set_value(value.clone, c) } end
Public Instance Methods
Checks whether two properties are equal.
Properties are equal only if both contain the same keys and the values under those keys are also equal.
@param [Property] other the property to compare this
with.
@return [Boolean]
CssCompare::CSS::Component::Base#==
# File lib/css_compare/css/component/property.rb, line 44 def ==(other) return super(@values, other.values) unless font? equals?(@values, other.values) end
Whether the property is a complex one. One property, that can be separated into smaller chunks of elementary properties.
Example:
`border: 1px solid black` => `{ border-width: 1px; border-style: solid; border-color: black; }`
@see process
@return [Boolean]
# File lib/css_compare/css/component/property.rb, line 111 def complex? COMPLEX_PROPERTIES.include?(@name) false end
Creates a deep copy of this property.
@return [Property]
# File lib/css_compare/css/component/property.rb, line 119 def deep_copy copy = dup copy.values = {} @values.each { |k, v| copy.values[k] = v.clone } copy end
Merges the property with another one.
@param [Property] property to be merged with ‘self` @return [Void]
# File lib/css_compare/css/component/property.rb, line 53 def merge(property) property.values.each { |cond, v| set_value(v, cond) } end
Replaces the original value of the property under a certain media query condition.
If the value does not exist under the specified condition, it is added into the set of property values. Otherwise, it rewrites the current value if it’s not set as important. However, if the replacing value is also set as important, the current value will be replaced with the new one.
If the condition does not exist but there is an important global value assigned to the property, the replacing value will be used only, if it’s set to important, too. Otherwise, the global value should be cloned in there.
@param [Value] val that should replace the current value @param [String] condition the circumstance, under which
the property should take the new value.
@return [Void]
# File lib/css_compare/css/component/property.rb, line 75 def set_value(val, condition = 'all') global_value = @values['all'] val_to_replace = value(condition) # Check, whether the condition exists if val_to_replace @values[condition].value = val if val.important? || !val_to_replace.important? else @values[condition] = if global_value && global_value.important? val.important? ? val : global_value.clone else val end end end
Creates the JSON representation of this property.
@return [Hash]
# File lib/css_compare/css/component/property.rb, line 129 def to_json @values.inject({}) { |result, (k, v)| result.update(k => v.to_s) } end
Returns the property’s value taken under a certain circumstance
@return [#to_s]
# File lib/css_compare/css/component/property.rb, line 94 def value(condition = 'all') @values[condition] end
Private Instance Methods
# File lib/css_compare/css/component/property.rb, line 145 def font? @name['font'] end
Checks, whether an unprocessed value is important or not
@return [Boolean]
# File lib/css_compare/css/component/property.rb, line 141 def important_value?(val) val.to_s.include?('!important') end
Breaks down complex properties like ‘border` into smaller chunks (`border-width`, `border-style`, `border-color`)
@return [Array<Property>]
# File lib/css_compare/css/component/property.rb, line 153 def process return nil unless complex? [] end