Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /*** |
michael@0 | 2 | |
michael@0 | 3 | MochiKit.DOM 1.4 |
michael@0 | 4 | |
michael@0 | 5 | See <http://mochikit.com/> for documentation, downloads, license, etc. |
michael@0 | 6 | |
michael@0 | 7 | (c) 2005 Bob Ippolito. All rights Reserved. |
michael@0 | 8 | |
michael@0 | 9 | ***/ |
michael@0 | 10 | |
michael@0 | 11 | if (typeof(dojo) != 'undefined') { |
michael@0 | 12 | dojo.provide("MochiKit.DOM"); |
michael@0 | 13 | dojo.require("MochiKit.Base"); |
michael@0 | 14 | } |
michael@0 | 15 | if (typeof(JSAN) != 'undefined') { |
michael@0 | 16 | JSAN.use("MochiKit.Base", []); |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | try { |
michael@0 | 20 | if (typeof(MochiKit.Base) == 'undefined') { |
michael@0 | 21 | throw ""; |
michael@0 | 22 | } |
michael@0 | 23 | } catch (e) { |
michael@0 | 24 | throw "MochiKit.DOM depends on MochiKit.Base!"; |
michael@0 | 25 | } |
michael@0 | 26 | |
michael@0 | 27 | if (typeof(MochiKit.DOM) == 'undefined') { |
michael@0 | 28 | MochiKit.DOM = {}; |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | MochiKit.DOM.NAME = "MochiKit.DOM"; |
michael@0 | 32 | MochiKit.DOM.VERSION = "1.4"; |
michael@0 | 33 | MochiKit.DOM.__repr__ = function () { |
michael@0 | 34 | return "[" + this.NAME + " " + this.VERSION + "]"; |
michael@0 | 35 | }; |
michael@0 | 36 | MochiKit.DOM.toString = function () { |
michael@0 | 37 | return this.__repr__(); |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | MochiKit.DOM.EXPORT = [ |
michael@0 | 41 | "removeEmptyTextNodes", |
michael@0 | 42 | "formContents", |
michael@0 | 43 | "currentWindow", |
michael@0 | 44 | "currentDocument", |
michael@0 | 45 | "withWindow", |
michael@0 | 46 | "withDocument", |
michael@0 | 47 | "registerDOMConverter", |
michael@0 | 48 | "coerceToDOM", |
michael@0 | 49 | "createDOM", |
michael@0 | 50 | "createDOMFunc", |
michael@0 | 51 | "isChildNode", |
michael@0 | 52 | "getNodeAttribute", |
michael@0 | 53 | "removeNodeAttribute", |
michael@0 | 54 | "setNodeAttribute", |
michael@0 | 55 | "updateNodeAttributes", |
michael@0 | 56 | "appendChildNodes", |
michael@0 | 57 | "insertSiblingNodesAfter", |
michael@0 | 58 | "insertSiblingNodesBefore", |
michael@0 | 59 | "replaceChildNodes", |
michael@0 | 60 | "removeElement", |
michael@0 | 61 | "swapDOM", |
michael@0 | 62 | "BUTTON", |
michael@0 | 63 | "TT", |
michael@0 | 64 | "PRE", |
michael@0 | 65 | "H1", |
michael@0 | 66 | "H2", |
michael@0 | 67 | "H3", |
michael@0 | 68 | "BR", |
michael@0 | 69 | "CANVAS", |
michael@0 | 70 | "HR", |
michael@0 | 71 | "LABEL", |
michael@0 | 72 | "TEXTAREA", |
michael@0 | 73 | "FORM", |
michael@0 | 74 | "STRONG", |
michael@0 | 75 | "SELECT", |
michael@0 | 76 | "OPTION", |
michael@0 | 77 | "OPTGROUP", |
michael@0 | 78 | "LEGEND", |
michael@0 | 79 | "FIELDSET", |
michael@0 | 80 | "P", |
michael@0 | 81 | "UL", |
michael@0 | 82 | "OL", |
michael@0 | 83 | "LI", |
michael@0 | 84 | "TD", |
michael@0 | 85 | "TR", |
michael@0 | 86 | "THEAD", |
michael@0 | 87 | "TBODY", |
michael@0 | 88 | "TFOOT", |
michael@0 | 89 | "TABLE", |
michael@0 | 90 | "TH", |
michael@0 | 91 | "INPUT", |
michael@0 | 92 | "SPAN", |
michael@0 | 93 | "A", |
michael@0 | 94 | "DIV", |
michael@0 | 95 | "IMG", |
michael@0 | 96 | "getElement", |
michael@0 | 97 | "$", |
michael@0 | 98 | "getElementsByTagAndClassName", |
michael@0 | 99 | "addToCallStack", |
michael@0 | 100 | "addLoadEvent", |
michael@0 | 101 | "focusOnLoad", |
michael@0 | 102 | "setElementClass", |
michael@0 | 103 | "toggleElementClass", |
michael@0 | 104 | "addElementClass", |
michael@0 | 105 | "removeElementClass", |
michael@0 | 106 | "swapElementClass", |
michael@0 | 107 | "hasElementClass", |
michael@0 | 108 | "escapeHTML", |
michael@0 | 109 | "toHTML", |
michael@0 | 110 | "emitHTML", |
michael@0 | 111 | "scrapeText", |
michael@0 | 112 | "isParent", |
michael@0 | 113 | "getFirstParentByTagAndClassName", |
michael@0 | 114 | "makeClipping", |
michael@0 | 115 | "undoClipping", |
michael@0 | 116 | "makePositioned", |
michael@0 | 117 | "undoPositioned", |
michael@0 | 118 | "getFirstElementByTagAndClassName" |
michael@0 | 119 | ]; |
michael@0 | 120 | |
michael@0 | 121 | MochiKit.DOM.EXPORT_OK = [ |
michael@0 | 122 | "domConverters" |
michael@0 | 123 | ]; |
michael@0 | 124 | |
michael@0 | 125 | MochiKit.DOM.DEPRECATED = [ |
michael@0 | 126 | ['computedStyle', 'MochiKit.Style.getStyle', '1.4'], |
michael@0 | 127 | /** @id MochiKit.DOM.elementDimensions */ |
michael@0 | 128 | ['elementDimensions', 'MochiKit.Style.getElementDimensions', '1.4'], |
michael@0 | 129 | /** @id MochiKit.DOM.elementPosition */ |
michael@0 | 130 | ['elementPosition', 'MochiKit.Style.getElementPosition', '1.4'], |
michael@0 | 131 | ['hideElement', 'MochiKit.Style.hideElement', '1.4'], |
michael@0 | 132 | /** @id MochiKit.DOM.setElementDimensions */ |
michael@0 | 133 | ['setElementDimensions', 'MochiKit.Style.setElementDimensions', '1.4'], |
michael@0 | 134 | /** @id MochiKit.DOM.setElementPosition */ |
michael@0 | 135 | ['setElementPosition', 'MochiKit.Style.setElementPosition', '1.4'], |
michael@0 | 136 | ['setDisplayForElement', 'MochiKit.Style.setDisplayForElement', '1.4'], |
michael@0 | 137 | /** @id MochiKit.DOM.setOpacity */ |
michael@0 | 138 | ['setOpacity', 'MochiKit.Style.setOpacity', '1.4'], |
michael@0 | 139 | ['showElement', 'MochiKit.Style.showElement', '1.4'], |
michael@0 | 140 | /** @id MochiKit.DOM.Coordinates */ |
michael@0 | 141 | ['Coordinates', 'MochiKit.Style.Coordinates', '1.4'], // FIXME: broken |
michael@0 | 142 | /** @id MochiKit.DOM.Dimensions */ |
michael@0 | 143 | ['Dimensions', 'MochiKit.Style.Dimensions', '1.4'] // FIXME: broken |
michael@0 | 144 | ]; |
michael@0 | 145 | |
michael@0 | 146 | /** @id MochiKit.DOM.getViewportDimensions */ |
michael@0 | 147 | MochiKit.DOM.getViewportDimensions = new Function('' + |
michael@0 | 148 | 'if (!MochiKit["Style"]) {' + |
michael@0 | 149 | ' throw new Error("This function has been deprecated and depends on MochiKit.Style.");' + |
michael@0 | 150 | '}' + |
michael@0 | 151 | 'return MochiKit.Style.getViewportDimensions.apply(this, arguments);'); |
michael@0 | 152 | |
michael@0 | 153 | MochiKit.Base.update(MochiKit.DOM, { |
michael@0 | 154 | |
michael@0 | 155 | /** @id MochiKit.DOM.currentWindow */ |
michael@0 | 156 | currentWindow: function () { |
michael@0 | 157 | return MochiKit.DOM._window; |
michael@0 | 158 | }, |
michael@0 | 159 | |
michael@0 | 160 | /** @id MochiKit.DOM.currentDocument */ |
michael@0 | 161 | currentDocument: function () { |
michael@0 | 162 | return MochiKit.DOM._document; |
michael@0 | 163 | }, |
michael@0 | 164 | |
michael@0 | 165 | /** @id MochiKit.DOM.withWindow */ |
michael@0 | 166 | withWindow: function (win, func) { |
michael@0 | 167 | var self = MochiKit.DOM; |
michael@0 | 168 | var oldDoc = self._document; |
michael@0 | 169 | var oldWin = self._window; |
michael@0 | 170 | var rval; |
michael@0 | 171 | try { |
michael@0 | 172 | self._window = win; |
michael@0 | 173 | self._document = win.document; |
michael@0 | 174 | rval = func(); |
michael@0 | 175 | } catch (e) { |
michael@0 | 176 | self._window = oldWin; |
michael@0 | 177 | self._document = oldDoc; |
michael@0 | 178 | throw e; |
michael@0 | 179 | } |
michael@0 | 180 | self._window = oldWin; |
michael@0 | 181 | self._document = oldDoc; |
michael@0 | 182 | return rval; |
michael@0 | 183 | }, |
michael@0 | 184 | |
michael@0 | 185 | /** @id MochiKit.DOM.formContents */ |
michael@0 | 186 | formContents: function (elem/* = document.body */) { |
michael@0 | 187 | var names = []; |
michael@0 | 188 | var values = []; |
michael@0 | 189 | var m = MochiKit.Base; |
michael@0 | 190 | var self = MochiKit.DOM; |
michael@0 | 191 | if (typeof(elem) == "undefined" || elem === null) { |
michael@0 | 192 | elem = self._document.body; |
michael@0 | 193 | } else { |
michael@0 | 194 | elem = self.getElement(elem); |
michael@0 | 195 | } |
michael@0 | 196 | m.nodeWalk(elem, function (elem) { |
michael@0 | 197 | var name = elem.name; |
michael@0 | 198 | if (m.isNotEmpty(name)) { |
michael@0 | 199 | var tagName = elem.tagName.toUpperCase(); |
michael@0 | 200 | if (tagName === "INPUT" |
michael@0 | 201 | && (elem.type == "radio" || elem.type == "checkbox") |
michael@0 | 202 | && !elem.checked |
michael@0 | 203 | ) { |
michael@0 | 204 | return null; |
michael@0 | 205 | } |
michael@0 | 206 | if (tagName === "SELECT") { |
michael@0 | 207 | if (elem.type == "select-one") { |
michael@0 | 208 | if (elem.selectedIndex >= 0) { |
michael@0 | 209 | var opt = elem.options[elem.selectedIndex]; |
michael@0 | 210 | var v = opt.value; |
michael@0 | 211 | if (!v) { |
michael@0 | 212 | var h = opt.outerHTML; |
michael@0 | 213 | // internet explorer sure does suck. |
michael@0 | 214 | if (h && !h.match(/^[^>]+\svalue\s*=/i)) { |
michael@0 | 215 | v = opt.text; |
michael@0 | 216 | } |
michael@0 | 217 | } |
michael@0 | 218 | names.push(name); |
michael@0 | 219 | values.push(v); |
michael@0 | 220 | return null; |
michael@0 | 221 | } |
michael@0 | 222 | // no form elements? |
michael@0 | 223 | names.push(name); |
michael@0 | 224 | values.push(""); |
michael@0 | 225 | return null; |
michael@0 | 226 | } else { |
michael@0 | 227 | var opts = elem.options; |
michael@0 | 228 | if (!opts.length) { |
michael@0 | 229 | names.push(name); |
michael@0 | 230 | values.push(""); |
michael@0 | 231 | return null; |
michael@0 | 232 | } |
michael@0 | 233 | for (var i = 0; i < opts.length; i++) { |
michael@0 | 234 | var opt = opts[i]; |
michael@0 | 235 | if (!opt.selected) { |
michael@0 | 236 | continue; |
michael@0 | 237 | } |
michael@0 | 238 | var v = opt.value; |
michael@0 | 239 | if (!v) { |
michael@0 | 240 | var h = opt.outerHTML; |
michael@0 | 241 | // internet explorer sure does suck. |
michael@0 | 242 | if (h && !h.match(/^[^>]+\svalue\s*=/i)) { |
michael@0 | 243 | v = opt.text; |
michael@0 | 244 | } |
michael@0 | 245 | } |
michael@0 | 246 | names.push(name); |
michael@0 | 247 | values.push(v); |
michael@0 | 248 | } |
michael@0 | 249 | return null; |
michael@0 | 250 | } |
michael@0 | 251 | } |
michael@0 | 252 | if (tagName === "FORM" || tagName === "P" || tagName === "SPAN" |
michael@0 | 253 | || tagName === "DIV" |
michael@0 | 254 | ) { |
michael@0 | 255 | return elem.childNodes; |
michael@0 | 256 | } |
michael@0 | 257 | names.push(name); |
michael@0 | 258 | values.push(elem.value || ''); |
michael@0 | 259 | return null; |
michael@0 | 260 | } |
michael@0 | 261 | return elem.childNodes; |
michael@0 | 262 | }); |
michael@0 | 263 | return [names, values]; |
michael@0 | 264 | }, |
michael@0 | 265 | |
michael@0 | 266 | /** @id MochiKit.DOM.withDocument */ |
michael@0 | 267 | withDocument: function (doc, func) { |
michael@0 | 268 | var self = MochiKit.DOM; |
michael@0 | 269 | var oldDoc = self._document; |
michael@0 | 270 | var rval; |
michael@0 | 271 | try { |
michael@0 | 272 | self._document = doc; |
michael@0 | 273 | rval = func(); |
michael@0 | 274 | } catch (e) { |
michael@0 | 275 | self._document = oldDoc; |
michael@0 | 276 | throw e; |
michael@0 | 277 | } |
michael@0 | 278 | self._document = oldDoc; |
michael@0 | 279 | return rval; |
michael@0 | 280 | }, |
michael@0 | 281 | |
michael@0 | 282 | /** @id MochiKit.DOM.registerDOMConverter */ |
michael@0 | 283 | registerDOMConverter: function (name, check, wrap, /* optional */override) { |
michael@0 | 284 | MochiKit.DOM.domConverters.register(name, check, wrap, override); |
michael@0 | 285 | }, |
michael@0 | 286 | |
michael@0 | 287 | /** @id MochiKit.DOM.coerceToDOM */ |
michael@0 | 288 | coerceToDOM: function (node, ctx) { |
michael@0 | 289 | var m = MochiKit.Base; |
michael@0 | 290 | var im = MochiKit.Iter; |
michael@0 | 291 | var self = MochiKit.DOM; |
michael@0 | 292 | if (im) { |
michael@0 | 293 | var iter = im.iter; |
michael@0 | 294 | var repeat = im.repeat; |
michael@0 | 295 | var map = m.map; |
michael@0 | 296 | } |
michael@0 | 297 | var domConverters = self.domConverters; |
michael@0 | 298 | var coerceToDOM = arguments.callee; |
michael@0 | 299 | var NotFound = m.NotFound; |
michael@0 | 300 | while (true) { |
michael@0 | 301 | if (typeof(node) == 'undefined' || node === null) { |
michael@0 | 302 | return null; |
michael@0 | 303 | } |
michael@0 | 304 | // this is a safari childNodes object, avoiding crashes w/ attr |
michael@0 | 305 | // lookup |
michael@0 | 306 | if (typeof(node) == "function" && |
michael@0 | 307 | typeof(node.length) == "number" && |
michael@0 | 308 | !(node instanceof Function)) { |
michael@0 | 309 | node = im.list(node); |
michael@0 | 310 | } |
michael@0 | 311 | if (typeof(node.nodeType) != 'undefined' && node.nodeType > 0) { |
michael@0 | 312 | return node; |
michael@0 | 313 | } |
michael@0 | 314 | if (typeof(node) == 'number' || typeof(node) == 'boolean') { |
michael@0 | 315 | node = node.toString(); |
michael@0 | 316 | // FALL THROUGH |
michael@0 | 317 | } |
michael@0 | 318 | if (typeof(node) == 'string') { |
michael@0 | 319 | return self._document.createTextNode(node); |
michael@0 | 320 | } |
michael@0 | 321 | if (typeof(node.__dom__) == 'function') { |
michael@0 | 322 | node = node.__dom__(ctx); |
michael@0 | 323 | continue; |
michael@0 | 324 | } |
michael@0 | 325 | if (typeof(node.dom) == 'function') { |
michael@0 | 326 | node = node.dom(ctx); |
michael@0 | 327 | continue; |
michael@0 | 328 | } |
michael@0 | 329 | if (typeof(node) == 'function') { |
michael@0 | 330 | node = node.apply(ctx, [ctx]); |
michael@0 | 331 | continue; |
michael@0 | 332 | } |
michael@0 | 333 | |
michael@0 | 334 | if (im) { |
michael@0 | 335 | // iterable |
michael@0 | 336 | var iterNodes = null; |
michael@0 | 337 | try { |
michael@0 | 338 | iterNodes = iter(node); |
michael@0 | 339 | } catch (e) { |
michael@0 | 340 | // pass |
michael@0 | 341 | } |
michael@0 | 342 | if (iterNodes) { |
michael@0 | 343 | return map(coerceToDOM, iterNodes, repeat(ctx)); |
michael@0 | 344 | } |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | // adapter |
michael@0 | 348 | try { |
michael@0 | 349 | node = domConverters.match(node, ctx); |
michael@0 | 350 | continue; |
michael@0 | 351 | } catch (e) { |
michael@0 | 352 | if (e != NotFound) { |
michael@0 | 353 | throw e; |
michael@0 | 354 | } |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | // fallback |
michael@0 | 358 | return self._document.createTextNode(node.toString()); |
michael@0 | 359 | } |
michael@0 | 360 | // mozilla warnings aren't too bright |
michael@0 | 361 | return undefined; |
michael@0 | 362 | }, |
michael@0 | 363 | |
michael@0 | 364 | /** @id MochiKit.DOM.isChildNode */ |
michael@0 | 365 | isChildNode: function (node, maybeparent) { |
michael@0 | 366 | var self = MochiKit.DOM; |
michael@0 | 367 | if (typeof(node) == "string") { |
michael@0 | 368 | node = self.getElement(node); |
michael@0 | 369 | } |
michael@0 | 370 | if (typeof(maybeparent) == "string") { |
michael@0 | 371 | maybeparent = self.getElement(maybeparent); |
michael@0 | 372 | } |
michael@0 | 373 | if (node === maybeparent) { |
michael@0 | 374 | return true; |
michael@0 | 375 | } |
michael@0 | 376 | while (node && node.tagName.toUpperCase() != "BODY") { |
michael@0 | 377 | node = node.parentNode; |
michael@0 | 378 | if (node === maybeparent) { |
michael@0 | 379 | return true; |
michael@0 | 380 | } |
michael@0 | 381 | } |
michael@0 | 382 | return false; |
michael@0 | 383 | }, |
michael@0 | 384 | |
michael@0 | 385 | /** @id MochiKit.DOM.setNodeAttribute */ |
michael@0 | 386 | setNodeAttribute: function (node, attr, value) { |
michael@0 | 387 | var o = {}; |
michael@0 | 388 | o[attr] = value; |
michael@0 | 389 | try { |
michael@0 | 390 | return MochiKit.DOM.updateNodeAttributes(node, o); |
michael@0 | 391 | } catch (e) { |
michael@0 | 392 | // pass |
michael@0 | 393 | } |
michael@0 | 394 | return null; |
michael@0 | 395 | }, |
michael@0 | 396 | |
michael@0 | 397 | /** @id MochiKit.DOM.getNodeAttribute */ |
michael@0 | 398 | getNodeAttribute: function (node, attr) { |
michael@0 | 399 | var self = MochiKit.DOM; |
michael@0 | 400 | var rename = self.attributeArray.renames[attr]; |
michael@0 | 401 | node = self.getElement(node); |
michael@0 | 402 | try { |
michael@0 | 403 | if (rename) { |
michael@0 | 404 | return node[rename]; |
michael@0 | 405 | } |
michael@0 | 406 | return node.getAttribute(attr); |
michael@0 | 407 | } catch (e) { |
michael@0 | 408 | // pass |
michael@0 | 409 | } |
michael@0 | 410 | return null; |
michael@0 | 411 | }, |
michael@0 | 412 | |
michael@0 | 413 | /** @id MochiKit.DOM.removeNodeAttribute */ |
michael@0 | 414 | removeNodeAttribute: function (node, attr) { |
michael@0 | 415 | var self = MochiKit.DOM; |
michael@0 | 416 | var rename = self.attributeArray.renames[attr]; |
michael@0 | 417 | node = self.getElement(node); |
michael@0 | 418 | try { |
michael@0 | 419 | if (rename) { |
michael@0 | 420 | return node[rename]; |
michael@0 | 421 | } |
michael@0 | 422 | return node.removeAttribute(attr); |
michael@0 | 423 | } catch (e) { |
michael@0 | 424 | // pass |
michael@0 | 425 | } |
michael@0 | 426 | return null; |
michael@0 | 427 | }, |
michael@0 | 428 | |
michael@0 | 429 | /** @id MochiKit.DOM.updateNodeAttributes */ |
michael@0 | 430 | updateNodeAttributes: function (node, attrs) { |
michael@0 | 431 | var elem = node; |
michael@0 | 432 | var self = MochiKit.DOM; |
michael@0 | 433 | if (typeof(node) == 'string') { |
michael@0 | 434 | elem = self.getElement(node); |
michael@0 | 435 | } |
michael@0 | 436 | if (attrs) { |
michael@0 | 437 | var updatetree = MochiKit.Base.updatetree; |
michael@0 | 438 | if (self.attributeArray.compliant) { |
michael@0 | 439 | // not IE, good. |
michael@0 | 440 | for (var k in attrs) { |
michael@0 | 441 | var v = attrs[k]; |
michael@0 | 442 | if (typeof(v) == 'object' && typeof(elem[k]) == 'object') { |
michael@0 | 443 | if (k == "style" && MochiKit.Style) { |
michael@0 | 444 | MochiKit.Style.setStyle(elem, v); |
michael@0 | 445 | } else { |
michael@0 | 446 | updatetree(elem[k], v); |
michael@0 | 447 | } |
michael@0 | 448 | } else if (k.substring(0, 2) == "on") { |
michael@0 | 449 | if (typeof(v) == "string") { |
michael@0 | 450 | v = new Function(v); |
michael@0 | 451 | } |
michael@0 | 452 | elem[k] = v; |
michael@0 | 453 | } else { |
michael@0 | 454 | elem.setAttribute(k, v); |
michael@0 | 455 | } |
michael@0 | 456 | } |
michael@0 | 457 | } else { |
michael@0 | 458 | // IE is insane in the membrane |
michael@0 | 459 | var renames = self.attributeArray.renames; |
michael@0 | 460 | for (var k in attrs) { |
michael@0 | 461 | v = attrs[k]; |
michael@0 | 462 | var renamed = renames[k]; |
michael@0 | 463 | if (k == "style" && typeof(v) == "string") { |
michael@0 | 464 | elem.style.cssText = v; |
michael@0 | 465 | } else if (typeof(renamed) == "string") { |
michael@0 | 466 | elem[renamed] = v; |
michael@0 | 467 | } else if (typeof(elem[k]) == 'object' |
michael@0 | 468 | && typeof(v) == 'object') { |
michael@0 | 469 | if (k == "style" && MochiKit.Style) { |
michael@0 | 470 | MochiKit.Style.setStyle(elem, v); |
michael@0 | 471 | } else { |
michael@0 | 472 | updatetree(elem[k], v); |
michael@0 | 473 | } |
michael@0 | 474 | } else if (k.substring(0, 2) == "on") { |
michael@0 | 475 | if (typeof(v) == "string") { |
michael@0 | 476 | v = new Function(v); |
michael@0 | 477 | } |
michael@0 | 478 | elem[k] = v; |
michael@0 | 479 | } else { |
michael@0 | 480 | elem.setAttribute(k, v); |
michael@0 | 481 | } |
michael@0 | 482 | } |
michael@0 | 483 | } |
michael@0 | 484 | } |
michael@0 | 485 | return elem; |
michael@0 | 486 | }, |
michael@0 | 487 | |
michael@0 | 488 | /** @id MochiKit.DOM.appendChildNodes */ |
michael@0 | 489 | appendChildNodes: function (node/*, nodes...*/) { |
michael@0 | 490 | var elem = node; |
michael@0 | 491 | var self = MochiKit.DOM; |
michael@0 | 492 | if (typeof(node) == 'string') { |
michael@0 | 493 | elem = self.getElement(node); |
michael@0 | 494 | } |
michael@0 | 495 | var nodeStack = [ |
michael@0 | 496 | self.coerceToDOM( |
michael@0 | 497 | MochiKit.Base.extend(null, arguments, 1), |
michael@0 | 498 | elem |
michael@0 | 499 | ) |
michael@0 | 500 | ]; |
michael@0 | 501 | var concat = MochiKit.Base.concat; |
michael@0 | 502 | while (nodeStack.length) { |
michael@0 | 503 | var n = nodeStack.shift(); |
michael@0 | 504 | if (typeof(n) == 'undefined' || n === null) { |
michael@0 | 505 | // pass |
michael@0 | 506 | } else if (typeof(n.nodeType) == 'number') { |
michael@0 | 507 | elem.appendChild(n); |
michael@0 | 508 | } else { |
michael@0 | 509 | nodeStack = concat(n, nodeStack); |
michael@0 | 510 | } |
michael@0 | 511 | } |
michael@0 | 512 | return elem; |
michael@0 | 513 | }, |
michael@0 | 514 | |
michael@0 | 515 | |
michael@0 | 516 | /** @id MochiKit.DOM.insertSiblingNodesBefore */ |
michael@0 | 517 | insertSiblingNodesBefore: function (node/*, nodes...*/) { |
michael@0 | 518 | var elem = node; |
michael@0 | 519 | var self = MochiKit.DOM; |
michael@0 | 520 | if (typeof(node) == 'string') { |
michael@0 | 521 | elem = self.getElement(node); |
michael@0 | 522 | } |
michael@0 | 523 | var nodeStack = [ |
michael@0 | 524 | self.coerceToDOM( |
michael@0 | 525 | MochiKit.Base.extend(null, arguments, 1), |
michael@0 | 526 | elem |
michael@0 | 527 | ) |
michael@0 | 528 | ]; |
michael@0 | 529 | var parentnode = elem.parentNode; |
michael@0 | 530 | var concat = MochiKit.Base.concat; |
michael@0 | 531 | while (nodeStack.length) { |
michael@0 | 532 | var n = nodeStack.shift(); |
michael@0 | 533 | if (typeof(n) == 'undefined' || n === null) { |
michael@0 | 534 | // pass |
michael@0 | 535 | } else if (typeof(n.nodeType) == 'number') { |
michael@0 | 536 | parentnode.insertBefore(n, elem); |
michael@0 | 537 | } else { |
michael@0 | 538 | nodeStack = concat(n, nodeStack); |
michael@0 | 539 | } |
michael@0 | 540 | } |
michael@0 | 541 | return parentnode; |
michael@0 | 542 | }, |
michael@0 | 543 | |
michael@0 | 544 | /** @id MochiKit.DOM.insertSiblingNodesAfter */ |
michael@0 | 545 | insertSiblingNodesAfter: function (node/*, nodes...*/) { |
michael@0 | 546 | var elem = node; |
michael@0 | 547 | var self = MochiKit.DOM; |
michael@0 | 548 | |
michael@0 | 549 | if (typeof(node) == 'string') { |
michael@0 | 550 | elem = self.getElement(node); |
michael@0 | 551 | } |
michael@0 | 552 | var nodeStack = [ |
michael@0 | 553 | self.coerceToDOM( |
michael@0 | 554 | MochiKit.Base.extend(null, arguments, 1), |
michael@0 | 555 | elem |
michael@0 | 556 | ) |
michael@0 | 557 | ]; |
michael@0 | 558 | |
michael@0 | 559 | if (elem.nextSibling) { |
michael@0 | 560 | return self.insertSiblingNodesBefore(elem.nextSibling, nodeStack); |
michael@0 | 561 | } |
michael@0 | 562 | else { |
michael@0 | 563 | return self.appendChildNodes(elem.parentNode, nodeStack); |
michael@0 | 564 | } |
michael@0 | 565 | }, |
michael@0 | 566 | |
michael@0 | 567 | /** @id MochiKit.DOM.replaceChildNodes */ |
michael@0 | 568 | replaceChildNodes: function (node/*, nodes...*/) { |
michael@0 | 569 | var elem = node; |
michael@0 | 570 | var self = MochiKit.DOM; |
michael@0 | 571 | if (typeof(node) == 'string') { |
michael@0 | 572 | elem = self.getElement(node); |
michael@0 | 573 | arguments[0] = elem; |
michael@0 | 574 | } |
michael@0 | 575 | var child; |
michael@0 | 576 | while ((child = elem.firstChild)) { |
michael@0 | 577 | elem.removeChild(child); |
michael@0 | 578 | } |
michael@0 | 579 | if (arguments.length < 2) { |
michael@0 | 580 | return elem; |
michael@0 | 581 | } else { |
michael@0 | 582 | return self.appendChildNodes.apply(this, arguments); |
michael@0 | 583 | } |
michael@0 | 584 | }, |
michael@0 | 585 | |
michael@0 | 586 | /** @id MochiKit.DOM.createDOM */ |
michael@0 | 587 | createDOM: function (name, attrs/*, nodes... */) { |
michael@0 | 588 | var elem; |
michael@0 | 589 | var self = MochiKit.DOM; |
michael@0 | 590 | var m = MochiKit.Base; |
michael@0 | 591 | if (typeof(attrs) == "string" || typeof(attrs) == "number") { |
michael@0 | 592 | var args = m.extend([name, null], arguments, 1); |
michael@0 | 593 | return arguments.callee.apply(this, args); |
michael@0 | 594 | } |
michael@0 | 595 | if (typeof(name) == 'string') { |
michael@0 | 596 | // Internet Explorer is dumb |
michael@0 | 597 | var xhtml = self._xhtml; |
michael@0 | 598 | if (attrs && !self.attributeArray.compliant) { |
michael@0 | 599 | // http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/name_2.asp |
michael@0 | 600 | var contents = ""; |
michael@0 | 601 | if ('name' in attrs) { |
michael@0 | 602 | contents += ' name="' + self.escapeHTML(attrs.name) + '"'; |
michael@0 | 603 | } |
michael@0 | 604 | if (name == 'input' && 'type' in attrs) { |
michael@0 | 605 | contents += ' type="' + self.escapeHTML(attrs.type) + '"'; |
michael@0 | 606 | } |
michael@0 | 607 | if (contents) { |
michael@0 | 608 | name = "<" + name + contents + ">"; |
michael@0 | 609 | xhtml = false; |
michael@0 | 610 | } |
michael@0 | 611 | } |
michael@0 | 612 | var d = self._document; |
michael@0 | 613 | if (xhtml && d === document) { |
michael@0 | 614 | elem = d.createElementNS("http://www.w3.org/1999/xhtml", name); |
michael@0 | 615 | } else { |
michael@0 | 616 | elem = d.createElement(name); |
michael@0 | 617 | } |
michael@0 | 618 | } else { |
michael@0 | 619 | elem = name; |
michael@0 | 620 | } |
michael@0 | 621 | if (attrs) { |
michael@0 | 622 | self.updateNodeAttributes(elem, attrs); |
michael@0 | 623 | } |
michael@0 | 624 | if (arguments.length <= 2) { |
michael@0 | 625 | return elem; |
michael@0 | 626 | } else { |
michael@0 | 627 | var args = m.extend([elem], arguments, 2); |
michael@0 | 628 | return self.appendChildNodes.apply(this, args); |
michael@0 | 629 | } |
michael@0 | 630 | }, |
michael@0 | 631 | |
michael@0 | 632 | /** @id MochiKit.DOM.createDOMFunc */ |
michael@0 | 633 | createDOMFunc: function (/* tag, attrs, *nodes */) { |
michael@0 | 634 | var m = MochiKit.Base; |
michael@0 | 635 | return m.partial.apply( |
michael@0 | 636 | this, |
michael@0 | 637 | m.extend([MochiKit.DOM.createDOM], arguments) |
michael@0 | 638 | ); |
michael@0 | 639 | }, |
michael@0 | 640 | |
michael@0 | 641 | /** @id MochiKit.DOM.removeElement */ |
michael@0 | 642 | removeElement: function (elem) { |
michael@0 | 643 | var e = MochiKit.DOM.getElement(elem); |
michael@0 | 644 | e.parentNode.removeChild(e); |
michael@0 | 645 | return e; |
michael@0 | 646 | }, |
michael@0 | 647 | |
michael@0 | 648 | /** @id MochiKit.DOM.swapDOM */ |
michael@0 | 649 | swapDOM: function (dest, src) { |
michael@0 | 650 | var self = MochiKit.DOM; |
michael@0 | 651 | dest = self.getElement(dest); |
michael@0 | 652 | var parent = dest.parentNode; |
michael@0 | 653 | if (src) { |
michael@0 | 654 | src = self.getElement(src); |
michael@0 | 655 | parent.replaceChild(src, dest); |
michael@0 | 656 | } else { |
michael@0 | 657 | parent.removeChild(dest); |
michael@0 | 658 | } |
michael@0 | 659 | return src; |
michael@0 | 660 | }, |
michael@0 | 661 | |
michael@0 | 662 | /** @id MochiKit.DOM.getElement */ |
michael@0 | 663 | getElement: function (id) { |
michael@0 | 664 | var self = MochiKit.DOM; |
michael@0 | 665 | if (arguments.length == 1) { |
michael@0 | 666 | return ((typeof(id) == "string") ? |
michael@0 | 667 | self._document.getElementById(id) : id); |
michael@0 | 668 | } else { |
michael@0 | 669 | return MochiKit.Base.map(self.getElement, arguments); |
michael@0 | 670 | } |
michael@0 | 671 | }, |
michael@0 | 672 | |
michael@0 | 673 | /** @id MochiKit.DOM.getElementsByTagAndClassName */ |
michael@0 | 674 | getElementsByTagAndClassName: function (tagName, className, |
michael@0 | 675 | /* optional */parent) { |
michael@0 | 676 | var self = MochiKit.DOM; |
michael@0 | 677 | if (typeof(tagName) == 'undefined' || tagName === null) { |
michael@0 | 678 | tagName = '*'; |
michael@0 | 679 | } |
michael@0 | 680 | if (typeof(parent) == 'undefined' || parent === null) { |
michael@0 | 681 | parent = self._document; |
michael@0 | 682 | } |
michael@0 | 683 | parent = self.getElement(parent); |
michael@0 | 684 | var children = (parent.getElementsByTagName(tagName) |
michael@0 | 685 | || self._document.all); |
michael@0 | 686 | if (typeof(className) == 'undefined' || className === null) { |
michael@0 | 687 | return MochiKit.Base.extend(null, children); |
michael@0 | 688 | } |
michael@0 | 689 | |
michael@0 | 690 | var elements = []; |
michael@0 | 691 | for (var i = 0; i < children.length; i++) { |
michael@0 | 692 | var child = children[i]; |
michael@0 | 693 | var cls = child.className; |
michael@0 | 694 | if (!cls) { |
michael@0 | 695 | continue; |
michael@0 | 696 | } |
michael@0 | 697 | var classNames = cls.split(' '); |
michael@0 | 698 | for (var j = 0; j < classNames.length; j++) { |
michael@0 | 699 | if (classNames[j] == className) { |
michael@0 | 700 | elements.push(child); |
michael@0 | 701 | break; |
michael@0 | 702 | } |
michael@0 | 703 | } |
michael@0 | 704 | } |
michael@0 | 705 | |
michael@0 | 706 | return elements; |
michael@0 | 707 | }, |
michael@0 | 708 | |
michael@0 | 709 | _newCallStack: function (path, once) { |
michael@0 | 710 | var rval = function () { |
michael@0 | 711 | var callStack = arguments.callee.callStack; |
michael@0 | 712 | for (var i = 0; i < callStack.length; i++) { |
michael@0 | 713 | if (callStack[i].apply(this, arguments) === false) { |
michael@0 | 714 | break; |
michael@0 | 715 | } |
michael@0 | 716 | } |
michael@0 | 717 | if (once) { |
michael@0 | 718 | try { |
michael@0 | 719 | this[path] = null; |
michael@0 | 720 | } catch (e) { |
michael@0 | 721 | // pass |
michael@0 | 722 | } |
michael@0 | 723 | } |
michael@0 | 724 | }; |
michael@0 | 725 | rval.callStack = []; |
michael@0 | 726 | return rval; |
michael@0 | 727 | }, |
michael@0 | 728 | |
michael@0 | 729 | /** @id MochiKit.DOM.addToCallStack */ |
michael@0 | 730 | addToCallStack: function (target, path, func, once) { |
michael@0 | 731 | var self = MochiKit.DOM; |
michael@0 | 732 | var existing = target[path]; |
michael@0 | 733 | var regfunc = existing; |
michael@0 | 734 | if (!(typeof(existing) == 'function' |
michael@0 | 735 | && typeof(existing.callStack) == "object" |
michael@0 | 736 | && existing.callStack !== null)) { |
michael@0 | 737 | regfunc = self._newCallStack(path, once); |
michael@0 | 738 | if (typeof(existing) == 'function') { |
michael@0 | 739 | regfunc.callStack.push(existing); |
michael@0 | 740 | } |
michael@0 | 741 | target[path] = regfunc; |
michael@0 | 742 | } |
michael@0 | 743 | regfunc.callStack.push(func); |
michael@0 | 744 | }, |
michael@0 | 745 | |
michael@0 | 746 | /** @id MochiKit.DOM.addLoadEvent */ |
michael@0 | 747 | addLoadEvent: function (func) { |
michael@0 | 748 | var self = MochiKit.DOM; |
michael@0 | 749 | self.addToCallStack(self._window, "onload", func, true); |
michael@0 | 750 | |
michael@0 | 751 | }, |
michael@0 | 752 | |
michael@0 | 753 | /** @id MochiKit.DOM.focusOnLoad */ |
michael@0 | 754 | focusOnLoad: function (element) { |
michael@0 | 755 | var self = MochiKit.DOM; |
michael@0 | 756 | self.addLoadEvent(function () { |
michael@0 | 757 | element = self.getElement(element); |
michael@0 | 758 | if (element) { |
michael@0 | 759 | element.focus(); |
michael@0 | 760 | } |
michael@0 | 761 | }); |
michael@0 | 762 | }, |
michael@0 | 763 | |
michael@0 | 764 | /** @id MochiKit.DOM.setElementClass */ |
michael@0 | 765 | setElementClass: function (element, className) { |
michael@0 | 766 | var self = MochiKit.DOM; |
michael@0 | 767 | var obj = self.getElement(element); |
michael@0 | 768 | if (self.attributeArray.compliant) { |
michael@0 | 769 | obj.setAttribute("class", className); |
michael@0 | 770 | } else { |
michael@0 | 771 | obj.setAttribute("className", className); |
michael@0 | 772 | } |
michael@0 | 773 | }, |
michael@0 | 774 | |
michael@0 | 775 | /** @id MochiKit.DOM.toggleElementClass */ |
michael@0 | 776 | toggleElementClass: function (className/*, element... */) { |
michael@0 | 777 | var self = MochiKit.DOM; |
michael@0 | 778 | for (var i = 1; i < arguments.length; i++) { |
michael@0 | 779 | var obj = self.getElement(arguments[i]); |
michael@0 | 780 | if (!self.addElementClass(obj, className)) { |
michael@0 | 781 | self.removeElementClass(obj, className); |
michael@0 | 782 | } |
michael@0 | 783 | } |
michael@0 | 784 | }, |
michael@0 | 785 | |
michael@0 | 786 | /** @id MochiKit.DOM.addElementClass */ |
michael@0 | 787 | addElementClass: function (element, className) { |
michael@0 | 788 | var self = MochiKit.DOM; |
michael@0 | 789 | var obj = self.getElement(element); |
michael@0 | 790 | var cls = obj.className; |
michael@0 | 791 | // trivial case, no className yet |
michael@0 | 792 | if (cls == undefined || cls.length === 0) { |
michael@0 | 793 | self.setElementClass(obj, className); |
michael@0 | 794 | return true; |
michael@0 | 795 | } |
michael@0 | 796 | // the other trivial case, already set as the only class |
michael@0 | 797 | if (cls == className) { |
michael@0 | 798 | return false; |
michael@0 | 799 | } |
michael@0 | 800 | var classes = cls.split(" "); |
michael@0 | 801 | for (var i = 0; i < classes.length; i++) { |
michael@0 | 802 | // already present |
michael@0 | 803 | if (classes[i] == className) { |
michael@0 | 804 | return false; |
michael@0 | 805 | } |
michael@0 | 806 | } |
michael@0 | 807 | // append class |
michael@0 | 808 | self.setElementClass(obj, cls + " " + className); |
michael@0 | 809 | return true; |
michael@0 | 810 | }, |
michael@0 | 811 | |
michael@0 | 812 | /** @id MochiKit.DOM.removeElementClass */ |
michael@0 | 813 | removeElementClass: function (element, className) { |
michael@0 | 814 | var self = MochiKit.DOM; |
michael@0 | 815 | var obj = self.getElement(element); |
michael@0 | 816 | var cls = obj.className; |
michael@0 | 817 | // trivial case, no className yet |
michael@0 | 818 | if (cls == undefined || cls.length === 0) { |
michael@0 | 819 | return false; |
michael@0 | 820 | } |
michael@0 | 821 | // other trivial case, set only to className |
michael@0 | 822 | if (cls == className) { |
michael@0 | 823 | self.setElementClass(obj, ""); |
michael@0 | 824 | return true; |
michael@0 | 825 | } |
michael@0 | 826 | var classes = cls.split(" "); |
michael@0 | 827 | for (var i = 0; i < classes.length; i++) { |
michael@0 | 828 | // already present |
michael@0 | 829 | if (classes[i] == className) { |
michael@0 | 830 | // only check sane case where the class is used once |
michael@0 | 831 | classes.splice(i, 1); |
michael@0 | 832 | self.setElementClass(obj, classes.join(" ")); |
michael@0 | 833 | return true; |
michael@0 | 834 | } |
michael@0 | 835 | } |
michael@0 | 836 | // not found |
michael@0 | 837 | return false; |
michael@0 | 838 | }, |
michael@0 | 839 | |
michael@0 | 840 | /** @id MochiKit.DOM.swapElementClass */ |
michael@0 | 841 | swapElementClass: function (element, fromClass, toClass) { |
michael@0 | 842 | var obj = MochiKit.DOM.getElement(element); |
michael@0 | 843 | var res = MochiKit.DOM.removeElementClass(obj, fromClass); |
michael@0 | 844 | if (res) { |
michael@0 | 845 | MochiKit.DOM.addElementClass(obj, toClass); |
michael@0 | 846 | } |
michael@0 | 847 | return res; |
michael@0 | 848 | }, |
michael@0 | 849 | |
michael@0 | 850 | /** @id MochiKit.DOM.hasElementClass */ |
michael@0 | 851 | hasElementClass: function (element, className/*...*/) { |
michael@0 | 852 | var obj = MochiKit.DOM.getElement(element); |
michael@0 | 853 | var cls = obj.className; |
michael@0 | 854 | if (!cls) { |
michael@0 | 855 | return false; |
michael@0 | 856 | } |
michael@0 | 857 | var classes = cls.split(" "); |
michael@0 | 858 | for (var i = 1; i < arguments.length; i++) { |
michael@0 | 859 | var good = false; |
michael@0 | 860 | for (var j = 0; j < classes.length; j++) { |
michael@0 | 861 | if (classes[j] == arguments[i]) { |
michael@0 | 862 | good = true; |
michael@0 | 863 | break; |
michael@0 | 864 | } |
michael@0 | 865 | } |
michael@0 | 866 | if (!good) { |
michael@0 | 867 | return false; |
michael@0 | 868 | } |
michael@0 | 869 | } |
michael@0 | 870 | return true; |
michael@0 | 871 | }, |
michael@0 | 872 | |
michael@0 | 873 | /** @id MochiKit.DOM.escapeHTML */ |
michael@0 | 874 | escapeHTML: function (s) { |
michael@0 | 875 | return s.replace(/&/g, "&" |
michael@0 | 876 | ).replace(/"/g, """ |
michael@0 | 877 | ).replace(/</g, "<" |
michael@0 | 878 | ).replace(/>/g, ">"); |
michael@0 | 879 | }, |
michael@0 | 880 | |
michael@0 | 881 | /** @id MochiKit.DOM.toHTML */ |
michael@0 | 882 | toHTML: function (dom) { |
michael@0 | 883 | return MochiKit.DOM.emitHTML(dom).join(""); |
michael@0 | 884 | }, |
michael@0 | 885 | |
michael@0 | 886 | /** @id MochiKit.DOM.emitHTML */ |
michael@0 | 887 | emitHTML: function (dom, /* optional */lst) { |
michael@0 | 888 | if (typeof(lst) == 'undefined' || lst === null) { |
michael@0 | 889 | lst = []; |
michael@0 | 890 | } |
michael@0 | 891 | // queue is the call stack, we're doing this non-recursively |
michael@0 | 892 | var queue = [dom]; |
michael@0 | 893 | var self = MochiKit.DOM; |
michael@0 | 894 | var escapeHTML = self.escapeHTML; |
michael@0 | 895 | var attributeArray = self.attributeArray; |
michael@0 | 896 | while (queue.length) { |
michael@0 | 897 | dom = queue.pop(); |
michael@0 | 898 | if (typeof(dom) == 'string') { |
michael@0 | 899 | lst.push(dom); |
michael@0 | 900 | } else if (dom.nodeType == 1) { |
michael@0 | 901 | // we're not using higher order stuff here |
michael@0 | 902 | // because safari has heisenbugs.. argh. |
michael@0 | 903 | // |
michael@0 | 904 | // I think it might have something to do with |
michael@0 | 905 | // garbage collection and function calls. |
michael@0 | 906 | lst.push('<' + dom.tagName.toLowerCase()); |
michael@0 | 907 | var attributes = []; |
michael@0 | 908 | var domAttr = attributeArray(dom); |
michael@0 | 909 | for (var i = 0; i < domAttr.length; i++) { |
michael@0 | 910 | var a = domAttr[i]; |
michael@0 | 911 | attributes.push([ |
michael@0 | 912 | " ", |
michael@0 | 913 | a.name, |
michael@0 | 914 | '="', |
michael@0 | 915 | escapeHTML(a.value), |
michael@0 | 916 | '"' |
michael@0 | 917 | ]); |
michael@0 | 918 | } |
michael@0 | 919 | attributes.sort(); |
michael@0 | 920 | for (i = 0; i < attributes.length; i++) { |
michael@0 | 921 | var attrs = attributes[i]; |
michael@0 | 922 | for (var j = 0; j < attrs.length; j++) { |
michael@0 | 923 | lst.push(attrs[j]); |
michael@0 | 924 | } |
michael@0 | 925 | } |
michael@0 | 926 | if (dom.hasChildNodes()) { |
michael@0 | 927 | lst.push(">"); |
michael@0 | 928 | // queue is the FILO call stack, so we put the close tag |
michael@0 | 929 | // on first |
michael@0 | 930 | queue.push("</" + dom.tagName.toLowerCase() + ">"); |
michael@0 | 931 | var cnodes = dom.childNodes; |
michael@0 | 932 | for (i = cnodes.length - 1; i >= 0; i--) { |
michael@0 | 933 | queue.push(cnodes[i]); |
michael@0 | 934 | } |
michael@0 | 935 | } else { |
michael@0 | 936 | lst.push('/>'); |
michael@0 | 937 | } |
michael@0 | 938 | } else if (dom.nodeType == 3) { |
michael@0 | 939 | lst.push(escapeHTML(dom.nodeValue)); |
michael@0 | 940 | } |
michael@0 | 941 | } |
michael@0 | 942 | return lst; |
michael@0 | 943 | }, |
michael@0 | 944 | |
michael@0 | 945 | /** @id MochiKit.DOM.scrapeText */ |
michael@0 | 946 | scrapeText: function (node, /* optional */asArray) { |
michael@0 | 947 | var rval = []; |
michael@0 | 948 | (function (node) { |
michael@0 | 949 | var cn = node.childNodes; |
michael@0 | 950 | if (cn) { |
michael@0 | 951 | for (var i = 0; i < cn.length; i++) { |
michael@0 | 952 | arguments.callee.call(this, cn[i]); |
michael@0 | 953 | } |
michael@0 | 954 | } |
michael@0 | 955 | var nodeValue = node.nodeValue; |
michael@0 | 956 | if (typeof(nodeValue) == 'string') { |
michael@0 | 957 | rval.push(nodeValue); |
michael@0 | 958 | } |
michael@0 | 959 | })(MochiKit.DOM.getElement(node)); |
michael@0 | 960 | if (asArray) { |
michael@0 | 961 | return rval; |
michael@0 | 962 | } else { |
michael@0 | 963 | return rval.join(""); |
michael@0 | 964 | } |
michael@0 | 965 | }, |
michael@0 | 966 | |
michael@0 | 967 | /** @id MochiKit.DOM.removeEmptyTextNodes */ |
michael@0 | 968 | removeEmptyTextNodes: function (element) { |
michael@0 | 969 | element = MochiKit.DOM.getElement(element); |
michael@0 | 970 | for (var i = 0; i < element.childNodes.length; i++) { |
michael@0 | 971 | var node = element.childNodes[i]; |
michael@0 | 972 | if (node.nodeType == 3 && !/\S/.test(node.nodeValue)) { |
michael@0 | 973 | node.parentNode.removeChild(node); |
michael@0 | 974 | } |
michael@0 | 975 | } |
michael@0 | 976 | }, |
michael@0 | 977 | |
michael@0 | 978 | /** @id MochiKit.DOM.makeClipping */ |
michael@0 | 979 | makeClipping: function (element) { |
michael@0 | 980 | element = MochiKit.DOM.getElement(element); |
michael@0 | 981 | var oldOverflow = element.style.overflow; |
michael@0 | 982 | if ((MochiKit.Style.getStyle(element, 'overflow') || 'visible') != 'hidden') { |
michael@0 | 983 | element.style.overflow = 'hidden'; |
michael@0 | 984 | } |
michael@0 | 985 | return oldOverflow; |
michael@0 | 986 | }, |
michael@0 | 987 | |
michael@0 | 988 | /** @id MochiKit.DOM.undoClipping */ |
michael@0 | 989 | undoClipping: function (element, overflow) { |
michael@0 | 990 | element = MochiKit.DOM.getElement(element); |
michael@0 | 991 | if (!overflow) { |
michael@0 | 992 | return; |
michael@0 | 993 | } |
michael@0 | 994 | element.style.overflow = overflow; |
michael@0 | 995 | }, |
michael@0 | 996 | |
michael@0 | 997 | /** @id MochiKit.DOM.makePositioned */ |
michael@0 | 998 | makePositioned: function (element) { |
michael@0 | 999 | element = MochiKit.DOM.getElement(element); |
michael@0 | 1000 | var pos = MochiKit.Style.getStyle(element, 'position'); |
michael@0 | 1001 | if (pos == 'static' || !pos) { |
michael@0 | 1002 | element.style.position = 'relative'; |
michael@0 | 1003 | // Opera returns the offset relative to the positioning context, |
michael@0 | 1004 | // when an element is position relative but top and left have |
michael@0 | 1005 | // not been defined |
michael@0 | 1006 | if (/Opera/.test(navigator.userAgent)) { |
michael@0 | 1007 | element.style.top = 0; |
michael@0 | 1008 | element.style.left = 0; |
michael@0 | 1009 | } |
michael@0 | 1010 | } |
michael@0 | 1011 | }, |
michael@0 | 1012 | |
michael@0 | 1013 | /** @id MochiKit.DOM.undoPositioned */ |
michael@0 | 1014 | undoPositioned: function (element) { |
michael@0 | 1015 | element = MochiKit.DOM.getElement(element); |
michael@0 | 1016 | if (element.style.position == 'relative') { |
michael@0 | 1017 | element.style.position = element.style.top = element.style.left = element.style.bottom = element.style.right = ''; |
michael@0 | 1018 | } |
michael@0 | 1019 | }, |
michael@0 | 1020 | |
michael@0 | 1021 | /** @id MochiKit.DOM.getFirstElementByTagAndClassName */ |
michael@0 | 1022 | getFirstElementByTagAndClassName: function (tagName, className, |
michael@0 | 1023 | /* optional */parent) { |
michael@0 | 1024 | var self = MochiKit.DOM; |
michael@0 | 1025 | if (typeof(tagName) == 'undefined' || tagName === null) { |
michael@0 | 1026 | tagName = '*'; |
michael@0 | 1027 | } |
michael@0 | 1028 | if (typeof(parent) == 'undefined' || parent === null) { |
michael@0 | 1029 | parent = self._document; |
michael@0 | 1030 | } |
michael@0 | 1031 | parent = self.getElement(parent); |
michael@0 | 1032 | var children = (parent.getElementsByTagName(tagName) |
michael@0 | 1033 | || self._document.all); |
michael@0 | 1034 | if (typeof(className) == 'undefined' || className === null) { |
michael@0 | 1035 | return children[0]; |
michael@0 | 1036 | } |
michael@0 | 1037 | |
michael@0 | 1038 | for (var i = 0; i < children.length; i++) { |
michael@0 | 1039 | var child = children[i]; |
michael@0 | 1040 | var classNames = child.className.split(' '); |
michael@0 | 1041 | for (var j = 0; j < classNames.length; j++) { |
michael@0 | 1042 | if (classNames[j] == className) { |
michael@0 | 1043 | return child; |
michael@0 | 1044 | } |
michael@0 | 1045 | } |
michael@0 | 1046 | } |
michael@0 | 1047 | }, |
michael@0 | 1048 | |
michael@0 | 1049 | /** @id MochiKit.DOM.getFirstParentByTagAndClassName */ |
michael@0 | 1050 | getFirstParentByTagAndClassName: function (elem, tagName, className) { |
michael@0 | 1051 | var self = MochiKit.DOM; |
michael@0 | 1052 | elem = self.getElement(elem); |
michael@0 | 1053 | if (typeof(tagName) == 'undefined' || tagName === null) { |
michael@0 | 1054 | tagName = '*'; |
michael@0 | 1055 | } else { |
michael@0 | 1056 | tagName = tagName.toUpperCase(); |
michael@0 | 1057 | } |
michael@0 | 1058 | if (typeof(className) == 'undefined' || className === null) { |
michael@0 | 1059 | className = null; |
michael@0 | 1060 | } |
michael@0 | 1061 | |
michael@0 | 1062 | var classList = ''; |
michael@0 | 1063 | var curTagName = ''; |
michael@0 | 1064 | while (elem && elem.tagName) { |
michael@0 | 1065 | elem = elem.parentNode; |
michael@0 | 1066 | if (tagName == '*' && className === null) { |
michael@0 | 1067 | return elem; |
michael@0 | 1068 | } |
michael@0 | 1069 | classList = elem.className.split(' '); |
michael@0 | 1070 | curTagName = elem.tagName.toUpperCase(); |
michael@0 | 1071 | if (className === null && tagName == curTagName) { |
michael@0 | 1072 | return elem; |
michael@0 | 1073 | } else if (className !== null) { |
michael@0 | 1074 | for (var i = 0; i < classList.length; i++) { |
michael@0 | 1075 | if (tagName == '*' && classList[i] == className) { |
michael@0 | 1076 | return elem; |
michael@0 | 1077 | } else if (tagName == curTagName && classList[i] == className) { |
michael@0 | 1078 | return elem; |
michael@0 | 1079 | } |
michael@0 | 1080 | } |
michael@0 | 1081 | } |
michael@0 | 1082 | } |
michael@0 | 1083 | return elem; |
michael@0 | 1084 | }, |
michael@0 | 1085 | |
michael@0 | 1086 | /** @id MochiKit.DOM.isParent */ |
michael@0 | 1087 | isParent: function (child, element) { |
michael@0 | 1088 | if (!child.parentNode || child == element) { |
michael@0 | 1089 | return false; |
michael@0 | 1090 | } |
michael@0 | 1091 | |
michael@0 | 1092 | if (child.parentNode == element) { |
michael@0 | 1093 | return true; |
michael@0 | 1094 | } |
michael@0 | 1095 | |
michael@0 | 1096 | return MochiKit.DOM.isParent(child.parentNode, element); |
michael@0 | 1097 | }, |
michael@0 | 1098 | |
michael@0 | 1099 | __new__: function (win) { |
michael@0 | 1100 | |
michael@0 | 1101 | var m = MochiKit.Base; |
michael@0 | 1102 | if (typeof(document) != "undefined") { |
michael@0 | 1103 | this._document = document; |
michael@0 | 1104 | var kXULNSURI = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; |
michael@0 | 1105 | this._xhtml = (document.documentElement && |
michael@0 | 1106 | document.createElementNS && |
michael@0 | 1107 | document.documentElement.namespaceURI === kXULNSURI); |
michael@0 | 1108 | } else if (MochiKit.MockDOM) { |
michael@0 | 1109 | this._document = MochiKit.MockDOM.document; |
michael@0 | 1110 | } |
michael@0 | 1111 | this._window = win; |
michael@0 | 1112 | |
michael@0 | 1113 | this.domConverters = new m.AdapterRegistry(); |
michael@0 | 1114 | |
michael@0 | 1115 | var __tmpElement = this._document.createElement("span"); |
michael@0 | 1116 | var attributeArray; |
michael@0 | 1117 | if (__tmpElement && __tmpElement.attributes && |
michael@0 | 1118 | __tmpElement.attributes.length > 0) { |
michael@0 | 1119 | // for braindead browsers (IE) that insert extra junk |
michael@0 | 1120 | var filter = m.filter; |
michael@0 | 1121 | attributeArray = function (node) { |
michael@0 | 1122 | return filter(attributeArray.ignoreAttrFilter, node.attributes); |
michael@0 | 1123 | }; |
michael@0 | 1124 | attributeArray.ignoreAttr = {}; |
michael@0 | 1125 | var attrs = __tmpElement.attributes; |
michael@0 | 1126 | var ignoreAttr = attributeArray.ignoreAttr; |
michael@0 | 1127 | for (var i = 0; i < attrs.length; i++) { |
michael@0 | 1128 | var a = attrs[i]; |
michael@0 | 1129 | ignoreAttr[a.name] = a.value; |
michael@0 | 1130 | } |
michael@0 | 1131 | attributeArray.ignoreAttrFilter = function (a) { |
michael@0 | 1132 | return (attributeArray.ignoreAttr[a.name] != a.value); |
michael@0 | 1133 | }; |
michael@0 | 1134 | attributeArray.compliant = false; |
michael@0 | 1135 | attributeArray.renames = { |
michael@0 | 1136 | "class": "className", |
michael@0 | 1137 | "checked": "defaultChecked", |
michael@0 | 1138 | "usemap": "useMap", |
michael@0 | 1139 | "for": "htmlFor", |
michael@0 | 1140 | "readonly": "readOnly", |
michael@0 | 1141 | "colspan": "colSpan", |
michael@0 | 1142 | "bgcolor": "bgColor", |
michael@0 | 1143 | "cellspacing": "cellSpacing", |
michael@0 | 1144 | "cellpadding": "cellPadding" |
michael@0 | 1145 | }; |
michael@0 | 1146 | } else { |
michael@0 | 1147 | attributeArray = function (node) { |
michael@0 | 1148 | /*** |
michael@0 | 1149 | |
michael@0 | 1150 | Return an array of attributes for a given node, |
michael@0 | 1151 | filtering out attributes that don't belong for |
michael@0 | 1152 | that are inserted by "Certain Browsers". |
michael@0 | 1153 | |
michael@0 | 1154 | ***/ |
michael@0 | 1155 | return node.attributes; |
michael@0 | 1156 | }; |
michael@0 | 1157 | attributeArray.compliant = true; |
michael@0 | 1158 | attributeArray.renames = {}; |
michael@0 | 1159 | } |
michael@0 | 1160 | this.attributeArray = attributeArray; |
michael@0 | 1161 | |
michael@0 | 1162 | // FIXME: this really belongs in Base, and could probably be cleaner |
michael@0 | 1163 | var _deprecated = function(fromModule, arr) { |
michael@0 | 1164 | var modules = arr[1].split('.'); |
michael@0 | 1165 | var str = ''; |
michael@0 | 1166 | var obj = {}; |
michael@0 | 1167 | |
michael@0 | 1168 | str += 'if (!MochiKit.' + modules[1] + ') { throw new Error("'; |
michael@0 | 1169 | str += 'This function has been deprecated and depends on MochiKit.'; |
michael@0 | 1170 | str += modules[1] + '.");}'; |
michael@0 | 1171 | str += 'return MochiKit.' + modules[1] + '.' + arr[0]; |
michael@0 | 1172 | str += '.apply(this, arguments);'; |
michael@0 | 1173 | |
michael@0 | 1174 | obj[modules[2]] = new Function(str); |
michael@0 | 1175 | MochiKit.Base.update(MochiKit[fromModule], obj); |
michael@0 | 1176 | } |
michael@0 | 1177 | for (var i; i < MochiKit.DOM.DEPRECATED.length; i++) { |
michael@0 | 1178 | _deprecated('DOM', MochiKit.DOM.DEPRECATED[i]); |
michael@0 | 1179 | } |
michael@0 | 1180 | |
michael@0 | 1181 | // shorthand for createDOM syntax |
michael@0 | 1182 | var createDOMFunc = this.createDOMFunc; |
michael@0 | 1183 | /** @id MochiKit.DOM.UL */ |
michael@0 | 1184 | this.UL = createDOMFunc("ul"); |
michael@0 | 1185 | /** @id MochiKit.DOM.OL */ |
michael@0 | 1186 | this.OL = createDOMFunc("ol"); |
michael@0 | 1187 | /** @id MochiKit.DOM.LI */ |
michael@0 | 1188 | this.LI = createDOMFunc("li"); |
michael@0 | 1189 | /** @id MochiKit.DOM.TD */ |
michael@0 | 1190 | this.TD = createDOMFunc("td"); |
michael@0 | 1191 | /** @id MochiKit.DOM.TR */ |
michael@0 | 1192 | this.TR = createDOMFunc("tr"); |
michael@0 | 1193 | /** @id MochiKit.DOM.TBODY */ |
michael@0 | 1194 | this.TBODY = createDOMFunc("tbody"); |
michael@0 | 1195 | /** @id MochiKit.DOM.THEAD */ |
michael@0 | 1196 | this.THEAD = createDOMFunc("thead"); |
michael@0 | 1197 | /** @id MochiKit.DOM.TFOOT */ |
michael@0 | 1198 | this.TFOOT = createDOMFunc("tfoot"); |
michael@0 | 1199 | /** @id MochiKit.DOM.TABLE */ |
michael@0 | 1200 | this.TABLE = createDOMFunc("table"); |
michael@0 | 1201 | /** @id MochiKit.DOM.TH */ |
michael@0 | 1202 | this.TH = createDOMFunc("th"); |
michael@0 | 1203 | /** @id MochiKit.DOM.INPUT */ |
michael@0 | 1204 | this.INPUT = createDOMFunc("input"); |
michael@0 | 1205 | /** @id MochiKit.DOM.SPAN */ |
michael@0 | 1206 | this.SPAN = createDOMFunc("span"); |
michael@0 | 1207 | /** @id MochiKit.DOM.A */ |
michael@0 | 1208 | this.A = createDOMFunc("a"); |
michael@0 | 1209 | /** @id MochiKit.DOM.DIV */ |
michael@0 | 1210 | this.DIV = createDOMFunc("div"); |
michael@0 | 1211 | /** @id MochiKit.DOM.IMG */ |
michael@0 | 1212 | this.IMG = createDOMFunc("img"); |
michael@0 | 1213 | /** @id MochiKit.DOM.BUTTON */ |
michael@0 | 1214 | this.BUTTON = createDOMFunc("button"); |
michael@0 | 1215 | /** @id MochiKit.DOM.TT */ |
michael@0 | 1216 | this.TT = createDOMFunc("tt"); |
michael@0 | 1217 | /** @id MochiKit.DOM.PRE */ |
michael@0 | 1218 | this.PRE = createDOMFunc("pre"); |
michael@0 | 1219 | /** @id MochiKit.DOM.H1 */ |
michael@0 | 1220 | this.H1 = createDOMFunc("h1"); |
michael@0 | 1221 | /** @id MochiKit.DOM.H2 */ |
michael@0 | 1222 | this.H2 = createDOMFunc("h2"); |
michael@0 | 1223 | /** @id MochiKit.DOM.H3 */ |
michael@0 | 1224 | this.H3 = createDOMFunc("h3"); |
michael@0 | 1225 | /** @id MochiKit.DOM.BR */ |
michael@0 | 1226 | this.BR = createDOMFunc("br"); |
michael@0 | 1227 | /** @id MochiKit.DOM.HR */ |
michael@0 | 1228 | this.HR = createDOMFunc("hr"); |
michael@0 | 1229 | /** @id MochiKit.DOM.LABEL */ |
michael@0 | 1230 | this.LABEL = createDOMFunc("label"); |
michael@0 | 1231 | /** @id MochiKit.DOM.TEXTAREA */ |
michael@0 | 1232 | this.TEXTAREA = createDOMFunc("textarea"); |
michael@0 | 1233 | /** @id MochiKit.DOM.FORM */ |
michael@0 | 1234 | this.FORM = createDOMFunc("form"); |
michael@0 | 1235 | /** @id MochiKit.DOM.P */ |
michael@0 | 1236 | this.P = createDOMFunc("p"); |
michael@0 | 1237 | /** @id MochiKit.DOM.SELECT */ |
michael@0 | 1238 | this.SELECT = createDOMFunc("select"); |
michael@0 | 1239 | /** @id MochiKit.DOM.OPTION */ |
michael@0 | 1240 | this.OPTION = createDOMFunc("option"); |
michael@0 | 1241 | /** @id MochiKit.DOM.OPTGROUP */ |
michael@0 | 1242 | this.OPTGROUP = createDOMFunc("optgroup"); |
michael@0 | 1243 | /** @id MochiKit.DOM.LEGEND */ |
michael@0 | 1244 | this.LEGEND = createDOMFunc("legend"); |
michael@0 | 1245 | /** @id MochiKit.DOM.FIELDSET */ |
michael@0 | 1246 | this.FIELDSET = createDOMFunc("fieldset"); |
michael@0 | 1247 | /** @id MochiKit.DOM.STRONG */ |
michael@0 | 1248 | this.STRONG = createDOMFunc("strong"); |
michael@0 | 1249 | /** @id MochiKit.DOM.CANVAS */ |
michael@0 | 1250 | this.CANVAS = createDOMFunc("canvas"); |
michael@0 | 1251 | |
michael@0 | 1252 | /** @id MochiKit.DOM.$ */ |
michael@0 | 1253 | this.$ = this.getElement; |
michael@0 | 1254 | |
michael@0 | 1255 | this.EXPORT_TAGS = { |
michael@0 | 1256 | ":common": this.EXPORT, |
michael@0 | 1257 | ":all": m.concat(this.EXPORT, this.EXPORT_OK) |
michael@0 | 1258 | }; |
michael@0 | 1259 | |
michael@0 | 1260 | m.nameFunctions(this); |
michael@0 | 1261 | |
michael@0 | 1262 | } |
michael@0 | 1263 | }); |
michael@0 | 1264 | |
michael@0 | 1265 | |
michael@0 | 1266 | MochiKit.DOM.__new__(((typeof(window) == "undefined") ? this : window)); |
michael@0 | 1267 | |
michael@0 | 1268 | // |
michael@0 | 1269 | // XXX: Internet Explorer blows |
michael@0 | 1270 | // |
michael@0 | 1271 | if (MochiKit.__export__) { |
michael@0 | 1272 | withWindow = MochiKit.DOM.withWindow; |
michael@0 | 1273 | withDocument = MochiKit.DOM.withDocument; |
michael@0 | 1274 | } |
michael@0 | 1275 | |
michael@0 | 1276 | MochiKit.Base._exportSymbols(this, MochiKit.DOM); |