Permalink
Browse files

Fixes #6318: Incorrect whitespace handling/parsing of XML documents

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8158 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information...
1 parent c8cf1a2 commit 9f15226e4605ba4b6c5ba918b64d7209ceb77540 @nateabele nateabele committed Apr 28, 2009
Showing with 26 additions and 1 deletion.
  1. +2 −1 cake/libs/xml.php
  2. +24 −0 cake/tests/cases/libs/xml.test.php
View
@@ -880,10 +880,11 @@ function load($input) {
*/
function parse() {
$this->__initParser();
+ $this->__rawData = trim($this->__rawData);
$this->__header = trim(str_replace(
a('<' . '?', '?' . '>'),
a('', ''),
- substr(trim($this->__rawData), 0, strpos($this->__rawData, "\n"))
+ substr($this->__rawData, 0, strpos($this->__rawData, '?' . '>'))
));
xml_parse_into_struct($this->__parser, $this->__rawData, $vals);
@@ -389,6 +389,30 @@ function testAddAndRemoveAttributes() {
$result = $node->removeAttribute('missing');
$this->assertFalse($result);
}
+
+ /**
+ * Tests that XML documents with non-standard spacing (i.e. leading whitespace, whole document
+ * on one line) still parse properly.
+ *
+ * @return void
+ */
+ function testParsingWithNonStandardWhitespace() {
+ $raw = '<?xml version="1.0" encoding="ISO-8859-1" ?><prices><price>1.0</price></prices>';
+ $array = array('Prices' => array('price' => 1.0));
+
+ $xml = new Xml($raw);
+ $this->assertEqual($xml->toArray(), $array);
+ $this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"');
+
+ $xml = new Xml(' ' . $raw);
+ $this->assertEqual($xml->toArray(), $array);
+ $this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"');
+
+ $xml = new Xml("\n" . $raw);
+ $this->assertEqual($xml->toArray(), $array);
+ $this->assertEqual($xml->__header, 'xml version="1.0" encoding="ISO-8859-1"');
+ }
+
/* Not implemented yet */
/* function testChildFilter() {
$input = array(

0 comments on commit 9f15226

Please sign in to comment.