Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaks include undesired spaces #213

Closed
jacwright opened this issue Dec 3, 2018 · 6 comments
Closed

Breaks include undesired spaces #213

jacwright opened this issue Dec 3, 2018 · 6 comments
Labels

Comments

@jacwright
Copy link
Contributor

jacwright commented Dec 3, 2018

paragraph.createTextRun().break(); is adding a bunch of spaces on the line of the break.

I think it might be because a linebreak is added to the document.xml like this:

                <w:br>
                </w:br>

and should be like this:

                <w:br></w:br>

or

                <w:br />
@jacwright
Copy link
Contributor Author

It appears that the xml module has a bug where it does not handle empty arrays correctly. It turns an empty array into an array with 2 empty strings, so { "w:br": [] } turns into { "name": "w:br", content: ["", ""] } which prevents it from coming out as <w:br/>.

A PR fix is here for xml is here dylang/node-xml#41. Not sure if it will be accepted as this project looks unmaintained.

The other options are to:

  1. set empty children to null instead of [] or
  2. let certain nodes specify they are empty, so Break could have this.isEmpty = true in the constructor and then prepForXml() can set null as the content when isEmpty is true.

I am willing to help with this if you let me know which solution you would prefer.

jacwright added a commit to jacwright/docx that referenced this issue Dec 3, 2018
See dolanmiu#213

Empty XML nodes are not closed and can have spaces in them which adds unwanted spaces in Word documents when indenting. This will allow
@jacwright
Copy link
Contributor Author

Never mind. I was using paragraph.createTextRun().break() which is what was causing this issue. paragraph.addRun(new Run().break()) is the correct way to add breaks in a paragraph.

@dolanmiu
Copy link
Owner

dolanmiu commented Dec 3, 2018

👍

@bba278
Copy link

bba278 commented Jan 28, 2019

Both paragraph.createTextRun().break() and paragraph.addRun(new Run().break()) create two empty lines, one for the Paragraph and another for the Run

I simply have the following function, which I call after I've created the Document object

// Helper function creates the general Document layout
  function createPret4Document (doc, jbName, fmName, pdValue, pdText) {
    doc.createParagraph(null).addRun(new Run().break())// eslint-disable-line
    createTable(doc, jbName, fmName, pdValue, pdText)
  }

Any suggestions?

@dolanmiu
Copy link
Owner

I'll need to look into this

@dolanmiu dolanmiu added the bug label Jan 28, 2019
@dolanmiu dolanmiu reopened this Jan 28, 2019
@bba278
Copy link

bba278 commented Jan 30, 2019

Thank you, @dolanmiu.

For now, I have solved the problem with the following line:

doc.createParagraph(null).addRun(new Run(null).tab())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants