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

PHPOffice / PHPWord / 6316385574

26 Sep 2023 05:41PM UTC coverage: 81.642% (+0.08%) from 81.565%
6316385574

push

github

web-flow
Merge pull request #2477 from PHPOffice/math

Formula : Add Element (& Writer/Reader Word2007/ODText)

128 of 128 new or added lines in 12 files covered. (100.0%)

11069 of 13558 relevant lines covered (81.64%)

24.68 hits per line

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

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

18
namespace PhpOffice\PhpWord\Reader\ODText;
19

20
use DateTime;
21
use PhpOffice\Math\Reader\MathML;
22
use PhpOffice\PhpWord\Element\TrackChange;
23
use PhpOffice\PhpWord\PhpWord;
24
use PhpOffice\PhpWord\Shared\XMLReader;
25

26
/**
27
 * Content reader.
28
 *
29
 * @since 0.10.0
30
 */
31
class Content extends AbstractPart
32
{
33
    /**
34
     * Read content.xml.
35
     */
36
    public function read(PhpWord $phpWord): void
37
    {
38
        $xmlReader = new XMLReader();
2✔
39
        $xmlReader->getDomFromZip($this->docFile, $this->xmlFile);
2✔
40

41
        $trackedChanges = [];
2✔
42

43
        $nodes = $xmlReader->getElements('office:body/office:text/*');
2✔
44
        if ($nodes->length > 0) {
2✔
45
            $section = $phpWord->addSection();
2✔
46
            foreach ($nodes as $node) {
2✔
47
                // $styleName = $xmlReader->getAttribute('text:style-name', $node);
48
                switch ($node->nodeName) {
2✔
49
                    case 'text:h': // Heading
2✔
50
                        $depth = $xmlReader->getAttribute('text:outline-level', $node);
1✔
51
                        $section->addTitle($node->nodeValue, $depth);
1✔
52

53
                        break;
1✔
54
                    case 'text:p': // Paragraph
2✔
55
                        $element = $xmlReader->getElement('draw:frame/draw:object', $node);
2✔
56
                        if ($element) {
2✔
57
                            $mathFile = str_replace('./', '', $element->getAttribute('xlink:href')) . '/content.xml';
1✔
58

59
                            $xmlReaderObject = new XMLReader();
1✔
60
                            $mathElement = $xmlReaderObject->getDomFromZip($this->docFile, $mathFile);
1✔
61
                            if ($mathElement) {
1✔
62
                                $mathXML = $mathElement->saveXML($mathElement);
1✔
63

64
                                if (is_string($mathXML)) {
1✔
65
                                    $reader = new MathML();
1✔
66
                                    $math = $reader->read($mathXML);
1✔
67

68
                                    $section->addFormula($math);
1✔
69
                                }
70
                            }
71
                        } else {
72
                            $children = $node->childNodes;
1✔
73
                            foreach ($children as $child) {
1✔
74
                                switch ($child->nodeName) {
1✔
75
                                    case 'text:change-start':
1✔
76
                                        $changeId = $child->getAttribute('text:change-id');
×
77
                                        if (isset($trackedChanges[$changeId])) {
×
78
                                            $changed = $trackedChanges[$changeId];
×
79
                                        }
80

81
                                        break;
×
82
                                    case 'text:change-end':
1✔
83
                                        unset($changed);
×
84

85
                                        break;
×
86
                                    case 'text:change':
1✔
87
                                        $changeId = $child->getAttribute('text:change-id');
×
88
                                        if (isset($trackedChanges[$changeId])) {
×
89
                                            $changed = $trackedChanges[$changeId];
×
90
                                        }
91

92
                                        break;
×
93
                                }
94
                            }
95

96
                            $element = $section->addText($node->nodeValue);
1✔
97
                            if (isset($changed) && is_array($changed)) {
1✔
98
                                $element->setTrackChange($changed['changed']);
×
99
                                if (isset($changed['textNodes'])) {
×
100
                                    foreach ($changed['textNodes'] as $changedNode) {
×
101
                                        $element = $section->addText($changedNode->nodeValue);
×
102
                                        $element->setTrackChange($changed['changed']);
×
103
                                    }
104
                                }
105
                            }
106
                        }
107

108
                        break;
2✔
109
                    case 'text:list': // List
2✔
110
                        $listItems = $xmlReader->getElements('text:list-item/text:p', $node);
1✔
111
                        foreach ($listItems as $listItem) {
1✔
112
                            // $listStyleName = $xmlReader->getAttribute('text:style-name', $listItem);
113
                            $section->addListItem($listItem->nodeValue, 0);
1✔
114
                        }
115

116
                        break;
1✔
117
                    case 'text:tracked-changes':
2✔
118
                        $changedRegions = $xmlReader->getElements('text:changed-region', $node);
×
119
                        foreach ($changedRegions as $changedRegion) {
×
120
                            $type = ($changedRegion->firstChild->nodeName == 'text:insertion') ? TrackChange::INSERTED : TrackChange::DELETED;
×
121
                            $creatorNode = $xmlReader->getElements('office:change-info/dc:creator', $changedRegion->firstChild);
×
122
                            $author = $creatorNode[0]->nodeValue;
×
123
                            $dateNode = $xmlReader->getElements('office:change-info/dc:date', $changedRegion->firstChild);
×
124
                            $date = $dateNode[0]->nodeValue;
×
125
                            $date = preg_replace('/\.\d+$/', '', $date);
×
126
                            $date = DateTime::createFromFormat('Y-m-d\TH:i:s', $date);
×
127
                            $changed = new TrackChange($type, $author, $date);
×
128
                            $textNodes = $xmlReader->getElements('text:deletion/text:p', $changedRegion);
×
129
                            $trackedChanges[$changedRegion->getAttribute('text:id')] = ['changed' => $changed, 'textNodes' => $textNodes];
×
130
                        }
131

132
                        break;
×
133
                }
134
            }
135
        }
136
    }
137
}
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