class Sass::Media::Query

A single media query.

[ [ONLY | NOT]? S* media_type S* | expression ] [ AND S* expression ]*

Attributes

expressions[RW]

The trailing expressions in the query.

When parsed as Sass code, each expression contains strings and SassScript nodes. When parsed as CSS, each one contains a single string.

@return [Array<Array<String, Sass::Script::Tree::Node>>]

modifier[RW]

The modifier for the query.

When parsed as Sass code, this contains strings and SassScript nodes. When parsed as CSS, it contains a single string (accessible via {#resolved_modifier}).

@return [Array<String, Sass::Script::Tree::Node>]

type[RW]

The type of the query (e.g. `“screen”` or `“print”`).

When parsed as Sass code, this contains strings and SassScript nodes. When parsed as CSS, it contains a single string (accessible via {#resolved_type}).

@return [Array<String, Sass::Script::Tree::Node>]

Public Class Methods

new(modifier, type, expressions) click to toggle source

@param modifier [Array<String, Sass::Script::Tree::Node>] See {#modifier} @param type [Array<String, Sass::Script::Tree::Node>] See {#type} @param expressions [Array<Array<String, Sass::Script::Tree::Node>>] See {#expressions}

# File lib/sass/media.rb, line 96
def initialize(modifier, type, expressions)
  @modifier = modifier
  @type = type
  @expressions = expressions
end

Public Instance Methods

deep_copy() click to toggle source

Returns a deep copy of this query and all its children.

@return [Query]

# File lib/sass/media.rb, line 194
def deep_copy
  Query.new(
    modifier.map {|c| c.is_a?(Sass::Script::Tree::Node) ? c.deep_copy : c},
    type.map {|c| c.is_a?(Sass::Script::Tree::Node) ? c.deep_copy : c},
    expressions.map {|e| e.map {|c| c.is_a?(Sass::Script::Tree::Node) ? c.deep_copy : c}})
end
merge(other) click to toggle source

Merges this query with another. The returned query queries for the intersection between the two inputs.

Both queries should be resolved.

@param other [Query] @return [Query?] The merged query, or nil if there is no intersection.

# File lib/sass/media.rb, line 123
def merge(other)
  m1, t1 = resolved_modifier.downcase, resolved_type.downcase
  m2, t2 = other.resolved_modifier.downcase, other.resolved_type.downcase
  t1 = t2 if t1.empty?
  t2 = t1 if t2.empty?
  if (m1 == 'not') ^ (m2 == 'not')
    return if t1 == t2
    type = m1 == 'not' ? t2 : t1
    mod = m1 == 'not' ? m2 : m1
  elsif m1 == 'not' && m2 == 'not'
    # CSS has no way of representing "neither screen nor print"
    return unless t1 == t2
    type = t1
    mod = 'not'
  elsif t1 != t2
    return
  else # t1 == t2, neither m1 nor m2 are "not"
    type = t1
    mod = m1.empty? ? m2 : m1
  end
  Query.new([mod], [type], other.expressions + expressions)
end
resolved_modifier() click to toggle source

See {#modifier}. @return [String]

# File lib/sass/media.rb, line 104
def resolved_modifier
  # modifier should contain only a single string
  modifier.first || ''
end
resolved_type() click to toggle source

See {#type}. @return [String]

# File lib/sass/media.rb, line 111
def resolved_type
  # type should contain only a single string
  type.first || ''
end
to_a() click to toggle source

@see {MediaQuery#to_a}

# File lib/sass/media.rb, line 181
def to_a
  res = []
  res += modifier
  res << ' ' unless modifier.empty?
  res += type
  res << ' and ' unless type.empty? || expressions.empty?
  res += Sass::Util.intersperse(expressions, ' and ').flatten
  res
end
to_css() click to toggle source

Returns the CSS for the media query.

@return [String]

# File lib/sass/media.rb, line 149
def to_css
  css = ''
  css << resolved_modifier
  css << ' ' unless resolved_modifier.empty?
  css << resolved_type
  css << ' and ' unless resolved_type.empty? || expressions.empty?
  css << expressions.map do |e|
    # It's possible for there to be script nodes in Expressions even when
    # we're converting to CSS in the case where we parsed the document as
    # CSS originally (as in css_test.rb).
    e.map {|c| c.is_a?(Sass::Script::Tree::Node) ? c.to_sass : c.to_s}.join
  end.join(' and ')
  css
end
to_src(options) click to toggle source

Returns the Sass/SCSS code for the media query.

@param options [{Symbol => Object}] An options hash (see {Sass::CSS#initialize}). @return [String]

# File lib/sass/media.rb, line 168
def to_src(options)
  src = ''
  src << Sass::Media._interp_to_src(modifier, options)
  src << ' ' unless modifier.empty?
  src << Sass::Media._interp_to_src(type, options)
  src << ' and ' unless type.empty? || expressions.empty?
  src << expressions.map do |e|
    Sass::Media._interp_to_src(e, options)
  end.join(' and ')
  src
end