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
This commit is contained in:
nate 2009-04-28 13:23:37 +00:00
parent c8cf1a2c5b
commit 9f15226e46
2 changed files with 26 additions and 1 deletions

View file

@ -880,10 +880,11 @@ class Xml extends XmlNode {
*/
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);

View file

@ -389,6 +389,30 @@ class XmlTest extends CakeTestCase {
$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(