Skip to content

Commit 6b17196

Browse files
committed
Merge branch 'tandoan-master'
2 parents 86b4ef3 + 285bfce commit 6b17196

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/Google/Spreadsheet/CellFeed.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,12 @@ public function createCell($row, $col, $value)
232232
xmlns:gs=\"http://schemas.google.com/spreadsheets/2006\">
233233
</entry>
234234
");
235-
236-
$child = $entry->addChild("content", $value);
235+
236+
// use ->content instead of addChild('content', $value)
237+
// due to addChild not escaping & properly.
238+
// http://php.net/manual/en/simplexmlelement.addchild.php#112204
239+
$entry->content = $value;
240+
$child = $entry->content;
237241
$child->addAttribute("type", "text");
238242
$child = $entry->addChild("title");
239243
$child->addAttribute("type", "text");
@@ -243,7 +247,9 @@ public function createCell($row, $col, $value)
243247
$link->addAttribute("type", "application/atom+xml");
244248
$link->addAttribute("href", $this->getPostUrl() . "/R" . $row . "C" . $col);
245249

246-
$child = $entry->addChild("xmlns:gs:cell", $value);
250+
$elementType = 'gs:cell';
251+
$entry->{$elementType} = $value;
252+
$child = $entry->{$elementType};
247253
$child->addAttribute("row", $row);
248254
$child->addAttribute("col", $col);
249255
$child->addAttribute("inputValue", $value);

tests/Google/Spreadsheet/CellFeedTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,23 @@ public function testCreateCell()
185185
$this->assertTrue($actual instanceof CellEntry);
186186
}
187187

188+
public function testCreateCellWithAmpersand()
189+
{
190+
$mockCellFeed = $this->getMockBuilder(CellFeed::class)
191+
->setMethods(["getPostUrl"])
192+
->disableOriginalConstructor()
193+
->getMock();
194+
$mockCellFeed->expects($this->any())
195+
->method("getPostUrl")
196+
->will($this->returnValue("https://spreadsheets.google.com/"));
197+
198+
$actual = $mockCellFeed->createCell(2, 1, "a & b < c");
199+
200+
$this->assertTrue($actual instanceof CellEntry);
201+
202+
$expectedXML = file_get_contents(__DIR__."/xml/cell-feed-with-ampersand.xml");
203+
$actualXML = $actual->getXML()->asXML();
204+
$this->assertXmlStringEqualsXmlString($actualXML,$expectedXML);
205+
}
206+
188207
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0"?>
2+
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006">
3+
<content type="text">a &amp; b &lt; c</content>
4+
<title type="text"/>
5+
<id>https://spreadsheets.google.com//R2C1</id>
6+
<link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com//R2C1"/>
7+
<gs:cell row="2" col="1" inputValue="a &amp; b &lt; c">a &amp; b &lt; c</gs:cell>
8+
</entry>

0 commit comments

Comments
 (0)