Back to index

php5  5.3.10
text.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Christian Stocker <chregu@php.net>                          |
00016    |          Rob Richards <rrichards@php.net>                            |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: text.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifdef HAVE_CONFIG_H
00023 #include "config.h"
00024 #endif
00025 
00026 #include "php.h"
00027 #if HAVE_LIBXML && HAVE_DOM
00028 #include "php_dom.h"
00029 #include "dom_ce.h"
00030 
00031 /* {{{ arginfo */
00032 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_split_text, 0, 0, 1)
00033        ZEND_ARG_INFO(0, offset)
00034 ZEND_END_ARG_INFO();
00035 
00036 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_is_whitespace_in_element_content, 0, 0, 0)
00037 ZEND_END_ARG_INFO();
00038 
00039 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_replace_whole_text, 0, 0, 1)
00040        ZEND_ARG_INFO(0, content)
00041 ZEND_END_ARG_INFO();
00042 
00043 ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_text_construct, 0, 0, 0)
00044        ZEND_ARG_INFO(0, value)
00045 ZEND_END_ARG_INFO();
00046 /* }}} */
00047 
00048 /*
00049 * class DOMText extends DOMCharacterData 
00050 *
00051 * URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1312295772
00052 * Since: 
00053 */
00054 
00055 const zend_function_entry php_dom_text_class_functions[] = {
00056        PHP_FALIAS(splitText, dom_text_split_text, arginfo_dom_text_split_text)
00057        PHP_FALIAS(isWhitespaceInElementContent, dom_text_is_whitespace_in_element_content, arginfo_dom_text_is_whitespace_in_element_content)
00058        PHP_FALIAS(isElementContentWhitespace, dom_text_is_whitespace_in_element_content, arginfo_dom_text_is_whitespace_in_element_content)
00059        PHP_FALIAS(replaceWholeText, dom_text_replace_whole_text, arginfo_dom_text_replace_whole_text)
00060        PHP_ME(domtext, __construct, arginfo_dom_text_construct, ZEND_ACC_PUBLIC)
00061        PHP_FE_END
00062 };
00063 
00064 /* {{{ proto void DOMText::__construct([string value]); */
00065 PHP_METHOD(domtext, __construct)
00066 {
00067 
00068        zval *id;
00069        xmlNodePtr nodep = NULL, oldnode = NULL;
00070        dom_object *intern;
00071        char *value = NULL;
00072        int value_len;
00073        zend_error_handling error_handling;
00074 
00075        zend_replace_error_handling(EH_THROW, dom_domexception_class_entry, &error_handling TSRMLS_CC);
00076        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|s", &id, dom_text_class_entry, &value, &value_len) == FAILURE) {
00077               zend_restore_error_handling(&error_handling TSRMLS_CC);
00078               return;
00079        }
00080 
00081        zend_restore_error_handling(&error_handling TSRMLS_CC);
00082        nodep = xmlNewText((xmlChar *) value);
00083 
00084        if (!nodep) {
00085               php_dom_throw_error(INVALID_STATE_ERR, 1 TSRMLS_CC);
00086               RETURN_FALSE;
00087        }
00088 
00089        intern = (dom_object *)zend_object_store_get_object(id TSRMLS_CC);
00090        if (intern != NULL) {
00091               oldnode = dom_object_get_node(intern);
00092               if (oldnode != NULL) {
00093                      php_libxml_node_free_resource(oldnode  TSRMLS_CC);
00094               }
00095               php_libxml_increment_node_ptr((php_libxml_node_object *)intern, nodep, (void *)intern TSRMLS_CC);
00096        }
00097 }
00098 /* }}} end DOMText::__construct */
00099 
00100 /* {{{ wholeText     string 
00101 readonly=yes 
00102 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-wholeText
00103 Since: DOM Level 3
00104 */
00105 int dom_text_whole_text_read(dom_object *obj, zval **retval TSRMLS_DC)
00106 {
00107        xmlNodePtr node;
00108        xmlChar *wholetext = NULL;
00109 
00110        node = dom_object_get_node(obj);
00111 
00112        if (node == NULL) {
00113               php_dom_throw_error(INVALID_STATE_ERR, 0 TSRMLS_CC);
00114               return FAILURE;
00115        }
00116 
00117        /* Find starting text node */
00118        while (node->prev && ((node->prev->type == XML_TEXT_NODE) || (node->prev->type == XML_CDATA_SECTION_NODE))) {
00119               node = node->prev;
00120        }
00121 
00122        /* concatenate all adjacent text and cdata nodes */
00123        while (node && ((node->type == XML_TEXT_NODE) || (node->type == XML_CDATA_SECTION_NODE))) {
00124               wholetext = xmlStrcat(wholetext, node->content);
00125               node = node->next;
00126        }
00127 
00128        ALLOC_ZVAL(*retval);
00129        if (wholetext != NULL) {
00130               ZVAL_STRING(*retval, wholetext, 1);
00131               xmlFree(wholetext);
00132        } else {
00133               ZVAL_EMPTY_STRING(*retval);
00134        }
00135 
00136        return SUCCESS;
00137 }
00138 
00139 /* }}} */
00140 
00141 /* {{{ proto DOMText dom_text_split_text(int offset);
00142 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-ID-38853C1D
00143 Since: 
00144 */
00145 PHP_FUNCTION(dom_text_split_text)
00146 {
00147        zval       *id;
00148        xmlChar    *cur;
00149        xmlChar    *first;
00150        xmlChar    *second;
00151        xmlNodePtr  node;
00152        xmlNodePtr  nnode;
00153        long        offset;
00154        int         ret;
00155        int         length;
00156        dom_object    *intern;
00157 
00158        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Ol", &id, dom_text_class_entry, &offset) == FAILURE) {
00159               return;
00160        }
00161        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
00162 
00163        if (node->type != XML_TEXT_NODE && node->type != XML_CDATA_SECTION_NODE) {
00164               RETURN_FALSE;
00165        }
00166 
00167        cur = xmlNodeGetContent(node);
00168        if (cur == NULL) {
00169               RETURN_FALSE;
00170        }
00171        length = xmlUTF8Strlen(cur);
00172 
00173        if (offset > length || offset < 0) {
00174               xmlFree(cur);
00175               RETURN_FALSE;
00176        }
00177 
00178        first = xmlUTF8Strndup(cur, offset);
00179        second = xmlUTF8Strsub(cur, offset, length - offset);
00180        
00181        xmlFree(cur);
00182 
00183        xmlNodeSetContent(node, first);
00184        nnode = xmlNewDocText(node->doc, second);
00185        
00186        xmlFree(first);
00187        xmlFree(second);
00188 
00189        if (nnode == NULL) {
00190               RETURN_FALSE;
00191        }
00192 
00193        if (node->parent != NULL) {
00194               nnode->type = XML_ELEMENT_NODE;
00195               xmlAddNextSibling(node, nnode);
00196               nnode->type = XML_TEXT_NODE;
00197        }
00198        
00199        return_value = php_dom_create_object(nnode, &ret, NULL, return_value, intern TSRMLS_CC);
00200 }
00201 /* }}} end dom_text_split_text */
00202 
00203 /* {{{ proto boolean dom_text_is_whitespace_in_element_content();
00204 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-isWhitespaceInElementContent
00205 Since: DOM Level 3
00206 */
00207 PHP_FUNCTION(dom_text_is_whitespace_in_element_content)
00208 {
00209        zval       *id;
00210        xmlNodePtr  node;
00211        dom_object    *intern;
00212 
00213        if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &id, dom_text_class_entry) == FAILURE) {
00214               return;
00215        }
00216        DOM_GET_OBJ(node, id, xmlNodePtr, intern);
00217 
00218        if (xmlIsBlankNode(node)) {
00219               RETURN_TRUE;
00220        } else {
00221               RETURN_FALSE;
00222        }
00223 }
00224 /* }}} end dom_text_is_whitespace_in_element_content */
00225 
00226 /* {{{ proto DOMText dom_text_replace_whole_text(string content);
00227 URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#core-Text3-replaceWholeText
00228 Since: DOM Level 3
00229 */
00230 PHP_FUNCTION(dom_text_replace_whole_text)
00231 {
00232  DOM_NOT_IMPLEMENTED();
00233 }
00234 /* }}} end dom_text_replace_whole_text */
00235 
00236 #endif
00237 
00238 /*
00239  * Local variables:
00240  * tab-width: 4
00241  * c-basic-offset: 4
00242  * End:
00243  * vim600: noet sw=4 ts=4 fdm=marker
00244  * vim<600: noet sw=4 ts=4
00245  */