Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
46 / 46 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
1 / 1 |
HTML | |
100.00% |
46 / 46 |
|
100.00% |
13 / 13 |
22 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
save | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getContent | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
6 | |||
getEditCallback | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setEditCallback | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isPdf | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNotes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
addNote | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultGenericFont | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setDefaultGenericFont | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultWhiteSpace | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setDefaultWhiteSpace | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
escapeHTML | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 |
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\Writer; |
19 | |
20 | use PhpOffice\PhpWord\PhpWord; |
21 | use PhpOffice\PhpWord\Settings; |
22 | use PhpOffice\PhpWord\Shared\Validate; |
23 | use PhpOffice\PhpWord\Style\Font; |
24 | |
25 | /** |
26 | * HTML writer. |
27 | * |
28 | * Not supported: PreserveText, PageBreak, Object |
29 | * |
30 | * @since 0.10.0 |
31 | */ |
32 | class HTML extends AbstractWriter implements WriterInterface |
33 | { |
34 | /** |
35 | * Is the current writer creating PDF? |
36 | * |
37 | * @var bool |
38 | */ |
39 | protected $isPdf = false; |
40 | |
41 | /** |
42 | * Footnotes and endnotes collection. |
43 | * |
44 | * @var array |
45 | */ |
46 | protected $notes = []; |
47 | |
48 | /** |
49 | * Callback for editing generated html. |
50 | * |
51 | * @var null|callable |
52 | */ |
53 | private $editCallback; |
54 | |
55 | /** |
56 | * Default generic name for default font for html. |
57 | * |
58 | * @var string |
59 | */ |
60 | private $defaultGenericFont = ''; |
61 | |
62 | /** |
63 | * Default white space style for html. |
64 | * |
65 | * @var string |
66 | */ |
67 | private $defaultWhiteSpace = ''; |
68 | |
69 | /** |
70 | * Create new instance. |
71 | */ |
72 | public function __construct(?PhpWord $phpWord = null) |
73 | { |
74 | $this->setPhpWord($phpWord); |
75 | |
76 | $this->parts = ['Head', 'Body']; |
77 | foreach ($this->parts as $partName) { |
78 | $partClass = 'PhpOffice\\PhpWord\\Writer\\HTML\\Part\\' . $partName; |
79 | if (class_exists($partClass)) { |
80 | /** @var \PhpOffice\PhpWord\Writer\HTML\Part\AbstractPart $part Type hint */ |
81 | $part = new $partClass(); |
82 | $part->setParentWriter($this); |
83 | $this->writerParts[strtolower($partName)] = $part; |
84 | } |
85 | } |
86 | } |
87 | |
88 | /** |
89 | * Save PhpWord to file. |
90 | */ |
91 | public function save(string $filename): void |
92 | { |
93 | $this->writeFile($this->openFile($filename), $this->getContent()); |
94 | } |
95 | |
96 | /** |
97 | * Get content. |
98 | * |
99 | * @return string |
100 | * |
101 | * @since 0.11.0 |
102 | */ |
103 | public function getContent() |
104 | { |
105 | $content = ''; |
106 | |
107 | $content .= '<!DOCTYPE html>' . PHP_EOL; |
108 | $content .= '<!-- Generated by PHPWord -->' . PHP_EOL; |
109 | $langtext = ''; |
110 | $phpWord = $this->getPhpWord(); |
111 | $lang = $phpWord->getSettings()->getThemeFontLang(); |
112 | if (!empty($lang)) { |
113 | $lang2 = $lang->getLatin(); |
114 | if (!$lang2) { |
115 | $lang2 = $lang->getEastAsia(); |
116 | } |
117 | if (!$lang2) { |
118 | $lang2 = $lang->getBidirectional(); |
119 | } |
120 | if ($lang2) { |
121 | $langtext = " lang='" . $lang2 . "'"; |
122 | } |
123 | } |
124 | $content .= "<html$langtext>" . PHP_EOL; |
125 | $content .= $this->getWriterPart('Head')->write(); |
126 | $content .= $this->getWriterPart('Body')->write(); |
127 | $content .= '</html>' . PHP_EOL; |
128 | |
129 | // Trigger a callback for editing the entire HTML |
130 | $callback = $this->editCallback; |
131 | if ($callback !== null) { |
132 | $content = $callback($content); |
133 | } |
134 | |
135 | return $content; |
136 | } |
137 | |
138 | /** |
139 | * Return the callback to edit the entire HTML. |
140 | */ |
141 | public function getEditCallback(): ?callable |
142 | { |
143 | return $this->editCallback; |
144 | } |
145 | |
146 | /** |
147 | * Set a callback to edit the entire HTML. |
148 | * |
149 | * The callback must accept the HTML as string as first parameter, |
150 | * and it must return the edited HTML as string. |
151 | */ |
152 | public function setEditCallback(?callable $callback): self |
153 | { |
154 | $this->editCallback = $callback; |
155 | |
156 | return $this; |
157 | } |
158 | |
159 | /** |
160 | * Get is PDF. |
161 | * |
162 | * @return bool |
163 | */ |
164 | public function isPdf() |
165 | { |
166 | return $this->isPdf; |
167 | } |
168 | |
169 | /** |
170 | * Get notes. |
171 | * |
172 | * @return array |
173 | */ |
174 | public function getNotes() |
175 | { |
176 | return $this->notes; |
177 | } |
178 | |
179 | /** |
180 | * Add note. |
181 | * |
182 | * @param int $noteId |
183 | * @param string $noteMark |
184 | */ |
185 | public function addNote($noteId, $noteMark): void |
186 | { |
187 | $this->notes[$noteId] = $noteMark; |
188 | } |
189 | |
190 | /** |
191 | * Get generic name for default font for html. |
192 | */ |
193 | public function getDefaultGenericFont(): string |
194 | { |
195 | return $this->defaultGenericFont; |
196 | } |
197 | |
198 | /** |
199 | * Set generic name for default font for html. |
200 | */ |
201 | public function setDefaultGenericFont(string $value): self |
202 | { |
203 | $this->defaultGenericFont = Validate::validateCSSGenericFont($value); |
204 | |
205 | return $this; |
206 | } |
207 | |
208 | /** |
209 | * Get default white space style for html. |
210 | */ |
211 | public function getDefaultWhiteSpace(): string |
212 | { |
213 | return $this->defaultWhiteSpace; |
214 | } |
215 | |
216 | /** |
217 | * Set default white space style for html. |
218 | */ |
219 | public function setDefaultWhiteSpace(string $value): self |
220 | { |
221 | $this->defaultWhiteSpace = Validate::validateCSSWhiteSpace($value); |
222 | |
223 | return $this; |
224 | } |
225 | |
226 | /** |
227 | * Escape string or not depending on setting. |
228 | */ |
229 | public function escapeHTML(string $txt): string |
230 | { |
231 | if (Settings::isOutputEscapingEnabled()) { |
232 | return htmlspecialchars($txt, ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8'); |
233 | } |
234 | |
235 | return $txt; |
236 | } |
237 | } |