It's an obscure nut I'm trying to crack and even trying to put it into a question is difficult so best I show what I have and what I want to get:
<div>
<span>A</span>
<span style='font-weight:bold'>B</span>
<span style='font-weight:bold;font-style:italic'>C</span>
<span style='font-weight:bold'>B</span>
<span style='font-style:italic'>C</span>
<span>A</span>
</div>
I'm trying to devise an algorithm to convert this to:
<div>
A
<span style='font-weight:bold'>
B
<span style='font-style:italic'>C</span>
B
</span>
<span style='font-style:italic'>C</span>
A
</div>
This is a simplified example of course but the goal is to take a series of sequential spans with various inline styles and convert to a simpler result that displays identically. I have spent days on this and come tantalizingly close but always found cases where it broke. I don't need it to work with all styles but font-weight, font-style, color, font-size and font-family are needed. I know it's not likely but if someone out there has already done something like this I would love to see how as it would save me a world of pain.
I have made a function to return an object of common styles of 2 elements so for example:
cs = getCommonStyles([cn,c]);
cn and c are span elements with inline styles and cs is an object containing the common styles. An example return value from this function might be:
{fontWeight:"bold", fontStyle:"italic"}
where both styles are present in each element. So any suggested answer is welcome to assume use of such function.
UPDATE: The above is just an example. The combination of styles is arbitrary as is the number of sequential input spans which could number in the hundreds. The html will be sent to the server and I'm trying to cut down on the amount of data.