From 1b5df3684400a1fd3721781d167e3eb6b46f2933 Mon Sep 17 00:00:00 2001 From: Evert Pot Date: Thu, 19 Mar 2015 01:42:25 -0400 Subject: [PATCH] Correctly handling empty namespaces xmlns="". Fixes #40 --- CHANGELOG.md | 7 +++++++ composer.json | 3 ++- lib/Version.php | 2 +- lib/Writer.php | 13 ++++++++++--- tests/Sabre/XML/WriterTest.php | 2 +- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee9c003..c1518dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ ChangeLog ========= +0.4.1 (2015-03-19) +----------------- + +* #40: An element with an empty namespace (xmlns="") is not allowed to have a + prefix. This is now fixed. + + 0.4.0 (2015-03-18) ------------------ diff --git a/composer.json b/composer.json index 9825916..805e920 100644 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "bin" : [ ], "require-dev": { - "squizlabs/php_codesniffer": "*" + "squizlabs/php_codesniffer": "*", + "phpunit/phpunit" : "*" } } diff --git a/lib/Version.php b/lib/Version.php index 492b9ab..cd7c338 100644 --- a/lib/Version.php +++ b/lib/Version.php @@ -14,6 +14,6 @@ class Version { /** * Full version number */ - const VERSION = '0.4.0'; + const VERSION = '0.4.1'; } diff --git a/lib/Writer.php b/lib/Writer.php index 6a0f745..11b9543 100644 --- a/lib/Writer.php +++ b/lib/Writer.php @@ -166,10 +166,17 @@ function startElement($name) { $result = $this->startElementNS($this->namespaceMap[$namespace], $localName, null); } else { - if (!isset($this->adhocNamespaces[$namespace])) { - $this->adhocNamespaces[$namespace] = 'x' . (count($this->adhocNamespaces)+1); + // An empty namespace means it's the global namespace. This is + // allowed, but it mustn't get a prefix. + if ($namespace==="") { + $result = $this->startElement($localName); + $this->writeAttribute('xmlns', ''); + } else { + if (!isset($this->adhocNamespaces[$namespace])) { + $this->adhocNamespaces[$namespace] = 'x' . (count($this->adhocNamespaces)+1); + } + $result = $this->startElementNS($this->adhocNamespaces[$namespace], $localName, $namespace); } - $result = $this->startElementNS($this->adhocNamespaces[$namespace], $localName, $namespace); } } else { diff --git a/tests/Sabre/XML/WriterTest.php b/tests/Sabre/XML/WriterTest.php index 6d4558e..3d1c7c5 100644 --- a/tests/Sabre/XML/WriterTest.php +++ b/tests/Sabre/XML/WriterTest.php @@ -164,7 +164,7 @@ function testEmptyNamespace() { ], << - bar + bar HI