From 0cd2484e1f00a90d73fc7fbb238048e7bb5e3270 Mon Sep 17 00:00:00 2001 From: zhaowenkai <799480165@qq.com> Date: Wed, 29 May 2024 09:36:19 +0800 Subject: [PATCH] Fixed `&` search after consuming parenthesis --- src/Compiler/Parser.cs | 2 +- src/Compiler/Utility.cs | 4 ++-- test/CssInCSharp.Tests/ParserTests.cs | 19 +++++++++++++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Compiler/Parser.cs b/src/Compiler/Parser.cs index 8416673..0c732df 100644 --- a/src/Compiler/Parser.cs +++ b/src/Compiler/Parser.cs @@ -42,7 +42,7 @@ public static List Parse(Tokenizer tokenizer, string value, Element roo case 40: if (previous != 108 && CharAt(characters, length - 1) == 58) { - if (IndexOf(characters += Replace(tokenizer.Delimit(character), "&", "&\f"), "&\f") != -1) + if (IndexOf(characters += Replace(tokenizer.Delimit(character), "&", "&\f"), "&\f", Abs(index != 0 ? points[index - 1] : 0)) != -1) ampersand = -1; break; } diff --git a/src/Compiler/Utility.cs b/src/Compiler/Utility.cs index 4a69f69..17c8db0 100644 --- a/src/Compiler/Utility.cs +++ b/src/Compiler/Utility.cs @@ -11,9 +11,9 @@ public static int CharAt(string value, int index) return (int) value[index]; } - public static int IndexOf(string value, string search) + public static int IndexOf(string value, string search, int position) { - return value.IndexOf(search); + return value.IndexOf(search, position); } public static string Replace(string value, string pattern, string replacement, bool all = false) diff --git a/test/CssInCSharp.Tests/ParserTests.cs b/test/CssInCSharp.Tests/ParserTests.cs index 17e5e5f..1f82d98 100644 --- a/test/CssInCSharp.Tests/ParserTests.cs +++ b/test/CssInCSharp.Tests/ParserTests.cs @@ -142,6 +142,21 @@ public void Ampersand_In_String() ).ShouldBe(".user [href=\"https://css-tricks.com?a=1&b=2\"]{color:red;}"); } + [Fact] + public void Ampersand_In_First_Selector_Within_A_CommaSeparated_List() + { + stylis(@" + div { + display: flex; + + &.foo, + p:not(:last-child) { + background: red; + } + } + ").ShouldBe(".user div{display:flex;}.user div.foo,.user div p:not(:last-child){background:red;}"); + } + [Fact] public void Escaped_Chars_In_Selector_Identifiers() { @@ -769,6 +784,10 @@ public void Context_Character() .ShouldBe(string.Join("", ".user{background:url[img}.png];}", ".user .a{background:url[img}.png];}")); + stylis("background: url(i&m&g.png);.a {background: url(i&m&g.png);}") + .ShouldBe(string.Join("", + ".user{background:url(i&m&g.png);}", + ".user .a{background:url(i&m&g.png);}")); } [Fact]