Skip to content

CloneBlock regexp does not replace the block if line-drop #1800

Open
@juzser

Description

@juzser

Describe the Bug

When you have multiple Blocks in multiple Rows, some of blocks cannot be replaced
Only the last block was replaced.
This because some word file will drop-line after xml tag, and the regular expression engine in different versions cannot find the matched block.

Steps to Reproduce

See the image below, only blocks #6 has been replaced.

  • TemplateProcessor
  • The template has a table with clone rows. Each row has different dynamic data.
  • The blocks name in each row will be block#1, block#2,...

screenshot

<?php
...
$this->processor->cloneRow('client', $amount);
for (...) {
    $suffix = '#' . ($i + 1);
    // block_year.
    $this->processor->cloneBlock('block_year' . $suffix);
}

Expected Behavior

Replaced all Blocks

Current Behavior

Only blocks in the last row was replaced

Context

Please fill in your environment information:

  • PHP Version: 7.3.9
  • PHPWord Version: 0.17.0

Fix & Pull Request:

File: TemplateProcessor.php
Line: 740

Change the regexp pattern to:

'/(.*((?s)<w:p\b(?:(?!<w:p\b).)*?\${' . $blockname . '}<\/w:.*?p>))(.*)((?s)<w:p\b(?:(?!<w:p\b).)*?\${\/' . $blockname . '}<\/w:.*?p>)/is',

Describe:

  • Do not find for xml.
  • Start find the <w:p tag that closest to the blockname.
  • This <w:p does not contain any other <w:p inside it.

After fix:
fixed

Hope this helps.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions