Skip to content

Commit

Permalink
fix CSSObject array merge error when key is the same.
Browse files Browse the repository at this point in the history
  • Loading branch information
yoli799480165 authored Jul 16, 2024
1 parent e2d756f commit b10dfc0
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 30 deletions.
10 changes: 5 additions & 5 deletions examples/Examples.Basic/Pages/Animation.razor
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
AnimationDuration = "3s",
AnimationName = new Keyframe("animation-transform")
{
["from"] = new()
["from"] = new CSSObject()
{
Transform = "translateX(0px)",
Opacity = 1
},
["to"] = new()
["to"] = new CSSObject()
{
Transform = "translateX(100px)",
Opacity = 0.2f
Expand Down Expand Up @@ -75,15 +75,15 @@
AnimationDuration = "3s",
AnimationName = new Keyframe("animation-offset")
{
["0%"] = new()
["0%"] = new CSSObject()
{
Background = "red"
},
["72%"] = new()
["72%"] = new CSSObject()
{
Background = "blue"
},
["100%"] = new()
["100%"] = new CSSObject()
{
Background = "green"
}
Expand Down
35 changes: 16 additions & 19 deletions src/Css/CSSObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using static CssInCSharp.Compiler.Serializer;
using System;
using static CssInCSharp.Compiler.Serializer;
using static CssInCSharp.Compiler.Parser;
using static CssInCSharp.Constant;
using System.Collections.Generic;
Expand All @@ -10,10 +11,10 @@ namespace CssInCSharp
{
public sealed partial class CSSObject
{
private readonly Dictionary<string, CSSObject> _styles = new();
private readonly Dictionary<string, CSSInterpolation> _styles = new();
private readonly Dictionary<string, IProperty> _properties = new();
public Dictionary<string, IProperty> GetProperties() => _properties;
public Dictionary<string, CSSObject> GetStyles() => _styles;
public Dictionary<string, CSSInterpolation> GetStyles() => _styles;

public CSSInterpolation this[string key]
{
Expand Down Expand Up @@ -74,15 +75,21 @@ internal string ParseStyle(bool root, bool injectHash, string hashId, List<(stri
mergedKey = "";
nextRoot = true;
}

sb.Append($"{mergedKey}{{{subStyle.Value.ParseStyle(nextRoot, subInjectHash, hashId, effects)}}}");
var values = subStyle.Value.ToCssArray();
var valueStr = new StringBuilder();
foreach (var value in values)
{
valueStr.Append(value.ParseStyle(nextRoot, subInjectHash, hashId, effects));
}
sb.Append($"{mergedKey}{{{valueStr}}}");
}

return sb.ToString();
}

public CSSObject Merge(CSSObject css)
{
if (css == null) return this;
var props = css.GetProperties();
foreach (var prop in props)
{
Expand All @@ -96,7 +103,7 @@ public CSSObject Merge(CSSObject css)
if (_styles.TryGetValue(style.Key, out var value))
{
// if exists, merge to sub style sheet.
value.Merge(style.Value);
value.AsT0.Merge(style.Value.AsT0);
}
else
{
Expand All @@ -108,15 +115,6 @@ public CSSObject Merge(CSSObject css)
return this;
}

public CSSObject Merge(CSSObject[] objects)
{
foreach (var css in objects)
{
Merge(css);
}
return this;
}

private void SetStyle(string key, CSSInterpolation value)
{
if (key == null) return;
Expand All @@ -138,14 +136,13 @@ private void SetStyle(string key, CSSInterpolation value)
/*
* if is CSSObject or CSSObject[]
*/
var cssObject = value.IsT0 ? value.AsT0 : new CSSObject().Merge(value.ToCssArray());
if (cssObject == null) return;
if (key == MERGE_OPERATOR)
{
Merge(cssObject);
if (!value.IsT0) throw new Exception("Invalid merge value type.");
Merge(value.AsT0);
return;
}
_styles[key] = cssObject;
_styles[key] = value;
}

private string InjectSelectorHash(string key, string hashId)
Expand Down
6 changes: 3 additions & 3 deletions src/Css/Keyframe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace CssInCSharp
public sealed class Keyframe
{
private readonly string _name;
private readonly Dictionary<string, CSSObject> _styles = new();
private readonly Dictionary<string, CSSInterpolation> _styles = new();

private Keyframe()
{
Expand All @@ -23,7 +23,7 @@ public Keyframe(string name, CSSObject css)
_styles = css.GetStyles();
}

public CSSObject this[string key]
public CSSInterpolation this[string key]
{
get => _styles[key];
set => _styles[key] = value;
Expand All @@ -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, false, string.Empty)}}}");
sb.Append($"{subStyle.Key}{{{subStyle.Value.AsT0.ParseStyle(true, false, string.Empty)}}}");
}
sb.Append("}");
return (effectName, sb.ToString());
Expand Down
29 changes: 28 additions & 1 deletion test/CssInCSharp.Tests/CSSObjectTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Shouldly;
using Newtonsoft.Json.Linq;
using Shouldly;
using Xunit;

namespace CssInCSharp.Tests
Expand Down Expand Up @@ -150,5 +151,31 @@ public void Should_Where_Not_Inject_With_Media()

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

[Fact]
public void Should_Media_Array_Merge_Into_One()
{
var css = new CSSObject
{
["@media (max-width: 575px)"] = new CSSInterpolation[]
{
new CSSObject()
{
[$".ant-form-item .ant-form-item-label"] = new CSSObject()
{
Padding = 0,
}
},
new CSSObject()
{
[$".ant-col-xs-24.ant-form-item-label"] = new CSSObject()
{
Margin = 0,
}
}
}
};
css.SerializeCss("css-3nv711").ShouldBe("@media (max-width: 575px){:where(.css-3nv711).ant-form-item .ant-form-item-label{padding:0;}:where(.css-3nv711).ant-col-xs-24.ant-form-item-label{margin:0;}}");
}
}
}
4 changes: 2 additions & 2 deletions test/CssInCSharp.Tests/StyleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public void Should_AnimationName_Render_As_A_Separate_Style_Tag()
AnimationDuration = "3s",
AnimationName = new Keyframe("transformAnimation")
{
["from"] = new()
["from"] = new CSSObject()
{
Transform = "translateX(0px)",
Opacity = 1
},
["to"] = new()
["to"] = new CSSObject()
{
Transform = "translateX(100px)",
Opacity = 0.2f
Expand Down

0 comments on commit b10dfc0

Please sign in to comment.