class Sass::Media::Query
A single media query.
[ [ONLY | NOT]? S* media_type S* | expression ] [ AND S* expression ]*
Attributes
Public Class Methods
@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
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
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
See {#modifier}. @return [String]
# File lib/sass/media.rb, line 104 def resolved_modifier # modifier should contain only a single string modifier.first || '' end
See {#type}. @return [String]
# File lib/sass/media.rb, line 111 def resolved_type # type should contain only a single string type.first || '' end
@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
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
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