1 if (navigator.vendor == "Apple Computer, Inc." || navigator.vendor == "KDE") { // WebCore/KHTML
3 Crossbrowser HTMLElement Prototyping
4 Copyright (C) 2005 Jason Davis, http://www.browserland.org
5 Additional thanks to Brothercake, http://www.brothercake.com
7 This code is licensed under the LGPL:
8 http://www.gnu.org/licenses/lgpl.html
13 window["HTML" + i + "Element"] = document.createElement(c[ i ]).constructor;
15 Html: "html", Head: "head", Link: "link", Title: "title", Meta: "meta",
16 Base: "base", IsIndex: "isindex", Style: "style", Body: "body", Form: "form",
17 Select: "select", OptGroup: "optgroup", Option: "option", Input: "input",
18 TextArea: "textarea", Button: "button", Label: "label", FieldSet: "fieldset",
19 Legend: "legend", UList: "ul", OList: "ol", DList: "dl", Directory: "dir",
20 Menu: "menu", LI: "li", Div: "div", Paragraph: "p", Heading: "h1", Quote: "q",
21 Pre: "pre", BR: "br", BaseFont: "basefont", Font: "font", HR: "hr", Mod: "ins",
22 Anchor: "a", Image: "img", Object: "object", Param: "param", Applet: "applet",
23 Map: "map", Area: "area", Script: "script", Table: "table", TableCaption: "caption",
24 TableCol: "col", TableSection: "tbody", TableRow: "tr", TableCell: "td",
25 FrameSet: "frameset", Frame: "frame", IFrame: "iframe"
28 function HTMLElement() {}
29 //HTMLElement.prototype = HTMLHtmlElement.__proto__.__proto__;
30 var HTMLDocument = document.constructor;
31 var HTMLCollection = document.links.constructor;
32 var HTMLOptionsCollection = document.createElement("select").options.constructor;
33 var Text = document.createTextNode("").constructor;
36 // More efficient for Safari 2
37 function Document() {}
39 function HTMLCollection() {}
40 function HTMLElement() {}
42 Document.prototype = window["[[DOMDocument]]"];
43 Event.prototype = window["[[DOMEvent]]"];
44 HTMLCollection.prototype = window["[[HTMLCollection.prototype]]"];
45 HTMLElement.prototype = window["[[DOMElement.prototype]]"];
46 Node.prototype = window["[[DOMNode.prototype]]"];
49 /* custom extensions to the DOM api */
50 HTMLElement.prototype.addInterface = function(objectInterface) {
51 Object.extend(this, objectInterface);
56 HTMLElement.prototype.childNodesWithTag = function(tagName) {
57 var matchingNodes = new Array();
58 var tagName = tagName.toUpperCase();
60 for (var i = 0; i < this.childNodes.length; i++) {
61 if (typeof(this.childNodes[i]) == "object"
62 && this.childNodes[i].tagName
63 && this.childNodes[i].tagName.toUpperCase() == tagName)
64 matchingNodes.push(this.childNodes[i]);
70 HTMLElement.prototype.addClassName = function(className) {
71 var classStr = '' + this.getAttribute("class");
73 position = classStr.indexOf(className, 0);
75 classStr = classStr + ' ' + className;
76 this.setAttribute('class', classStr);
80 HTMLElement.prototype.removeClassName = function(className) {
81 var classStr = '' + this.getAttribute('class');
83 position = classStr.indexOf(className, 0);
84 while (position > -1) {
85 classStr1 = classStr.substring(0, position);
86 classStr2 = classStr.substring(position + 10, classStr.length);
87 classStr = classStr1 + classStr2;
88 position = classStr.indexOf(className, 0);
91 this.setAttribute('class', classStr);
94 HTMLElement.prototype.hasClassName = function(className) {
95 var classStr = '' + this.getAttribute('class');
96 position = classStr.indexOf(className, 0);
97 return (position > -1);
100 HTMLElement.prototype.getParentWithTagName = function(tagName) {
101 var currentElement = this;
102 tagName = tagName.toUpperCase();
104 currentElement = currentElement.parentNode;
105 while (currentElement
106 && currentElement.tagName != tagName) {
107 currentElement = currentElement.parentNode;
110 return currentElement;
113 HTMLElement.prototype.cascadeLeftOffset = function() {
114 var currentElement = this;
117 while (currentElement) {
118 offset += currentElement.offsetLeft;
119 currentElement = currentElement.getParentWithTagName("div");
125 HTMLElement.prototype.cascadeTopOffset = function() {
126 var currentElement = this;
131 while (currentElement
132 && currentElement instanceof HTMLElement) {
133 offset += currentElement.offsetTop;
134 currentElement = currentElement.parentNode;
141 HTMLElement.prototype.dump = function(additionalInfo, additionalKeys) {
142 var id = this.getAttribute("id");
143 var nclass = this.getAttribute("class");
145 var str = this.tagName;
147 str += "; id = " + id;
149 str += "; class = " + nclass;
152 str += "; " + additionalInfo;
155 for (var i = 0; i < additionalKeys.length; i++) {
156 var value = this.getAttribute(additionalKeys[i]);
158 str += "; " + additionalKeys[i] + " = " + value;
164 HTMLElement.prototype.getSelectedNodes = function() {
165 var selArray = new Array();
167 for (var i = 0; i < this.childNodes.length; i++) {
168 node = this.childNodes.item(i);
169 if (node.nodeType == 1
170 && isNodeSelected(node))
177 HTMLElement.prototype.getSelectedNodesId = function() {
178 var selArray = new Array();
180 for (var i = 0; i < this.childNodes.length; i++) {
181 node = this.childNodes.item(i);
182 if (node.nodeType == 1
183 && isNodeSelected(node))
184 selArray.push(node.getAttribute("id"));
190 HTMLElement.prototype.onContextMenu = function(event) {
191 var popup = this.sogoContextMenu;
193 if (document.currentPopupMenu)
194 hideMenu(event, document.currentPopupMenu);
196 var menuTop = event.pageY;
197 var menuLeft = event.pageX;
198 var heightDiff = (window.innerHeight
199 - (menuTop + popup.offsetHeight));
201 menuTop += heightDiff;
203 var leftDiff = (window.innerWidth
204 - (menuLeft + popup.offsetWidth));
206 menuLeft -= popup.offsetWidth;
208 popup.style.top = menuTop + "px;";
209 popup.style.left = menuLeft + "px;";
210 popup.style.visibility = "visible;";
212 bodyOnClick = "" + document.body.getAttribute("onclick");
213 document.body.setAttribute("onclick", "onBodyClick(event);");
214 document.currentPopupMenu = popup;
217 HTMLElement.prototype.attachMenu = function(menuName) {
218 this.sogoContextMenu = $(menuName);
219 this.addEventListener("contextmenu", this.onContextMenu, true);
222 HTMLElement.prototype.select = function() {
223 this.addClassName('_selected');
226 HTMLElement.prototype.deselect = function() {
227 this.removeClassName('_selected');
230 HTMLElement.prototype.deselectAll = function () {
231 for (var i = 0; i < this.childNodes.length; i++) {
232 var node = this.childNodes.item(i);
233 if (node.nodeType == 1)