C#读取和写入特定的CSS类

时间:2022-10-29 17:03:15

I'm trying to write a C# application that will perform an operation on some CSS files. Say you have the following CSS code:

我正在尝试编写一个C#应用程序,它将对某些CSS文件执行操作。假设您有以下CSS代码:

.ClassOne
{   
    color: #FFFFFF;    
    font-weight:normal;
    font-size: 9pt;
    font-family: Tahoma;                    
    vertical-align: middle;
    border: solid 1px #7F7F7F;  
    padding: 1px 1px 1px 1px;
    background-color: #B48410;
    background-position: center center;
}

.ClassTwo
{
    color: #FFFFFF;        
        background-repeat: repeat-x;
    background-color: #000000;
}

.ClassThree
{   
    color: #000000;    
    font-weight:normal;
    font-size: 9pt;
    font-family: Tahoma;                    
    vertical-align: left;
    border: solid 1px #F3Dc51;  
    padding: 1px 1px 1px 1px;
    background-color: #A32DF1;
}

What I would like to do is search the file for specific classes, say ClassOne, then store the background-color element applicable to that class. This wouldn't have to be done for all classes in the CSS file, so in the example above I might just want to store the background-color for ClassOne and ClassThree.

我想要做的是搜索文件中的特定类,比如ClassOne,然后存储适用于该类的background-color元素。这不必对CSS文件中的所有类进行,因此在上面的示例中,我可能只想存储ClassOne和ClassThree的背景颜色。

The application should then be copy the stored values into another CSS file, with the same classes.

然后,应用程序应将存储的值复制到具有相同类的另一个CSS文件中。

I will know the classes that these operations have to be performed on.

我将知道必须执行这些操作的类。

I have taken a look at ExCSS, but i'm not sure how to use it, and if it would be helpful in this instance.

我看过ExCSS,但我不知道如何使用它,如果它在这个例子中有用。

Can anyone help?

有人可以帮忙吗?

1 个解决方案

#1


0  

This should get you started on ExCss, at least on the parsing part:

这应该让你开始使用ExCss,至少在解析部分:

    [Test]
    public void Banana([Values (".ClassThree")] string targetClass, [Values ("#A32DF1")] string expectedColor)
    {
        var parser = new Parser();
        var sheet = parser.Parse(sample);
        var result = sheet.Rulesets
            .Where(s => s.Selector.ToString() == targetClass)
            .SelectMany(r => r.Declarations)
            .FirstOrDefault(d => d.Name.Equals("background-color", StringComparison.InvariantCultureIgnoreCase))
            .Term
            .ToString();
        Assert.AreEqual(expectedColor, result);            
    }

What you'll end up doing, perhaps instead of using Linq, is to loop through your css and build a new css with the values in the declarations that match what you are targeting. For example, below, which is probably not optimally efficient, but could do the trick:

你最终要做的事情,也许不是使用Linq,就是循环你的css并使用与你所针对的声明匹配的声明中的值来构建一个新的css。例如,下面,这可能不是最佳效率,但可以做到这一点:

        var targetClasses = new List<string> { ".ClassOne", ".ClassThree" };
        var targetDecls = new List<string> { "background-color" };

        var parser = new Parser();
        var sheet = parser.Parse(sample);
        foreach (var r in sheet.Rulesets)
        {
            if (targetClasses.Contains(r.Selector.ToString()))
            {
                Debug.WriteLine(r.Selector.ToString());
                Debug.WriteLine("{");
                foreach (var d in r.Declarations)
                {
                    if (targetDecls.Contains(d.Name))
                    {
                        Debug.WriteLine("\t" + d);
                    }
                }
                Debug.WriteLine("}");
            }
        }

#1


0  

This should get you started on ExCss, at least on the parsing part:

这应该让你开始使用ExCss,至少在解析部分:

    [Test]
    public void Banana([Values (".ClassThree")] string targetClass, [Values ("#A32DF1")] string expectedColor)
    {
        var parser = new Parser();
        var sheet = parser.Parse(sample);
        var result = sheet.Rulesets
            .Where(s => s.Selector.ToString() == targetClass)
            .SelectMany(r => r.Declarations)
            .FirstOrDefault(d => d.Name.Equals("background-color", StringComparison.InvariantCultureIgnoreCase))
            .Term
            .ToString();
        Assert.AreEqual(expectedColor, result);            
    }

What you'll end up doing, perhaps instead of using Linq, is to loop through your css and build a new css with the values in the declarations that match what you are targeting. For example, below, which is probably not optimally efficient, but could do the trick:

你最终要做的事情,也许不是使用Linq,就是循环你的css并使用与你所针对的声明匹配的声明中的值来构建一个新的css。例如,下面,这可能不是最佳效率,但可以做到这一点:

        var targetClasses = new List<string> { ".ClassOne", ".ClassThree" };
        var targetDecls = new List<string> { "background-color" };

        var parser = new Parser();
        var sheet = parser.Parse(sample);
        foreach (var r in sheet.Rulesets)
        {
            if (targetClasses.Contains(r.Selector.ToString()))
            {
                Debug.WriteLine(r.Selector.ToString());
                Debug.WriteLine("{");
                foreach (var d in r.Declarations)
                {
                    if (targetDecls.Contains(d.Name))
                    {
                        Debug.WriteLine("\t" + d);
                    }
                }
                Debug.WriteLine("}");
            }
        }