karnov / htmltoword

Ruby html to word gem
MIT License
177 stars 70 forks source link

Fix text style combinations rendering #87

Open saraoswald opened 1 year ago

saraoswald commented 1 year ago

I've been using this solution for months and haven't encountered any issues, so I wanted to share 👍

The Issue

Generated documents with multiple text styles applied, e.g. <b><i>some text</i></b>, were only being displayed with one of those styles applied when opened in Word (version 16.70).

For example, a piece of text that's underlined, bold, and italic would only appear as italic in Word.

The issue occurs when Word renders some text that has this kind of formatting, with separate <w:rPr> tags for each style:

<w:r>
  <w:rPr>
    <w:i/>
  </w:rPr>
  <w:rPr>
    <w:b/>
  </w:rPr>
  <w:t xml:space="preserve">some text</w:t>
</w:r>

Word will only display one <w:rPr> at a time, so in this case the text would appear to only be italic.

The Solution

I added explicit checks within base.xlst for each combination of formatting. This results in a singular <w:rPr> tag for each block of formatted text, as opposed to a separate tag for each style.

I also updated the spec files to reflect this change.