Skip to content

Commit e2d756f

Browse files
fix inject hash error with media
1 parent bd30f98 commit e2d756f

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

src/Css/CSSObject.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public override string ToString()
2828

2929
public string SerializeCss(string hashId, List<(string, string)> effects = null)
3030
{
31-
return Serialize(Compile(ParseStyle(true, hashId, effects)), Stringify);
31+
return Serialize(Compile(ParseStyle(true, false, hashId, effects)), Stringify);
3232
}
3333

34-
internal string ParseStyle(bool root, string hashId, List<(string, string)> effects = null)
34+
internal string ParseStyle(bool root, bool injectHash, string hashId, List<(string, string)> effects = null)
3535
{
3636
var sb = new StringBuilder();
3737

@@ -54,20 +54,28 @@ internal string ParseStyle(bool root, string hashId, List<(string, string)> effe
5454
// sub style sheet
5555
foreach (var subStyle in _styles)
5656
{
57-
var mergedKey = subStyle.Key.Trim();
58-
var nextRoot = false;
59-
if (mergedKey.StartsWith("@"))
57+
var subInjectHash = false;
58+
var nextRoot = false;
59+
var mergedKey = subStyle.Key.Trim();
60+
61+
if ((root || injectHash) && !string.IsNullOrEmpty(hashId))
62+
{
63+
if (mergedKey.StartsWith("@"))
64+
{
65+
subInjectHash = true;
66+
}
67+
else
68+
{
69+
mergedKey = InjectSelectorHash(mergedKey, hashId);
70+
}
71+
}
72+
else if (root && string.IsNullOrEmpty(hashId) && (mergedKey == "&" || mergedKey == ""))
6073
{
61-
// if is media type, skip and insert hashId from subStyle.
62-
root = false;
74+
mergedKey = "";
6375
nextRoot = true;
6476
}
6577

66-
if (root && !string.IsNullOrEmpty(hashId))
67-
{
68-
mergedKey = InjectSelectorHash(mergedKey, hashId);
69-
}
70-
sb.Append($"{mergedKey}{{{subStyle.Value.ParseStyle(nextRoot, hashId, effects)}}}");
78+
sb.Append($"{mergedKey}{{{subStyle.Value.ParseStyle(nextRoot, subInjectHash, hashId, effects)}}}");
7179
}
7280

7381
return sb.ToString();

src/Css/Keyframe.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public CSSObject this[string key]
3636
sb.Append($"@keyframes {effectName}{{");
3737
foreach (var subStyle in _styles)
3838
{
39-
sb.Append($"{subStyle.Key}{{{subStyle.Value.ParseStyle(true, string.Empty)}}}");
39+
sb.Append($"{subStyle.Key}{{{subStyle.Value.ParseStyle(true, false, string.Empty)}}}");
4040
}
4141
sb.Append("}");
4242
return (effectName, sb.ToString());

test/CssInCSharp.Tests/CSSObjectTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,33 @@ public void Should_Where_Inject_To_All_Selectors()
122122
};
123123
css2.SerializeCss("css-3nv711").ShouldBe(":where(.css-3nv711)[class^=\"ant-affix\"]::before,:where(.css-3nv711)[class*=\" ant-affix\"]::before,:where(.css-3nv711)[class^=\"ant-affix\"]::after,:where(.css-3nv711)[class*=\" ant-affix\"]::after{box-sizing:border-box;}");
124124
}
125+
126+
[Fact]
127+
public void Should_Where_Not_Inject_With_Media()
128+
{
129+
var css3 = new CSSObject()
130+
{
131+
[".ant-modal-root"] = new CSSObject()
132+
{
133+
["@media (max-width: 767)"] = new CSSObject()
134+
{
135+
[".ant-modal"] = new CSSObject()
136+
{
137+
MaxWidth = "calc(100vw - 16px)",
138+
Margin = "8 auto",
139+
},
140+
[".ant-modal-centered"] = new CSSObject()
141+
{
142+
[".ant-modal"] = new CSSObject()
143+
{
144+
Flex = 1
145+
}
146+
}
147+
}
148+
}
149+
};
150+
151+
css3.SerializeCss("css-3nv711").ShouldBe("@media (max-width: 767){:where(.css-3nv711).ant-modal-root .ant-modal{max-width:calc(100vw - 16px);margin:8 auto;}:where(.css-3nv711).ant-modal-root .ant-modal-centered .ant-modal{flex:1;}}");
152+
}
125153
}
126154
}

0 commit comments

Comments
 (0)