• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

PHPOffice / PHPWord / 13025401639

29 Jan 2025 05:48AM UTC coverage: 96.825% (-0.4%) from 97.217%
13025401639

Pull #2562

github

web-flow
Merge ebe3e5dac into 2d2759585
Pull Request #2562: TemplateProcessor SetComplexBlock/Value and Sections

6 of 7 new or added lines in 1 file covered. (85.71%)

245 existing lines in 40 files now uncovered.

12227 of 12628 relevant lines covered (96.82%)

34.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.73
/src/PhpWord/Reader/ODText/Content.php
1
<?php
2

3
/**
4
 * This file is part of PHPWord - A pure PHP library for reading and writing
5
 * word processing documents.
6
 *
7
 * PHPWord is free software distributed under the terms of the GNU Lesser
8
 * General Public License version 3 as published by the Free Software Foundation.
9
 *
10
 * For the full copyright and license information, please read the LICENSE
11
 * file that was distributed with this source code. For the full list of
12
 * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
13
 *
14
 * @see         https://github.com/PHPOffice/PHPWord
15
 *
16
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
17
 */
18

19
namespace PhpOffice\PhpWord\Reader\ODText;
20

21
use DateTime;
22
use DOMElement;
23
use DOMNodeList;
24
use PhpOffice\Math\Reader\MathML;
25
use PhpOffice\PhpWord\Element\Section;
26
use PhpOffice\PhpWord\Element\TrackChange;
27
use PhpOffice\PhpWord\PhpWord;
28
use PhpOffice\PhpWord\Shared\XMLReader;
29

30
/**
31
 * Content reader.
32
 *
33
 * @since 0.10.0
34
 */
35
class Content extends AbstractPart
36
{
37
    /** @var ?Section */
38
    private $section;
39

40
    /**
41
     * Read content.xml.
42
     */
43
    public function read(PhpWord $phpWord): void
44
    {
45
        $xmlReader = new XMLReader();
4✔
46
        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
4✔
47

48
        $trackedChanges = [];
4✔
49

50
        $nodes = $xmlReader->getElements('office:body/office:text/*');
4✔
51
        $this->section = null;
4✔
52
        $this->processNodes($nodes, $xmlReader, $phpWord);
4✔
53
        $this->section = null;
4✔
54
    }
55

56
    /** @param DOMNodeList<DOMElement> $nodes */
57
    public function processNodes(DOMNodeList $nodes, XMLReader $xmlReader, PhpWord $phpWord): void
58
    {
59
        if ($nodes->length > 0) {
4✔
60
            foreach ($nodes as $node) {
4✔
61
                // $styleName = $xmlReader->getAttribute('text:style-name', $node);
62
                switch ($node->nodeName) {
4✔
63
                    case 'text:h': // Heading
4✔
64
                        $depth = $xmlReader->getAttribute('text:outline-level', $node);
1✔
65
                        $this->getSection($phpWord)->addTitle($node->nodeValue, $depth);
1✔
66

67
                        break;
1✔
68
                    case 'text:p': // Paragraph
4✔
69
                        $styleName = $xmlReader->getAttribute('text:style-name', $node);
4✔
70
                        if (substr($styleName, 0, 2) === 'SB') {
4✔
71
                            break;
1✔
72
                        }
73
                        $element = $xmlReader->getElement('draw:frame/draw:object', $node);
4✔
74
                        if ($element) {
4✔
75
                            $mathFile = str_replace('./', '', $element->getAttribute('xlink:href')) . '/content.xml';
1✔
76

77
                            $xmlReaderObject = new XMLReader();
1✔
78
                            $mathElement = $xmlReaderObject->getDomFromZip($this->docFile, $mathFile);
1✔
79
                            if ($mathElement) {
1✔
80
                                $mathXML = $mathElement->saveXML($mathElement);
1✔
81

82
                                if (is_string($mathXML)) {
1✔
83
                                    $reader = new MathML();
1✔
84
                                    $math = $reader->read($mathXML);
1✔
85

86
                                    $this->getSection($phpWord)->addFormula($math);
1✔
87
                                }
88
                            }
89
                        } else {
90
                            $children = $node->childNodes;
3✔
91
                            $spans = false;
3✔
92
                            /** @var DOMElement $child */
93
                            foreach ($children as $child) {
3✔
94
                                switch ($child->nodeName) {
3✔
95
                                    case 'text:change-start':
3✔
96
                                        $changeId = $child->getAttribute('text:change-id');
×
97
                                        if (isset($trackedChanges[$changeId])) {
×
UNCOV
98
                                            $changed = $trackedChanges[$changeId];
×
99
                                        }
100

UNCOV
101
                                        break;
×
102
                                    case 'text:change-end':
3✔
UNCOV
103
                                        unset($changed);
×
104

UNCOV
105
                                        break;
×
106
                                    case 'text:change':
3✔
107
                                        $changeId = $child->getAttribute('text:change-id');
×
108
                                        if (isset($trackedChanges[$changeId])) {
×
UNCOV
109
                                            $changed = $trackedChanges[$changeId];
×
110
                                        }
111

UNCOV
112
                                        break;
×
113
                                    case 'text:span':
3✔
114
                                        $spans = true;
2✔
115

116
                                        break;
2✔
117
                                }
118
                            }
119

120
                            if ($spans) {
3✔
121
                                $element = $this->getSection($phpWord)->addTextRun();
2✔
122
                                foreach ($children as $child) {
2✔
123
                                    switch ($child->nodeName) {
2✔
124
                                        case 'text:span':
2✔
125
                                            /** @var DOMElement $child2 */
126
                                            foreach ($child->childNodes as $child2) {
2✔
127
                                                switch ($child2->nodeName) {
2✔
128
                                                    case '#text':
2✔
129
                                                        $element->addText($child2->nodeValue);
2✔
130

131
                                                        break;
2✔
132
                                                    case 'text:tab':
1✔
133
                                                        $element->addText("\t");
1✔
134

135
                                                        break;
1✔
136
                                                    case 'text:s':
1✔
137
                                                        $spaces = (int) $child2->getAttribute('text:c') ?: 1;
1✔
138
                                                        $element->addText(str_repeat(' ', $spaces));
1✔
139

140
                                                        break;
1✔
141
                                                }
142
                                            }
143

144
                                            break;
2✔
145
                                    }
146
                                }
147
                            } else {
148
                                $element = $this->getSection($phpWord)->addText($node->nodeValue);
2✔
149
                            }
150
                            if (isset($changed) && is_array($changed)) {
3✔
151
                                $element->setTrackChange($changed['changed']);
×
152
                                if (isset($changed['textNodes'])) {
×
153
                                    foreach ($changed['textNodes'] as $changedNode) {
×
154
                                        $element = $this->getSection($phpWord)->addText($changedNode->nodeValue);
×
UNCOV
155
                                        $element->setTrackChange($changed['changed']);
×
156
                                    }
157
                                }
158
                            }
159
                        }
160

161
                        break;
4✔
162
                    case 'text:list': // List
3✔
163
                        $listItems = $xmlReader->getElements('text:list-item/text:p', $node);
1✔
164
                        foreach ($listItems as $listItem) {
1✔
165
                            // $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
166
                            $this->getSection($phpWord)->addListItem($listItem->nodeValue, 0);
1✔
167
                        }
168

169
                        break;
1✔
170
                    case 'text:tracked-changes':
3✔
171
                        $changedRegions = $xmlReader->getElements('text:changed-region', $node);
1✔
172
                        foreach ($changedRegions as $changedRegion) {
1✔
173
                            $type = ($changedRegion->firstChild->nodeName == 'text:insertion') ? TrackChange::INSERTED : TrackChange::DELETED;
×
174
                            $creatorNode = $xmlReader->getElements('office:change-info/dc:creator', $changedRegion->firstChild);
×
175
                            $author = $creatorNode[0]->nodeValue;
×
176
                            $dateNode = $xmlReader->getElements('office:change-info/dc:date', $changedRegion->firstChild);
×
177
                            $date = $dateNode[0]->nodeValue;
×
178
                            $date = preg_replace('/\.\d+$/', '', $date);
×
179
                            $date = DateTime::createFromFormat('Y-m-d\TH:i:s', $date);
×
180
                            $changed = new TrackChange($type, $author, $date);
×
181
                            $textNodes = $xmlReader->getElements('text:deletion/text:p', $changedRegion);
×
UNCOV
182
                            $trackedChanges[$changedRegion->getAttribute('text:id')] = ['changed' => $changed, 'textNodes' => $textNodes];
×
183
                        }
184

185
                        break;
1✔
186
                    case 'text:section': // Section
3✔
187
                        // $sectionStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
188
                        $this->section = $phpWord->addSection();
1✔
189
                        $children = $node->childNodes;
1✔
190
                        $this->processNodes($children, $xmlReader, $phpWord);
1✔
191

192
                        break;
1✔
193
                }
194
            }
195
        }
196
    }
197

198
    private function getSection(PhpWord $phpWord): Section
199
    {
200
        $section = $this->section;
4✔
201
        if ($section === null) {
4✔
202
            $section = $this->section = $phpWord->addSection();
3✔
203
        }
204

205
        return $section;
4✔
206
    }
207
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc