jsreport / jsreport-docx

jsreport recipe rendering word docx
MIT License
10 stars 3 forks source link

Troubleshooting issue with {{docxPageBreak}} when preceded by {{if}} without any space #25

Closed pdecat closed 3 years ago

pdecat commented 3 years ago

Hi,

I'm facing weird issues with {{docxPageBreak}}, especially when it is preceded by another expression like an {{if}} without any space.

To reproduce the issue, I'm trying to update the test/page-break-single-paragraph.docx document used in the unit test labelled page break in single paragraph.

The original template content in that document is:

Demo{{docxPageBreak}}break

which obviously lets the unit test pass normally:

./node_modules/.bin/mocha --grep "page break in single paragraph"

  docx
    ✓ page break in single paragraph (164ms)
    ✓ page break in single paragraph (sharing text nodes) (97ms)

  2 passing (486ms)

If I change it to:

Demo{{#if true}}{{docxPageBreak}}{{/if}}break

and run the test, it times out after a minute, apparently going into an endless loop or recursive call:

./node_modules/.bin/mocha --grep "page break in single paragraph"

  docx
    1) page break in single paragraph
    ✓ page break in single paragraph (sharing text nodes) (113ms)

  1 passing (1m)
  1 failing

  2) "after each" hook

Adding a space between the first two handlebars expressions:

Demo{{#if true}} {{docxPageBreak}}{{/if}}break

resolves the timing out (but the test fails for another "normal" reason has the expected assertion is no longer fulfilled):

./node_modules/.bin/mocha --grep "page break in single paragraph"

  docx
    1) page break in single paragraph
    ✓ page break in single paragraph (sharing text nodes) (89ms)

  1 passing (488ms)
  1 failing

  1) docx
       page break in single paragraph:

      AssertionError: expected ' ' to equal 'break'
      + expected - actual

      -
      +break

      at Assertion.fail (node_modules/should/cjs/should.js:275:17)
      at Assertion.value (node_modules/should/cjs/should.js:356:19)
      at Context.<anonymous> (test/test.js:3879:30)

I've tried to debug the issue with the nodejs inspector but did not manage to identify what was going on yet.

pdecat commented 3 years ago

After looking at the XML generated by LibreOffice when editing the document, I've noticed that the timeout occurs when the content is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" mc:Ignorable="w14 wp14">
  <w:body>
    <w:p>
      <w:pPr>
        <w:pStyle w:val="Normal"/>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
      </w:pPr>
      <w:r>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
        <w:t>Demo</w:t>
      </w:r>
      <w:r>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
        <w:t>{{#if true}}</w:t>
      </w:r>
      <w:r>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
        <w:t>{{docxPageBreak}}</w:t>
      </w:r>
      <w:r>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
        <w:t>{{/if}}</w:t>
      </w:r>
      <w:r>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
        <w:t>break</w:t>
      </w:r>
    </w:p>
    <w:sectPr>
      <w:type w:val="nextPage"/>
      <w:pgSz w:w="11906" w:h="16838"/>
      <w:pgMar w:left="1701" w:right="1701" w:header="0" w:top="1417" w:footer="0" w:bottom="1417" w:gutter="0"/>
      <w:pgNumType w:fmt="decimal"/>
      <w:formProt w:val="false"/>
      <w:textDirection w:val="lrTb"/>
      <w:docGrid w:type="default" w:linePitch="360" w:charSpace="0"/>
    </w:sectPr>
  </w:body>
</w:document>

But the test passes if I change it to:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">
  <w:body>
    <w:p w:rsidR="007A07FF" w:rsidRPr="006F3F58" w:rsidRDefault="006F3F58">
      <w:pPr>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
      </w:pPr>
      <w:r>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
        <w:t>Demo{{#if true}}{{docxPageBreak}}{{/if}}break</w:t>
      </w:r>
      <w:bookmarkStart w:id="0" w:name="_GoBack"/>
      <w:bookmarkEnd w:id="0"/>
    </w:p>
    <w:sectPr w:rsidR="007A07FF" w:rsidRPr="006F3F58" w:rsidSect="0078580D">
      <w:pgSz w:w="11900" w:h="16840"/>
      <w:pgMar w:top="1417" w:right="1701" w:bottom="1417" w:left="1701" w:header="708" w:footer="708" w:gutter="0"/>
      <w:cols w:space="708"/>
      <w:docGrid w:linePitch="360"/>
    </w:sectPr>
  </w:body>
</w:document>

Note: the original XML is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid wp14">
  <w:body>
    <w:p w:rsidR="007A07FF" w:rsidRPr="006F3F58" w:rsidRDefault="006F3F58">
      <w:pPr>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
      </w:pPr>
      <w:r>
        <w:rPr>
          <w:lang w:val="es-ES"/>
        </w:rPr>
        <w:t>Demo{{docxPageBreak}}break</w:t>
      </w:r>
      <w:bookmarkStart w:id="0" w:name="_GoBack"/>
      <w:bookmarkEnd w:id="0"/>
    </w:p>
    <w:sectPr w:rsidR="007A07FF" w:rsidRPr="006F3F58" w:rsidSect="0078580D">
      <w:pgSz w:w="11900" w:h="16840"/>
      <w:pgMar w:top="1417" w:right="1701" w:bottom="1417" w:left="1701" w:header="708" w:footer="708" w:gutter="0"/>
      <w:cols w:space="708"/>
      <w:docGrid w:linePitch="360"/>
    </w:sectPr>
  </w:body>
</w:document>
bjrmatos commented 3 years ago

thanks for the bug report, i've pushed the fix for this, let me know how it goes for you

and run the test, it times out after a minute, apparently going into an endless loop or recursive call:

this looks like a bug with something in the test suite, if i change the assertion from

paragraphNodes[0].getElementsByTagName('w:br').should.have.length(1)

to

paragraphNodes[0].getElementsByTagName('w:br').length.should.be.eql(1)

it no longer hangs and shows the failed assertion

Adding a space between the first two handlebars expressions:, resolves the timing out (but the test fails for another "normal" reason has the expected assertion is no longer fulfilled)

yes, this is expected, because there is now a space character at the place of the assertion, i've added tests that cover the cases you are mentioning.

pdecat commented 3 years ago

Awesome, it resolves the issue I was facing in my real template. Thanks @bjrmatos!