Skip to content
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

fix inject hash error with media #29

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/Css/CSSObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public override string ToString()

public string SerializeCss(string hashId, List<(string, string)> effects = null)
{
return Serialize(Compile(ParseStyle(true, hashId, effects)), Stringify);
return Serialize(Compile(ParseStyle(true, false, hashId, effects)), Stringify);
}

internal string ParseStyle(bool root, string hashId, List<(string, string)> effects = null)
internal string ParseStyle(bool root, bool injectHash, string hashId, List<(string, string)> effects = null)
{
var sb = new StringBuilder();

Expand All @@ -54,20 +54,28 @@ internal string ParseStyle(bool root, string hashId, List<(string, string)> effe
// sub style sheet
foreach (var subStyle in _styles)
{
var mergedKey = subStyle.Key.Trim();
var nextRoot = false;
if (mergedKey.StartsWith("@"))
var subInjectHash = false;
var nextRoot = false;
var mergedKey = subStyle.Key.Trim();

if ((root || injectHash) && !string.IsNullOrEmpty(hashId))
{
if (mergedKey.StartsWith("@"))
{
subInjectHash = true;
}
else
{
mergedKey = InjectSelectorHash(mergedKey, hashId);
}
}
else if (root && string.IsNullOrEmpty(hashId) && (mergedKey == "&" || mergedKey == ""))
{
// if is media type, skip and insert hashId from subStyle.
root = false;
mergedKey = "";
nextRoot = true;
}

if (root && !string.IsNullOrEmpty(hashId))
{
mergedKey = InjectSelectorHash(mergedKey, hashId);
}
sb.Append($"{mergedKey}{{{subStyle.Value.ParseStyle(nextRoot, hashId, effects)}}}");
sb.Append($"{mergedKey}{{{subStyle.Value.ParseStyle(nextRoot, subInjectHash, hashId, effects)}}}");
}

return sb.ToString();
Expand Down
2 changes: 1 addition & 1 deletion src/Css/Keyframe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public CSSObject this[string key]
sb.Append($"@keyframes {effectName}{{");
foreach (var subStyle in _styles)
{
sb.Append($"{subStyle.Key}{{{subStyle.Value.ParseStyle(true, string.Empty)}}}");
sb.Append($"{subStyle.Key}{{{subStyle.Value.ParseStyle(true, false, string.Empty)}}}");
}
sb.Append("}");
return (effectName, sb.ToString());
Expand Down
28 changes: 28 additions & 0 deletions test/CssInCSharp.Tests/CSSObjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,33 @@ public void Should_Where_Inject_To_All_Selectors()
};
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;}");
}

[Fact]
public void Should_Where_Not_Inject_With_Media()
{
var css3 = new CSSObject()
{
[".ant-modal-root"] = new CSSObject()
{
["@media (max-width: 767)"] = new CSSObject()
{
[".ant-modal"] = new CSSObject()
{
MaxWidth = "calc(100vw - 16px)",
Margin = "8 auto",
},
[".ant-modal-centered"] = new CSSObject()
{
[".ant-modal"] = new CSSObject()
{
Flex = 1
}
}
}
}
};

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;}}");
}
}
}
Loading