Sha256: f881386b6cd1b6c8e089fd871be519408b85d377879d1ffd724b18297670b8fc

Contents?: true

Size: 1.47 KB

Versions: 27

Compression:

Stored size: 1.47 KB

Contents

module Compass::SassExtensions::Functions::Selectors
  COMMA_SEPARATOR = /\s*,\s*/

  # Permute multiple selectors each of which may be comma delimited, the end result is
  # a new selector that is the equivalent of nesting each under the previous selector.
  # To illustrate, the following mixins are equivalent:
  # =mixin-a(!selector1, !selector2, !selector3)
  #   #{!selector1}
  #     #{selector2}
  #       #{selector3}
  #         width: 2px
  # =mixin-b(!selector1, !selector2)
  #   #{nest(!selector, !selector2, !selector3)}
  #     width: 2px
  def nest(*arguments)
    nested = arguments.map{|a| a.value}.inject do |memo,arg|
      ancestors = memo.split(COMMA_SEPARATOR)
      descendants = arg.split(COMMA_SEPARATOR)
      ancestors.map{|a| descendants.map{|d| "#{a} #{d}"}.join(", ")}.join(", ")
    end
    Sass::Script::String.new(nested)
  end

  # Permute two selectors, the first may be comma delimited.
  # The end result is a new selector that is the equivalent of nesting the second
  # selector under the first one in a sass file and preceding it with an &.
  # To illustrate, the following mixins are equivalent:
  # =mixin-a(!selector, !to_append)
  #   #{!selector}
  #     &#{!to_append}
  #       width: 2px
  # =mixin-b(!selector, !to_append)
  #   #{append_selector(!selector, !to_append)}
  #     width: 2px
  def append_selector(selector, to_append)
    Sass::Script::String.new(selector.value.split(COMMA_SEPARATOR).map{|s| "#{s}#{to_append}"}.join(", "))
  end

end

Version data entries

27 entries across 27 versions & 4 rubygems

Version Path
alphasights-compass-0.8.12 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.7.0 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.0 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.1 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.10 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.11 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.12 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.13 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.14 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.15 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.16 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.17 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.2 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.3 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.4 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.5 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.6 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.7 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.8 lib/compass/sass_extensions/functions/selectors.rb
chriseppstein-compass-0.8.9 lib/compass/sass_extensions/functions/selectors.rb