From 828a12ffac60b2fb7e6b0931f610b7ca9f53ffc2 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sat, 22 Jun 2024 08:14:44 +0200 Subject: Props: Support recursive var() if referring to subtree --- src/_functions.scss | 71 ++++++++++++++++++++------------------ src/_props.scss | 99 +++++++++++++++++++++++++++++------------------------ 2 files changed, 92 insertions(+), 78 deletions(-) (limited to 'src') diff --git a/src/_functions.scss b/src/_functions.scss index 9dd14b1..7d4a695 100644 --- a/src/_functions.scss +++ b/src/_functions.scss @@ -10,7 +10,10 @@ //// @use 'sass:map'; +@use 'sass:list'; @use 'sass:math'; +@use 'sass:string'; +@use 'sass:meta'; @use './vars'; /// @@ -23,10 +26,10 @@ /// @return {string} A string with all instances of $search replaced with $replace /// @function str-replace($string, $search, $replace) { - $index: str-index($string, $search); + $index: string.index($string, $search); @if $index { - @return str-slice($string, 1, $index - 1) + $replace + str-replace(str-slice($string, $index + str-length($search)), $search, $replace); + @return string.slice($string, 1, $index - 1) + $replace + str-replace(string.slice($string, $index + string.length($search)), $search, $replace); } @return $string; @@ -44,9 +47,9 @@ $result: ''; @each $item in $list { - $result: $result + if(length($item) > 1, str-implode($item, $glue), $item); + $result: $result + if(list.length($item) > 1, str-implode($item, $glue), $item); - @if $item != nth($list, length($list)) { + @if $item != list.nth($list, list.length($list)) { $result: $result + $glue; } } @@ -63,13 +66,13 @@ /// /// @return {list} A slice of the list /// -@function list-slice($list, $start: 1, $end: length($list)) { +@function list-slice($list, $start: 1, $end: list.length($list)) { $result: (); @if $end >= $start { @for $i from $start through $end { @if $i != 0 { - $result: append($result, nth($list, $i), list-separator($list)); + $result: list.append($result, list.nth($list, $i), list.separator($list)); } } } @@ -86,11 +89,11 @@ /// @return {list} A list with $value at the beginning, followed by the other items /// @function list-prepend($list, $value) { - $result: append((), $value, list-separator($list)); + $result: list.append((), $value, list.separator($list)); - @if length($list) > 0 { - @for $i from 1 through length($list) { - $result: append($result, nth($list, $i), list-separator($list)); + @if list.length($list) > 0 { + @for $i from 1 through list.length($list) { + $result: list.append($result, list.nth($list, $i), list.separator($list)); } } @@ -105,13 +108,13 @@ /// @return {list} Teh reversed list /// @function list-reverse($list) { - @if length($list) == 0 { + @if list.length($list) == 0 { @return $list; } $result: (); - @for $i from length($list) * -1 through -1 { - $result: append($result, nth($list, abs($i))); + @for $i from list.length($list) * -1 through -1 { + $result: list.append($result, list.nth($list, math.abs($i))); } @return $result; } @@ -126,11 +129,11 @@ /// /// @return {list} Sorted list /// -@function quicksort($l, $left: 1, $right: length($l)) { +@function quicksort($l, $left: 1, $right: list.length($l)) { @if $left < $right { $pvr: quicksort-partition($l, $left, $right); - $pivot: nth($pvr, 1); - $l: nth($pvr, 2); + $pivot: list.nth($pvr, 1); + $l: list.nth($pvr, 2); $l: quicksort($l, $left, $pivot); $l: quicksort($l, $pivot + 1, $right); } @@ -145,30 +148,30 @@ $start: true; $i: $left; $j: $right - 1; - $pivot: nth($l, $right); + $pivot: list.nth($l, $right); @while ($i < $j) or $start { - @while (nth($l, $i) < $pivot) and ($i < $right - 1) { + @while (list.nth($l, $i) < $pivot) and ($i < $right - 1) { $i: $i + 1; } - @while (nth($l, $j)>= $pivot) and ($j > $left) { + @while (list.nth($l, $j)>= $pivot) and ($j > $left) { $j: $j - 1; } @if $i < $j { - $i-val: nth($l, $i); - $l: set-nth($l, $i, nth($l, $j)); - $l: set-nth($l, $j, $i-val); + $i-val: list.nth($l, $i); + $l: list.set-nth($l, $i, list.nth($l, $j)); + $l: list.set-nth($l, $j, $i-val); } $start: false; } - @if nth($l, $i) > $pivot { - $i-val: nth($l, $i); - $l: set-nth($l, $i, nth($l, $right)); - $l: set-nth($l, $right, $i-val); + @if list.nth($l, $i) > $pivot { + $i-val: list.nth($l, $i); + $l: list.set-nth($l, $i, list.nth($l, $right)); + $l: list.set-nth($l, $right, $i-val); } @return $i $l; @@ -185,10 +188,10 @@ /// @return {any} Either the value assigned to $key or $default /// @function map-get-default($map, $key, $keys...) { - $default: nth($keys, length($keys)); - $keys: list-slice($keys, 1, length($keys) - 1); + $default: list.nth($keys, list.length($keys)); + $keys: list-slice($keys, 1, list.length($keys) - 1); - @return if(map-has-key($map, $key, $keys...), map-get($map, $key, $keys...), $default); + @return if(map.has-key($map, $key, $keys...), map.get($map, $key, $keys...), $default); } /// @@ -204,11 +207,11 @@ @each $key, $value in $map { $value-str: ''; - @if type-of($value) == map { + @if meta.type-of($value) == map { $value-str: '[ ' + map-print($value) + ' ]'; - } @else if type-of($value) == list { + } @else if meta.type-of($value) == list { $value-str: '[ ' + str-implode($value, ', ') + ' ]'; - } @else if type-of($value) == string { + } @else if meta.type-of($value) == string { $value-str: '\'' + $value + '\''; } @else { $value-str: $value; @@ -243,8 +246,8 @@ @if not $sel-match { $suf-match: true; - @for $i from 1 through length($suffix) { - @if $suf-match and (nth($sel, -$i) != nth($suffix, -$i)) { + @for $i from 1 through list.length($suffix) { + @if $suf-match and (list.nth($sel, -$i) != list.nth($suffix, -$i)) { $suf-match: false; } } diff --git a/src/_props.scss b/src/_props.scss index 1494203..14295bd 100644 --- a/src/_props.scss +++ b/src/_props.scss @@ -11,6 +11,9 @@ //// @use 'sass:map'; +@use 'sass:list'; +@use 'sass:string'; +@use 'sass:meta'; @use './functions'; @use './contexts'; @@ -63,7 +66,7 @@ $namespace-context-id: 'namespace' !default; $ns-key: get-ns-key(); @if $ns-key != null { - $key: append($ns-key, $key); + $key: list.append($ns-key, $key); } @else { $key: ($key); } @@ -84,8 +87,8 @@ $namespace-context-id: 'namespace' !default; @function namespace() { $noop: contexts.assert-stack-must-contain($namespace-context-id, 'namespace'); - $data: nth(contexts.get($namespace-context-id, 'namespace'), 2); - $name: map-get($data, 'name'); + $data: list.nth(contexts.get($namespace-context-id, 'namespace'), 2); + $name: map.get($data, 'name'); @return $name; } @@ -126,15 +129,15 @@ $namespace-context-id: 'namespace' !default; } } - @if map-has-key($trees, $tree) { + @if map.has-key($trees, $tree) { @if $merge { - $map: map.deep-merge(map-get($trees, $tree), $map); + $map: map.deep-merge(map.get($trees, $tree), $map); } @else { @error 'Property tree #{inspect($tree)} does already exist.'; } } - $trees: map-merge($trees, ($tree: $map)) !global; + $trees: map.merge($trees, ($tree: $map)) !global; @return null; } @@ -156,11 +159,11 @@ $namespace-context-id: 'namespace' !default; /// @throw If the property tree does not exist /// @function clear($tree: $default-tree) { - @if not map-has-key($trees, $tree) { + @if not map.has-key($trees, $tree) { @error 'Property tree "#{inspect($tree)}" does not exist.'; } - $trees: map-remove($trees, $tree) !global; + $trees: map.remove($trees, $tree) !global; @return null; } @@ -177,11 +180,11 @@ $namespace-context-id: 'namespace' !default; /// @throw If there was no match for $key and $default is null /// @function get-static($key: (), $tree: $default-tree, $default: null, $global: false) { - @if not map-has-key($trees, $tree) { + @if not map.has-key($trees, $tree) { @error 'Unknown tree "#{$tree}".'; } - $result: map-get($trees, $tree); + $result: map.get($trees, $tree); @if not $global { $ns-key: get-ns-key(); @@ -190,28 +193,28 @@ $namespace-context-id: 'namespace' !default; $orig-key: $key; $key: $ns-key; - @if type-of($orig-key) == list { + @if meta.type-of($orig-key) == list { @each $subkey in $orig-key { - $key: append($key, $subkey); + $key: list.append($key, $subkey); } } @else { - $key: append($key, $orig-key); + $key: list.append($key, $orig-key); } } } - @if type-of($key) == list { + @if meta.type-of($key) == list { $stop: false; @each $k in $key { - @if not $stop and map-has-key($result, $k) { - $result: map-get($result, $k); + @if not $stop and map.has-key($result, $k) { + $result: map.get($result, $k); - @if type-of($result) == list and nth($result, 1) == 'iro-prop-ref' { - @if length($result) == 2 { - $result: get-static($tree: nth($result, 2), $global: true); + @if meta.type-of($result) == list and list.nth($result, 1) == 'iro-prop-ref' { + @if list.length($result) == 2 { + $result: get-static($tree: list.nth($result, 2), $global: true); } @else { - $result: get-static(nth($result, 3), nth($result, 2), $global: true); + $result: get-static(list.nth($result, 3), nth($result, 2), $global: true); } } } @else { @@ -223,13 +226,13 @@ $namespace-context-id: 'namespace' !default; $result: null; } } @else { - $result: map-get($result, $key); + $result: map.get($result, $key); - @if type-of($result) == list and nth($result, 1) == 'iro-prop-ref' { - @if length($result) == 2 { - $result: get-static($tree: nth($result, 2), $global: true); + @if meta.type-of($result) == list and list.nth($result, 1) == 'iro-prop-ref' { + @if list.length($result) == 2 { + $result: get-static($tree: list.nth($result, 2), $global: true); } @else { - $result: get-static(nth($result, 3), nth($result, 2), $global: true); + $result: get-static(list.nth($result, 3), nth($result, 2), $global: true); } } } @@ -256,7 +259,15 @@ $namespace-context-id: 'namespace' !default; /// @function get($key, $tree: $default-tree, $default: null, $global: false) { @if $tree != null { - $noop: get-static($key, $tree, $default, $global); + $value: get-static($key, $tree, $default, $global); + + @if meta.type-of($value) == map { + $result: (); + @each $k in map.keys($value) { + $result: map.set($result, $k, get(list.append($key, $k), $tree, $default, $global)); + } + @return $result; + } } @if not $global { @@ -266,19 +277,19 @@ $namespace-context-id: 'namespace' !default; $orig-key: $key; $key: $ns-key; - @if type-of($orig-key) == list { + @if meta.type-of($orig-key) == list { @each $subkey in $orig-key { - $key: append($key, $subkey); + $key: list.append($key, $subkey); } } @else { - $key: append($key, $orig-key); + $key: list.append($key, $orig-key); } } } $native-var: ''; - @if type-of($key) == list { + @if meta.type-of($key) == list { @each $subkey in $key { $native-var: $native-var + $subkey; } @@ -301,9 +312,9 @@ $namespace-context-id: 'namespace' !default; /// @mixin assign($tree: $default-tree, $root: (), $skip: (), $prefix: $root, $global: false) { $map: get-static($root, $tree, $global: $global); - $map: map-remove($map, $skip...); + $map: map.remove($map, $skip...); - @if type-of($prefix) == list { + @if meta.type-of($prefix) == list { $prefix: functions.str-implode($prefix); } @@ -324,19 +335,19 @@ $namespace-context-id: 'namespace' !default; @mixin assign-internal($map, $prefix: '', $ref-depth: $native-assign-max-depth) { @each $key, $value in $map { $rd: $ref-depth; - @if type-of($value) == list and length($value) > 0 and nth($value, 1) == 'iro-prop-ref' { + @if meta.type-of($value) == list and list.length($value) > 0 and list.nth($value, 1) == 'iro-prop-ref' { @if $ref-depth != 0 { $rd: $rd - 1; - @if length($value) == 2 { - $value: get-static($tree: nth($value, 2)); + @if list.length($value) == 2 { + $value: get-static($tree: list.nth($value, 2)); } @else { - $value: get-static(nth($value, 3), nth($value, 2)); + $value: get-static(list.nth($value, 3), nth($value, 2)); } } @else { $value: null; } } - @if type-of($value) != map and $value != () { + @if meta.type-of($value) != map and $value != () { #{$prefix + $key}: #{$value}; } @else { @include assign-internal($value, $prefix + $key, $rd); @@ -351,11 +362,11 @@ $namespace-context-id: 'namespace' !default; /// @function validate($map) { @each $key, $value in $map { - @if str-index($key, '--') != 1 { + @if string.index($key, '--') != 1 { @return false; } - @if type-of($value) == map { + @if meta.type-of($value) == map { @if not validate($value) { @return false; } @@ -384,12 +395,12 @@ $namespace-context-id: 'namespace' !default; $key: $ns-key; @if $orig-key != null { - @if type-of($orig-key) == list { + @if meta.type-of($orig-key) == list { @each $subkey in $orig-key { - $key: append($key, $subkey); + $key: list.append($key, $subkey); } } @else { - $key: append($key, $orig-key); + $key: list.append($key, $orig-key); } } } @@ -414,8 +425,8 @@ $namespace-context-id: 'namespace' !default; @return null; } - $data: nth($ctx, 2); - $key: map-get($data, 'key'); + $data: list.nth($ctx, 2); + $key: map.get($data, 'key'); @return $key; } -- cgit v1.2.3-70-g09d2