Performing read and write operations on targeted CSS classes using C#

I'm currently developing a C# application that needs to manipulate CSS files. Imagine you have the following CSS code snippet:

.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;
}

My goal is to search for specific classes in the file, such as ClassOne, and extract the corresponding background-color property for those classes. This task only needs to be done for certain classes in the CSS file - for example, I may only want to extract the background-color values for ClassOne and ClassThree.

The resulting application should then transfer these stored values to another CSS file with the same set of classes.

I am aware of ExCSS but unsure about its utility in this context and how to effectively leverage it. Can someone provide guidance on this matter?

Answer №1

To kickstart your journey with ExCss, you can begin by focusing on the parsing aspect. Here's a snippet to guide you:

    [Test]
    public void Apple([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);            
    }

Instead of relying on Linq, you may consider iterating through your CSS and constructing a new CSS containing values from declarations that match your target criteria. Here's an example approach:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can you position an element at the bottom of a div using CSS?

Could you please assist me in aligning an element to the bottom of a div using CSS? Below is a screenshot of my webpage for reference: http://prntscr.com/5ivlm8 (I have indicated with an arrow where I want the element to be) Here is the code snippet of m ...

Retrieve the text content of the paragraph element when its parent is clicked. The paragraph element belongs to a group that contains many

In a function I'm working on, I need to retrieve the specific text within a p element when its parent (.photoBackground) is clicked. The purpose of this operation is to grab the caption for a gallery, where there are multiple p elements with the same ...

Creating rounded corners in Firefox can result in a gap between the border and the background

I'm trying to round the corners of a div, but in Firefox there seems to be an issue with whitespace between the border and background color. Check out my demo fiddle. <div>&nbsp;</div> div { margin: 20px; width: 250px; ...

The partnership between Selenium and WhitePages has created an innovative solution

I am currently attempting to register on . The registration process requires entering my first name, last name, email address, and password. For the first name field, I attempted to locate the element by CSS using the syntax "input#name_fname", however, I ...

Trouble with webpage design persists following recent modifications

Today, I decided to make some alterations on the header.php file of my WordPress website in order to tweak the flag icons displayed at the top of the page. However, when I refreshed the page, everything looked completely chaotic. Even after undoing all th ...

Using Roboto Typeface in Cascading Style Sheets

I am currently working on implementing the "Roboto" font in my Prestashop website. The design files I received are in .psd format, and the graphic designer used fonts "Roboto Medium" and "Roboto Regular". To clarify, if I want to use Roboto Normal, can I s ...

"Looking to swap out the Angular theme's CSS stylesheet for your entire application? Here's

I was initially facing an issue while trying to import CSS through index.html as I kept getting a MIME type error: enter image description here The browser refused to apply the stylesheet from 'http://localhost:4200/css/pink-bluegrey.css' because ...

Discovering the most recently selected element in jQuery

Currently reading a jQuery tutorial from the Head First series and practicing with an example where I need to identify the last selected element. In Chapter 2, there is a task involving four images on a page. When a user clicks on any of these images, the ...

Tips for switching a group of buttons within a userscript by clicking a single button?

Apologies if my wording is not clear, allow me to clarify. I am in the process of developing a userscript that will display a set of buttons below a main button when clicked. These additional buttons will serve different functions and should disappear whe ...

How to use AngularJS to collapse various panels with unique content

Hey everyone, I'm working on developing a collapsible panel using Angular. The panel should consist of a header and body to display the content. The desired behavior is that when a button is clicked, the content collapses down, and clicking the same b ...

When my viewport's size is less than 998px, a blank space appears

While configuring media queries in my HTML and CSS project, everything seemed to be working well. However, I noticed that when I shrink the screen, there is an empty white space on the right side and a horizontal scroll bar appears at the bottom without an ...

Conceal elements of a rectangular shape using CSS

https://i.stack.imgur.com/ebaeI.jpg I need help hiding the second rectangular shape on my website. I want the width to be 100% and responsive, but whenever I use position: absolute; with right:-10%, it's not working as expected. Even trying body { ma ...

Adjust the vertical size of the slider in Jssor

Hi there! I'm currently working on a slider that I want to have a dynamic width (100% of the container) and a static height of 550px on PCs, while being responsive on mobile devices. Below is my code snippet: <div class="col-md-6 right-col" id="sl ...

Display or conceal a division underneath a dropdown menu based on selections retrieved from a SQL Server database

Presented here is my JavaScript code. function appendItemforPurchaseOrder() { debugger var rowNumber = parseInt($(".itemmapContainer").attr("data-rownumber")); rowNumber = isNaN(rowNumber) ? 1 : rowNumber + 1; var addNewItemDetailHtml = ...

The jQuery remove function will only take effect on the second click following an AJAX request

I'm facing an issue with my jQuery code where two divs contain lists of links, triggering AJAX calls to append JSON data to a separate div. Upon clicking a link, the corresponding link div should hide. There's also a third div with the class "pan ...

Website layout looking unique due to pixel width adaptation from standard width

I'm currently working on a website with full width design. On my computer, the website displays as full width, but on other computers it shows extra space on the sides. How can I ensure that this website is truly full width? .wrapper_boxed { wid ...

How to perfectly center the Bootstrap Modal using CSS styling

Looking for a way to vertically align the Bootstrap Modal? I've attempted using CSS with no success. I tried this: .modal-dialog { display: inline-block; text-align: left; vertical-align: middle; } However, the modal still displays in t ...

Interacting with hyperlinks within a changing table using Selenium in C#

TABLE ISSUES I am facing a challenge with my table as I need to click on the "Review Vacancy" link but I do not know how to accomplish this task. It would be greatly appreciated if someone could provide me with some code that I can review and understand. ...

What is causing my Fabric.js canvas to malfunction?

Here is the link to my JSFiddle project: http://jsfiddle.net/UTf87/ I am facing an issue where the rectangle I intended to display on my canvas is not showing up. Can anyone help me figure out why? HTML: <div id="CanvasContainer"> <canvas id ...

The issue with collapsible elements not functioning properly in an Angular application involving CSS and JS

My implementation of collapsible in a plain HTML page looks like this: <!DOCTYPE html> <html> <head> <title></title> <style> button.accordion { background-color: #777; color: white; cursor: pointer; p ...