Sass selector.parse()

Beginner
8 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

selector.parse($selector) returns $selector in the Sass selector value format: a comma-separated list (selector list) of space-separated lists (complex selectors) of unquoted strings (compound selectors). Load with @use "sass:selector".

Official sample idea.

parse(".main aside:hover, .sidebar p") becomes two complex selectors: .main aside:hover and .sidebar p.

Why parse?

Most selector helpers already accept strings. Use parse when you need to walk structure with list.length / list.nth, loop complexes, or normalize input.

Browsers see a string again.

Interpolating a parsed selector usually prints a normal selector. The nested list is for Sass code—see also meta.inspect().

Syntax

SCSS
@use "sass:selector";

selector.parse($selector) //=> selector (list)

$parsed: selector.parse(".main aside:hover, .sidebar p");
// two complex selectors:
//   (.main aside:hover)
//   (.sidebar p)

// Feed into other helpers:
selector.nest($parsed, "&:hover");
selector.append(selector.parse(".btn"), ".primary");

// Global alias (legacy): selector-parse($selector)
// Prefer selector.parse after @use "sass:selector"

Pattern: pass a selector string → get a nested list → inspect with sass:list or pass to other selector helpers.

Selector Value Format

Three nested levels, matching the module docs:

LevelMeaningExample piece
Outer list (comma)Selector list.main aside:hover, .sidebar p
Inner list (space)Complex selector.main aside:hover
Unquoted stringCompound selector.main, aside:hover
Official shape.

Returned selectors are always this nested list form—even when you started from a plain string.

SCSS Examples

Examples use @use "sass:selector". Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).

Example 1 — Official Parse Sample

Parse a selector list and inspect type + printed form.

SCSS
@use "sass:meta";
@use "sass:selector";

$parsed: selector.parse(".main aside:hover, .sidebar p");

.probe {
  --inspect: #{meta.inspect($parsed)};
  --type: #{meta.type-of($parsed)};
}

How It Works

  1. parse turns the string into a Sass list.
  2. type-of reports list.
  3. inspect prints a normal-looking selector again.
  4. Matches the official Sass docs sample idea.

Example 2 — Walk Complex & Compound Parts

Use sass:list to measure both levels of the official sample.

SCSS
@use "sass:list";
@use "sass:meta";
@use "sass:selector";

$parsed: selector.parse(".main aside:hover, .sidebar p");
$first: list.nth($parsed, 1);

.probe {
  --complex-count: #{list.length($parsed)};
  --compounds-in-first: #{list.length($first)};
  --compound-1: #{meta.inspect(list.nth($first, 1))};
  --compound-2: #{meta.inspect(list.nth($first, 2))};
}

How It Works

  1. Comma splits the outer list into 2 complexes.
  2. First complex has 2 compounds (space).
  3. .main is the first compound.
  4. aside:hover is the second.

Example 3 — Parse Then Nest

Parse a descendant selector, then nest a parent pseudo onto it.

SCSS
@use "sass:selector";

$parsed: selector.parse(".card .title");

#{selector.nest($parsed, "&:hover")} {
  color: teal;
}

How It Works

  1. parse normalizes the input selector.
  2. nest accepts the parsed value.
  3. &:hover attaches to the complex selector.
  4. Result is a usable CSS rule.

Example 4 — String vs Parsed Input

Many helpers accept either—parse when you need the list form.

SCSS
@use "sass:selector";

.compare {
  --from-string: #{selector.append(".btn", ".primary")};
  --from-parsed: #{selector.append(selector.parse(".btn"), ".primary")};
}

How It Works

  1. String path works for simple calls.
  2. Parsed path produces the same result.
  3. Choose parse when you will also walk the structure.
  4. Helpers accept both forms.

Example 5 — Loop Each Complex Selector

Parse a list, then @each over the complex selectors.

SCSS
@use "sass:meta";
@use "sass:selector";

@mixin dump-complex($selector) {
  $parsed: selector.parse($selector);
  @each $complex in $parsed {
    .dump {
      content: meta.inspect($complex);
    }
  }
}

@include dump-complex(".hero h1, .hero p");

How It Works

  1. Comma splits the outer list.
  2. @each walks one complex at a time.
  3. First pass: .hero h1.
  4. Second pass: .hero p.

Edge Cases & Pitfalls

Most parse confusion is mistaking the nested list for CSS syntax.

format

Nested lists, not CSS tokens

The structure is for Sass. CSS output looks like a normal selector again.

optional

Strings often work already

Parse when you need structure—not for every append/nest call.

list

Use sass:list to walk it

list.length and list.nth open the complex and compound levels.

simples

Need simple selectors?

Split a compound with simple-selectors.

Sass Support

Prefer the module form with current Dart Sass.

APIStatus
selector.parse()Dart Sass 1.23+ via @use "sass:selector"
Return typeSelector (nested list)
InputSelector string (or compatible value)
Legacy globalselector-parse($selector)

Compile with the Dart Sass CLI.

Key Takeaways

  • Returns a nested list — comma complexes, space compounds.
  • Walk with sass:list when you need counts or parts.
  • Feeds other helpers like nest and append.
  • Prefer @use "sass:selector" (Dart Sass 1.23+).

One line: to turn a selector string into Sass’s nested list format, use selector.parse($selector).

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:selector"
2. Pass a selector string to parse
3. Use list.length / list.nth to walk levels
4. Feed the result into nest, append, etc.
5. Remember CSS output looks like a normal selector

Frequently Asked Questions

selector.parse($selector) returns $selector in the Sass selector value format: a comma-separated list of complex selectors, each a space-separated list of compound selectors (unquoted strings).
Most selector helpers accept strings. parse is useful when you want to inspect structure with list.length / list.nth, loop complex selectors, or normalize input before further selector work.
parse(".main aside:hover, .sidebar p") becomes a list of two complex selectors: (.main aside:hover) and (.sidebar p). Each complex selector is itself a list of compounds.
Yes. selector-parse($selector) is the legacy global name. Prefer selector.parse after @use "sass:selector".
Yes. Interpolating a parsed selector with #{$parsed} (or meta.inspect) usually prints a normal selector string again.
Dart Sass 1.23+. LibSass and Ruby Sass do not load sass:selector with @use; they may still offer the legacy global name.

Next: Sass selector.replace()

Replace a selector piece with another using @extend-style unification.

selector.replace() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful