]> granicus.if.org Git - php/commitdiff
- Added domxml_elem_set_attribute_node() method. (Rob Richards)
authorChristian Stocker <chregu@php.net>
Thu, 3 Apr 2003 10:21:19 +0000 (10:21 +0000)
committerChristian Stocker <chregu@php.net>
Thu, 3 Apr 2003 10:21:19 +0000 (10:21 +0000)
ext/domxml/php_domxml.c
ext/domxml/php_domxml.h

index 5d9057f6dd1dd53e68b17875a729c3f5d9a5eac2..d08e3a4cd488d85442fdd35db5acdb6bc1a53a71 100644 (file)
@@ -429,9 +429,7 @@ static zend_function_entry php_domxmlelement_class_functions[] = {
        PHP_FALIAS(set_attribute,                       domxml_elem_set_attribute,              NULL)
        PHP_FALIAS(remove_attribute,            domxml_elem_remove_attribute,   NULL)
        PHP_FALIAS(get_attribute_node,          domxml_elem_get_attribute_node, NULL)
-/* since this function is not implemented, outcomment it for the time beeing
        PHP_FALIAS(set_attribute_node,          domxml_elem_set_attribute_node, NULL)
-*/
 #if defined(LIBXML_XPATH_ENABLED)                      
        PHP_FALIAS(get_elements_by_tagname,     domxml_elem_get_elements_by_tagname,    NULL)
 #endif
@@ -2849,32 +2847,72 @@ PHP_FUNCTION(domxml_elem_get_attribute_node)
 
 /* {{{ proto bool domxml_elem_set_attribute_node(object attr)
    Sets value of given attribute */
-/* since this function is not implemented, outcomment it for the time beeing
 PHP_FUNCTION(domxml_elem_set_attribute_node)
 {
-       zval *id, **arg1, *rv = NULL;
+       zval *id, *node, *rv = NULL;
        xmlNode *nodep;
-       xmlAttr *attrp, *newattrp;
+       xmlAttr *attrp, *newattrp, *existattrp;
        int ret;
 
-       if ((ZEND_NUM_ARGS() == 1) && (zend_get_parameters_ex(1, &arg1) == FAILURE) {
-               WRONG_PARAM_COUNT;
+       DOMXML_GET_THIS_OBJ(nodep, id, le_domxmlnodep);
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &node) == FAILURE) {
+               return;
        }
 
-       id = getThis();
-       nodep = php_dom_get_object(id, le_domxmlelementp, 0 TSRMLS_CC);
-       attrp = php_dom_get_object(*arg1, le_domxmlattrp, 0 TSRMLS_CC);
+       DOMXML_GET_OBJ(attrp, node, le_domxmlnodep);
 
-       FIXME: The following line doesn't work 
-       newattrp = xmlCopyProp(nodep, attrp);
-       if (!newattrp) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such attribute '%s'", attrp->name);
+       if (attrp->type != XML_ATTRIBUTE_NODE) {
+               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Attribute node is required");
                RETURN_FALSE;
        }
 
+
+       existattrp = xmlHasProp(nodep,attrp->name);
+       if (existattrp != NULL) {
+               /* We cannot unlink an existing attribute as it may never be freed
+               Only the content of the text node of an attribute node is transfered over */
+
+               xmlChar *mem;
+               xmlNode *first, *firstattrp;
+
+               first = existattrp->children;
+               firstattrp = attrp->children;
+               if (mem = xmlNodeGetContent(firstattrp)) {
+                       if (!first) {
+                               xmlNodeSetContent((xmlNode *) existattrp, mem);
+                       } else {
+                               xmlNodeSetContent(first, mem);
+                       }
+                       xmlFree(mem);
+                       newattrp = existattrp;
+               } else {
+                       RETURN_FALSE;
+               }
+       } else {
+               /* xmlCopyProp does not add the copy to the element node.
+                       It does set the parent of the copy to the element node however */
+               newattrp = xmlCopyProp(nodep, attrp);
+               if (!newattrp) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "No such attribute '%s'", attrp->name);
+                       RETURN_FALSE;
+               } else {
+                       xmlAttr *prop;
+                       prop = nodep->properties;
+                       if (prop == NULL) {
+                               nodep->properties = newattrp;
+                       } else {
+                               while (prop->next != NULL) {
+                                       prop = prop->next;
+                               }
+                               prop->next = newattrp;
+                               newattrp->prev = prop;
+                       }
+               }
+       }
+
        DOMXML_RET_OBJ(rv, (xmlNodePtr) newattrp, &ret);
 }
-*/
 /* }}} */
 
 /* {{{ proto string domxml_elem_has_attribute(string attrname)
index 557b2d0f05bc7c7b53edd4dd78d5ccab0b2c2e33..7f2977a59ca82389c0b8287666d9cc07c9042df8 100644 (file)
@@ -170,9 +170,7 @@ PHP_FUNCTION(domxml_elem_get_attribute);
 PHP_FUNCTION(domxml_elem_set_attribute);
 PHP_FUNCTION(domxml_elem_remove_attribute);
 PHP_FUNCTION(domxml_elem_get_attribute_node);
-/* since this function is not really implemented, outcomment it for the time beeing
 PHP_FUNCTION(domxml_elem_set_attribute_node);
-*/
 PHP_FUNCTION(domxml_elem_get_elements_by_tagname);
 PHP_FUNCTION(domxml_elem_has_attribute);
 /* Class CData methods */