-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Description
Issue
setValueForPart
treats MAXIMUM_REPLACEMENTS_DEFAULT
as unlimited replacements. setImageValue
does not. When setImageValue
is called without an explicit $limit
, replacement is inconsistent:
If all placeholder occurrences are byte-for-byte identical (e.g. repeated <w:t>${Image}</w:t>
), all are replaced.
If placeholders differ in formatting/whitespace (e.g. <w:t xml:space="preserve"> ${Image}</w:t>
vs <w:t>${Image}</w:t>
), only the first occurrence is replaced.
Actual behavior
Only the first occurrence is replaced if placeholders are not exactly identical in the XML.
Workaround
Pass an explicit very large limit to simulate "no limit":
$templateProcessor->setImageValue('Image', $image, PHP_INT_MAX);
Note: this workaround has a minor side effect. addImageToRelations
may be called for images that were already replaced. That adds duplicate relation entries to the generated .docx and introduces a small performance cost. This can be addressed in the same fix by avoiding duplicate relation additions or checking for existing relations before adding.
Proposed solution
Make setImageValue
treat MAXIMUM_REPLACEMENTS_DEFAULT
as unlimited, consistent with setValueForPart.
Prevent duplicate addImageToRelations calls when using unlimited replacements.
Expected behavior
All placeholders should be replaced consistently when the default limit is used, matching setValueForPart behavior where MAXIMUM_REPLACEMENTS_DEFAULT
means no limit.
Steps to reproduce
XML:
<w:t xml:space="preserve"> ${Image}</w:t>
<w:t>${Image}</w:t>
Call:
$templateProcessor->setImageValue('Image', $image);
Next steps
I can open a pull request with the fix and tests if maintainers agree.
PHPWord version(s) where the bug happened
1.4
PHP version(s) where the bug happened
8.4