nodelist.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include "zend_interfaces.h"
  24. /*
  25. * class DOMNodeList
  26. *
  27. * URL: https://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-536297177
  28. * Since:
  29. */
  30. static int get_nodelist_length(dom_object *obj)
  31. {
  32. dom_nnodemap_object *objmap = (dom_nnodemap_object *) obj->ptr;
  33. if (!objmap) {
  34. return 0;
  35. }
  36. if (objmap->ht) {
  37. return xmlHashSize(objmap->ht);
  38. }
  39. if (objmap->nodetype == DOM_NODESET) {
  40. HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
  41. return zend_hash_num_elements(nodeht);
  42. }
  43. xmlNodePtr nodep = dom_object_get_node(objmap->baseobj);
  44. if (!nodep) {
  45. return 0;
  46. }
  47. int count = 0;
  48. if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
  49. xmlNodePtr curnode = nodep->children;
  50. if (curnode) {
  51. count++;
  52. while (curnode->next != NULL) {
  53. count++;
  54. curnode = curnode->next;
  55. }
  56. }
  57. } else {
  58. if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
  59. nodep = xmlDocGetRootElement((xmlDoc *) nodep);
  60. } else {
  61. nodep = nodep->children;
  62. }
  63. dom_get_elements_by_tag_name_ns_raw(
  64. nodep, (char *) objmap->ns, (char *) objmap->local, &count, -1);
  65. }
  66. return count;
  67. }
  68. /* {{{ length int
  69. readonly=yes
  70. URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-203510337
  71. Since:
  72. */
  73. int dom_nodelist_length_read(dom_object *obj, zval *retval)
  74. {
  75. ZVAL_LONG(retval, get_nodelist_length(obj));
  76. return SUCCESS;
  77. }
  78. /* {{{ */
  79. PHP_METHOD(DOMNodeList, count)
  80. {
  81. zval *id;
  82. dom_object *intern;
  83. id = ZEND_THIS;
  84. if (zend_parse_parameters_none() == FAILURE) {
  85. RETURN_THROWS();
  86. }
  87. intern = Z_DOMOBJ_P(id);
  88. RETURN_LONG(get_nodelist_length(intern));
  89. }
  90. /* }}} end dom_nodelist_count */
  91. /* }}} */
  92. /* {{{ URL: http://www.w3.org/TR/2003/WD-DOM-Level-3-Core-20030226/DOM3-Core.html#ID-844377136
  93. Since:
  94. */
  95. PHP_METHOD(DOMNodeList, item)
  96. {
  97. zval *id;
  98. zend_long index;
  99. int ret;
  100. dom_object *intern;
  101. xmlNodePtr itemnode = NULL;
  102. dom_nnodemap_object *objmap;
  103. xmlNodePtr nodep, curnode;
  104. int count = 0;
  105. id = ZEND_THIS;
  106. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &index) == FAILURE) {
  107. RETURN_THROWS();
  108. }
  109. if (index >= 0) {
  110. intern = Z_DOMOBJ_P(id);
  111. objmap = (dom_nnodemap_object *)intern->ptr;
  112. if (objmap != NULL) {
  113. if (objmap->ht) {
  114. if (objmap->nodetype == XML_ENTITY_NODE) {
  115. itemnode = php_dom_libxml_hash_iter(objmap->ht, index);
  116. } else {
  117. itemnode = php_dom_libxml_notation_iter(objmap->ht, index);
  118. }
  119. } else {
  120. if (objmap->nodetype == DOM_NODESET) {
  121. HashTable *nodeht = HASH_OF(&objmap->baseobj_zv);
  122. zval *entry = zend_hash_index_find(nodeht, index);
  123. if (entry) {
  124. ZVAL_COPY(return_value, entry);
  125. return;
  126. }
  127. } else if (objmap->baseobj) {
  128. nodep = dom_object_get_node(objmap->baseobj);
  129. if (nodep) {
  130. if (objmap->nodetype == XML_ATTRIBUTE_NODE || objmap->nodetype == XML_ELEMENT_NODE) {
  131. curnode = nodep->children;
  132. while (count < index && curnode != NULL) {
  133. count++;
  134. curnode = curnode->next;
  135. }
  136. itemnode = curnode;
  137. } else {
  138. if (nodep->type == XML_DOCUMENT_NODE || nodep->type == XML_HTML_DOCUMENT_NODE) {
  139. nodep = xmlDocGetRootElement((xmlDoc *) nodep);
  140. } else {
  141. nodep = nodep->children;
  142. }
  143. itemnode = dom_get_elements_by_tag_name_ns_raw(nodep, (char *) objmap->ns, (char *) objmap->local, &count, index);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. if (itemnode) {
  150. DOM_RET_OBJ(itemnode, &ret, objmap->baseobj);
  151. return;
  152. }
  153. }
  154. RETVAL_NULL();
  155. }
  156. /* }}} end dom_nodelist_item */
  157. ZEND_METHOD(DOMNodeList, getIterator)
  158. {
  159. if (zend_parse_parameters_none() == FAILURE) {
  160. return;
  161. }
  162. zend_create_internal_iterator_zval(return_value, ZEND_THIS);
  163. }
  164. #endif