-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
string.split() function #1950
Comments
Selectors aren't strings. They're lists of lists. If you want to remove or capture part of a selector, you use a loop.
Output:
|
Yes I understand.
You're correct. As it turns out, in my experimentation, where I was mistaken was that even if there is one selector, i.e.
I was expecting |
I guess, assuming certain constraints on the input string, and only wanting |
@davidkpiano True. Sorry I wasn't more clear.
.foo .bar {
test: nth(nth(&, 1), 2);
} note: selector-parse() isn't required when you're dealing with |
With regards to comment on .foo .bar {
test: nth(nth(selector-parse(".foo .bar"), 1), 2); // .bar
} I suspect there may be limitations on the types of strings this works with i.e. valid selectors (maybe?) |
(Sorry, deleted my comment as it was redundant.) Maybe a $test: selector-split(".foo .bar", " ");
// => ((.foo,), (.bar,))
$test: selector-split(".foo .bar > .baz", ">");
// => ((.foo, .bar), (.baz,)) |
whitespace is a combinator. it is the "descendant combinator". Anyways, a |
+1 for a str-split or selector-split function |
@nex3 should this also come with a feature check flag? |
Generally we don't add feature flags for functions, since they can be detected with |
Makes sense
…On Sat., 19 May 2018, 9:02 pm Natalie Weizenbaum, ***@***.***> wrote:
Generally we don't add feature flags for functions, since they can be
detected with function-exists().
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1950 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAjZWIqGbgmUxzqKV-oW_qnP6xAUo9Qaks5t0GwygaJpZM4G-jlh>
.
|
something like this seems like a fairly elegant work around to me: .foo .bar {
// this spoofs the selectors of `&` into a Sass list
$selectors: str-replace(inspect(&), ' ', ', ');
// selector is now split - do whatever you need to do
...
// when finished, rebuild selector
$selectors: selector-parse($selectors);
} Sorry if this was already clear from previous comments. |
The proposal has landed. We'll keep it open for comment for a month, then land it. |
Looking over this again as we start to move towards implementation, I had a thought: should this return a bracketed list? The main reason to return an unbracketed list is historical precedent. No existing function returns an unbracketed list by default. However, I think this might be a less compelling argument than it seems at first glance. Here are all the functions that currently return lists:
Given that the precedent is not actually very strong for returning unbracketed lists, I think we should consider making all new functions that return lists default to returning bracketed lists unless there's a specific reason not to. Similarly to how we default to returning quoted rather than unquoted strings, more explicit delineators of values eases debugging by making it clearer to users what types they're working with. To generalize this principle: Sass design should encourage code that's not directly emitting CSS to use more explicit syntax for constructs that CSS natively supports implicit syntax for. @dvdherron What do you think? |
@nex3 I think it makes perfect sense to have all functions that return lists default to returning bracketed lists. I'll make another draft of the proposal with the relevant changes. Will have a draft of tests ready soon too. |
Closes #1821 See sass/sass#1950
It would be really useful to have a
str-split($string, $delimiter:" ")
function akin to JavaScript, or Ruby.The primary use case I run into is splitting selector strings i.e.
It's rather difficult to get
.bar
from&
without boilerplate string functions or importing something like sassdash.A another use case I run into is remove part of selector. As it stands
selector-replace
does not allow you to remove part of a selector (i.e. replace it with""
ornull
). Being able to easily split selector strings would makes this much easier.The text was updated successfully, but these errors were encountered: