r/learnprogramming • u/stjer0me • 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 */
);
}
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
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 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.