Class: CssCompare::CSS::Component::Selector

Inherits:
Base
  • Object
show all
Defined in:
lib/css_compare/css/component/selector.rb

Overview

Represents one simple selector sequence, like .a.b.c > div.f:first-child.

Direct Known Subclasses

MarginBox

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#equals?

Constructor Details

#initialize(name, properties, conditions) ⇒ Selector

Returns a new instance of Selector.

Parameters:

  • name (String)

    the selector’s name

  • properties (Array<Sass::Tree::PropNode>)

    to be included

  • conditions (Array<String>)

    @media query conditions



22
23
24
25
26
# File 'lib/css_compare/css/component/selector.rb', line 22

def initialize(name, properties, conditions)
  @name = name.strip
  @properties = {}
  process_properties(properties, conditions)
end

Instance Attribute Details

#nameString

Returns selector’s name.

Returns:

  • (String)

    selector’s name



10
11
12
# File 'lib/css_compare/css/component/selector.rb', line 10

def name
  @name
end

#propertiesHash{String => Component::Property}

Hash of the selector’s properties. Could have been an array but this structure has been chosen, so the properties’ lookup gets optimized.

Returns:



17
18
19
# File 'lib/css_compare/css/component/selector.rb', line 17

def properties
  @properties
end

Instance Method Details

#==(other) ⇒ Boolean

Checks, whether two selector are equal.

Two selectors are equal only if they both have declared the same properties and they are also equal.

Parameters:

  • other (Selector)

    the selector to compare this with.

Returns:

  • (Boolean)


35
36
37
# File 'lib/css_compare/css/component/selector.rb', line 35

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

#add_property(prop, deep_copy = false) ⇒ Void

Adds a property to the existing set of properties of this selector.

If the property does not exist, it will be added. Otherwise the values of the properties will be merged.

Parameters:

  • prop (Property)

    the property to add

  • deep_copy (Boolean) (defaults to: false)

    tells, whether a deep copy should be applied onto the property.

Returns:

  • (Void)

See Also:

  • CssCompare::CSS::Component::Selector.{Property{Property#merge}


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/css_compare/css/component/selector.rb', line 63

def add_property(prop, deep_copy = false)
  name = prop.name
  if @properties[name]
    @properties[name].merge(prop)
  else
    @properties[name] = if deep_copy
                          prop.deep_copy
                        else
                          prop
                        end
  end
end

#deep_copy(name = @name) ⇒ Selector

Creates a deep copy of this selector.

Parameters:

  • name (String) (defaults to: @name)

    the new name of the selector’s copy.

Returns:



81
82
83
84
85
86
87
# File 'lib/css_compare/css/component/selector.rb', line 81

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

#merge(other) ⇒ Void

Combines the selector’s properties with the properties of another selector.

Parameters:

Returns:

  • (Void)


45
46
47
48
49
# File 'lib/css_compare/css/component/selector.rb', line 45

def merge(other)
  other.properties.each do |_, prop|
    add_property(prop, true)
  end
end

#to_jsonHash

Creates the JSON representation of this selector.

Returns:

  • (Hash)


92
93
94
95
96
97
98
99
# File 'lib/css_compare/css/component/selector.rb', line 92

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