Module: Sass::Script::Functions

Defined in:
lib/simple-compass/sass/functions/selectors.rb,
lib/simple-compass/sass/functions/sprites.rb,
lib/simple-compass/sass/functions/display.rb,
lib/simple-compass/sass/functions/lists.rb

Defined Under Namespace

Modules: VariableReader

Constant Summary collapse

COMMA_SEPARATOR =
/\s*,\s*/
ZERO =
Sass::Script::Number::new(0)
VALID_SELECTORS =
%w(hover active target)
DEFAULT_DISPLAY =
{
  :block => %w{address article aside blockquote center dir div dd details dl dt fieldset
                figcaption figure form footer frameset h1 h2 h3 h4 h5 h6 hr header hgroup
                isindex menu nav noframes noscript ol p pre section summary ul},
  :inline => %w{a abbr acronym audio b basefont bdo big br canvas cite code command
                datalist dfn em embed font i img input keygen kbd label mark meter output
                progress q rp rt ruby s samp select small span strike strong sub
                sup textarea time tt u var video wbr},
  :"inline-block" => %w{img},
  :table => %w{table},
  :"list-item" => %w{li},
  :"table-row-group" => %w{tbody},
  :"table-header-group" => %w{thead},
  :"table-footer-group" => %w{tfoot},
  :"table-row" => %w{tr},
  :"table-cell" => %w{th td},
  :"html5-block" => %w{article aside details figcaption figure footer header hgroup menu nav section summary},
  :"html5-inline" => %w{audio canvas command datalist embed keygen mark meter output progress rp rt ruby time video wbr},
}

Instance Method Summary collapse

Instance Method Details

#_compass_list(arg) ⇒ Object

Returns a list object from a value that was passed. This can be used to unpack a space separated list that got turned into a string by sass before it was passed to a mixin.



43
44
45
46
47
48
49
# File 'lib/simple-compass/sass/functions/lists.rb', line 43

def _compass_list(arg)
  if arg.is_a?(Sass::Script::List)
    Sass::Script::List.new(arg.value.dup, arg.separator)
  else
    Sass::Script::List.new([arg], :space)
  end
end

#_compass_list_size(list) ⇒ Object

Returns the size of the list.



62
63
64
65
# File 'lib/simple-compass/sass/functions/lists.rb', line 62

def _compass_list_size(list)
  assert_list list
  Sass::Script::Number.new(list.value.size)
end

#_compass_nth(list, place) ⇒ Object

Get the nth value from a list



29
30
31
32
33
34
35
36
37
38
# File 'lib/simple-compass/sass/functions/lists.rb', line 29

def _compass_nth(list, place)
  assert_type list, :List
  if place.value == "first"
    list.value.first
  elsif place.value == "last"
    list.value.last
  else
    list.value[place.value - 1]
  end
end

#_compass_slice(list, start_index, end_index = nil) ⇒ Object

slice a sublist from a list



68
69
70
71
72
73
74
75
# File 'lib/simple-compass/sass/functions/lists.rb', line 68

def _compass_slice(list, start_index, end_index = nil)
  end_index ||= Sass::Script::Number.new(-1)
  start_index = start_index.value
  end_index = end_index.value
  start_index -= 1 unless start_index < 0
  end_index -= 1 unless end_index < 0
  Sass::Script::List.new list.values[start_index..end_index], list.separator
end

#_compass_space_list(list) ⇒ Object

If the argument is a list, it will return a new list that is space delimited Otherwise it returns a new, single element, space-delimited list.



53
54
55
56
57
58
59
# File 'lib/simple-compass/sass/functions/lists.rb', line 53

def _compass_space_list(list)
  if list.is_a?(Sass::Script::List)
    Sass::Script::List.new(list.value.dup, :space)
  else
    Sass::Script::List.new([list], :space)
  end
end

#append_selector(selector, to_append) ⇒ Object

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


35
36
37
38
39
40
# File 'lib/simple-compass/sass/functions/selectors.rb', line 35

def append_selector(selector, to_append)
  ancestors = selector.value.split(COMMA_SEPARATOR)
  descendants = to_append.value.split(COMMA_SEPARATOR)
  nested = ancestors.map{|a| descendants.map{|d| "#{a}#{d}"}.join(", ")}.join(", ")
  Sass::Script::String.new(nested)
end

#blank(obj) ⇒ Object

Returns true when the object is false, an empty string, or an empty list



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/simple-compass/sass/functions/lists.rb', line 4

def blank(obj)
  case obj
  when Sass::Script::Bool
    Sass::Script::Bool.new !obj.to_bool
  when Sass::Script::String
    Sass::Script::Bool.new obj.value.strip.size == 0
  when Sass::Script::List
    Sass::Script::Bool.new obj.value.size == 0 || obj.value.all?{|el| blank(el).to_bool}
  else
    Sass::Script::Bool.new false
  end
end

#compact(*args) ⇒ Object

Returns a new list after removing any non-true values



18
19
20
21
22
23
24
25
26
# File 'lib/simple-compass/sass/functions/lists.rb', line 18

def compact(*args)
  sep = :comma
  if args.size == 1 && args.first.is_a?(Sass::Script::List)
    list = args.first
    args = list.value
    sep = list.separator
  end
  Sass::Script::List.new(args.reject{|a| !a.to_bool}, sep)
end

#elements_of_type(display) ⇒ Object

returns a comma delimited string for all the elements according to their default css3 display value.



24
25
26
# File 'lib/simple-compass/sass/functions/display.rb', line 24

def elements_of_type(display)
  Sass::Script::String.new(DEFAULT_DISPLAY.fetch(display.value.to_sym).join(", "))
end

#first_value_of(list) ⇒ Object

returns the first value of a space delimited list.



83
84
85
86
87
88
89
90
91
# File 'lib/simple-compass/sass/functions/lists.rb', line 83

def first_value_of(list)
  if list.is_a?(Sass::Script::String)
    Sass::Script::String.new(list.value.split(/\s+/).first)
  elsif defined?(Sass::Script::List) && list.is_a?(Sass::Script::List)
    list.value.first
  else
    list
  end
end

#headers(from = nil, to = nil) ⇒ Object Also known as: headings

Return the header selectors for the levels indicated Defaults to all headers h1 through h6 For example: headers(all) => h1, h2, h3, h4, h5, h6 headers(4) => h1, h2, h3, h4 headers(2,4) => h2, h3, h4



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simple-compass/sass/functions/selectors.rb', line 48

def headers(from = nil, to = nil)
  if from && !to
    if from.is_a?(Sass::Script::String) && from.value == "all"
      from = Sass::Script::Number.new(1)
      to = Sass::Script::Number.new(6)
    else
      to = from
      from = Sass::Script::Number.new(1)
    end
  else
    from ||= Sass::Script::Number.new(1)
    to ||= Sass::Script::Number.new(6)
  end
  Sass::Script::String.new((from.value..to.value).map{|n| "h#{n}"}.join(", "))
end

#inline_sprite(map) ⇒ Object

Returns the sprite file as an inline image

@include "icon/*.png";
 #{$icon-sprite-base-class} {
   background-image: inline-sprite($icon-sprites);
  }


30
31
32
33
34
# File 'lib/simple-compass/sass/functions/sprites.rb', line 30

def inline_sprite(map)
  verify_map(map, "sprite-url")
  map.generate
  inline_image(sprite_path(map))
end

#nest(*arguments) ⇒ Object

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


15
16
17
18
19
20
21
22
# File 'lib/simple-compass/sass/functions/selectors.rb', line 15

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

#reject(list, *values) ⇒ Object

removes the given values from the list.



78
79
80
# File 'lib/simple-compass/sass/functions/lists.rb', line 78

def reject(list, *values)
  Sass::Script::List.new(list.value.reject{|v| values.any?{|o| v == o}}, list.separator)
end

#sprite(map, sprite, offset_x = ZERO, offset_y = ZERO) ⇒ Object

Returns the image and background position for use in a single shorthand property:

$icons: sprite-map("icons/*.png"); // contains icons/new.png among others.
background: sprite($icons, new) no-repeat;

Becomes:

background: url('/images/icons.png?12345678') 0 -24px no-repeat;


60
61
62
63
64
65
66
67
68
69
# File 'lib/simple-compass/sass/functions/sprites.rb', line 60

def sprite(map, sprite, offset_x = ZERO, offset_y = ZERO)
  sprite = convert_sprite_name(sprite)
  verify_map(map)
  unless sprite.is_a?(Sass::Script::String)
    raise Sass::SyntaxError, %Q(The second argument to sprite() must be a sprite name. See http://beta.compass-style.org/help/tutorials/spriting/ for more information.)
  end
  url = sprite_url(map)
  position = sprite_position(map, sprite, offset_x, offset_y)
  Sass::Script::List.new([url] + position.value, :space)
end

#sprite_does_not_have_parent(map, sprite) ⇒ Object

Returns boolean if sprite has a parent



96
97
98
99
100
101
# File 'lib/simple-compass/sass/functions/sprites.rb', line 96

def sprite_does_not_have_parent(map, sprite)
  sprite = convert_sprite_name(sprite)
  verify_map map
  verify_sprite sprite
  Sass::Script::Bool.new map.image_for(sprite.value).parent.nil?
end

#sprite_file(map, sprite) ⇒ Object

Returns the path to the original image file for the sprite with the given name



83
84
85
86
87
88
89
90
91
92
# File 'lib/simple-compass/sass/functions/sprites.rb', line 83

def sprite_file(map, sprite)
  sprite = convert_sprite_name(sprite)
  verify_map(map, "sprite")
  verify_sprite(sprite)
  if image = map.image_for(sprite.value)
    Sass::Script::String.new(image.file)
  else
    missing_image!(map, sprite)
  end
end

#sprite_has_selector(map, sprite, selector) ⇒ Object

Returns boolean if sprite has the selector



106
107
108
109
110
111
112
113
114
# File 'lib/simple-compass/sass/functions/sprites.rb', line 106

def sprite_has_selector(map, sprite, selector)
  sprite = convert_sprite_name(sprite)
  verify_map map
  verify_sprite sprite
  unless VALID_SELECTORS.include?(selector.value)
    raise Sass::SyntaxError, "Invalid Selctor did you mean one of: #{VALID_SELECTORS.join(', ')}"
  end
  Sass::Script::Bool.new map.send(:"has_#{selector.value}?", sprite.value)
end

#sprite_image(*args) ⇒ Object

Raises:

  • (Sass::SyntaxError)


173
174
175
# File 'lib/simple-compass/sass/functions/sprites.rb', line 173

def sprite_image(*args)
  raise Sass::SyntaxError, %Q(The sprite-image() function has been replaced by sprite(). See http://compass-style.org/help/tutorials/spriting/ for more information.)
end

#sprite_map(glob, kwargs = {}) ⇒ Object

Creates a Compass::SassExtensions::Sprites::SpriteMap object. A sprite map, when used in a property is the same as calling sprite-url. So the following background properties are equivalent:

$icons: sprite-map("icons/*.png");
background: sprite-url($icons) no-repeat;
background: $icons no-repeat;

The sprite map object will generate the sprite map image, if necessary, the first time it is converted to a url. Simply constructing it has no side-effects.



46
47
48
49
# File 'lib/simple-compass/sass/functions/sprites.rb', line 46

def sprite_map(glob, kwargs = {})
  kwargs.extend VariableReader
  Compass::SassExtensions::Sprites::SpriteMap.from_uri(glob, self, kwargs)
end

#sprite_map_name(map) ⇒ Object

Returns the name of a sprite map The name is derived from the folder than contains the sprites.



76
77
78
79
# File 'lib/simple-compass/sass/functions/sprites.rb', line 76

def sprite_map_name(map)
  verify_map(map, "sprite-map-name")
  Sass::Script::String.new(map.name)
end

#sprite_names(map) ⇒ Object

Returns a list of all sprite names



14
15
16
# File 'lib/simple-compass/sass/functions/sprites.rb', line 14

def sprite_names(map)
  Sass::Script::List.new(map.sprite_names.map { |f| Sass::Script::String.new(f) }, ' ')
end

#sprite_path(map) ⇒ Object

Returns the system path of the sprite file



20
21
22
# File 'lib/simple-compass/sass/functions/sprites.rb', line 20

def sprite_path(map)
  Sass::Script::String.new(map.name_and_hash)
end

#sprite_position(map, sprite = nil, offset_x = ZERO, offset_y = ZERO) ⇒ Object

Returns the position for the original image in the sprite. This is suitable for use as a value to background-position:

$icons: sprite-map("icons/*.png");
background-position: sprite-position($icons, new);

Might generate something like:

background-position: 0 -34px;

You can adjust the background relative to this position by passing values for ‘$offset-x` and `$offset-y`:

$icons: sprite-map("icons/*.png");
background-position: sprite-position($icons, new, 3px, -2px);

Would change the above output to:

background-position: 3px -36px;


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/simple-compass/sass/functions/sprites.rb', line 146

def sprite_position(map, sprite = nil, offset_x = ZERO, offset_y = ZERO)
  assert_type offset_x, :Number
  assert_type offset_y, :Number
  sprite = convert_sprite_name(sprite)
  verify_map(map, "sprite-position")
  unless sprite && sprite.is_a?(Sass::Script::String)
    raise Sass::SyntaxError, %Q(The second argument to sprite-position must be a sprite name. See http://beta.compass-style.org/help/tutorials/spriting/ for more information.)
  end
  image = map.image_for(sprite.value)
  unless image
    missing_image!(map, sprite)
  end
  if offset_x.unit_str == "%"
    x = offset_x # CE: Shouldn't this be a percentage of the total width?
  else
    x = offset_x.value - image.left
    x = Sass::Script::Number.new(x, x == 0 ? [] : ["px"])
  end
  y = offset_y.value - image.top
  y = Sass::Script::Number.new(y, y == 0 ? [] : ["px"])
  Sass::Script::List.new([x, y],:space)
end

#sprite_url(map) ⇒ Object

Returns a url to the sprite image.



120
121
122
123
124
# File 'lib/simple-compass/sass/functions/sprites.rb', line 120

def sprite_url(map)
  verify_map(map, "sprite-url")
  map.generate
  generated_image_url(Sass::Script::String.new("#{map.path}-s#{map.uniqueness_hash}.png"))
end