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

PHPOffice / PHPWord / 13174594449

06 Feb 2025 08:20AM UTC coverage: 96.745% (-0.004%) from 96.749%
13174594449

Pull #2533

github

web-flow
Merge e740deb7b into 1be7a807f
Pull Request #2533: Html Reader Process Titles as Headings Not Paragraphs

24 of 25 new or added lines in 3 files covered. (96.0%)

1 existing line in 1 file now uncovered.

12485 of 12905 relevant lines covered (96.75%)

34.25 hits per line

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

96.15
/src/PhpWord/Writer/HTML/Element/Title.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\Writer\HTML\Element;
20

21
use PhpOffice\PhpWord\Element\Title as PhpWordTitle;
22
use PhpOffice\PhpWord\Style;
23
use PhpOffice\PhpWord\Writer\HTML;
24
use PhpOffice\PhpWord\Writer\HTML\Style\Font;
25
use PhpOffice\PhpWord\Writer\HTML\Style\Paragraph;
26

27
/**
28
 * TextRun element HTML writer.
29
 *
30
 * @since 0.10.0
31
 */
32
class Title extends AbstractElement
33
{
34
    /**
35
     * Write heading.
36
     *
37
     * @return string
38
     */
39
    public function write()
40
    {
41
        if (!$this->element instanceof PhpWordTitle) {
5✔
42
            return '';
1✔
43
        }
44

45
        $tag = 'h' . $this->element->getDepth();
4✔
46

47
        $text = $this->element->getText();
4✔
48
        $paragraphStyle = null;
4✔
49
        if (is_string($text)) {
4✔
50
            $text = $this->parentWriter->escapeHTML($text);
3✔
51
        } else {
52
            $paragraphStyle = $text->getParagraphStyle();
2✔
53
            $writer = new Container($this->parentWriter, $text);
2✔
54
            $text = $writer->write();
2✔
55
        }
56
        $css = '';
4✔
57
        $write1 = $write2 = $write3 = '';
4✔
58
        $style = Style::getStyle('Heading_' . $this->element->getDepth());
4✔
59
        if ($style !== null) {
4✔
60
            $styleWriter = new Font($style);
3✔
61
            $write1 = $styleWriter->write();
3✔
62
        }
63
        if (is_object($paragraphStyle)) {
4✔
64
            $styleWriter = new Paragraph($paragraphStyle);
2✔
65
            $write3 = $styleWriter->write();
2✔
66
            if ($write1 !== '' && $write3 !== '') {
2✔
NEW
67
                $write2 = ' ';
×
68
            }
69
        }
70
        $css = "$write1$write2$write3";
4✔
71
        if ($css !== '') {
4✔
72
            $css = " style=\"$css\"";
3✔
73
        }
74

75
        $content = "<{$tag}{$css}>{$text}</{$tag}>" . PHP_EOL;
4✔
76

77
        return $content;
4✔
78
    }
79
}
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