attr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Christian Stocker <chregu@php.net> |
  14. | Rob Richards <rrichards@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php.h"
  21. #if defined(HAVE_LIBXML) && defined(HAVE_DOM)
  22. #include "php_dom.h"
  23. /*
  24. * class DOMAttr extends DOMNode
  25. *
  26. * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-637646024
  27. * Since:
  28. */
  29. /* {{{ */
  30. PHP_METHOD(DOMAttr, __construct)
  31. {
  32. xmlAttrPtr nodep = NULL;
  33. xmlNodePtr oldnode = NULL;
  34. dom_object *intern;
  35. char *name, *value = NULL;
  36. size_t name_len, value_len, name_valid;
  37. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &name, &name_len, &value, &value_len) == FAILURE) {
  38. RETURN_THROWS();
  39. }
  40. intern = Z_DOMOBJ_P(ZEND_THIS);
  41. name_valid = xmlValidateName((xmlChar *) name, 0);
  42. if (name_valid != 0) {
  43. php_dom_throw_error(INVALID_CHARACTER_ERR, 1);
  44. RETURN_THROWS();
  45. }
  46. nodep = xmlNewProp(NULL, (xmlChar *) name, (xmlChar *) value);
  47. if (!nodep) {
  48. php_dom_throw_error(INVALID_STATE_ERR, 1);
  49. RETURN_THROWS();
  50. }
  51. oldnode = dom_object_get_node(intern);
  52. if (oldnode != NULL) {
  53. php_libxml_node_free_resource(oldnode );
  54. }
  55. php_libxml_increment_node_ptr((php_libxml_node_object *)intern, (xmlNodePtr)nodep, (void *)intern);
  56. }
  57. /* }}} end DOMAttr::__construct */
  58. /* {{{ name string
  59. readonly=yes
  60. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-1112119403
  61. Since:
  62. */
  63. int dom_attr_name_read(dom_object *obj, zval *retval)
  64. {
  65. xmlAttrPtr attrp;
  66. attrp = (xmlAttrPtr) dom_object_get_node(obj);
  67. if (attrp == NULL) {
  68. php_dom_throw_error(INVALID_STATE_ERR, 1);
  69. return FAILURE;
  70. }
  71. ZVAL_STRING(retval, (char *) attrp->name);
  72. return SUCCESS;
  73. }
  74. /* }}} */
  75. /* {{{ specified boolean
  76. readonly=yes
  77. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-862529273
  78. Since:
  79. */
  80. int dom_attr_specified_read(dom_object *obj, zval *retval)
  81. {
  82. /* TODO */
  83. ZVAL_TRUE(retval);
  84. return SUCCESS;
  85. }
  86. /* }}} */
  87. /* {{{ value string
  88. readonly=no
  89. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-221662474
  90. Since:
  91. */
  92. int dom_attr_value_read(dom_object *obj, zval *retval)
  93. {
  94. xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
  95. xmlChar *content;
  96. if (attrp == NULL) {
  97. php_dom_throw_error(INVALID_STATE_ERR, 1);
  98. return FAILURE;
  99. }
  100. if ((content = xmlNodeGetContent((xmlNodePtr) attrp)) != NULL) {
  101. ZVAL_STRING(retval, (char *) content);
  102. xmlFree(content);
  103. } else {
  104. ZVAL_EMPTY_STRING(retval);
  105. }
  106. return SUCCESS;
  107. }
  108. int dom_attr_value_write(dom_object *obj, zval *newval)
  109. {
  110. zend_string *str;
  111. xmlAttrPtr attrp = (xmlAttrPtr) dom_object_get_node(obj);
  112. if (attrp == NULL) {
  113. php_dom_throw_error(INVALID_STATE_ERR, 1);
  114. return FAILURE;
  115. }
  116. str = zval_try_get_string(newval);
  117. if (UNEXPECTED(!str)) {
  118. return FAILURE;
  119. }
  120. if (attrp->children) {
  121. node_list_unlink(attrp->children);
  122. }
  123. xmlNodeSetContentLen((xmlNodePtr) attrp, (xmlChar *) ZSTR_VAL(str), ZSTR_LEN(str) + 1);
  124. zend_string_release_ex(str, 0);
  125. return SUCCESS;
  126. }
  127. /* }}} */
  128. /* {{{ ownerElement DOMElement
  129. readonly=yes
  130. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-ownerElement
  131. Since: DOM Level 2
  132. */
  133. int dom_attr_owner_element_read(dom_object *obj, zval *retval)
  134. {
  135. xmlNodePtr nodep, nodeparent;
  136. nodep = dom_object_get_node(obj);
  137. if (nodep == NULL) {
  138. php_dom_throw_error(INVALID_STATE_ERR, 1);
  139. return FAILURE;
  140. }
  141. nodeparent = nodep->parent;
  142. if (!nodeparent) {
  143. ZVAL_NULL(retval);
  144. return SUCCESS;
  145. }
  146. php_dom_create_object(nodeparent, retval, obj);
  147. return SUCCESS;
  148. }
  149. /* }}} */
  150. /* {{{ schemaTypeInfo DOMTypeInfo
  151. readonly=yes
  152. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-schemaTypeInfo
  153. Since: DOM Level 3
  154. */
  155. int dom_attr_schema_type_info_read(dom_object *obj, zval *retval)
  156. {
  157. /* TODO */
  158. ZVAL_NULL(retval);
  159. return SUCCESS;
  160. }
  161. /* }}} */
  162. /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#Attr-isId
  163. Since: DOM Level 3
  164. */
  165. PHP_METHOD(DOMAttr, isId)
  166. {
  167. zval *id;
  168. dom_object *intern;
  169. xmlAttrPtr attrp;
  170. id = ZEND_THIS;
  171. if (zend_parse_parameters_none() == FAILURE) {
  172. RETURN_THROWS();
  173. }
  174. DOM_GET_OBJ(attrp, id, xmlAttrPtr, intern);
  175. if (attrp->atype == XML_ATTRIBUTE_ID) {
  176. RETURN_TRUE;
  177. } else {
  178. RETURN_FALSE;
  179. }
  180. }
  181. /* }}} end dom_attr_is_id */
  182. #endif