5
votes

I've noticed an inconsistency in office-js between Word 2016, Word for Mac and Word Online.

When inserting a single <p> element with block/paragraph formatting, it seems that Word 2016 does not apply the paragraph formatting as defined in the style attribute of the <p> element. I can reproduce this both in the body of the document and in a Content Control.

When I execute this snippet in an empty document on Word 2016 or Word for Mac:

await Word.run(async (context) => {

    context.document.body.insertHtml(
        "<p style='text-align:right'>This should be right aligned!</p>",
        "replace");

    await context.sync();
});

the paragraph gets inserted but stays left aligned.

When I execute the same snippet on Word Online, the paragraph is right aligned, as expected.

ScriptLab Gist

An ugly workaround is to add a <br/> element after the <p> block:

"<p style='text-align:right'>This should be right aligned!</p><br/>",

This forces Word 2016 to apply the formatting of the <p> block, but we cannot apply this in all situations as it sometimes breaks the rest of the page. It's also not what we expect to have to do.

Is there another way to insert HTML that contains a single paragraph formatted <p> block that works on all hosts?

1
This certainly sounds like a bug. I'm not sure which behavior I expect to see here but it certainly should be consistent across platforms. I suspect the intended behavior of <p> depends on where you're inserting it (i.e. in the middle of an existing paragraph vs. on a new line). Which versions/builds of Word 2016 are you testing with and is this 100% consistent between Windows & Mac?Marc LaFleur
I've seen this before and I think it is a known bug. If memory serves, its the last paragraph that ignores the style attribute. Please try inserting multiple paragraphs in the same call of insertHtml, and try different kinds of formatting in the style attribute. I think you'll find that everything works except for the last paragraph.Rick Kirkham
@MarcLaFleur I would expect the behavior to be as on Word Online because that honors the paragraph formatting of the <p> element. I reproduced this on Word 2016 Version 1804 Build 9226.2021 (Office Insider). As far as I could tell, it behaves the same on Windows & Mac.Hlynur
@RickKirkham You are correct, if I insert two consecutive <p> blocks, the text-align style of the first block is applied but the text-align style of the second block is ignored. Funny enough, if I add a color style, it is applied as expected on both blocks. It seems that the problem is limited to paragraph formatting styles.Hlynur
@Hlynur I've seen it on font-family an other styles. I'm surprised it doesn't happen with color.Rick Kirkham

1 Answers

1
votes

This is a known bug. Until it is fixed, you've found what is probably the best workaround, which is to add the <br/> (or and empty <p></p>).