PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/test/slow/dom_document/1672.php

http://github.com/facebook/hiphop-php
PHP | 160 lines | 128 code | 22 blank | 10 comment | 6 complexity | 9099ba42915d5aee5871bca3c56a3460 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh
  2. function print_node($node)
  3. {
  4. print "Node Name: " . $node->nodeName;
  5. print "
  6. Node Type: " . $node->nodeType;
  7. if ($node->nodeType != 3) {
  8. $child_count = $node->childNodes->length;
  9. }
  10. else {
  11. $child_count = 0;
  12. }
  13. print "
  14. Num Children: " . $child_count;
  15. if($child_count <= 1){
  16. print "
  17. Node Content: " . $node->nodeValue;
  18. }
  19. print "
  20. ";
  21. }
  22. function print_node_list($nodelist)
  23. {
  24. foreach($nodelist as $node)
  25. {
  26. print_node($node);
  27. }
  28. }
  29. <<__EntryPoint>>
  30. function main_1672() {
  31. $xmlstr = "<?xml version='1.0' standalone='yes'?>
  32. <!DOCTYPE chapter SYSTEM '/share/sgml/Norman_Walsh/db3xml10/db3xml10.dtd'
  33. [ <!ENTITY sp \"spanish\">
  34. ]>
  35. <!-- lsfj -->
  36. <chapter language='en'><title language='en'>Title</title>
  37. <para language='ge'>
  38. &sp;
  39. <!-- comment -->
  40. <informaltable language='&sp;kkk'>
  41. <tgroup cols='3'>
  42. <tbody>
  43. <row><entry>a1</entry><entry morerows='1'>b1</entry><entry>c1</entry></row>
  44. <row><entry>a2</entry><entry>c2</entry></row>
  45. <row><entry>a3</entry><entry>b3</entry><entry>c3</entry></row>
  46. </tbody>
  47. </tgroup>
  48. </informaltable>
  49. </para>
  50. </chapter> ";
  51. echo "Test 1: accessing single nodes from php
  52. ";
  53. $dom = new domDocument;
  54. $dom->loadxml($xmlstr);
  55. if(!$dom) {
  56. echo "Error while parsing the document
  57. ";
  58. exit;
  59. }
  60. // children() of of document would result in a memleak
  61. //$children = $dom->children();
  62. //print_node_list($children);
  63. echo "--------- root
  64. ";
  65. $rootnode = $dom->documentElement;
  66. print_node($rootnode);
  67. echo "--------- children of root
  68. ";
  69. $children = $rootnode->childNodes;
  70. print_node_list($children);
  71. // The last node should be identical with the last entry in the children array
  72. echo "--------- last
  73. ";
  74. $last = $rootnode->lastChild;
  75. print_node($last);
  76. // The parent of this last node is the root again
  77. echo "--------- parent
  78. ";
  79. $parent = $last->parentNode;
  80. print_node($parent);
  81. // The children of this parent are the same children as one above
  82. echo "--------- children of parent
  83. ";
  84. $children = $parent->childNodes;
  85. print_node_list($children);
  86. echo "--------- creating a new attribute
  87. ";
  88. //This is worthless
  89. //$attr = $dom->createAttribute("src", "picture.gif");
  90. //print_r($attr);
  91. //$rootnode->set_attributeNode($attr);
  92. $attr = $rootnode->setAttribute("src", "picture.gif");
  93. $attr = $rootnode->getAttribute("src");
  94. print_r($attr);
  95. print "
  96. ";
  97. echo "--------- Get Attribute Node
  98. ";
  99. $attr = $rootnode->getAttributeNode("src");
  100. print_node($attr);
  101. echo "--------- Remove Attribute Node
  102. ";
  103. $attr = $rootnode->removeAttribute("src");
  104. print "Removed " . $attr . " attributes.
  105. ";
  106. echo "--------- attributes of rootnode
  107. ";
  108. $attrs = $rootnode->attributes;
  109. print_node_list($attrs);
  110. echo "--------- children of an attribute
  111. ";
  112. $children = $attrs->item(0)->childNodes;
  113. print_node_list($children);
  114. echo "--------- Add child to root
  115. ";
  116. $myelement = new domElement("Silly", "Symphony");
  117. $newchild = $rootnode->appendChild($myelement);
  118. print_node($newchild);
  119. print $dom->saveXML();
  120. print "
  121. ";
  122. echo "--------- Find element by tagname
  123. ";
  124. echo " Using dom
  125. ";
  126. $children = $dom->getElementsByTagname("Silly");
  127. print_node_list($children);
  128. echo " Using elem
  129. ";
  130. $children = $rootnode->getElementsByTagName("Silly");
  131. print_node_list($children);
  132. echo "--------- Unlink Node
  133. ";
  134. print_node($children->item(0));
  135. $rootnode->removeChild($children->item(0));
  136. print_node_list($rootnode->childNodes);
  137. print $dom->savexml();
  138. }