SassScript
SassScript is the Sass expression language. Understand the arguments passed
to SassFunction
s and return values using these types. The behaviors and
functionality here match that in
Sass itself.
Topics
Main types
class SassValue
class SassValue
Common behavior between values passed to or returned from Sass functions.
All Sass values can be treated as lists: singleton values like strings behave like
1-element lists and maps behave like lists of two-element (key, value) lists. Use the
Sequence
Sequence
conformance to access the list contents.
SassValue
is abstract, you cannot create instances. Instead create instances of subtypes
like SassColor
.
All the SassValue
types have value semantics and are almost all entirely immutable: the
only mutability is to do with caching of derived properties.
Declaration
public class SassValue: Hashable, Sequence, CustomStringConvertible, Sendable
class SassString
class SassString
A Sass string value.
Strings may be quoted.
SassString indexes
Sass models strings as a sequence of unicode scalars, not Swift’s primary view as a sequence of extended grapheme clusters. So any string index you receive through Sass applies to the unicode scalar view of the string.
Further, Sass models 1 as the index of the first scalar in the string, and count
as the index of the last.
This class offers a scalarIndexFrom(sassIndex:)
method to wrap up both parts of this
conversion, but offers only sympathy to users having to deal with the results.
SassString
conforms to Sequence
Sequence
via SassValue
. This sequence is a singleton sequence
containing the string value, not a sequence of scalars.
Declaration
class SassNumber
class SassNumber
A Sass numeric value.
Sass numbers are Double
Double
s with units.
Sass has its own rules for numeric equality and integer conversion that use only the first
10 decimal places. Use the SassNumber
methods to convert to integers and test for ranges.
The units on a SassNumber
can describe the result of multiplying and dividing numbers with
primitive units. Best described in the
Sass docs.
The CSS values spec defines several common
units and how to convert between them, for example px to pt. asConvertedTo(...)
implements these conversions which lets you, for example, write a function that accepts any length unit
that you can easily convert to your preferred unit.
Because of units, SassNumber
is not Comparable
Comparable
— there is no ordering relation
possible between “5 cm” and “12 kHz”. It is Equatable
Equatable
though respecting unit conversion
so that “1 cm” == “10 mm”.
Declaration
class SassColor
class SassColor
A Sass color value.
Supports the bare features of CSS Color Module Level 4 by supporting a wide range of color spaces, but does not offer anything particularly profound for working with such values such as converting between them or mixing colours or policing channel values outside of the legacy spaces.
Parameter values follow web standards rather the Apple SDK standards, so for example ‘red’ is modelled as an integer in 0…255.
Declaration
enum SassConstants
enum SassConstants
SassScript constant values.
Declaration
public enum SassConstants
class SassList
class SassList
A Sass list value.
Sass lists have a separator and may be surrounded with brackets.
All Sass values can be treated as lists so much list-like behavior is available via
SassValue
. SassList
is mostly useful for constructing your own multi-element lists.
Declaration
class SassArgumentList
class SassArgumentList
A Sass value representing a $arg...
function argument list.
This type is a SassList
holding positional arguments with an addition set of
named keyword arguments. See the Sass docs.
You’ll typically use this type to access arguments passed in this way to a custom Sass
function. Be careful in other scenarios: the keyword argument part of the type is excluded
from equality and listification, meaning it is easy to accidentally lose that part of the type
when passing instances through generic SassValue
code.
Declaration
class SassMap
class SassMap
A Sass map value.
Sass maps are Dictionary
Dictionary
-like collections. The keys can be any kind of
SassValue
.
When a map is viewed as a Sass list then the list is of two-element lists, one for each key-value pair in the map. The pairs are in no particular order.
Declaration
class SassCalculation
class SassCalculation
A Sass calculation value.
These correspond to Sass calc()
, min()
, max()
, or clamp()
expressions.
See the Sass docs.
The Sass compiler simplifies these before sending them to custom functions: this means that if you
receive a SassCalculation
argument then it cannot be further simplified at compile time,
for example calc(20px + 30%)
.
The API here allows you to construct SassCalculation
s representing calc()
-type expressions
including invalid ones such as calc(20px, 30px)
as though you were writing a stylesheet. The
validity is checked – and the overall expression simplified – by the compiler when it receives the value.
Declaration
class SassMixin
class SassMixin
A Sass mixin.
Values representing mixins can only be created by the compiler. See the Sass docs.
Declaration
Function object types
class SassCompilerFunction
class SassCompilerFunction
A Sass compiler function.
A compiler function is an opaque function defined by the Sass compiler that can be passed
as an argument to or returned by a SassFunction
.
Right now there is no way to explicitly request they be executed; all you can do with this type is validate that it appears when you expect it to and pass it back to the compiler when needed.
Declaration
class SassDynamicFunction
class SassDynamicFunction
A dynamic Sass function.
These are Sass functions, written in Swift, that are not declared up-front to the compiler when
starting compilation. Instead they are returned as SassValue
s from other SassFunction
s
(that were declared up-front) so the compiler can call them later on.
Declaration
Less-commonly used types
protocol SassValueVisitor
protocol SassValueVisitor
A protocol for implementing polymorphic operations over SassValue
objects.
Declaration
public protocol SassValueVisitor
class SassNull
class SassNull
The Sass null
value.
You cannot create instances of this type: use SassConstants.null
instead.
Declaration
class SassBool
class SassBool
A Sass boolean value.
You cannot create instances of this type: use SassConstants.true
and SassConstants.false
instead.