PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.16k stars 2.68k forks source link

[BUGFIX] Handle direct and nested title text paths #2598

Open sbuerk opened 3 months ago

sbuerk commented 3 months ago

It seems that in newer formats it's possible that the w:r for an pStyle can be deeper nested as a sibling of the parent note.

Older versions:

<w:p
    w:rsidR="00000000" w:rsidDel="00000000"
    w:rsidP="00000000" w:rsidRDefault="00000000"
    w:rsidRPr="00000000" w14:paraId="00000001">
    <w:pPr>
        <w:pStyle w:val="Heading1"/>
        <w:rPr/>
    </w:pPr>
    <w:r w:rsidDel="00000000" w:rsidR="00000000"
         w:rsidRPr="00000000">
        <w:rPr>
            <w:rtl w:val="0"/>
        </w:rPr>
        <w:t xml:space="preserve">Überschrift 1</w:t>
    </w:r>
</w:p>

versu newer versions:

<w:p w14:paraId="64815DFA" w14:textId="77777777"
     w:rsidR="002A1C92" w:rsidRDefault="00000000">
    <w:pPr>
        <w:pStyle w:val="berschrift1"/>
    </w:pPr>
    <w:sdt>
        <w:sdtPr>
            <w:id w:val="-1128864550"/>
            <w:placeholder>
                <w:docPart w:val="6F00E81731533248A49CB16AC5C3C3DE"/>
            </w:placeholder>
            <w:temporary/>
            <w:showingPlcHdr/>
            <w15:appearance w15:val="hidden"/>
        </w:sdtPr>
        <w:sdtContent>
            <w:r w:rsidR="00D04D7B">
                <w:t>Überschrift 1</w:t>
            </w:r>
        </w:sdtContent>
    </w:sdt>
</w:p>

Therefore, this change now uses a double check for the direct and the nested variant to detect the content (text) of a title text section:

// old
    'w:r',
    $domNode
);

// new
    'w:r|w:sdt/w:sdtContent/w:r',
    $domNode
);

in \PhpOffice\PhpWord\Reader\Word2007\AbstractPart::readParagraph().

coveralls commented 3 months ago

Coverage Status

coverage: 97.217%. remained the same when pulling 289f57cef9c7ed8220d5f283f8c9a39372900258 on sbuerk:stefan-2 into 8b891bb6842dd383f679b47898fad0b7c181f325 on PHPOffice:master.