Skip to content

Commit

Permalink
Making setting public for Prose configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-scott-78 committed Apr 9, 2022
1 parent 4e4c7b5 commit 63acd04
Show file tree
Hide file tree
Showing 10 changed files with 396 additions and 177 deletions.
4 changes: 2 additions & 2 deletions src/MonorailCss/Css/CssColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CssColor(int r, int g, int b)
/// <returns>Color in the format of rgb( r g b).</returns>
public string AsRgb()
{
return $"rgb({_r} {_g} {_b})";
return $"rgba({_r}, {_g}, {_b}, 1)";
}

/// <summary>
Expand All @@ -64,6 +64,6 @@ public string AsRgb()
/// <returns>The color in the format of rgb(r g b / opacity).</returns>
public string AsRgbWithOpacity(string opacity)
{
return $"rgb({_r} {_g} {_b} / {opacity})";
return $"rgba({_r}, {_g}, {_b}, {opacity})";
}
}
41 changes: 37 additions & 4 deletions src/MonorailCss/Css/CssDeclarationList.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Immutable;

namespace MonorailCss.Css;

Expand Down Expand Up @@ -75,23 +76,55 @@ IEnumerator IEnumerable.GetEnumerator()
/// </summary>
public class CssRuleSetList : IEnumerable<CssRuleSet>
{
private readonly List<CssRuleSet> _declarations = new();
private ImmutableDictionary<CssSelector, CssRuleSet> _declarations = ImmutableDictionary<CssSelector, CssRuleSet>.Empty;

/// <summary>
/// Adds a new rule set to the list.
/// </summary>
/// <param name="ruleSet">The rule set to add.</param>
public void Add(CssRuleSet ruleSet) => _declarations.Add(ruleSet);
public void Add(CssRuleSet ruleSet)
{
if (_declarations.TryGetValue(ruleSet.Selector, out var existingRuleSet))
{
_declarations = _declarations.SetItem(ruleSet.Selector, existingRuleSet + ruleSet);
}
else
{
_declarations = _declarations.Add(ruleSet.Selector, ruleSet);
}
}

/// <inheritdoc />
public IEnumerator<CssRuleSet> GetEnumerator()
{
return _declarations.GetEnumerator();
return _declarations.Values.GetEnumerator();
}

/// <inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return _declarations.GetEnumerator();
return _declarations.Values.GetEnumerator();
}

/// <summary>
/// Adds two rule set lists together.
/// </summary>
/// <param name="ruleSet1">The first rule set.</param>
/// <param name="ruleSet2">The second rule set.</param>
/// <returns>A new rule set with the existing rules.</returns>
public static CssRuleSetList operator +(CssRuleSetList ruleSet1, CssRuleSetList ruleSet2)
{
var newRuleSetList = new CssRuleSetList();
foreach (var ruleSet in ruleSet1)
{
newRuleSetList.Add(ruleSet);
}

foreach (var ruleSet in ruleSet2)
{
newRuleSetList.Add(ruleSet);
}

return newRuleSetList;
}
}
8 changes: 6 additions & 2 deletions src/MonorailCss/Css/CssProperties.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System.Diagnostics.CodeAnalysis;
using CaseExtensions;
#pragma warning disable CS1591
#pragma warning disable SA1600

namespace MonorailCss.Css;

// ReSharper disable UnusedMember.Global
/// <summary>
/// Collection of stand CSS property names.
/// </summary>
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private")]
internal static class CssProperties
public static class CssProperties
{
// future me will convert these to constants.
public static string Background = nameof(Background).ToKebabCase();
Expand Down
53 changes: 45 additions & 8 deletions src/MonorailCss/Css/CssRuleSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,28 @@ public record CssStylesheet(ImmutableList<CssMediaRule> MediaRules);
/// </summary>
/// <param name="Selector">The CSS selector.</param>
/// <param name="DeclarationList">The CSS declaration list.</param>
public record CssRuleSet(CssSelector Selector, CssDeclarationList DeclarationList);
public record CssRuleSet(CssSelector Selector, CssDeclarationList DeclarationList)
{
/// <summary>
/// Adds two rule sets together.
/// </summary>
/// <param name="ruleSet1">The first rule set.</param>
/// <param name="ruleSet2">The second rule set.</param>
/// <returns>A new instance of the two rule sets combined.</returns>
/// <exception cref="InvalidOperationException">Throws if the rule sets have different selectors.</exception>
public static CssRuleSet operator +(CssRuleSet ruleSet1, CssRuleSet ruleSet2)
{
if (ruleSet1.Selector.Equals(ruleSet2.Selector) == false)
{
throw new InvalidOperationException("Cannot add ruleset with different selectors.");
}

return ruleSet1 with
{
DeclarationList = ruleSet1.DeclarationList + ruleSet2.DeclarationList,
};
}
}

/// <summary>
/// Represents a CSS selector.
Expand All @@ -41,6 +62,28 @@ public override string ToString()
return sb.ToString();
}

/// <inheritdoc />
public virtual bool Equals(CssSelector? other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return Selector == other.Selector && PseudoClass == other.PseudoClass && PseudoElement == other.PseudoElement;
}

/// <inheritdoc />
public override int GetHashCode()
{
return HashCode.Combine(Selector, PseudoClass, PseudoElement);
}

/// <summary>
/// Returns a new CSS selector from a string.
/// </summary>
Expand All @@ -61,10 +104,4 @@ public record CssDeclaration(string Property, string Value);
/// </summary>
/// <param name="Features">A list of media rules features.</param>
/// <param name="RuleSets">The defined rule sets for the media rule feature.</param>
public record CssMediaRule(ImmutableList<string> Features, ImmutableList<CssRuleSet> RuleSets);

/// <summary>
/// Represents the root media rule with no features.
/// </summary>
/// <param name="RuleSets">The rule sets.</param>
public record RootMediaRule(ImmutableList<CssRuleSet> RuleSets) : CssMediaRule(ImmutableList<string>.Empty, RuleSets);
public record CssMediaRule(ImmutableList<string> Features, ImmutableList<CssRuleSet> RuleSets);
21 changes: 18 additions & 3 deletions src/MonorailCss/Plugins/CssSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@

namespace MonorailCss.Plugins;

internal record CssSettings
/// <summary>
/// Configuration of a CSS settings.
/// </summary>
public record CssSettings
{
/// <summary>
/// Gets the declarations for the root element.
/// </summary>
public CssDeclarationList Css { get; init; } = new();

public ImmutableList<CssRuleSet> ChildRules { get; init; } = ImmutableList<CssRuleSet>.Empty;
/// <summary>
/// Gets the child rules for the element.
/// </summary>
public CssRuleSetList ChildRules { get; init; } = new CssRuleSetList();

/// <summary>
/// Combines two settings together. The right operator will override.
/// </summary>
/// <param name="settings1">The first settings.</param>
/// <param name="settings2">The second settings.</param>
/// <returns>A new settings instance.</returns>
public static CssSettings operator +(CssSettings settings1, CssSettings settings2)
{
return settings1 with
{
ChildRules = settings1.ChildRules.AddRange(settings2.ChildRules),
ChildRules = settings1.ChildRules + settings2.ChildRules,
Css = settings1.Css + settings2.Css,
};
}
Expand Down

0 comments on commit 63acd04

Please sign in to comment.