]> granicus.if.org Git - php/commitdiff
@- Added domxml_doc_create_document_fragment() and
authorChristian Stocker <chregu@php.net>
Sat, 5 Apr 2003 19:56:41 +0000 (19:56 +0000)
committerChristian Stocker <chregu@php.net>
Sat, 5 Apr 2003 19:56:41 +0000 (19:56 +0000)
@    domxml_document_fragment_open_mem(string) method (Christian)
Useful for adding and parsing (well-balanced) document fragments.
- Bumped up API version number

ext/domxml/php_domxml.c
ext/domxml/php_domxml.h

index d08e3a4cd488d85442fdd35db5acdb6bc1a53a71..e665c15fb9f315a7ed4c63b8b2e8de24ecca6527 100644 (file)
@@ -152,6 +152,7 @@ static int le_domxmlattrp;
 static int le_domxmlcdatap;
 static int le_domxmltextp;
 static int le_domxmlpip;
+static int le_domxmldocumentfragmentp;
 static int le_domxmlcommentp;
 static int le_domxmlnotationp;
 static int le_domxmlparserp;
@@ -182,6 +183,7 @@ zend_class_entry *domxmlattr_class_entry;
 zend_class_entry *domxmlcdata_class_entry;
 zend_class_entry *domxmltext_class_entry;
 zend_class_entry *domxmlpi_class_entry;
+zend_class_entry *domxmldocumentfragment_class_entry;
 zend_class_entry *domxmlcomment_class_entry;
 zend_class_entry *domxmlnotation_class_entry;
 zend_class_entry *domxmlentity_class_entry;
@@ -312,6 +314,7 @@ static function_entry php_domxmldoc_class_functions[] = {
        PHP_FALIAS(create_cdata_section,        domxml_doc_create_cdata_section,        NULL)
        PHP_FALIAS(create_entity_reference,     domxml_doc_create_entity_reference,     NULL)
        PHP_FALIAS(create_processing_instruction,       domxml_doc_create_processing_instruction,       NULL)
+       PHP_FALIAS(create_document_fragment,    domxml_doc_create_document_fragment,    NULL)
        PHP_FALIAS(get_elements_by_tagname,     domxml_doc_get_elements_by_tagname,     NULL)
        PHP_FALIAS(get_element_by_id,           domxml_doc_get_element_by_id,   NULL)
        /* Everything below this comment is none DOM compliant */
@@ -480,6 +483,11 @@ static zend_function_entry php_domxmlpi_class_functions[] = {
        {NULL, NULL, NULL}
 };
 
+static zend_function_entry php_domxmldocumentfragment_class_functions[] = {
+               PHP_FALIAS(open_mem,                                    domxml_document_fragment_open_mem,                              NULL)
+               {NULL, NULL, NULL}
+};
+
 #if defined(LIBXML_XPATH_ENABLED)
 static zend_function_entry php_xpathctx_class_functions[] = {
        PHP_FALIAS(xpath_eval,                          xpath_eval,                                             NULL)
@@ -1241,6 +1249,17 @@ PHPAPI zval *php_domobject_new(xmlNodePtr obj, int *found, zval *wrapper_in  TSR
                        break;
                }
 
+               case XML_DOCUMENT_FRAG_NODE:
+               {
+                       xmlNodePtr nodep = obj;
+                       if(!wrapper_in)
+                               object_init_ex(wrapper, domxmldocumentfragment_class_entry);
+                       add_property_stringl(wrapper, "name", "#document-fragment", 18, 1);
+                       rsrc_type = le_domxmldocumentfragmentp;
+                       add_property_long(wrapper, "type", Z_TYPE_P(nodep));
+                       break;
+               }
+               
                /* FIXME: nodes of type XML_DTD_NODE used to be domxmldtd_class_entry.
                 * but the DOM Standard doesn't have a DomDtd class. The DocumentType
                 * class seems to be want we need and the libxml dtd functions are
@@ -1461,7 +1480,7 @@ xmlDocPtr php_dom_xmlSAXParse(xmlSAXHandlerPtr sax, const char *buffer, int size
        }
 
        xmlFreeParserCtxt(ctxt);
-    
+
        return(ret);
 }
 
@@ -1485,6 +1504,7 @@ PHP_MINIT_FUNCTION(domxml)
        le_domxmlcdatap = zend_register_list_destructors_ex(php_free_xml_node, NULL, "domcdata", module_number);
        le_domxmlentityrefp = zend_register_list_destructors_ex(php_free_xml_node, NULL, "domentityref", module_number);
        le_domxmlpip = zend_register_list_destructors_ex(php_free_xml_node, NULL, "dompi", module_number);
+       le_domxmldocumentfragmentp = zend_register_list_destructors_ex(php_free_xml_node, NULL, "domdocumentfragment", module_number);
        le_domxmlparserp =      zend_register_list_destructors_ex(php_free_xml_parser, NULL, "domparser", module_number);
        le_domxmldoctypep = zend_register_list_destructors_ex(php_free_xml_node, NULL, "domdocumenttype", module_number);
        le_domxmldocp = zend_register_list_destructors_ex(php_free_xml_doc, NULL, "domdocument", module_number);
@@ -1539,6 +1559,9 @@ PHP_MINIT_FUNCTION(domxml)
        INIT_OVERLOADED_CLASS_ENTRY(ce, "domprocessinginstruction", php_domxmlpi_class_functions, NULL, NULL, NULL);
        domxmlpi_class_entry = zend_register_internal_class_ex(&ce, domxmlnode_class_entry, NULL TSRMLS_CC);
 
+       INIT_OVERLOADED_CLASS_ENTRY(ce, "domdocumentfragment", php_domxmldocumentfragment_class_functions, NULL, NULL, NULL);
+       domxmldocumentfragment_class_entry = zend_register_internal_class_ex(&ce, domxmlnode_class_entry, NULL TSRMLS_CC);
+
        INIT_OVERLOADED_CLASS_ENTRY(ce, "domnotation", php_domxmlnotation_class_functions, NULL, NULL, NULL);
        domxmlnotation_class_entry = zend_register_internal_class_ex(&ce, domxmlnode_class_entry, NULL TSRMLS_CC);
 
@@ -2360,15 +2383,21 @@ PHP_FUNCTION(domxml_node_append_child)
                }
        }
        /* end libxml2 code */
-       
-       if (NULL == new_child) {
+       if (child->type == XML_DOCUMENT_FRAG_NODE) {
+               new_child = xmlAddChildList(parent, child->children);
+               if (NULL != new_child) {
+                       /* the children are copied, not moved, but domstandard wants to move it 
+                          therefore we delete the reference here */
+                       child->children = NULL;
+               }
+       } else if (NULL == new_child) {
                new_child = xmlAddChild(parent, child);
        }
 
        if (NULL == new_child) {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't append node");
                RETURN_FALSE;
-       }
+       } 
 
        DOMXML_RET_OBJ(rv, new_child, &ret);
 }
@@ -2460,13 +2489,27 @@ PHP_FUNCTION(domxml_node_insert_before)
                        new_child = xmlAddPrevSibling(refp, child);
                }
        } else {
-               /* first unlink node, if child is already a child of parent
-                       for some strange reason, this is needed
-                */
-               if (child->parent == parent){
-                       xmlUnlinkNode(child);
+               
+               if (child->type == XML_DOCUMENT_FRAG_NODE) {
+                       new_child = xmlAddChildList(parent, child->children);
+                       if (NULL != new_child) {
+                       /* the children are copied, not moved, but domstandard wants to move it 
+                          therefore we delete the reference here */
+                               
+                          child->children = NULL;
+                       }
+               } else {
+               
+                       /* first unlink node, if child is already a child of parent
+                               for some strange reason, this is needed
+                        */
+               
+                       if (child->parent == parent){
+                               xmlUnlinkNode(child);
+                       }
+               
+                       new_child = xmlAddChild(parent, child);
                }
-               new_child = xmlAddChild(parent, child);
        }
                
 
@@ -3612,6 +3655,54 @@ PHP_FUNCTION(domxml_doc_create_processing_instruction)
 }
 /* }}} */
 
+/* {{{ proto object domxml_doc_create_document_fragement()
+   Creates new document fragement node */
+PHP_FUNCTION(domxml_doc_create_document_fragment)
+{
+       zval *id, *rv = NULL;
+       xmlNode *node;
+       xmlDocPtr docp = NULL;
+       int ret;
+
+       DOMXML_PARAM_NONE(docp, id, le_domxmldocumentfragmentp);
+       
+
+       node = xmlNewDocFragment (docp);
+       if (!node) {
+               RETURN_FALSE;
+       }
+       DOMXML_RET_OBJ(rv, node, &ret); 
+}
+/* }}} */
+
+/* {{{ proto bool domxml_document_framgent_open_mem(string buf)
+   Parses a string with a well-balanced XML-Fragment and appends it to the document-fragment */
+PHP_FUNCTION(domxml_document_fragment_open_mem)
+{
+       zval *id;
+       xmlNodePtr dfp = NULL, last = NULL;     
+       char *buf;
+       int ret, buf_len;
+       xmlNodePtr lst;
+       
+       DOMXML_PARAM_TWO(dfp, id, le_domxmldocumentfragmentp,"s",&buf, &buf_len);
+
+       ret = xmlParseBalancedChunkMemory(dfp->doc, NULL, NULL, 0, (xmlChar *) buf, &lst);
+       if (ret != 0) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Input string is not balanced (well-formed)");   
+                       RETURN_FALSE;
+       }
+       
+       last = xmlAddChildList(dfp, lst);
+       
+       if (last == NULL) {
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not add child list");   
+                       RETURN_FALSE;
+       }
+       
+       RETURN_TRUE;
+}
+
 /* {{{ proto object domxml_doc_imported_node(object node, bool recursive)
    Creates new element node */
 PHP_FUNCTION(domxml_doc_imported_node)
index 7f2977a59ca82389c0b8287666d9cc07c9042df8..0053a54cff4a623fc565a017936a8222442e8afc 100644 (file)
@@ -53,7 +53,7 @@
     therefore it's easier for the script-programmers to check, what's working how
    Can be checked with phpversion("domxml");
 */
-#define DOMXML_API_VERSION "20020814"
+#define DOMXML_API_VERSION "20030405"
 
 extern zend_module_entry domxml_module_entry;
 #define domxml_module_ptr &domxml_module_entry
@@ -94,6 +94,7 @@ PHP_FUNCTION(domxml_doc_create_element_ns);
 PHP_FUNCTION(domxml_doc_create_text_node);
 PHP_FUNCTION(domxml_doc_create_comment);
 PHP_FUNCTION(domxml_doc_create_processing_instruction);
+PHP_FUNCTION(domxml_doc_create_document_fragment);
 PHP_FUNCTION(domxml_doc_create_attribute);
 PHP_FUNCTION(domxml_doc_create_cdata_section);
 PHP_FUNCTION(domxml_doc_create_entity_reference);
@@ -185,10 +186,13 @@ PHP_FUNCTION(domxml_entity_public_id);
 PHP_FUNCTION(domxml_entity_system_id);
 PHP_FUNCTION(domxml_entity_notation_name);
 
-/* Class ProcessingInstructions */
+/* Class ProcessingInstructions methods*/
 PHP_FUNCTION(domxml_pi_target);
 PHP_FUNCTION(domxml_pi_data);
 
+/* Class DocumentFragment methods */
+PHP_FUNCTION(domxml_document_fragment_open_mem);
+
 /* Class Parser methods */
 PHP_FUNCTION(domxml_parser);
 PHP_FUNCTION(domxml_parser_add_chunk);