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