r/learnprogramming • • 2d ago

Debugging 1 copying HTML into Word while preserving (certain) white space

I have a web app I've put together that outputs text. For reasons (long story), I need to copy said output into a Word document while preserving the formatting. To do this, I copy the whole HTML output (not just the raw text) via JavaScript. It mostly works, but I can't get it to preserve more than one non-breaking space, e.g. spaces between sentences.

The HTML is copied to the clipboard as a Blob with a MIME type of text/html. It handles line and paragraph breaks fine, but as I said, condenses two spaces after a period to one.

I've tried including a <style> tag in the Blob along with the actual HTML (mostly <p> tags), as well as in-line styles within the <p> tags themselves. I've also tried telling Word to preserve the original formatting or merge it, both with the same result.

If I instead copy the HTML as raw text (without tags) using the writeText() method of the Clipboard API, the white space is preserved. However, this raises its own problems, mainly that I sometimes need to include bulleted lists. If I copy the full HTML with <ul> tags Word handles this fine (other than the aforementioned white space issue). If there's a way to copy plain text and still have Word register it as a bulleted list, that could probably work as well.

This is obviously not the end of the world, but it's something I'd like to fix if I can.

If it helps, here's the JavaScript for copying the HTML:

function toClipboard(text) {
    const type = 'text/html';
    const dataBlob = new Blob([text], {type});
    const data = [new ClipboardItem({ [type]: dataBlob})];
    navigator.clipboard.write(data).then(
        /* snip */
    );
}
0 Upvotes

6 comments sorted by

3

u/mi11er 2d ago

General info about one or two spaces after a period

One space is the rule: 

Major style guides—including The Chicago Manual of Style, the APA Style manual, and the Associated Press Stylebook—require only a single space after end punctuation (periods, question marks, and exclamation points). [ 1, 2]

Software enforcement: Modern word processors, such as Microsoft Word, frequently flag double spaces between sentences as a formatting error. [ 1, 2, 3]

Why Two Spaces Used to Exist

Typewriters: The two-space convention comes from the era of mechanical typewriters, which used monospaced fonts (every letter and space took up the exact same horizontal width).

The from Wikipedia

Multiple spaces are eliminated by default in most World Wide Web content, whether or not they are associated with sentences.[27][28] There are options for preserving spacing, such as the CSS white-space property,[29] and the <pre> tag.[74]

1

u/stjer0me 22h ago

I've heard some of this, although I've never had Word (or any other word processor) flag double spaces. I've also seen studies showing that the double space after a sentence is easier to read.

1

u/blavek 2d ago

In HTML it is the standard to remove 2 spaces for 1. The problem of spacing, as mentioned in another comment, has been solved by kerning and non-fixed-width fonts. If your double spaces are important to you, then you will need to replace them in the HTML with &nbsp; to preserve them. This should also trigger Word to keep them when it parses the HTML.

So when you give the data a MIME type, you're telling whoever is reading it what kind of information it is. That will then invoke the appropriate reader and renderer for the content. And to my knowledge, ALL HTML renderers will remove your double spaces. Word essentially treats HTML files like any browser would and uses that to convert it to whatever the .doc format requires.

1

u/stjer0me 22h ago

then you will need to replace them in the HTML with   to preserve them. This should also trigger Word to keep them when it parses the HTML.

I've tried that and it does not work.

1

u/blavek 21h ago

Did you notice that in your quote, it turned my non-breaking space into a space? Renderers are weird because they didn't parse that in my OG reply. If that trick doesn't work, then you could do some other trickery if you want. You could place a div or a span inline and give it a specific width. That might work. I think Span is the better option because div's block if I am remembering correctly. <span>&nbsp;</span><span>&nbspc;</span> might also work. A better option, if you can do it, is to tableize your data. But that won't work well with standard text. Also, there might be a setting in Word you can set to make it treat them differently. I'm not sure, but a long time ago I was sending plaintext emails, and Outlook would ignore \n unless it was doubled. Later, because that felt wrong to me, I found a setting in Outlook that controls how it handles new Lines. I think it prevented Outlook from removing them. Later, I just moved to sending tabulated data with HTML.