MockDOM.js (3414B)
1 /*** 2 3 MochiKit.MockDOM 1.4 4 5 See <http://mochikit.com/> for documentation, downloads, license, etc. 6 7 (c) 2005 Bob Ippolito. All rights Reserved. 8 9 ***/ 10 11 if (typeof(MochiKit) == "undefined") { 12 MochiKit = {}; 13 } 14 15 if (typeof(MochiKit.MockDOM) == "undefined") { 16 MochiKit.MockDOM = {}; 17 } 18 19 MochiKit.MockDOM.NAME = "MochiKit.MockDOM"; 20 MochiKit.MockDOM.VERSION = "1.4"; 21 22 MochiKit.MockDOM.__repr__ = function () { 23 return "[" + this.NAME + " " + this.VERSION + "]"; 24 }; 25 26 /** @id MochiKit.MockDOM.toString */ 27 MochiKit.MockDOM.toString = function () { 28 return this.__repr__(); 29 }; 30 31 /** @id MochiKit.MockDOM.createDocument */ 32 MochiKit.MockDOM.createDocument = function () { 33 var doc = new MochiKit.MockDOM.MockElement("DOCUMENT"); 34 doc.body = doc.createElement("BODY"); 35 doc.appendChild(doc.body); 36 return doc; 37 }; 38 39 /** @id MochiKit.MockDOM.MockElement */ 40 MochiKit.MockDOM.MockElement = function (name, data, ownerDocument) { 41 this.tagName = this.nodeName = name.toUpperCase(); 42 this.ownerDocument = ownerDocument || null; 43 if (name == "DOCUMENT") { 44 this.nodeType = 9; 45 this.childNodes = []; 46 } else if (typeof(data) == "string") { 47 this.nodeValue = data; 48 this.nodeType = 3; 49 } else { 50 this.nodeType = 1; 51 this.childNodes = []; 52 } 53 if (name.substring(0, 1) == "<") { 54 var nameattr = name.substring( 55 name.indexOf('"') + 1, name.lastIndexOf('"')); 56 name = name.substring(1, name.indexOf(" ")); 57 this.tagName = this.nodeName = name.toUpperCase(); 58 this.setAttribute("name", nameattr); 59 } 60 }; 61 62 MochiKit.MockDOM.MockElement.prototype = { 63 /** @id MochiKit.MockDOM.MockElement.prototype.createElement */ 64 createElement: function (tagName) { 65 return new MochiKit.MockDOM.MockElement(tagName, null, this.nodeType == 9 ? this : this.ownerDocument); 66 }, 67 /** @id MochiKit.MockDOM.MockElement.prototype.createTextNode */ 68 createTextNode: function (text) { 69 return new MochiKit.MockDOM.MockElement("text", text, this.nodeType == 9 ? this : this.ownerDocument); 70 }, 71 /** @id MochiKit.MockDOM.MockElement.prototype.setAttribute */ 72 setAttribute: function (name, value) { 73 this[name] = value; 74 }, 75 /** @id MochiKit.MockDOM.MockElement.prototype.getAttribute */ 76 getAttribute: function (name) { 77 return this[name]; 78 }, 79 /** @id MochiKit.MockDOM.MockElement.prototype.appendChild */ 80 appendChild: function (child) { 81 this.childNodes.push(child); 82 }, 83 /** @id MochiKit.MockDOM.MockElement.prototype.toString */ 84 toString: function () { 85 return "MockElement(" + this.tagName + ")"; 86 }, 87 /** @id MochiKit.MockDOM.MockElement.prototype.getElementsByTagName */ 88 getElementsByTagName: function (tagName) { 89 var foundElements = []; 90 MochiKit.Base.nodeWalk(this, function(node){ 91 if (tagName == '*' || tagName == node.tagName) { 92 foundElements.push(node); 93 return node.childNodes; 94 } 95 }); 96 return foundElements; 97 } 98 }; 99 100 /** @id MochiKit.MockDOM.EXPORT_OK */ 101 MochiKit.MockDOM.EXPORT_OK = [ 102 "mockElement", 103 "createDocument" 104 ]; 105 106 /** @id MochiKit.MockDOM.EXPORT */ 107 MochiKit.MockDOM.EXPORT = [ 108 "document" 109 ]; 110 111 MochiKit.MockDOM.__new__ = function () { 112 this.document = this.createDocument(); 113 }; 114 115 MochiKit.MockDOM.__new__();