rrichards               Sun Aug 28 12:23:26 2005 EDT

  Added files:                 (Branch: PHP_5_1)
    /php-src/ext/dom/tests      bug34276.phpt 

  Modified files:              
    /php-src/ext/dom    element.c 
  Log:
  MFH: Fixed bug #34276 (setAttributeNS doesn't work with default namespace)
  Add test
  
http://cvs.php.net/diff.php/php-src/ext/dom/element.c?r1=1.36&r2=1.36.2.1&ty=u
Index: php-src/ext/dom/element.c
diff -u php-src/ext/dom/element.c:1.36 php-src/ext/dom/element.c:1.36.2.1
--- php-src/ext/dom/element.c:1.36      Wed Aug  3 10:07:03 2005
+++ php-src/ext/dom/element.c   Sun Aug 28 12:23:25 2005
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: element.c,v 1.36 2005/08/03 14:07:03 sniper Exp $ */
+/* $Id: element.c,v 1.36.2.1 2005/08/28 16:23:25 rrichards Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -501,6 +501,42 @@
 }
 /* }}} end dom_element_get_attribute_ns */
 
+static xmlNsPtr _dom_new_reconNs(xmlDocPtr doc, xmlNodePtr tree, xmlNsPtr ns) {
+    xmlNsPtr def;
+    xmlChar prefix[50];
+    int counter = 1;
+
+       if ((tree == NULL) || (ns == NULL) || (ns->type != XML_NAMESPACE_DECL)) 
{
+               return NULL;
+       }
+
+       /* Code taken from libxml2 (2.6.20) xmlNewReconciliedNs
+        *
+        * Find a close prefix which is not already in use.
+        * Let's strip namespace prefixes longer than 20 chars !
+        */
+       if (ns->prefix == NULL)
+               snprintf((char *) prefix, sizeof(prefix), "default");
+       else
+               snprintf((char *) prefix, sizeof(prefix), "%.20s", (char 
*)ns->prefix);
+
+       def = xmlSearchNs(doc, tree, prefix);
+       while (def != NULL) {
+               if (counter > 1000) return(NULL);
+               if (ns->prefix == NULL)
+                       snprintf((char *) prefix, sizeof(prefix), "default%d", 
counter++);
+               else
+                       snprintf((char *) prefix, sizeof(prefix), "%.20s%d", 
+                       (char *)ns->prefix, counter++);
+               def = xmlSearchNs(doc, tree, prefix);
+       }
+
+       /*
+        * OK, now we are ready to create a new one.
+        */
+       def = xmlNewNs(tree, ns->href, prefix);
+       return(def);
+}
 
 /* {{{ proto void dom_element_set_attribute_ns(string namespaceURI, string 
qualifiedName, string value);
 URL: 
http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-ElSetAttrNS
@@ -550,8 +586,21 @@
                                nsptr = dom_get_nsdecl(elemp, localname);
                        } else {
                                nsptr = xmlSearchNsByHref(elemp->doc, elemp, 
uri);
-                               while (nsptr && nsptr->prefix == NULL) {
-                                       nsptr = nsptr->next;
+                               if (nsptr && nsptr->prefix == NULL) {
+                                       xmlNsPtr tmpnsptr;
+
+                                       tmpnsptr = nsptr->next;
+                                       while (tmpnsptr) {
+                                               if ((tmpnsptr->prefix != NULL) 
&& (tmpnsptr->href != NULL) && 
+                                                       
(xmlStrEqual(tmpnsptr->href, (xmlChar *) uri))) {
+                                                       nsptr = tmpnsptr;
+                                                       break;
+                                               }
+                                               tmpnsptr = tmpnsptr->next;
+                                       }
+                                       if (tmpnsptr == NULL) {
+                                               nsptr = 
_dom_new_reconNs(elemp->doc, elemp, nsptr);
+                                       }
                                }
                        }
 

http://cvs.php.net/co.php/php-src/ext/dom/tests/bug34276.phpt?r=1.1&p=1
Index: php-src/ext/dom/tests/bug34276.phpt
+++ php-src/ext/dom/tests/bug34276.phpt
--TEST--
Bug # 34276: setAttributeNS and default namespace
--SKIPIF--
<?php require_once('skipif.php'); ?>
--FILE--
<?php
$xml = <<<HERE
<?xml version="1.0" encoding="ISO-8859-1" ?>
<foo xmlns="http://www.example.com/ns/foo";
     xmlns:fubar="http://www.example.com/ns/fubar"; attra="attra" />
HERE;

function dump($elems) {
        foreach ($elems as $elem) {
                var_dump($elem->nodeName);
                dump($elem->childNodes);
        }
}

$dom = new DOMDocument();
$dom->loadXML($xml);
$foo = $dom->documentElement;
var_dump($foo->hasAttributeNS('http://www.example.com/ns/foo', 'attra'));
var_dump($foo->getAttributeNS('http://www.example.com/ns/foo', 'attra'));

$foo->setAttributeNS('http://www.example.com/ns/foo', 'attra', 'attranew');
$foo->setAttributeNS('http://www.example.com/ns/fubar', 'attrb', 'attrbnew');
$foo->setAttributeNS('http://www.example.com/ns/foo', 'attrc', 'attrc');

var_dump($foo->getAttributeNS('http://www.example.com/ns/foo', 'attra'));
var_dump($foo->getAttributeNS('http://www.example.com/ns/fubar', 'attrb'));
var_dump($foo->getAttributeNS('http://www.example.com/ns/foo', 'attrc'));

print $dom->saveXML();
?>
--EXPECT--
bool(false)
string(0) ""
string(8) "attranew"
string(8) "attrbnew"
string(5) "attrc"
<?xml version="1.0" encoding="ISO-8859-1"?>
<foo xmlns="http://www.example.com/ns/foo"; 
xmlns:fubar="http://www.example.com/ns/fubar"; 
xmlns:default="http://www.example.com/ns/foo"; attra="attra" 
default:attra="attranew" fubar:attrb="attrbnew" default:attrc="attrc"/>

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to