Class: Sass::Media::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/sass/media.rb

Overview

A single media query.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modifier, type, expressions) ⇒ Query

Returns a new instance of Query.

Parameters:



96
97
98
99
100
# File 'lib/sass/media.rb', line 96

def initialize(modifier, type, expressions)
  @modifier = modifier
  @type = type
  @expressions = expressions
end

Instance Attribute Details

#expressionsArray<Array<String, Sass::Script::Tree::Node>>

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.

Returns:



91
92
93
# File 'lib/sass/media.rb', line 91

def expressions
  @expressions
end

#modifierArray<String, Sass::Script::Tree::Node>

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).

Returns:



74
75
76
# File 'lib/sass/media.rb', line 74

def modifier
  @modifier
end

#typeArray<String, Sass::Script::Tree::Node>

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).

Returns:



83
84
85
# File 'lib/sass/media.rb', line 83

def type
  @type
end

Instance Method Details

#deep_copyQuery

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

Returns:



194
195
196
197
198
199
# 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) ⇒ Query?

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

Both queries should be resolved.

Parameters:

Returns:

  • (Query?)

    The merged query, or nil if there is no intersection.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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_modifierString

See #modifier.

Returns:

  • (String)


104
105
106
107
# File 'lib/sass/media.rb', line 104

def resolved_modifier
  # modifier should contain only a single string
  modifier.first || ''
end

#resolved_typeString

See #type.

Returns:

  • (String)


111
112
113
114
# File 'lib/sass/media.rb', line 111

def resolved_type
  # type should contain only a single string
  type.first || ''
end

#to_a

See Also:

  • Sass::Media::Query.\{MediaQuery\{MediaQuery#to\_a}


181
182
183
184
185
186
187
188
189
# 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_cssString

Returns the CSS for the media query.

Returns:

  • (String)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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) ⇒ String

Returns the Sass/SCSS code for the media query.

Parameters:

  • options ({Symbol => Object})

    An options hash (see CSS#initialize).

Returns:

  • (String)


168
169
170
171
172
173
174
175
176
177
178
# 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