Basic Java HTML parser for extracting CSS attributes

Is there a way to parse the style attribute of a basic <font> tag and then convert it back to simple html attributes? For example, if I have this string

<font style="font-family:tahoma;font-size:24px;color:#9900CC;">
, is it possible to convert it to
<font size="24" color="#9900CC" face="tahoma">
using regex or any other method?

Any help would be appreciated. Thank you.

Answer №1

Here is a clever solution I came up with that may not be the cleanest, but it gets the job done effectively:

public static String convertCSStoHTML(String css)
{

    List<List<String>> allStyleTags = regexFindMultiStrings("<style[^>]*(type=['\"][^'\"]+['\"])[^>]*>", css);
    String allAttributes = "";

    for(int i=0; i<allStyleTags.size(); i++)
    {
        String style = allStyleTags.get(i).get(0);
        String fontSize = regexFindString("font-size:([^0-9]+)", style);
        String fontColor = regexFindString("color:([^;]+);", style);
        String fontFamily = regexFindString("font-family:([^;]+)", style);

        if(!fontSize.isEmpty())
             allAttributes += "size=\""+fontSize+"\" ";

        if(!fontColor.isEmpty())
             allAttributes += "color=\"" + fontColor + "\" ";

        if(!fontFamily.isEmpty())
             allAttributes += "face=\""+fontFamily+"\"";

        //replace the first occurrence with modified attributes
        css = css.replaceFirst(style, allAttributes);

        //reset attribute string
        allAttributes = "";
    }

    return css;

}

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

Determine the specific value of an HTML table cell by specifying the column name for a specific row

One challenge I am facing is dynamically creating a table using JSON without knowing in advance the number of columns and/or rows that will be generated. I have successfully added a clicked event for the row that gets selected. In every table, there will ...

Could someone kindly clarify the workings of this jQuery script for me?

I found this code online, but I'm struggling to comprehend its functionality. In order to make any edits for my needs, I need to understand how it operates. The purpose of this script is to close a panel with an upward slide animation when the "x" but ...

How do you add dynamic content to modals in AngularJS and Angular Strap based on the button that was clicked?

Is there a way to make the buttons in the AngularJS + AngularStrap modal more dynamic? Currently, they display static content, but I would like them to show the content of the first column (first name) of the exact row where the button was clicked. What is ...

Obtain the dropdown menu links for every row in a table using Python3 along with Selenium

Seeking assistance as a beginner in Python, I am attempting to automate the downloading process of old newspaper archives from a specific website () using the script provided below. Despite my efforts, I have encountered difficulty in getting the script t ...

Troubleshooting: Twitter Bootstrap dropdown feature experiencing issues within navigation bar

I am facing an issue with getting the bootstrap dropdown to function properly. I have tested it on my mobile device and noticed that the menu does not drop down as expected. Here is a DEMO Below is the code snippet: <nav class="navbar navbar-inverse ...

Choose the camera when utilizing the navigate.getUserMedia() function

I am currently utilizing the navigate.getUserMedia() method to record video on my mobile device and perform additional processing on it. However, at the moment, it is only capturing video using the front camera. How can I make it switch to the rear facing ...

Applying Bootstrap's inline styling to adjust the width of a label

I'm having trouble adjusting the width of a Bootstrap label using inline styles. Here is my code: <td><span class="label label-info" style="width:30px; text-align:center; background-color:#6d8cbf"> TEXT </span></td> Unfort ...

Encountering issues while trying to access a webpage using Selenium web driver 2.33 and Firefox 22

I am currently using Selenium webdriver version 2.33 and Firefox version 22. I have been attempting to open a URL with the code provided below. public class RunGoogle { public static void main(String[] args){ //System.setProperty("webdriv ...

Learn how to dynamically print the class name of elements and retrieve their corresponding XPath using Selenium WebDriver

To print either class "section__form" or "section__blank-slate" depending on visibility using the Selenium web driver. Case 1: Case 2: <body> <div class="main"> <div class="order-summary-recap"> <div class ...

Managing JSON data retrieval and manipulation techniques

My code is set up to display the image, title, and summary for all entries in a JSON file. However, I only want to display the image, title, and summary for the first entry, and only show the title for the rest of the entries. Please advise. <html> ...

Behavior of Bootstrap Modal When Closing

I'm encountering an issue with a button labeled Sign Up that is supposed to load content in the form of a Modal from another file. Initially, when I click the button for the first time, everything works smoothly without any errors or warnings. However ...

File uploading feature in Bootstrap (without the need for Javascript)

While I've seen this question posted in a similar style before, one critical point (in my opinion) hasn't been addressed - is there a way to achieve this without using JavaScript? On mobile devices, overscripted sites can sometimes have non-worki ...

Update location when visibility changes

Looking for a way to automatically refresh an HTML page when a specific div becomes visible, but seems like I am missing something. There is a JavaScript code, divslide.js, that changes the display style of the div from none to inline at predetermined time ...

Clicking on the images will display full page versions

Apologies for not making an attempt just yet, but I'm struggling to locate what I need. Here's the menu structure in question: <div class="overlay-navigation"> <nav role="navigation"> <ul class="test"> & ...

Find all paragraphs with the CSS property "background-color" using JQuery

My task involves modifying the CSS of a paragraph tag that has a background color of #aaddff. The code I have written seems to be missing something, as the border is not appearing as expected. Should I use element.style or is there a different approach I ...

I am struggling to link my JS application with a PHP file as it is showing "PHP file not found

I am facing an issue with my JS application. I am unable to send data from jQuery to a PHP file, as I encountered this error message: POST https://magazyn.rob-tech.pl/savesettings.php 404 The app is running on PORT=8080 npm run start and the package.json ...

how can I instruct assertJsonEquals to disregard a specific field when performing comparisons

I've been utilizing the assertJsonEquals function from a library called JsonUnit In my code, I'm doing the following: assertJsonEquals(resource("ExpecedResponse.json"), ActualResponse, when(IGNORING_ARRAY_ORDER)); The ...

Can anyone actually manage to preserve the backslash in a PHP string while encoding it through JSON?

After thoroughly searching through numerous discussions on this topic, I have encountered a specific issue. Occasionally, a user inputs a backslash in their text. To store this input data in my database as JSON, I serialize it using PHP (JSON encode and de ...

Is it necessary for two inline divs to be floated?

I am facing an issue with my layout. I have two child divs within a parent div, each set to 50% width and displayed inline. To style the parent div, I am using the :after pseudo-element to draw a horizontal rule () with 15px top and bottom margins. The pr ...

Creating a new web application, I require a loading overlay to appear during transitions between pages

I've checked high and low, but I can't seem to find the solution! My webapp has a page that is bogged down with data causing it to load slowly. Is there a way to display a loading div while transitioning to the next page? Perhaps something like ...