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