Module: Sass::Script::Value::Helpers

Included in:
Functions::EvaluationContext
Defined in:
/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb

Overview

Provides helper functions for creating sass values from within ruby methods.

Since:

Constant Summary

Instance Method Summary (collapse)

Instance Method Details

- (Sass::Script::Value::Bool) bool(value)

Construct a Sass Boolean.

Parameters:

  • value (Object)

    A ruby object that will be tested for truthiness.

Returns:

Since:

  • 3.3.0



11
12
13
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 11

def bool(value)
  Bool.new(value)
end

- (Boolean) calc?(literal)

Returns true when the literal is a string containing a calc().

Use #special_number? in preference to this.

Parameters:

Returns:

  • (Boolean)

    Boolean

Since:

  • 3.3.0



214
215
216
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 214

def calc?(literal)
  literal.is_a?(Sass::Script::Value::String) && literal.value =~ /calc\(/
end

- (Sass::Script::Value::Color) hex_color(value, alpha = nil)

Construct a Sass Color from a hex color string.

Parameters:

  • value (::String)

    A string representing a hex color. The leading hash ("#") is optional.

  • alpha (::Number) (defaults to: nil)

    The alpha channel. A number between 0 and 1.

Returns:

Since:

  • 3.3.0



21
22
23
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 21

def hex_color(value, alpha = nil)
  Color.from_hex(value, alpha)
end

- (Sass::Script::Value::Color) hsl_color(hue, saturation, lightness, alpha = nil)

Construct a Sass Color from hsl values.

Parameters:

  • hue (::Number)

    The hue of the color in degrees. A non-negative number, usually less than 360.

  • saturation (::Number)

    The saturation of the color. Must be between 0 and 100 inclusive.

  • lightness (::Number)

    The lightness of the color. Must be between 0 and 100 inclusive.

  • alpha (::Number) (defaults to: nil)

    The alpha channel. A number between 0 and 1.

Returns:

Since:

  • 3.3.0



36
37
38
39
40
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 36

def hsl_color(hue, saturation, lightness, alpha = nil)
  attrs = {:hue => hue, :saturation => saturation, :lightness => lightness}
  attrs[:alpha] = alpha if alpha
  Color.new(attrs)
end

- (Sass::Script::Value::List) list(*elements, separator:, bracketed:false) - (Sass::Script::Value::List) list(array, separator:, bracketed:false)

Overloads:

  • - (Sass::Script::Value::List) list(*elements, separator:, bracketed:false)

    Create a space-separated list from the arguments given.

    Parameters:

    • elements (Array<Sass::Script::Value::Base>)

      Each argument will be a list element.

    • separator (Symbol)

      Either :space or :comma.

    • bracketed (Boolean)

      Whether the list uses square brackets.

    Returns:

  • - (Sass::Script::Value::List) list(array, separator:, bracketed:false)

    Create a space-separated list from the array given.

    Parameters:

    • array (Array<Sass::Script::Value::Base>)

      A ruby array of Sass values to make into a list.

    • separator (Symbol)

      Either :space or :comma.

    • bracketed (Boolean)

      Whether the list uses square brackets.

    Returns:

Since:

  • 3.3.0



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 85

def list(*elements, separator: nil, bracketed: false)
  # Support passing separator as the last value in elements for
  # backwards-compatibility.
  if separator.nil?
    if elements.last.is_a?(Symbol)
      separator = elements.pop
    else
      raise ArgumentError.new("A separator of :space or :comma must be specified.")
    end
  end

  if elements.size == 1 && elements.first.is_a?(Array)
    elements = elements.first
  end
  Sass::Script::Value::List.new(elements, separator: separator, bracketed: bracketed)
end

- (Sass::Script::Value::Map) map(hash)

Construct a Sass map.

Parameters:

Returns:

Since:

  • 3.3.0



107
108
109
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 107

def map(hash)
  Map.new(hash)
end

- (Sass::Script::Value::Null) null

Create a sass null value.

Returns:

Since:

  • 3.3.0



114
115
116
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 114

def null
  Sass::Script::Value::Null.new
end

- (Sass::Script::Value::Number) number(number, unit_string = nil)

Construct a Sass Number from a ruby number.

Parameters:

  • number (::Number)

    A numeric value.

  • unit_string (::String) (defaults to: nil)

    A unit string of the form numeral_unit1 * numeral_unit2 ... / denominator_unit1 * denominator_unit2 ... this is the same format that is returned by the unit_str method

Returns:

See Also:

Since:

  • 3.3.0



67
68
69
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 67

def number(number, unit_string = nil)
  Number.new(number, *parse_unit_string(unit_string))
end

- (Sass::Selector::Sequence) parse_complex_selector(value, name = nil, allow_parent_ref = false)

Parses a user-provided complex selector.

A complex selector can contain combinators but cannot contain commas.

Parameters:

  • value (Sass::Script::Value::String, Sass::Script::Value::List)

    The selector to parse. This can be either a string or a list of strings.

  • name (Symbol, nil) (defaults to: nil)

    If provided, the name of the selector argument. This is used for error reporting.

  • allow_parent_ref (Boolean) (defaults to: false)

    Whether the parsed selector should allow parent references.

Returns:

Raises:

  • (ArgumentError)

Since:

  • 3.3.0



172
173
174
175
176
177
178
179
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 172

def parse_complex_selector(value, name = nil, allow_parent_ref = false)
  selector = parse_selector(value, name, allow_parent_ref)
  return seq if selector.members.length == 1

  err = "#{value.inspect} is not a complex selector"
  err = "$#{name.to_s.tr('_', '-')}: #{err}" if name
  raise ArgumentError.new(err)
end

- (Sass::Selector::SimpleSequence) parse_compound_selector(value, name = nil, allow_parent_ref = false)

Parses a user-provided compound selector.

A compound selector cannot contain combinators or commas.

Parameters:

  • value (Sass::Script::Value::String)

    The selector to parse.

  • name (Symbol, nil) (defaults to: nil)

    If provided, the name of the selector argument. This is used for error reporting.

  • allow_parent_ref (Boolean) (defaults to: false)

    Whether the parsed selector should allow parent references.

Returns:

Raises:

  • (ArgumentError)

Since:

  • 3.3.0



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 193

def parse_compound_selector(value, name = nil, allow_parent_ref = false)
  assert_type value, :String, name
  selector = parse_selector(value, name, allow_parent_ref)
  seq = selector.members.first
  sseq = seq.members.first
  if selector.members.length == 1 && seq.members.length == 1 &&
      sseq.is_a?(Sass::Selector::SimpleSequence)
    return sseq
  end

  err = "#{value.inspect} is not a compound selector"
  err = "$#{name.to_s.tr('_', '-')}: #{err}" if name
  raise ArgumentError.new(err)
end

- (Sass::Selector::CommaSequence) parse_selector(value, name = nil, allow_parent_ref = false)

Parses a user-provided selector.

Parameters:

  • value (Sass::Script::Value::String, Sass::Script::Value::List)

    The selector to parse. This can be either a string, a list of strings, or a list of lists of strings as returned by &.

  • name (Symbol, nil) (defaults to: nil)

    If provided, the name of the selector argument. This is used for error reporting.

  • allow_parent_ref (Boolean) (defaults to: false)

    Whether the parsed selector should allow parent references.

Returns:

Since:

  • 3.3.0



147
148
149
150
151
152
153
154
155
156
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 147

def parse_selector(value, name = nil, allow_parent_ref = false)
  str = normalize_selector(value, name)
  begin
    Sass::SCSS::StaticParser.new(str, nil, nil, 1, 1, allow_parent_ref).parse_selector
  rescue Sass::SyntaxError => e
    err = "#{value.inspect} is not a valid selector: #{e}"
    err = "$#{name.to_s.tr('_', '-')}: #{err}" if name
    raise ArgumentError.new(err)
  end
end

- (Sass::Script::Value::String) quoted_string(str)

Create a quoted string.

Parameters:

  • str (::String)

    A ruby string.

Returns:

Since:

  • 3.3.0



122
123
124
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 122

def quoted_string(str)
  Sass::Script::String.new(str, :string)
end

- (Sass::Script::Value::Color) rgb_color(red, green, blue, alpha = nil)

Construct a Sass Color from rgb values.

Parameters:

  • red (::Number)

    The red component. Must be between 0 and 255 inclusive.

  • green (::Number)

    The green component. Must be between 0 and 255 inclusive.

  • blue (::Number)

    The blue component. Must be between 0 and 255 inclusive.

  • alpha (::Number) (defaults to: nil)

    The alpha channel. A number between 0 and 1.

Returns:

Since:

  • 3.3.0



50
51
52
53
54
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 50

def rgb_color(red, green, blue, alpha = nil)
  attrs = {:red => red, :green => green, :blue => blue}
  attrs[:alpha] = alpha if alpha
  Color.new(attrs)
end

- (Boolean) special_number?(literal)

Returns whether the literal is a special CSS value that may evaluate to a number, such as calc() or var().

Parameters:

Returns:

  • (Boolean)

    Boolean

Since:

  • 3.3.0



223
224
225
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 223

def special_number?(literal)
  literal.is_a?(Sass::Script::Value::String) && literal.value =~ /(calc|var)\(/
end

- (Sass::Script::Value::String) unquoted_string(str) Also known as: identifier

Create an unquoted string.

Parameters:

  • str (::String)

    A ruby string.

Returns:

Since:

  • 3.3.0



130
131
132
# File '/home/nweiz/code/sass-site/.sass/lib/sass/script/value/helpers.rb', line 130

def unquoted_string(str)
  Sass::Script::String.new(str, :identifier)
end