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

vMerge value is Empty in MS Word 2021 #2330

Closed
kernusr opened this issue Nov 21, 2022 · 2 comments
Closed

vMerge value is Empty in MS Word 2021 #2330

kernusr opened this issue Nov 21, 2022 · 2 comments
Assignees
Milestone

Comments

@kernusr
Copy link
Contributor

kernusr commented Nov 21, 2022

Describe the Bug

In MS Word 2021 docx files save with empty w:val of w:vMerge when expected value "continue".

Steps to Reproduce

  1. Create new document in MS Word 2021
  2. Create table in document
  3. Merge multiple cells vertically
  4. Save the document and check word\document.xml

I tried saving:

  • as Word Document (*.docx)
  • as Strickt Open XML Document (*.docx)

In both cases I get the described problem

When I save in Libre Office the problem doesn't reproduce

What does this have to do with phpword?
When I want to save a file in html or pdf I get incorrect markup

Context

Please fill in your environment information:

  • PHPWord Version: 1.0
@kernusr
Copy link
Contributor Author

kernusr commented Nov 21, 2022

I can suggest a solution.

In method readStyleDefs for argument styleDefs add fifth parameter "default value"

protected function readStyleDefs(XMLReader $xmlReader, ?DOMElement $parentNode = null, $styleDefs = [])
{
$styles = [];
foreach ($styleDefs as $styleProp => $styleVal) {
[$method, $element, $attribute, $expected] = array_pad($styleVal, 4, null);
$element = $this->findPossibleElement($xmlReader, $parentNode, $element);
if ($element === null) {
continue;
}
if ($xmlReader->elementExists($element, $parentNode)) {
$node = $xmlReader->getElement($element, $parentNode);
$attribute = $this->findPossibleAttribute($xmlReader, $node, $attribute);
// Use w:val as default if no attribute assigned
$attribute = ($attribute === null) ? 'w:val' : $attribute;
$attributeValue = $xmlReader->getAttribute($attribute, $node);
$styleValue = $this->readStyleDef($method, $attributeValue, $expected);
if ($styleValue !== null) {
$styles[$styleProp] = $styleValue;
}
}
}
return $styles;
}

Modify the code as follows

    protected function readStyleDefs(XMLReader $xmlReader, ?DOMElement $parentNode = null, $styleDefs = [])
    {
        $styles = [];

        foreach ($styleDefs as $styleProp => $styleVal) {
            [$method, $element, $attribute, $expected, $default] = array_pad($styleVal, 4, null);

            $element = $this->findPossibleElement($xmlReader, $parentNode, $element);
            if ($element === null) {
                continue;
            }

            if ($xmlReader->elementExists($element, $parentNode)) {
                $node = $xmlReader->getElement($element, $parentNode);

                $attribute = $this->findPossibleAttribute($xmlReader, $node, $attribute);

                // Use w:val as default if no attribute assigned
                $attribute = ($attribute === null) ? 'w:val' : $attribute;
                $attributeValue = $xmlReader->getAttribute($attribute, $node) ?? $default;

                $styleValue = $this->readStyleDef($method, $attributeValue, $expected);
                if ($styleValue !== null) {
                    $styles[$styleProp] = $styleValue;
                }
            }
        }

        return $styles;
    }

This will allow default values to be passed for empty attributes. For example in a cell styles

'vMerge' => [self::READ_VALUE, 'w:vMerge'],

The code will look like this

'vMerge' => [self::READ_VALUE, 'w:vMerge', null, null, 'continue'],

@kernusr kernusr mentioned this issue Nov 22, 2022
3 tasks
@Progi1984
Copy link
Member

Fixed by @kernusr in #2336

@Progi1984 Progi1984 added this to the v1.1.0 milestone Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants