attr.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include "php.h"
  23. #if HAVE_LIBXML && HAVE_DOM
  24. #include "php_dom.h"
  25. /* {{{ arginfo */
  26. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_is_id, 0, 0, 0)
  27. ZEND_END_ARG_INFO();
  28. ZEND_BEGIN_ARG_INFO_EX(arginfo_dom_attr_construct, 0, 0, 1)
  29. ZEND_ARG_INFO(0, name)
  30. ZEND_ARG_INFO(0, value)
  31. ZEND_END_ARG_INFO();
  32. /* }}} */
  33. /*
  34. * class DOMAttr extends DOMNode
  35. *
  36. * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-637646024
  37. * Since:
  38. */
  39. const zend_function_entry php_dom_attr_class_functions[] = {
  40. PHP_FALIAS(isId, dom_attr_is_id, arginfo_dom_attr_is_id)
  41. PHP_ME(domattr, __construct, arginfo_dom_attr_construct, ZEND_ACC_PUBLIC)
  42. PHP_FE_END
  43. };
  44. /* {{{ proto DOMAttr::__construct(string name, [string value]) */
  45. PHP_METHOD(domattr, __construct)
  46. {
  47. zval *id = getThis();
  48. xmlAttrPtr nodep = NULL;
  49. xmlNodePtr oldnode = NULL;
  50. dom_object *intern;
  51. char *name, *value = NULL;
  52. size_t name_len, value_len, name_valid;
  53. if (zend_parse_parameters_throw(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
  54. return;
  55. }
  56. intern = Z_DOMOBJ_P(id);
  57. name_valid = xmlValidateName((xmlChar *) name, 0);
  58. if (name_valid != 0) {
  59. php_dom_throw_error(INVALID_CHARACTER_ERR, 1);
  60. RETURN_FALSE;
  61. }
  62. nodep = xmlNewProp(NULL, (xmlChar *) name, (xmlChar *) value);
  63. if (!nodep) {
  64. php_dom_throw_error(INVALID_STATE_ERR, 1);
  65. RETURN_FALSE;
  66. }
  67. oldnode = dom_object_get_node(intern);
  68. if (oldnode != NULL) {
  69. php_libxml_node_free_resource(oldnode );
  70. }
  71. php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern);
  72. }
  73. /* }}} end DOMAttr::__construct */
  74. /* {{{ name string
  75. readonly=yes
  76. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403
  77. Since:
  78. */
  79. int dom_attr_name_read(dom_object *obj, zval *retval)
  80. {
  81. xmlAttrPtr attrp;
  82. attrp = (xmlAttrPtr) dom_object_get_node(obj);
  83. if (attrp == NULL) {
  84. php_dom_throw_error(INVALID_STATE_ERR, 0);
  85. return FAILURE;
  86. }
  87. ZVAL_STRING(retval, (char *) attrp->name);
  88. return SUCCESS;
  89. }
  90. /* }}} */
  91. /* {{{ specified boolean
  92. readonly=yes
  93. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-862529273
  94. Since:
  95. */
  96. int dom_attr_specified_read(dom_object *obj, zval *retval)
  97. {
  98. /* TODO */
  99. ZVAL_TRUE(retval);
  100. return SUCCESS;
  101. }
  102. /* }}} */
  103. /* {{{ value string
  104. readonly=no
  105. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-221662474
  106. Since:
  107. */
  108. int dom_attr_value_read(dom_object *obj, zval *retval)
  109. {
  110. xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
  111. xmlChar *content;
  112. if (attrp == NULL) {
  113. php_dom_throw_error(INVALID_STATE_ERR, 0);
  114. return FAILURE;
  115. }
  116. if ((content = xmlNodeGetContent((xmlNodePtr) attrp)) != NULL) {
  117. ZVAL_STRING(retval, (char *) content);
  118. xmlFree(content);
  119. } else {
  120. ZVAL_EMPTY_STRING(retval);
  121. }
  122. return SUCCESS;
  123. }
  124. int dom_attr_value_write(dom_object *obj, zval *newval)
  125. {
  126. zend_string *str;
  127. xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
  128. if (attrp == NULL) {
  129. php_dom_throw_error(INVALID_STATE_ERR, 0);
  130. return FAILURE;
  131. }
  132. if (attrp->children) {
  133. node_list_unlink(attrp->children);
  134. }
  135. str = zval_get_string(newval);
  136. xmlNodeSetContentLen((xmlNodePtr) attrp, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str) + 1);
  137. zend_string_release_ex(str, 0);
  138. return SUCCESS;
  139. }
  140. /* }}} */
  141. /* {{{ ownerElement DOMElement
  142. readonly=yes
  143. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-ownerElement
  144. Since: DOM Level 2
  145. */
  146. int dom_attr_owner_element_read(dom_object *obj, zval *retval)
  147. {
  148. xmlNodePtr nodep, nodeparent;
  149. nodep = dom_object_get_node(obj);
  150. if (nodep == NULL) {
  151. php_dom_throw_error(INVALID_STATE_ERR, 0);
  152. return FAILURE;
  153. }
  154. nodeparent = nodep->parent;
  155. if (!nodeparent) {
  156. ZVAL_NULL(retval);
  157. return SUCCESS;
  158. }
  159. php_dom_create_object(nodeparent, retval, obj);
  160. return SUCCESS;
  161. }
  162. /* }}} */
  163. /* {{{ schemaTypeInfo DOMTypeInfo
  164. readonly=yes
  165. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-schemaTypeInfo
  166. Since: DOM Level 3
  167. */
  168. int dom_attr_schema_type_info_read(dom_object *obj, zval *retval)
  169. {
  170. /* TODO */
  171. ZVAL_NULL(retval);
  172. return SUCCESS;
  173. }
  174. /* }}} */
  175. /* {{{ proto bool dom_attr_is_id()
  176. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-isId
  177. Since: DOM Level 3
  178. */
  179. PHP_FUNCTION(dom_attr_is_id)
  180. {
  181. zval *id;
  182. dom_object *intern;
  183. xmlAttrPtr attrp;
  184. if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &id, dom_attr_class_entry) == FAILURE) {
  185. return;
  186. }
  187. DOM_GET_OBJ(attrp, id, xmlAttrPtr, intern);
  188. if (attrp->atype == XML_ATTRIBUTE_ID) {
  189. RETURN_TRUE;
  190. } else {
  191. RETURN_FALSE;
  192. }
  193. }
  194. /* }}} end dom_attr_is_id */
  195. #endif
  196. /*
  197. * Local variables:
  198. * tab-width: 4
  199. * c-basic-offset: 4
  200. * End:
  201. * vim600: noet sw=4 ts=4 fdm=marker
  202. * vim<600: noet sw=4 ts=4
  203. */