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

PHPOffice / PHPWord / 16421226744

21 Jul 2025 03:27PM UTC coverage: 96.757%. Remained the same
16421226744

Pull #2801

github

web-flow
Merge 4e2536ab5 into 0ab0b4940
Pull Request #2801: Easy way to add a table

12501 of 12920 relevant lines covered (96.76%)

35.44 hits per line

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

75.49
/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();
5✔
46
        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
5✔
47

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

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

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

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

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

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

99
                                        break;
×
100
                                    case 'text:change-end':
4✔
101
                                        unset($changed);
×
102

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

110
                                        break;
×
111
                                    case 'text:span':
4✔
112
                                        $spans = true;
3✔
113

114
                                        break;
3✔
115
                                }
116
                            }
117

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

129
                                                        break;
3✔
130
                                                    case 'text:tab':
1✔
131
                                                        $element->addText("\t");
1✔
132

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

138
                                                        break;
1✔
139
                                                }
140
                                            }
141

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

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

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

183
                        break;
2✔
184
                    case 'text:section': // Section
4✔
185
                        // $sectionStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
186
                        $this->section = $phpWord->addSection();
2✔
187
                        /** @var DOMNodeList<DOMElement> $children */
188
                        $children = $node->childNodes;
2✔
189
                        $this->processNodes($children, $xmlReader, $phpWord);
2✔
190

191
                        break;
2✔
192
                }
193
            }
194
        }
195
    }
196

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

204
        return $section;
5✔
205
    }
206
}
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