Behavioral Differences Between Dart Sass and Ruby Sass

Understand the key behavioral differences between Dart Sass and Ruby Sass, including changes in @extend, pseudo selectors, numeric precision, and more, with examples and issue references.

Dart Sass and Ruby Sass are two implementations of the Sass CSS preprocessor, but Dart Sass introduces intentional behavioral differences to address undesired behaviors in Ruby Sass. These changes prioritize correctness and efficiency, though they may break compatibility with Ruby Sass in specific cases. Below, we outline the key differences, with examples where applicable, and link to tracking issues for reference.

Simplified @extend Selector Support

Dart Sass restricts @extend and the second argument of selector-extend() to simple selectors, unlike Ruby Sass, which allows more complex selectors. This simplifies implementation and avoids ambiguous behavior.

Example:

// Ruby Sass (works)
%placeholder {
  color: blue;
}
.complex > .selector {
  @extend %placeholder;
}

// Dart Sass (errors)
%placeholder {
  color: blue;
}
.complex > .selector {
  @extend %placeholder; // Error: Only simple selectors allowed
}

See issue 1599.

No Subject Selector Support

Dart Sass does not support subject selectors. These are selectors with a ! marker, such as div !span. Ruby Sass allowed them, but they often behaved in confusing ways.

Example:

// Ruby Sass (works)
div !span {
  color: red;
}

// Dart Sass (not supported)
div !span {
  color: red; // Error: Subject selectors not supported
}

See issue 1126.

Flexible Pseudo Selector Arguments

Dart Sass parses pseudo selector arguments as <declaration-value>s. This is more flexible than Ruby Sass, which uses stricter parsing.

Example:

// Ruby Sass (limited parsing)
:pseudo(arg) {
  content: "Limited";
}

// Dart Sass (parses as declaration-value)
:pseudo(1 + 2) {
  content: "Flexible"; // Works in Dart Sass
}

See issue 2120.

Fixed Numeric Precision

Dart Sass rounds numbers to 10 digits. Ruby Sass does not. Its precision depends on the platform, so the same code can give different results on different machines.

Example:

// Ruby Sass (platform-dependent precision)
$num: 0.123456789012345;
width: $num * 1px; // Varies by platform

// Dart Sass (fixed precision)
$num: 0.123456789012345;
width: $num * 1px; // Always 0.1234567890px

See issue 1122.

Flexible Indented Syntax

Dart Sass does not require the whole file to use the same indentation. Ruby Sass does. This makes Dart Sass more forgiving.

Example:

// Ruby Sass (requires consistent indentation)
div
  color: blue
    span
      color: red // Error: Inconsistent indentation

// Dart Sass (allows mixed indentation)
div
  color: blue
    span
      color: red // Works

See issue 2176.

No Color Arithmetic on Separate Channels

Dart Sass does not allow adding or subtracting color channels one at a time. Ruby Sass did. Modern CSS no longer needs this, so Dart Sass dropped it.

Example:

// Ruby Sass (allows channel arithmetic)
$color: #ff0000 + #00ff00; // Results in #ffff00

// Dart Sass (not supported)
$color: #ff0000 + #00ff00; // Error: No channel-by-channel arithmetic

See issue 2144.

Stricter Unit Equality

In Dart Sass, unitless numbers are not considered equal to numbers with units, even if their values match. This also affects map keys.

Example:

// Ruby Sass (unitless == unit)
$map: (1: "one", 1px: "px");
@debug map-get($map, 1px); // Returns "one"

// Dart Sass (stricter)
$map: (1: "one", 1px: "px");
@debug map-get($map, 1px); // Returns "px"

See issue 1496.

Percentage-Based Alpha Values

In Dart Sass, the alpha value of rgba() and hsla() must be a percentage. Plain numbers like 0.5 are not allowed.

Example:

// Ruby Sass (allows various units)
color: rgba(255, 0, 0, 0.5);

// Dart Sass (percentage only)
color: rgba(255, 0, 0, 50%); // Works
color: rgba(255, 0, 0, 0.5); // Error: Only percentages allowed

See issue 1525.

Strict Function Argument Limits

Dart Sass throws an error when too many variable arguments are passed to a function, unlike Ruby Sass, which silently ignores extras.

Example:

// Ruby Sass (ignores extra args)
@function example($args...) {
  @return length($args);
}
@debug example(1, 2, 3, 4); // Returns 4

// Dart Sass (errors on extra args)
@function example($arg1) {
  @return $arg1;
}
@debug example(1, 2); // Error: Too many arguments

See issue 1408.

Extended @extend Across Media Queries

Dart Sass allows @extend to reach outside a media query if an identical @extend exists outside, improving flexibility.

Example:

// Ruby Sass (limited)
%placeholder {
  color: blue;
}
@media screen {
  .class {
    @extend %placeholder; // Fails
  }
}

// Dart Sass (works)
%placeholder {
  color: blue;
}
@media screen {
  .class {
    @extend %placeholder; // Succeeds if %placeholder exists outside
  }
}

See issue 1050.

Better Handling of Selector Pseudos

Dart Sass handles some selector pseudos with placeholders better than Ruby Sass. Ruby Sass may fail to compile them. Dart Sass compiles them correctly.

Example:

// Ruby Sass (may not compile)
:where(%placeholder) {
  color: blue;
}

// Dart Sass (compiles correctly)
:where(%placeholder) {
  color: blue; // Works efficiently
}

See issue 2228.

No Old-Style Property Syntax in Indented Syntax

Dart Sass does not support the old-style :property value syntax in indented syntax.

Example:

// Ruby Sass (works)
div
  :color blue

// Dart Sass (not supported)
div
  :color blue // Error: Not supported

See issue 2245.

No Reference Combinator

Dart Sass does not support the reference combinator (/for/), which Ruby Sass allows.

Example:

// Ruby Sass (works)
div /for/ span {
  color: red;
}

// Dart Sass (not supported)
div /for/ span {
  color: red; // Error: Reference combinator not supported
}

See issue 303.

Symmetrical Universal Selector Unification

Dart Sass treats universal selector unification the same way in both directions. Ruby Sass does not.

Example:

// Ruby Sass (asymmetric)
* + div {
  color: blue;
}

// Dart Sass (symmetric)
* + div {
  color: blue; // Consistent behavior
}

See issue 2247.

Non-Fatal @extend Failures

Dart Sass does not throw an error if @extend matches but fails to unify, unlike Ruby Sass.

Example:

// Ruby Sass (errors)
%placeholder {
  color: blue;
}
.class {
  @extend %placeholder; // Error if unification fails
}

// Dart Sass (no error)
%placeholder {
  color: blue;
}
.class {
  @extend %placeholder; // Silently skips if unification fails
}

See issue 2250.

UTF-8 Document Support

Dart Sass only reads UTF-8 files. Ruby Sass can read files in other encodings too. This is a limit of the Dart runtime, not a choice by the Sass team.

See dart-lang/sdk#11744.

Conclusion

Dart Sass diverges from Ruby Sass to improve correctness, performance, and alignment with modern CSS standards. These changes, while breaking compatibility in some cases, address long-standing issues in Ruby Sass. Developers should review these differences and test their stylesheets when migrating to Dart Sass, referencing the linked issues for further details.