Skip to content

Commit bcf20f3

Browse files
Kris2xAlkarex
andauthored
Improve XML element creation and add tests for special character handling (eq. &) (#31)
* Improve XML text element creation and add tests for special character handling (eq. &) * Revert unrelated change * Handle another error possibility * Handle special XML characters also for formula * Improve tests --------- Co-authored-by: Alexandre Alapetite <[email protected]> Co-authored-by: Alexandre Alapetite <[email protected]>
1 parent ded2d68 commit bcf20f3

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/XlsxFastEditor.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,13 +1028,20 @@ public function _makeNewSharedString(string $value): int
10281028
}
10291029

10301030
try {
1031-
$t = $dom->createElement('t', $value);
1031+
// First, we create an empty element t
1032+
$t = $dom->createElement('t');
1033+
if ($t === false) {
1034+
throw new XlsxFastEditorXmlException('Failed to create <t> element');
1035+
}
1036+
// Add content as a text node
1037+
$textNode = $dom->createTextNode($value);
1038+
if ($textNode === false) {
1039+
throw new XlsxFastEditorXmlException('Failed to create <t> text node');
1040+
}
1041+
$t->appendChild($textNode);
10321042
} catch (\DOMException $dex) {
10331043
throw new XlsxFastEditorXmlException('Error creating <t> in shared strings!', $dex->code, $dex);
10341044
}
1035-
if ($t === false) {
1036-
throw new XlsxFastEditorXmlException('Error creating <t> in shared strings!');
1037-
}
10381045
$si->appendChild($t);
10391046
if (!($dom->firstElementChild instanceof \DOMElement)) {
10401047
throw new XlsxFastEditorXmlException('Invalid shared strings!');

src/XlsxFastEditorCell.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,17 @@ public function writeFormula(string $value): void
307307
throw new XlsxFastEditorXmlException("Internal error accessing cell {$this->name()}!");
308308
}
309309
try {
310-
$f = $dom->createElement('f', $value);
311-
if (!($f instanceof \DOMElement)) {
310+
// First, we create an empty element t
311+
$f = $dom->createElement('f');
312+
if ($f === false) {
312313
throw new XlsxFastEditorXmlException("Error creating DOMElement of formula for cell {$this->name()}!");
313314
}
315+
// Add content as a text node
316+
$textNode = $dom->createTextNode($value);
317+
if ($textNode === false) {
318+
throw new XlsxFastEditorXmlException("Error creating text node of formula for cell {$this->name()}!");
319+
}
320+
$f->appendChild($textNode);
314321
} catch (\DOMException $dex) {
315322
throw new XlsxFastEditorXmlException("Error creating formula for cell {$this->name()}!", $dex->code, $dex);
316323
}

tests/test.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@
119119
$xlsxFastEditor->writeInt($sheet2, 'C3', -7);
120120
$xlsxFastEditor->writeFloat($sheet2, 'D3', 273.15);
121121

122+
// Writing special XML characters
123+
$xlsxFastEditor->writeString($sheet2, 'B5', '< " & \' >');
124+
$xlsxFastEditor->writeFormula($sheet2, 'C5', '=LEN("< & \' >")');
125+
122126
// Writing non-existing cells but existing lines
123127
$xlsxFastEditor->writeFormula($sheet2, 'I2', '=7*3');
124128
$xlsxFastEditor->writeString($sheet2, 'F2', 'γ');
@@ -166,6 +170,10 @@
166170

167171
assert($xlsxFastEditor->readString($sheet1, 'B2') === 'World');
168172

173+
// Test special XML characters
174+
assert($xlsxFastEditor->readString($sheet2, 'B5') === '< " & \' >');
175+
assert($xlsxFastEditor->readFormula($sheet2, 'C5') === '=LEN("< & \' >")');
176+
169177
$xlsxFastEditor->close();
170178

171179
// Verify by hand that the resulting file opens without warning in Microsoft Excel.

0 commit comments

Comments
 (0)