rrichards               Mon Aug 28 23:37:23 2006 UTC

  Modified files:              
    /php-src/ext/dom    element.c 
    /php-src/ext/dom/tests      bug38474.phpt 
  Log:
  MFB: fix #38474 (getAttribute select attribute by order, even when prefixed)
  add test
  
http://cvs.php.net/viewvc.cgi/php-src/ext/dom/element.c?r1=1.46&r2=1.47&diff_format=u
Index: php-src/ext/dom/element.c
diff -u php-src/ext/dom/element.c:1.46 php-src/ext/dom/element.c:1.47
--- php-src/ext/dom/element.c:1.46      Fri Aug  4 18:11:27 2006
+++ php-src/ext/dom/element.c   Mon Aug 28 23:37:23 2006
@@ -17,7 +17,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: element.c,v 1.46 2006/08/04 18:11:27 rrichards Exp $ */
+/* $Id: element.c,v 1.47 2006/08/28 23:37:23 rrichards Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -188,7 +188,24 @@
 
 /* }}} */
 
-
+static xmlAttrPtr dom_get_dom1_attribute(xmlNodePtr elem, xmlChar *name) {
+    int len;
+    const xmlChar *nqname;
+
+       nqname = xmlSplitQName3(name, &len);
+       if (nqname != NULL) {
+               xmlNsPtr ns;
+               xmlChar *prefix = xmlStrndup(name, len);
+               ns = xmlSearchNs(elem->doc, elem, prefix);
+               if (prefix != NULL) {
+                       xmlFree(prefix);
+               }
+               if (ns != NULL) {
+                       return xmlHasNsProp(elem, nqname, ns->href);
+               }
+       }
+       return xmlHasNsProp(elem, name, NULL);
+}
 
 /* {{{ proto string dom_element_get_attribute(string name);
 URL: 
http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-666EE0F9
@@ -198,8 +215,9 @@
 {
        zval *id;
        xmlNode *nodep;
-       char *name, *value;
+       char *name, *value = NULL;
        dom_object *intern;
+       xmlAttrPtr attr;
        int name_len;
 
        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), 
"Os", &id, dom_element_class_entry, &name, &name_len) == FAILURE) {
@@ -208,7 +226,15 @@
 
        DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
 
-       value = xmlGetProp(nodep, name);
+       attr = dom_get_dom1_attribute(nodep, (xmlChar *)name);
+       if (attr) {
+               if (attr->type == XML_ATTRIBUTE_NODE) {
+                       value = xmlNodeListGetString(attr->doc, attr->children, 
1);
+               } else {
+                       value = 
xmlStrdup(((xmlAttributePtr)attr)->defaultValue);
+               }
+       }
+       
        if (value == NULL) {
                RETURN_EMPTY_TEXT();
        } else {
@@ -248,7 +274,7 @@
                RETURN_FALSE;
        }
 
-       attr = xmlHasProp(nodep,name);
+       attr = dom_get_dom1_attribute(nodep, (xmlChar *)name);
        if (attr != NULL && attr->type != XML_ATTRIBUTE_DECL) {
                node_list_unlink(attr->children TSRMLS_CC);
        }
@@ -288,7 +314,7 @@
                RETURN_FALSE;
        }
 
-       attrp = xmlHasProp(nodep,name);
+       attrp = dom_get_dom1_attribute(nodep, (xmlChar *)name);
        if (attrp == NULL) {
                RETURN_FALSE;
        }
@@ -327,7 +353,7 @@
 
        DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
 
-       attrp = xmlHasProp(nodep,name);
+       attrp = dom_get_dom1_attribute(nodep, (xmlChar *)name);
        if (attrp == NULL) {
                RETURN_FALSE;
        }
@@ -851,8 +877,9 @@
        zval *id;
        xmlNode *nodep;
        dom_object *intern;
-       char *name, *value;
+       char *name;
        int name_len;
+       xmlAttrPtr attr;
 
        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), 
"Os&", &id, dom_element_class_entry, &name, &name_len, UG(utf8_conv)) == 
FAILURE) {
                return;
@@ -860,11 +887,10 @@
 
        DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
 
-       value = xmlGetProp(nodep, name);
-       if (value == NULL) {
+       attr = dom_get_dom1_attribute(nodep, (xmlChar *)name);
+       if (attr == NULL) {
                RETURN_FALSE;
        } else {
-               xmlFree(value);
                RETURN_TRUE;
        }
 }
http://cvs.php.net/viewvc.cgi/php-src/ext/dom/tests/bug38474.phpt?r1=1.1&r2=1.2&diff_format=u
Index: php-src/ext/dom/tests/bug38474.phpt
diff -u /dev/null php-src/ext/dom/tests/bug38474.phpt:1.2
--- /dev/null   Mon Aug 28 23:37:23 2006
+++ php-src/ext/dom/tests/bug38474.phpt Mon Aug 28 23:37:23 2006
@@ -0,0 +1,41 @@
+--TEST--
+Bug #38474 (getAttribute select attribute by order, even when prefixed)
+--SKIPIF--
+<?php require_once('skipif.inc'); ?>
+--FILE--
+<?php
+$xml = b'<node xmlns:pre="http://foo.com/tr/pre"; 
+              xmlns:post="http://foo.com/tr/post";
+              pre:type="bar" type="foo" ><sub /></node>';
+$dom = new DomDocument();
+$dom->loadXML($xml);
+echo $dom->firstChild->getAttribute('type')."\n";
+echo $dom->firstChild->getAttribute('pre:type')."\n";
+
+$dom->firstChild->setAttribute('pre:type', 'bar2');
+$dom->firstChild->setAttribute('type', 'foo2');
+$dom->firstChild->setAttribute('post:type', 'baz');
+$dom->firstChild->setAttribute('new:type', 'baz2');
+
+echo $dom->firstChild->getAttribute('type')."\n";
+echo $dom->firstChild->getAttribute('pre:type')."\n";
+echo $dom->firstChild->getAttribute('post:type')."\n";
+
+$dom->firstChild->removeAttribute('pre:type');
+$dom->firstChild->removeAttribute('type');
+
+echo $dom->firstChild->getAttribute('type')."\n";
+echo $dom->firstChild->getAttribute('pre:type')."\n";
+echo $dom->firstChild->getAttribute('post:type')."\n";
+echo $dom->firstChild->getAttribute('new:type');
+?>
+--EXPECT--
+foo
+bar
+foo2
+bar2
+baz
+
+
+baz
+baz2

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

Reply via email to