/testing/selenium-core/scripts/xmlextras.js

http://datanucleus-appengine.googlecode.com/ · JavaScript · 153 lines · 86 code · 24 blank · 43 comment · 22 complexity · 84cd7cb0009616db7292fa01ecb8c2e8 MD5 · raw file

  1. // This is a third party JavaScript library from
  2. // http://webfx.eae.net/dhtml/xmlextras/xmlextras.html
  3. // i.e. This has not been written by ThoughtWorks.
  4. //<script>
  5. //////////////////
  6. // Helper Stuff //
  7. //////////////////
  8. // used to find the Automation server name
  9. function getDomDocumentPrefix() {
  10. if (getDomDocumentPrefix.prefix)
  11. return getDomDocumentPrefix.prefix;
  12. var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
  13. var o;
  14. for (var i = 0; i < prefixes.length; i++) {
  15. try {
  16. // try to create the objects
  17. o = new ActiveXObject(prefixes[i] + ".DomDocument");
  18. return getDomDocumentPrefix.prefix = prefixes[i];
  19. }
  20. catch (ex) {};
  21. }
  22. throw new Error("Could not find an installed XML parser");
  23. }
  24. function getXmlHttpPrefix() {
  25. if (getXmlHttpPrefix.prefix)
  26. return getXmlHttpPrefix.prefix;
  27. var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
  28. var o;
  29. for (var i = 0; i < prefixes.length; i++) {
  30. try {
  31. // try to create the objects
  32. o = new ActiveXObject(prefixes[i] + ".XmlHttp");
  33. return getXmlHttpPrefix.prefix = prefixes[i];
  34. }
  35. catch (ex) {};
  36. }
  37. throw new Error("Could not find an installed XML parser");
  38. }
  39. //////////////////////////
  40. // Start the Real stuff //
  41. //////////////////////////
  42. // XmlHttp factory
  43. function XmlHttp() {}
  44. XmlHttp.create = function () {
  45. try {
  46. if (window.XMLHttpRequest) {
  47. var req = new XMLHttpRequest();
  48. // some versions of Moz do not support the readyState property
  49. // and the onreadystate event so we patch it!
  50. if (req.readyState == null) {
  51. req.readyState = 1;
  52. req.addEventListener("load", function () {
  53. req.readyState = 4;
  54. if (typeof req.onreadystatechange == "function")
  55. req.onreadystatechange();
  56. }, false);
  57. }
  58. return req;
  59. }
  60. if (window.ActiveXObject) {
  61. return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
  62. }
  63. }
  64. catch (ex) {}
  65. // fell through
  66. throw new Error("Your browser does not support XmlHttp objects");
  67. };
  68. // XmlDocument factory
  69. function XmlDocument() {}
  70. XmlDocument.create = function () {
  71. try {
  72. // DOM2
  73. if (document.implementation && document.implementation.createDocument) {
  74. var doc = document.implementation.createDocument("", "", null);
  75. // some versions of Moz do not support the readyState property
  76. // and the onreadystate event so we patch it!
  77. if (doc.readyState == null) {
  78. doc.readyState = 1;
  79. doc.addEventListener("load", function () {
  80. doc.readyState = 4;
  81. if (typeof doc.onreadystatechange == "function")
  82. doc.onreadystatechange();
  83. }, false);
  84. }
  85. return doc;
  86. }
  87. if (window.ActiveXObject)
  88. return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
  89. }
  90. catch (ex) {}
  91. throw new Error("Your browser does not support XmlDocument objects");
  92. };
  93. // Create the loadXML method and xml getter for Mozilla
  94. if (window.DOMParser &&
  95. window.XMLSerializer &&
  96. window.Node && Node.prototype && Node.prototype.__defineGetter__) {
  97. // XMLDocument did not extend the Document interface in some versions
  98. // of Mozilla. Extend both!
  99. //XMLDocument.prototype.loadXML =
  100. Document.prototype.loadXML = function (s) {
  101. // parse the string to a new doc
  102. var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
  103. // remove all initial children
  104. while (this.hasChildNodes())
  105. this.removeChild(this.lastChild);
  106. // insert and import nodes
  107. for (var i = 0; i < doc2.childNodes.length; i++) {
  108. this.appendChild(this.importNode(doc2.childNodes[i], true));
  109. }
  110. };
  111. /*
  112. * xml getter
  113. *
  114. * This serializes the DOM tree to an XML String
  115. *
  116. * Usage: var sXml = oNode.xml
  117. *
  118. */
  119. // XMLDocument did not extend the Document interface in some versions
  120. // of Mozilla. Extend both!
  121. /*
  122. XMLDocument.prototype.__defineGetter__("xml", function () {
  123. return (new XMLSerializer()).serializeToString(this);
  124. });
  125. */
  126. Document.prototype.__defineGetter__("xml", function () {
  127. return (new XMLSerializer()).serializeToString(this);
  128. });
  129. }