1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Christian Stocker <chregu@php.net> |
16 : | Rob Richards <rrichards@php.net> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : /* $Id: documentfragment.c 272374 2008-12-31 11:17:49Z sebastian $ */
21 :
22 : #ifdef HAVE_CONFIG_H
23 : #include "config.h"
24 : #endif
25 :
26 : #include "php.h"
27 : #if HAVE_LIBXML && HAVE_DOM
28 : #include "php_dom.h"
29 :
30 :
31 : /* {{{ arginfo */
32 : static
33 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_construct, 0, 0, 0)
34 : ZEND_END_ARG_INFO();
35 :
36 : static
37 : ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_documentfragement_appendXML, 0, 0, 1)
38 : ZEND_ARG_INFO(0, data)
39 : ZEND_END_ARG_INFO();
40 : /* }}} */
41 :
42 : /*
43 : * class DOMDocumentFragment extends DOMNode
44 : *
45 : * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-B63ED1A3
46 : * Since:
47 : */
48 :
49 : zend_function_entry php_dom_documentfragment_class_functions[] = {
50 : PHP_ME(domdocumentfragment, __construct, arginfo_dom_documentfragement_construct, ZEND_ACC_PUBLIC)
51 : PHP_ME(domdocumentfragment, appendXML, arginfo_dom_documentfragement_appendXML, ZEND_ACC_PUBLIC)
52 : {NULL, NULL, NULL}
53 : };
54 :
55 : /* {{{ proto void DOMDocumentFragment::__construct(); */
56 : PHP_METHOD(domdocumentfragment, __construct)
57 6 : {
58 :
59 : zval *id;
60 6 : xmlNodePtr nodep = NULL, oldnode = NULL;
61 : dom_object *intern;
62 :
63 6 : php_set_error_handling(EH_THROW, dom_domexception_class_entry TSRMLS_CC);
64 6 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_documentfragment_class_entry) == FAILURE) {
65 1 : php_std_error_handling();
66 1 : return;
67 : }
68 :
69 5 : php_std_error_handling();
70 5 : nodep = xmlNewDocFragment(NULL);
71 :
72 5 : if (!nodep) {
73 0 : php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
74 0 : RETURN_FALSE;
75 : }
76 :
77 5 : intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
78 5 : if (intern != NULL) {
79 5 : oldnode = dom_object_get_node(intern);
80 5 : if (oldnode != NULL) {
81 1 : php_libxml_node_free_resource(oldnode TSRMLS_CC);
82 : }
83 : /* php_dom_set_object(intern, nodep TSRMLS_CC); */
84 5 : php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC);
85 : }
86 : }
87 : /* }}} end DOMDocumentFragment::__construct */
88 :
89 : /* php_dom_xmlSetTreeDoc is a custom implementation of xmlSetTreeDoc
90 : needed for hack in appendXML due to libxml bug - no need to share this function */
91 5 : static void php_dom_xmlSetTreeDoc(xmlNodePtr tree, xmlDocPtr doc) {
92 : xmlAttrPtr prop;
93 : xmlNodePtr cur;
94 :
95 5 : if (tree) {
96 5 : if(tree->type == XML_ELEMENT_NODE) {
97 2 : prop = tree->properties;
98 5 : while (prop != NULL) {
99 1 : prop->doc = doc;
100 1 : if (prop->children) {
101 1 : cur = prop->children;
102 3 : while (cur != NULL) {
103 1 : php_dom_xmlSetTreeDoc(cur, doc);
104 1 : cur = cur->next;
105 : }
106 : }
107 1 : prop = prop->next;
108 : }
109 : }
110 5 : if (tree->children != NULL) {
111 2 : cur = tree->children;
112 6 : while (cur != NULL) {
113 2 : php_dom_xmlSetTreeDoc(cur, doc);
114 2 : cur = cur->next;
115 : }
116 : }
117 5 : tree->doc = doc;
118 : }
119 5 : }
120 :
121 : /* {{{ proto void DOMDocumentFragment::appendXML(string data); */
122 5 : PHP_METHOD(domdocumentfragment, appendXML) {
123 : zval *id;
124 : xmlNode *nodep;
125 : dom_object *intern;
126 5 : char *data = NULL;
127 5 : int data_len = 0;
128 : int err;
129 : xmlNodePtr lst;
130 :
131 5 : if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &id, dom_documentfragment_class_entry, &data, &data_len) == FAILURE) {
132 1 : return;
133 : }
134 :
135 4 : DOM_GET_OBJ(nodep, id, xmlNodePtr, intern);
136 :
137 4 : if (dom_node_is_read_only(nodep) == SUCCESS) {
138 1 : php_dom_throw_error(NO_MODIFICATION_ALLOWED_ERR, dom_get_strict_error(intern->document) TSRMLS_CC);
139 1 : RETURN_FALSE;
140 : }
141 :
142 3 : if (data) {
143 3 : err = xmlParseBalancedChunkMemory(nodep->doc, NULL, NULL, 0, data, &lst);
144 3 : if (err != 0) {
145 1 : RETURN_FALSE;
146 : }
147 : /* Following needed due to bug in libxml2 <= 2.6.14
148 : ifdef after next libxml release as bug is fixed in their cvs */
149 2 : php_dom_xmlSetTreeDoc(lst, nodep->doc);
150 : /* End stupid hack */
151 :
152 2 : xmlAddChildList(nodep,lst);
153 : }
154 :
155 2 : RETURN_TRUE;
156 : }
157 :
158 : #endif
|