MockDOM.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /***
  2. MochiKit.MockDOM 1.4
  3. See <http://mochikit.com/> for documentation, downloads, license, etc.
  4. (c) 2005 Bob Ippolito. All rights Reserved.
  5. ***/
  6. if (typeof(MochiKit) == "undefined") {
  7. MochiKit = {};
  8. }
  9. if (typeof(MochiKit.MockDOM) == "undefined") {
  10. MochiKit.MockDOM = {};
  11. }
  12. MochiKit.MockDOM.NAME = "MochiKit.MockDOM";
  13. MochiKit.MockDOM.VERSION = "1.4";
  14. MochiKit.MockDOM.__repr__ = function () {
  15. return "[" + this.NAME + " " + this.VERSION + "]";
  16. };
  17. /** @id MochiKit.MockDOM.toString */
  18. MochiKit.MockDOM.toString = function () {
  19. return this.__repr__();
  20. };
  21. /** @id MochiKit.MockDOM.createDocument */
  22. MochiKit.MockDOM.createDocument = function () {
  23. var doc = new MochiKit.MockDOM.MockElement("DOCUMENT");
  24. doc.body = doc.createElement("BODY");
  25. doc.appendChild(doc.body);
  26. return doc;
  27. };
  28. /** @id MochiKit.MockDOM.MockElement */
  29. MochiKit.MockDOM.MockElement = function (name, data) {
  30. this.tagName = this.nodeName = name.toUpperCase();
  31. if (typeof(data) == "string") {
  32. this.nodeValue = data;
  33. this.nodeType = 3;
  34. } else {
  35. this.nodeType = 1;
  36. this.childNodes = [];
  37. }
  38. if (name.substring(0, 1) == "<") {
  39. var nameattr = name.substring(
  40. name.indexOf('"') + 1, name.lastIndexOf('"'));
  41. name = name.substring(1, name.indexOf(" "));
  42. this.tagName = this.nodeName = name.toUpperCase();
  43. this.setAttribute("name", nameattr);
  44. }
  45. };
  46. MochiKit.MockDOM.MockElement.prototype = {
  47. /** @id MochiKit.MockDOM.MockElement.prototype.createElement */
  48. createElement: function (tagName) {
  49. return new MochiKit.MockDOM.MockElement(tagName);
  50. },
  51. /** @id MochiKit.MockDOM.MockElement.prototype.createTextNode */
  52. createTextNode: function (text) {
  53. return new MochiKit.MockDOM.MockElement("text", text);
  54. },
  55. /** @id MochiKit.MockDOM.MockElement.prototype.setAttribute */
  56. setAttribute: function (name, value) {
  57. this[name] = value;
  58. },
  59. /** @id MochiKit.MockDOM.MockElement.prototype.getAttribute */
  60. getAttribute: function (name) {
  61. return this[name];
  62. },
  63. /** @id MochiKit.MockDOM.MockElement.prototype.appendChild */
  64. appendChild: function (child) {
  65. this.childNodes.push(child);
  66. },
  67. /** @id MochiKit.MockDOM.MockElement.prototype.toString */
  68. toString: function () {
  69. return "MockElement(" + this.tagName + ")";
  70. }
  71. };
  72. /** @id MochiKit.MockDOM.EXPORT_OK */
  73. MochiKit.MockDOM.EXPORT_OK = [
  74. "mockElement",
  75. "createDocument"
  76. ];
  77. /** @id MochiKit.MockDOM.EXPORT */
  78. MochiKit.MockDOM.EXPORT = [
  79. "document"
  80. ];
  81. MochiKit.MockDOM.__new__ = function () {
  82. this.document = this.createDocument();
  83. };
  84. MochiKit.MockDOM.__new__();