Skip to content

Commit 7c9f42e

Browse files
authored
Merge pull request #4676 from oziriemeka/fix/4505-vml-namespace
Xlsx: register x and o namespaces for VML shapes; add test for #4505
2 parents ade225f + db1aba6 commit 7c9f42e

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

.php-cs-fixer.dist.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
'method_chaining_indentation' => true,
8585
'modernize_strpos' => true,
8686
'modernize_types_casting' => true,
87+
'modifier_keywords' => ['elements' => ['property', 'method']], // not const
88+
8789
'multiline_comment_opening_closing' => true,
8890
'multiline_whitespace_before_semicolons' => true,
8991
'native_constant_invocation' => false, // Micro optimization that look messy
@@ -236,7 +238,6 @@
236238
'types_spaces' => true,
237239
'unary_operator_spaces' => true,
238240
'use_arrow_functions' => true,
239-
'visibility_required' => ['elements' => ['property', 'method']], // not const
240241
'void_return' => true,
241242
'whitespace_after_comma_in_array' => true,
242243
'yoda_style' => false,

src/PhpSpreadsheet/Reader/Xlsx.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,10 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
12051205
$shapes = self::xpathNoFalse($vmlCommentsFile, '//v:shape');
12061206
foreach ($shapes as $shape) {
12071207
/** @var SimpleXMLElement $shape */
1208-
$shape->registerXPathNamespace('v', Namespaces::URN_VML);
1208+
$vmlNamespaces = $shape->getNamespaces();
1209+
$shape->registerXPathNamespace('v', $vmlNamespaces['v'] ?? Namespaces::URN_VML);
1210+
$shape->registerXPathNamespace('x', $vmlNamespaces['x'] ?? Namespaces::URN_EXCEL);
1211+
$shape->registerXPathNamespace('o', $vmlNamespaces['o'] ?? Namespaces::URN_MSOFFICE);
12091212

12101213
if (isset($shape['style'])) {
12111214
$style = (string) $shape['style'];
@@ -1230,6 +1233,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
12301233
$clientData = $clientData[0];
12311234

12321235
if (isset($clientData['ObjectType']) && (string) $clientData['ObjectType'] == 'Note') {
1236+
$clientData->registerXPathNamespace('x', $vmlNamespaces['x'] ?? Namespaces::URN_EXCEL);
12331237
$temp = $clientData->xpath('.//x:Row');
12341238
if (is_array($temp)) {
12351239
$row = $temp[0];
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
6+
7+
use PhpOffice\PhpSpreadsheet\Reader\Xlsx as XlsxReader;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue4505Test extends TestCase
11+
{
12+
private static string $file = 'tests/data/Reader/XLSX/issue.4505.namespace.xlsx';
13+
14+
public function testVmlProcessingWithXAndONamespaces(): void
15+
{
16+
$reader = new XlsxReader();
17+
$spreadsheet = $reader->load(self::$file);
18+
$sheet = $spreadsheet->getActiveSheet();
19+
20+
$comments = $sheet->getComments();
21+
self::assertArrayHasKey('A1', $comments);
22+
self::assertSame('right', $comments['A1']->getAlignment());
23+
self::assertSame("Some User:\nHello", (string) $comments['A1']->getText());
24+
$spreadsheet->disconnectWorksheets();
25+
}
26+
27+
public function testVmlFileContainsRequiredNamespaces(): void
28+
{
29+
$file = 'zip://' . self::$file . '#xl/drawings/vmlDrawing1.vml';
30+
$data = (string) file_get_contents($file);
31+
32+
self::assertStringContainsString('<ns1:shape ', $data); // usually v:shape
33+
self::assertStringContainsString('<ns3:shapelayout ns1:ext="edit">', $data); // usually o:shapelayout v:ext
34+
self::assertStringContainsString('<ns2:ClientData ObjectType="Note">', $data); // usually x:ClientData
35+
self::assertStringContainsString('ns3:insetmode', $data); // usually o:insetmode
36+
self::assertStringContainsString(
37+
'<ns2:TextHAlign>Right</ns2:TextHAlign>', // usually x:TextHAlign
38+
$data
39+
);
40+
}
41+
}
11.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)