module Compass::Core::SassExtensions::Functions::CrossBrowserSupport

Constants

CAPABILITY_OPTION_KEYS
CAPABILITY_OPTION_UNBOXER

Public Instance Methods

browser_capabilities() click to toggle source

The known capabilities of browsers.

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 89
def browser_capabilities
  list(Compass::Core::CanIUse.instance.capabilities.map{|c| identifier(c)}, :comma)
end
browser_prefix(browser, version = nil) click to toggle source

the prefix for the given browser.

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 120
def browser_prefix(browser, version = nil)
  assert_type browser, :String
  identifier(Compass::Core::CanIUse.instance.prefix(browser.value))
rescue ArgumentError => e
  raise Sass::SyntaxError.new(e.message)
end
browser_prefixes(browsers) click to toggle source

The prefixes used by the given browsers.

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 130
def browser_prefixes(browsers)
  browsers = list(browsers, :comma) if browsers.is_a?(Sass::Script::Value::String)
  assert_type browsers, :List
  browser_strings = browsers.value.map {|b| assert_type(b, :String); b.value }
  prefix_strings = Compass::Core::CanIUse.instance.prefixes(browser_strings)
  list(prefix_strings.map {|p| identifier(p)}, :comma)
rescue ArgumentError => e
  raise Sass::SyntaxError.new(e.message)
end
browser_ranges(capability, prefix = null(), include_unprefixed_versions = bool(true)) click to toggle source

Returns a map of browsers to the first version the capability became available without a prefix.

If a prefix is provided, only those browsers using that prefix will be returned and the minimum version will be when it first became available as a prefix or without a prefix.

If a browser does not have the capability, it will not included in the map.

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 217
def browser_ranges(capability, prefix = null(), include_unprefixed_versions = bool(true))
  assert_type capability, :String
  assert_type(prefix, :String) unless prefix == null()
  mins = Compass::Core::CanIUse.instance.browser_ranges(capability.value,
                                                        prefix.value,
                                                        include_unprefixed_versions.to_bool)
  Sass::Script::Value::Map.new(mins.inject({}) do |m, (h, range)|
    m[identifier(h)] = list(range.map{|version| quoted_string(version)}, :space)
    m
  end)
end
browser_requires_prefix(browser, version, capability, capability_options) click to toggle source

whether the browser uses a prefix for the given capability at the version specified or a later version. Returns the prefix it requires, or null.

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 105
def browser_requires_prefix(browser, version, capability, capability_options)
  assert_type browser, :String
  assert_type version, :String
  assert_type capability, :String
  p = Compass::Core::CanIUse.instance.requires_prefix(browser.value,
                                                version.value,
                                                capability.value,
                                                unbox_capability_options_list(capability_options))
  p ? identifier(p) : null()
rescue ArgumentError => e
  raise Sass::SyntaxError.new(e.message)
end
browser_versions(browser) click to toggle source

The versions for the given browser.

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 95
def browser_versions(browser)
  assert_type browser, :String
  list(Compass::Core::CanIUse.instance.versions(browser.value).map{|v| quoted_string(v)}, :comma)
rescue ArgumentError => e
  raise Sass::SyntaxError.new(e.message)
end
browsers(prefix = nil) click to toggle source

The known browsers.

If prefix is given, limits the returned browsers to those using the specified prefix.

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 76
def browsers(prefix = nil)
  browsers = if prefix
               assert_type prefix, :String
               Compass::Core::CanIUse.instance.browsers_with_prefix(prefix.value)
             else
               Compass::Core::CanIUse.instance.browsers
             end
  list(browsers.map{|b| identifier(b)}, :comma)
end
compare_browser_versions(browser, version1, version2) click to toggle source

Compares two browser versions. Returning:

  • 0 if they are the same

  • <0 if the first version is less than the second

  • >0 if the first version is more than the second

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 189
def compare_browser_versions(browser, version1, version2)
  assert_type browser, :String, :browser
  assert_type version1, :String, :version1
  assert_type version2, :String, :version2
  index1 = index2 = nil
  Compass::Core::CanIUse.instance.versions(browser.value).each_with_index do |v, i|
    index1 = i if v == version1.value
    index2 = i if v == version2.value
    break if index1 && index2
  end
  unless index1
    raise Sass::SyntaxError.new("#{version1} is not a version for #{browser}")
  end
  unless index2
    raise Sass::SyntaxError.new("#{version2} is not a version for #{browser}")
  end
  number(index1 <=> index2)
end
css2_fallback(value, css2_value) click to toggle source
# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 69
def css2_fallback(value, css2_value)
  CSS2FallbackValue.new(value, css2_value)
end
next_version(browser, version) click to toggle source

The version before the version for the browser specified

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 164
def next_version(browser, version)
  assert_type browser, :String
  assert_type version, :String
  next_version = Compass::Core::CanIUse.instance.next_version(browser.value, version.value)
  next_version.nil? ? null() : quoted_string(next_version)
end
omitted_usage(browser, min_version, max_version = nil) click to toggle source

The percent of users that are omitted by setting the min_version of browser as specified.

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 143
def omitted_usage(browser, min_version, max_version = nil)
  assert_type browser, :String
  assert_type min_version, :String, :min_version
  assert_type(max_version, :String, :max_version) if max_version
  versions = [min_version.value]
  versions << max_version.value if max_version
  number(Compass::Core::CanIUse.instance.omitted_usage(browser.value, *versions))
end
prefix(prefix, *objects) click to toggle source
# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 48
def prefix(prefix, *objects)
  assert_type prefix, :String if prefix.is_a?(Sass::Script::Value::Base)
  prefix = prefix.value if prefix.is_a?(Sass::Script::Value::String)
  prefix = prefix[1..-1] if prefix[0] == ?-
  if objects.size > 1
    self.prefix(prefix, list(objects, :comma))
  else
    object = objects.first
    if object.is_a?(Sass::Script::Value::List)
      list(object.value.map{|e|
        self.prefix(prefix, e)
      }, object.separator)
    elsif object.respond_to?(:supports?) && object.supports?(prefix) && object.respond_to?(:"to_#{prefix}")
      object.options = options
      object.send(:"to_#{prefix}")
    else
      object
    end
  end
end
prefix_usage(prefix, capability, capability_options) click to toggle source

The percent of users relying on a particular prefix

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 173
def prefix_usage(prefix, capability, capability_options)
  assert_type prefix, :String
  assert_type capability, :String
  number(Compass::Core::CanIUse.instance.prefixed_usage(prefix.value,
                                                  capability.value,
                                                  unbox_capability_options_list(capability_options)))
rescue ArgumentError => e
  raise Sass::SyntaxError.new(e.message)
end
prefixed(prefix, *args) click to toggle source

Check if any of the arguments passed require a vendor prefix.

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 31
def prefixed(prefix, *args)
  assert_type prefix, :String
  aspect = prefix.value.sub(/^-/,"")
  needed = args.any?{|a| a.respond_to?(:supports?) && a.supports?(aspect)}
  bool(needed)
end
previous_version(browser, version) click to toggle source

The version before the version for the browser specified

# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 155
def previous_version(browser, version)
  assert_type browser, :String
  assert_type version, :String
  previous = Compass::Core::CanIUse.instance.previous_version(browser.value, version.value)
  previous.nil? ? null() : quoted_string(previous)
end

Private Instance Methods

unbox_capability_options(capability_options) click to toggle source
# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 257
def unbox_capability_options(capability_options)
  assert_type capability_options, :Map
  result = {}
  capability_options.value.each do |k, v|
    assert_type k, :String
    key = CAPABILITY_OPTION_KEYS[k.value]
    unless key
      raise Sass::SyntaxError, "#{k} is not valid capability option"
    end
    result[key] = CAPABILITY_OPTION_UNBOXER[key].call(v)
  end
  result
end
unbox_capability_options_list(capability_options_list) click to toggle source
# File lib/compass/core/sass_extensions/functions/cross_browser_support.rb, line 233
def unbox_capability_options_list(capability_options_list)
  if capability_options_list.is_a?(Sass::Script::Value::Map)
    [unbox_capability_options(capability_options_list)]
  elsif capability_options_list.is_a?(Sass::Script::Value::List)
    capability_options_list.to_a.map{|opts| unbox_capability_options(opts) }
  else
    assert_type capability_options_list, :List
  end
end