class CssCompare::CSS::Component::PageSelector

Represents @page directive’s page selector (‘:right`, `LandscapeTable`, `CompanyLetterHead:first`) combined with the page body, that can contain a list of declarations and a list of page margin boxes.

The declarations not assigned to any margin symbol will be automatically grouped under the global margin symbol.

@see www.w3.org/TR/css3-page/

Constants

GLOBAL_MARGIN_SYMBOL

The global margin symbol.

Attributes

margin_boxes[RW]

The margin box directives containing the declarations.

@return [Hash{String => MarginBox}]

value[RW]

The value of this page selector

@return [String]

Public Class Methods

new(value, children, conditions) click to toggle source

@param [String] value the page selector @param [Array<Sass::Tree::Node>] children page body @param [Array<String>] conditions applying media query conditions

# File lib/css_compare/css/component/page_selector.rb, line 32
def initialize(value, children, conditions)
  @value = value
  @margin_boxes = {}
  process_margin_boxes(children, conditions)
end

Public Instance Methods

==(other) click to toggle source

Checks, whether two page selectors are equal.

Two page selectors are equal only if both have declared the same margin boxes and they are also equal.

@param [PageSelector] other the page selector to

compare this with.

@return [Boolean]

Calls superclass method CssCompare::CSS::Component::Base#==
# File lib/css_compare/css/component/page_selector.rb, line 47
def ==(other)
  super(@margin_boxes, other.margin_boxes)
end
deep_copy(value = @value) click to toggle source

Creates a deep copy of this page selector.

@param [String] value the new value of

the selector's copy.

@return [PageSelector] a copy of self

# File lib/css_compare/css/component/page_selector.rb, line 66
def deep_copy(value = @value)
  copy = dup
  copy.value = value
  copy.margin_boxes = @margin_boxes.inject({}) do |result, (k, v)|
    result.update(k => v.deep_copy)
  end
  copy
end
merge(other) click to toggle source

Merges this page selector with another one.

@param [PageSelector] @return [Void]

# File lib/css_compare/css/component/page_selector.rb, line 55
def merge(other)
  other.margin_boxes.each do |_, prop|
    add_margin_box(prop, true)
  end
end
to_json() click to toggle source

Creates the JSON representation of this page selector.

@return [Hash]

# File lib/css_compare/css/component/page_selector.rb, line 78
def to_json
  json = { :selector => @value, :margin_boxes => [] }
  @margin_boxes.inject(json[:margin_boxes]) do |result, (_k, v)|
    result << v.to_json
  end
  json
end

Private Instance Methods

add_margin_box(margin_box, deep_copy = false) click to toggle source

Adds a new or updates an already existing margin box.

@param [MarginBox] margin_box the margin box to be added @param [Boolean] deep_copy checks whether the margin_box

should be added by reference or its deep copied value.
# File lib/css_compare/css/component/page_selector.rb, line 93
def add_margin_box(margin_box, deep_copy = false)
  if @margin_boxes[margin_box.name]
    @margin_boxes[margin_box.name].merge(margin_box)
  else
    @margin_boxes[margin_box.name] = if deep_copy
                                       margin_box.deep_copy
                                     else
                                       margin_box
                                     end
  end
end
process_margin_boxes(nodes, conditions) click to toggle source

Processes and evaluates the page body.

@param [Array<Sass::Tree::Node>] nodes (see initialize) @param [Array<String>] conditions (see initialize)

# File lib/css_compare/css/component/page_selector.rb, line 109
def process_margin_boxes(nodes, conditions)
  nodes.each do |node|
    if node.is_a?(Sass::Tree::PropNode)
      margin_box = GLOBAL_MARGIN_SYMBOL
      children = [node]
    elsif node.is_a?(Sass::Tree::DirectiveNode)
      margin_box = node.resolved_value
      children = node.children
    else
      next # just ignore these nodes
    end
    add_margin_box(MarginBox.new(margin_box, children, conditions))
  end
end