| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2007 Apple Inc. All rights reserved. | |
| 3 * Copyright (C) 2012 Google Inc. All rights reserved. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions | |
| 7 * are met: | |
| 8 * | |
| 9 * 1. Redistributions of source code must retain the above copyright | |
| 10 * notice, this list of conditions and the following disclaimer. | |
| 11 * 2. Redistributions in binary form must reproduce the above copyright | |
| 12 * notice, this list of conditions and the following disclaimer in the | |
| 13 * documentation and/or other materials provided with the distribution. | |
| 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of | |
| 15 * its contributors may be used to endorse or promote products derived | |
| 16 * from this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY | |
| 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY | |
| 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
| 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 * | |
| 29 * Contains diff method based on Javascript Diff Algorithm By John Resig | |
| 30 * http://ejohn.org/files/jsdiff.js (released under the MIT license). | |
| 31 */ | |
| 32 | |
| 33 /** | |
| 34 * @param {number} offset | |
| 35 * @param {string} stopCharacters | |
| 36 * @param {!Node} stayWithinNode | |
| 37 * @param {string=} direction | |
| 38 * @return {!Range} | |
| 39 */ | |
| 40 Node.prototype.rangeOfWord = function(offset, stopCharacters, stayWithinNode, di
rection) | |
| 41 { | |
| 42 var startNode; | |
| 43 var startOffset = 0; | |
| 44 var endNode; | |
| 45 var endOffset = 0; | |
| 46 | |
| 47 if (!stayWithinNode) | |
| 48 stayWithinNode = this; | |
| 49 | |
| 50 if (!direction || direction === "backward" || direction === "both") { | |
| 51 var node = this; | |
| 52 while (node) { | |
| 53 if (node === stayWithinNode) { | |
| 54 if (!startNode) | |
| 55 startNode = stayWithinNode; | |
| 56 break; | |
| 57 } | |
| 58 | |
| 59 if (node.nodeType === Node.TEXT_NODE) { | |
| 60 var start = (node === this ? (offset - 1) : (node.nodeValue.leng
th - 1)); | |
| 61 for (var i = start; i >= 0; --i) { | |
| 62 if (stopCharacters.indexOf(node.nodeValue[i]) !== -1) { | |
| 63 startNode = node; | |
| 64 startOffset = i + 1; | |
| 65 break; | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 if (startNode) | |
| 71 break; | |
| 72 | |
| 73 node = node.traversePreviousNode(stayWithinNode); | |
| 74 } | |
| 75 | |
| 76 if (!startNode) { | |
| 77 startNode = stayWithinNode; | |
| 78 startOffset = 0; | |
| 79 } | |
| 80 } else { | |
| 81 startNode = this; | |
| 82 startOffset = offset; | |
| 83 } | |
| 84 | |
| 85 if (!direction || direction === "forward" || direction === "both") { | |
| 86 node = this; | |
| 87 while (node) { | |
| 88 if (node === stayWithinNode) { | |
| 89 if (!endNode) | |
| 90 endNode = stayWithinNode; | |
| 91 break; | |
| 92 } | |
| 93 | |
| 94 if (node.nodeType === Node.TEXT_NODE) { | |
| 95 var start = (node === this ? offset : 0); | |
| 96 for (var i = start; i < node.nodeValue.length; ++i) { | |
| 97 if (stopCharacters.indexOf(node.nodeValue[i]) !== -1) { | |
| 98 endNode = node; | |
| 99 endOffset = i; | |
| 100 break; | |
| 101 } | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 if (endNode) | |
| 106 break; | |
| 107 | |
| 108 node = node.traverseNextNode(stayWithinNode); | |
| 109 } | |
| 110 | |
| 111 if (!endNode) { | |
| 112 endNode = stayWithinNode; | |
| 113 endOffset = stayWithinNode.nodeType === Node.TEXT_NODE ? stayWithinN
ode.nodeValue.length : stayWithinNode.childNodes.length; | |
| 114 } | |
| 115 } else { | |
| 116 endNode = this; | |
| 117 endOffset = offset; | |
| 118 } | |
| 119 | |
| 120 var result = this.ownerDocument.createRange(); | |
| 121 result.setStart(startNode, startOffset); | |
| 122 result.setEnd(endNode, endOffset); | |
| 123 | |
| 124 return result; | |
| 125 } | |
| 126 | |
| 127 /** | |
| 128 * @param {!Node=} stayWithin | |
| 129 * @return {?Node} | |
| 130 */ | |
| 131 Node.prototype.traverseNextTextNode = function(stayWithin) | |
| 132 { | |
| 133 var node = this.traverseNextNode(stayWithin); | |
| 134 if (!node) | |
| 135 return null; | |
| 136 | |
| 137 while (node && node.nodeType !== Node.TEXT_NODE) | |
| 138 node = node.traverseNextNode(stayWithin); | |
| 139 | |
| 140 return node; | |
| 141 } | |
| 142 | |
| 143 /** | |
| 144 * @param {number} offset | |
| 145 * @return {!{container: !Node, offset: number}} | |
| 146 */ | |
| 147 Node.prototype.rangeBoundaryForOffset = function(offset) | |
| 148 { | |
| 149 var node = this.traverseNextTextNode(this); | |
| 150 while (node && offset > node.nodeValue.length) { | |
| 151 offset -= node.nodeValue.length; | |
| 152 node = node.traverseNextTextNode(this); | |
| 153 } | |
| 154 if (!node) | |
| 155 return { container: this, offset: 0 }; | |
| 156 return { container: node, offset: offset }; | |
| 157 } | |
| 158 | |
| 159 /** | |
| 160 * @param {number|undefined} x | |
| 161 * @param {number|undefined} y | |
| 162 * @param {!Element=} relativeTo | |
| 163 */ | |
| 164 Element.prototype.positionAt = function(x, y, relativeTo) | |
| 165 { | |
| 166 var shift = {x: 0, y: 0}; | |
| 167 if (relativeTo) | |
| 168 shift = relativeTo.boxInWindow(this.ownerDocument.defaultView); | |
| 169 | |
| 170 if (typeof x === "number") | |
| 171 this.style.setProperty("left", (shift.x + x) + "px"); | |
| 172 else | |
| 173 this.style.removeProperty("left"); | |
| 174 | |
| 175 if (typeof y === "number") | |
| 176 this.style.setProperty("top", (shift.y + y) + "px"); | |
| 177 else | |
| 178 this.style.removeProperty("top"); | |
| 179 } | |
| 180 | |
| 181 /** | |
| 182 * @return {boolean} | |
| 183 */ | |
| 184 Element.prototype.isScrolledToBottom = function() | |
| 185 { | |
| 186 // This code works only for 0-width border. | |
| 187 // Both clientHeight and scrollHeight are rounded to integer values, so we t
olerate | |
| 188 // one pixel error. | |
| 189 return Math.abs(this.scrollTop + this.clientHeight - this.scrollHeight) <= 1
; | |
| 190 } | |
| 191 | |
| 192 /** | |
| 193 * @param {!Node} fromNode | |
| 194 * @param {!Node} toNode | |
| 195 */ | |
| 196 function removeSubsequentNodes(fromNode, toNode) | |
| 197 { | |
| 198 for (var node = fromNode; node && node !== toNode; ) { | |
| 199 var nodeToRemove = node; | |
| 200 node = node.nextSibling; | |
| 201 nodeToRemove.remove(); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 /** | |
| 206 * @constructor | |
| 207 * @param {!Size} minimum | |
| 208 * @param {?Size=} preferred | |
| 209 */ | |
| 210 function Constraints(minimum, preferred) | |
| 211 { | |
| 212 /** | |
| 213 * @type {!Size} | |
| 214 */ | |
| 215 this.minimum = minimum; | |
| 216 | |
| 217 /** | |
| 218 * @type {!Size} | |
| 219 */ | |
| 220 this.preferred = preferred || minimum; | |
| 221 | |
| 222 if (this.minimum.width > this.preferred.width || this.minimum.height > this.
preferred.height) | |
| 223 throw new Error("Minimum size is greater than preferred."); | |
| 224 } | |
| 225 | |
| 226 /** | |
| 227 * @param {?Constraints} constraints | |
| 228 * @return {boolean} | |
| 229 */ | |
| 230 Constraints.prototype.isEqual = function(constraints) | |
| 231 { | |
| 232 return !!constraints && this.minimum.isEqual(constraints.minimum) && this.pr
eferred.isEqual(constraints.preferred); | |
| 233 } | |
| 234 | |
| 235 /** | |
| 236 * @param {!Constraints|number} value | |
| 237 * @return {!Constraints} | |
| 238 */ | |
| 239 Constraints.prototype.widthToMax = function(value) | |
| 240 { | |
| 241 if (typeof value === "number") | |
| 242 return new Constraints(this.minimum.widthToMax(value), this.preferred.wi
dthToMax(value)); | |
| 243 return new Constraints(this.minimum.widthToMax(value.minimum), this.preferre
d.widthToMax(value.preferred)); | |
| 244 } | |
| 245 | |
| 246 /** | |
| 247 * @param {!Constraints|number} value | |
| 248 * @return {!Constraints} | |
| 249 */ | |
| 250 Constraints.prototype.addWidth = function(value) | |
| 251 { | |
| 252 if (typeof value === "number") | |
| 253 return new Constraints(this.minimum.addWidth(value), this.preferred.addW
idth(value)); | |
| 254 return new Constraints(this.minimum.addWidth(value.minimum), this.preferred.
addWidth(value.preferred)); | |
| 255 } | |
| 256 | |
| 257 /** | |
| 258 * @param {!Constraints|number} value | |
| 259 * @return {!Constraints} | |
| 260 */ | |
| 261 Constraints.prototype.heightToMax = function(value) | |
| 262 { | |
| 263 if (typeof value === "number") | |
| 264 return new Constraints(this.minimum.heightToMax(value), this.preferred.h
eightToMax(value)); | |
| 265 return new Constraints(this.minimum.heightToMax(value.minimum), this.preferr
ed.heightToMax(value.preferred)); | |
| 266 } | |
| 267 | |
| 268 /** | |
| 269 * @param {!Constraints|number} value | |
| 270 * @return {!Constraints} | |
| 271 */ | |
| 272 Constraints.prototype.addHeight = function(value) | |
| 273 { | |
| 274 if (typeof value === "number") | |
| 275 return new Constraints(this.minimum.addHeight(value), this.preferred.add
Height(value)); | |
| 276 return new Constraints(this.minimum.addHeight(value.minimum), this.preferred
.addHeight(value.preferred)); | |
| 277 } | |
| 278 | |
| 279 /** | |
| 280 * @param {?Element=} containerElement | |
| 281 * @return {!Size} | |
| 282 */ | |
| 283 Element.prototype.measurePreferredSize = function(containerElement) | |
| 284 { | |
| 285 containerElement = containerElement || this.ownerDocument.body; | |
| 286 containerElement.appendChild(this); | |
| 287 var fakingComponentRoot = WebInspector.installComponentRootStyles(this); | |
| 288 this.positionAt(0, 0); | |
| 289 var result = new Size(this.offsetWidth, this.offsetHeight); | |
| 290 this.positionAt(undefined, undefined); | |
| 291 this.remove(); | |
| 292 if (fakingComponentRoot) | |
| 293 WebInspector.uninstallComponentRootStyles(this); | |
| 294 return result; | |
| 295 } | |
| 296 | |
| 297 /** | |
| 298 * @param {!Event} event | |
| 299 * @return {boolean} | |
| 300 */ | |
| 301 Element.prototype.containsEventPoint = function(event) | |
| 302 { | |
| 303 var box = this.getBoundingClientRect(); | |
| 304 return box.left < event.x && event.x < box.right && | |
| 305 box.top < event.y && event.y < box.bottom; | |
| 306 } | |
| 307 | |
| 308 /** | |
| 309 * @param {!Array.<string>} nameArray | |
| 310 * @return {?Node} | |
| 311 */ | |
| 312 Node.prototype.enclosingNodeOrSelfWithNodeNameInArray = function(nameArray) | |
| 313 { | |
| 314 for (var node = this; node && node !== this.ownerDocument; node = node.paren
tNodeOrShadowHost()) { | |
| 315 for (var i = 0; i < nameArray.length; ++i) { | |
| 316 if (node.nodeName.toLowerCase() === nameArray[i].toLowerCase()) | |
| 317 return node; | |
| 318 } | |
| 319 } | |
| 320 return null; | |
| 321 } | |
| 322 | |
| 323 /** | |
| 324 * @param {string} nodeName | |
| 325 * @return {?Node} | |
| 326 */ | |
| 327 Node.prototype.enclosingNodeOrSelfWithNodeName = function(nodeName) | |
| 328 { | |
| 329 return this.enclosingNodeOrSelfWithNodeNameInArray([nodeName]); | |
| 330 } | |
| 331 | |
| 332 /** | |
| 333 * @param {string} className | |
| 334 * @param {!Element=} stayWithin | |
| 335 * @return {?Element} | |
| 336 */ | |
| 337 Node.prototype.enclosingNodeOrSelfWithClass = function(className, stayWithin) | |
| 338 { | |
| 339 for (var node = this; node && node !== stayWithin && node !== this.ownerDocu
ment; node = node.parentNodeOrShadowHost()) { | |
| 340 if (node.nodeType === Node.ELEMENT_NODE && node.classList.contains(class
Name)) | |
| 341 return /** @type {!Element} */ (node); | |
| 342 } | |
| 343 return null; | |
| 344 } | |
| 345 | |
| 346 /** | |
| 347 * @return {?Element} | |
| 348 */ | |
| 349 Node.prototype.parentElementOrShadowHost = function() | |
| 350 { | |
| 351 var node = this.parentNode; | |
| 352 if (!node) | |
| 353 return null; | |
| 354 if (node.nodeType === Node.ELEMENT_NODE) | |
| 355 return /** @type {!Element} */ (node); | |
| 356 if (node.nodeType === Node.DOCUMENT_FRAGMENT_NODE) | |
| 357 return /** @type {!Element} */ (node.host); | |
| 358 return null; | |
| 359 } | |
| 360 | |
| 361 /** | |
| 362 * @return {?Node} | |
| 363 */ | |
| 364 Node.prototype.parentNodeOrShadowHost = function() | |
| 365 { | |
| 366 return this.parentNode || this.host || null; | |
| 367 } | |
| 368 | |
| 369 /** | |
| 370 * @param {string} query | |
| 371 * @return {?Node} | |
| 372 */ | |
| 373 Element.prototype.query = function(query) | |
| 374 { | |
| 375 return this.ownerDocument.evaluate(query, this, null, XPathResult.FIRST_ORDE
RED_NODE_TYPE, null).singleNodeValue; | |
| 376 } | |
| 377 | |
| 378 Element.prototype.removeChildren = function() | |
| 379 { | |
| 380 if (this.firstChild) | |
| 381 this.textContent = ""; | |
| 382 } | |
| 383 | |
| 384 /** | |
| 385 * @return {boolean} | |
| 386 */ | |
| 387 Element.prototype.isInsertionCaretInside = function() | |
| 388 { | |
| 389 var selection = window.getSelection(); | |
| 390 if (!selection.rangeCount || !selection.isCollapsed) | |
| 391 return false; | |
| 392 var selectionRange = selection.getRangeAt(0); | |
| 393 return selectionRange.startContainer.isSelfOrDescendant(this); | |
| 394 } | |
| 395 | |
| 396 /** | |
| 397 * @param {string} tagName | |
| 398 * @return {!Element} | |
| 399 * @suppressGlobalPropertiesCheck | |
| 400 */ | |
| 401 function createElement(tagName) | |
| 402 { | |
| 403 return document.createElement(tagName); | |
| 404 } | |
| 405 | |
| 406 /** | |
| 407 * @param {number|string} data | |
| 408 * @return {!Text} | |
| 409 * @suppressGlobalPropertiesCheck | |
| 410 */ | |
| 411 function createTextNode(data) | |
| 412 { | |
| 413 return document.createTextNode(data); | |
| 414 } | |
| 415 | |
| 416 /** | |
| 417 * @param {string} elementName | |
| 418 * @param {string=} className | |
| 419 * @return {!Element} | |
| 420 */ | |
| 421 Document.prototype.createElementWithClass = function(elementName, className) | |
| 422 { | |
| 423 var element = this.createElement(elementName); | |
| 424 if (className) | |
| 425 element.className = className; | |
| 426 return element; | |
| 427 } | |
| 428 | |
| 429 /** | |
| 430 * @param {string} elementName | |
| 431 * @param {string=} className | |
| 432 * @return {!Element} | |
| 433 * @suppressGlobalPropertiesCheck | |
| 434 */ | |
| 435 function createElementWithClass(elementName, className) | |
| 436 { | |
| 437 return document.createElementWithClass(elementName, className); | |
| 438 } | |
| 439 | |
| 440 /** | |
| 441 * @return {!DocumentFragment} | |
| 442 * @suppressGlobalPropertiesCheck | |
| 443 */ | |
| 444 function createDocumentFragment() | |
| 445 { | |
| 446 return document.createDocumentFragment(); | |
| 447 } | |
| 448 | |
| 449 /** | |
| 450 * @param {string} elementName | |
| 451 * @param {string=} className | |
| 452 * @return {!Element} | |
| 453 */ | |
| 454 Element.prototype.createChild = function(elementName, className) | |
| 455 { | |
| 456 var element = this.ownerDocument.createElementWithClass(elementName, classNa
me); | |
| 457 this.appendChild(element); | |
| 458 return element; | |
| 459 } | |
| 460 | |
| 461 DocumentFragment.prototype.createChild = Element.prototype.createChild; | |
| 462 | |
| 463 /** | |
| 464 * @param {string} text | |
| 465 * @return {!Text} | |
| 466 */ | |
| 467 Element.prototype.createTextChild = function(text) | |
| 468 { | |
| 469 var element = this.ownerDocument.createTextNode(text); | |
| 470 this.appendChild(element); | |
| 471 return element; | |
| 472 } | |
| 473 | |
| 474 DocumentFragment.prototype.createTextChild = Element.prototype.createTextChild; | |
| 475 | |
| 476 /** | |
| 477 * @param {...string} var_args | |
| 478 */ | |
| 479 Element.prototype.createTextChildren = function(var_args) | |
| 480 { | |
| 481 for (var i = 0, n = arguments.length; i < n; ++i) | |
| 482 this.createTextChild(arguments[i]); | |
| 483 } | |
| 484 | |
| 485 DocumentFragment.prototype.createTextChildren = Element.prototype.createTextChil
dren; | |
| 486 | |
| 487 /** | |
| 488 * @param {...!Element} var_args | |
| 489 */ | |
| 490 Element.prototype.appendChildren = function(var_args) | |
| 491 { | |
| 492 for (var i = 0, n = arguments.length; i < n; ++i) | |
| 493 this.appendChild(arguments[i]); | |
| 494 } | |
| 495 | |
| 496 /** | |
| 497 * @return {number} | |
| 498 */ | |
| 499 Element.prototype.totalOffsetLeft = function() | |
| 500 { | |
| 501 return this.totalOffset().left; | |
| 502 } | |
| 503 | |
| 504 /** | |
| 505 * @return {number} | |
| 506 */ | |
| 507 Element.prototype.totalOffsetTop = function() | |
| 508 { | |
| 509 return this.totalOffset().top; | |
| 510 } | |
| 511 | |
| 512 /** | |
| 513 * @return {!{left: number, top: number}} | |
| 514 */ | |
| 515 Element.prototype.totalOffset = function() | |
| 516 { | |
| 517 var rect = this.getBoundingClientRect(); | |
| 518 return { left: rect.left, top: rect.top }; | |
| 519 } | |
| 520 | |
| 521 /** | |
| 522 * @return {!{left: number, top: number}} | |
| 523 */ | |
| 524 Element.prototype.scrollOffset = function() | |
| 525 { | |
| 526 var curLeft = 0; | |
| 527 var curTop = 0; | |
| 528 for (var element = this; element; element = element.scrollParent) { | |
| 529 curLeft += element.scrollLeft; | |
| 530 curTop += element.scrollTop; | |
| 531 } | |
| 532 return { left: curLeft, top: curTop }; | |
| 533 } | |
| 534 | |
| 535 /** | |
| 536 * @constructor | |
| 537 * @param {number=} x | |
| 538 * @param {number=} y | |
| 539 * @param {number=} width | |
| 540 * @param {number=} height | |
| 541 */ | |
| 542 function AnchorBox(x, y, width, height) | |
| 543 { | |
| 544 this.x = x || 0; | |
| 545 this.y = y || 0; | |
| 546 this.width = width || 0; | |
| 547 this.height = height || 0; | |
| 548 } | |
| 549 | |
| 550 /** | |
| 551 * @param {!AnchorBox} box | |
| 552 * @return {!AnchorBox} | |
| 553 */ | |
| 554 AnchorBox.prototype.relativeTo = function(box) | |
| 555 { | |
| 556 return new AnchorBox( | |
| 557 this.x - box.x, this.y - box.y, this.width, this.height); | |
| 558 } | |
| 559 | |
| 560 /** | |
| 561 * @param {!Element} element | |
| 562 * @return {!AnchorBox} | |
| 563 */ | |
| 564 AnchorBox.prototype.relativeToElement = function(element) | |
| 565 { | |
| 566 return this.relativeTo(element.boxInWindow(element.ownerDocument.defaultView
)); | |
| 567 } | |
| 568 | |
| 569 /** | |
| 570 * @param {?AnchorBox} anchorBox | |
| 571 * @return {boolean} | |
| 572 */ | |
| 573 AnchorBox.prototype.equals = function(anchorBox) | |
| 574 { | |
| 575 return !!anchorBox && this.x === anchorBox.x && this.y === anchorBox.y && th
is.width === anchorBox.width && this.height === anchorBox.height; | |
| 576 } | |
| 577 | |
| 578 /** | |
| 579 * @param {!Window} targetWindow | |
| 580 * @return {!AnchorBox} | |
| 581 */ | |
| 582 Element.prototype.offsetRelativeToWindow = function(targetWindow) | |
| 583 { | |
| 584 var elementOffset = new AnchorBox(); | |
| 585 var curElement = this; | |
| 586 var curWindow = this.ownerDocument.defaultView; | |
| 587 while (curWindow && curElement) { | |
| 588 elementOffset.x += curElement.totalOffsetLeft(); | |
| 589 elementOffset.y += curElement.totalOffsetTop(); | |
| 590 if (curWindow === targetWindow) | |
| 591 break; | |
| 592 | |
| 593 curElement = curWindow.frameElement; | |
| 594 curWindow = curWindow.parent; | |
| 595 } | |
| 596 | |
| 597 return elementOffset; | |
| 598 } | |
| 599 | |
| 600 /** | |
| 601 * @param {!Window=} targetWindow | |
| 602 * @return {!AnchorBox} | |
| 603 */ | |
| 604 Element.prototype.boxInWindow = function(targetWindow) | |
| 605 { | |
| 606 targetWindow = targetWindow || this.ownerDocument.defaultView; | |
| 607 | |
| 608 var anchorBox = this.offsetRelativeToWindow(window); | |
| 609 anchorBox.width = Math.min(this.offsetWidth, window.innerWidth - anchorBox.x
); | |
| 610 anchorBox.height = Math.min(this.offsetHeight, window.innerHeight - anchorBo
x.y); | |
| 611 | |
| 612 return anchorBox; | |
| 613 } | |
| 614 | |
| 615 /** | |
| 616 * @param {string} text | |
| 617 */ | |
| 618 Element.prototype.setTextAndTitle = function(text) | |
| 619 { | |
| 620 this.textContent = text; | |
| 621 this.title = text; | |
| 622 } | |
| 623 | |
| 624 KeyboardEvent.prototype.__defineGetter__("data", function() | |
| 625 { | |
| 626 // Emulate "data" attribute from DOM 3 TextInput event. | |
| 627 // See http://www.w3.org/TR/DOM-Level-3-Events/#events-Events-TextEvent-data | |
| 628 switch (this.type) { | |
| 629 case "keypress": | |
| 630 if (!this.ctrlKey && !this.metaKey) | |
| 631 return String.fromCharCode(this.charCode); | |
| 632 else | |
| 633 return ""; | |
| 634 case "keydown": | |
| 635 case "keyup": | |
| 636 if (!this.ctrlKey && !this.metaKey && !this.altKey) | |
| 637 return String.fromCharCode(this.which); | |
| 638 else | |
| 639 return ""; | |
| 640 } | |
| 641 }); | |
| 642 | |
| 643 /** | |
| 644 * @param {boolean=} preventDefault | |
| 645 */ | |
| 646 Event.prototype.consume = function(preventDefault) | |
| 647 { | |
| 648 this.stopImmediatePropagation(); | |
| 649 if (preventDefault) | |
| 650 this.preventDefault(); | |
| 651 this.handled = true; | |
| 652 } | |
| 653 | |
| 654 /** | |
| 655 * @param {number=} start | |
| 656 * @param {number=} end | |
| 657 * @return {!Text} | |
| 658 */ | |
| 659 Text.prototype.select = function(start, end) | |
| 660 { | |
| 661 start = start || 0; | |
| 662 end = end || this.textContent.length; | |
| 663 | |
| 664 if (start < 0) | |
| 665 start = end + start; | |
| 666 | |
| 667 var selection = this.ownerDocument.defaultView.getSelection(); | |
| 668 selection.removeAllRanges(); | |
| 669 var range = this.ownerDocument.createRange(); | |
| 670 range.setStart(this, start); | |
| 671 range.setEnd(this, end); | |
| 672 selection.addRange(range); | |
| 673 return this; | |
| 674 } | |
| 675 | |
| 676 /** | |
| 677 * @return {?number} | |
| 678 */ | |
| 679 Element.prototype.selectionLeftOffset = function() | |
| 680 { | |
| 681 // Calculate selection offset relative to the current element. | |
| 682 | |
| 683 var selection = window.getSelection(); | |
| 684 if (!selection.containsNode(this, true)) | |
| 685 return null; | |
| 686 | |
| 687 var leftOffset = selection.anchorOffset; | |
| 688 var node = selection.anchorNode; | |
| 689 | |
| 690 while (node !== this) { | |
| 691 while (node.previousSibling) { | |
| 692 node = node.previousSibling; | |
| 693 leftOffset += node.textContent.length; | |
| 694 } | |
| 695 node = node.parentNodeOrShadowHost(); | |
| 696 } | |
| 697 | |
| 698 return leftOffset; | |
| 699 } | |
| 700 | |
| 701 /** | |
| 702 * @return {string} | |
| 703 */ | |
| 704 Node.prototype.deepTextContent = function() | |
| 705 { | |
| 706 var node = this.traverseNextTextNode(this); | |
| 707 var result = []; | |
| 708 var nonTextTags = { "STYLE": 1, "SCRIPT": 1 }; | |
| 709 while (node) { | |
| 710 if (!nonTextTags[node.parentElement.nodeName]) | |
| 711 result.push(node.textContent); | |
| 712 node = node.traverseNextTextNode(this); | |
| 713 } | |
| 714 return result.join(""); | |
| 715 } | |
| 716 | |
| 717 /** | |
| 718 * @param {?Node} node | |
| 719 * @return {boolean} | |
| 720 */ | |
| 721 Node.prototype.isAncestor = function(node) | |
| 722 { | |
| 723 if (!node) | |
| 724 return false; | |
| 725 | |
| 726 var currentNode = node.parentNodeOrShadowHost(); | |
| 727 while (currentNode) { | |
| 728 if (this === currentNode) | |
| 729 return true; | |
| 730 currentNode = currentNode.parentNodeOrShadowHost(); | |
| 731 } | |
| 732 return false; | |
| 733 } | |
| 734 | |
| 735 /** | |
| 736 * @param {?Node} descendant | |
| 737 * @return {boolean} | |
| 738 */ | |
| 739 Node.prototype.isDescendant = function(descendant) | |
| 740 { | |
| 741 return !!descendant && descendant.isAncestor(this); | |
| 742 } | |
| 743 | |
| 744 /** | |
| 745 * @param {?Node} node | |
| 746 * @return {boolean} | |
| 747 */ | |
| 748 Node.prototype.isSelfOrAncestor = function(node) | |
| 749 { | |
| 750 return !!node && (node === this || this.isAncestor(node)); | |
| 751 } | |
| 752 | |
| 753 /** | |
| 754 * @param {?Node} node | |
| 755 * @return {boolean} | |
| 756 */ | |
| 757 Node.prototype.isSelfOrDescendant = function(node) | |
| 758 { | |
| 759 return !!node && (node === this || this.isDescendant(node)); | |
| 760 } | |
| 761 | |
| 762 /** | |
| 763 * @param {!Node=} stayWithin | |
| 764 * @return {?Node} | |
| 765 */ | |
| 766 Node.prototype.traverseNextNode = function(stayWithin) | |
| 767 { | |
| 768 if (this.firstChild) | |
| 769 return this.firstChild; | |
| 770 | |
| 771 if (this.shadowRoot) | |
| 772 return this.shadowRoot; | |
| 773 | |
| 774 if (stayWithin && this === stayWithin) | |
| 775 return null; | |
| 776 | |
| 777 var node = this.nextSibling; | |
| 778 if (node) | |
| 779 return node; | |
| 780 | |
| 781 node = this; | |
| 782 while (node && !node.nextSibling && (!stayWithin || !node.parentNodeOrShadow
Host() || node.parentNodeOrShadowHost() !== stayWithin)) | |
| 783 node = node.parentNodeOrShadowHost(); | |
| 784 if (!node) | |
| 785 return null; | |
| 786 | |
| 787 return node.nextSibling; | |
| 788 } | |
| 789 | |
| 790 /** | |
| 791 * @param {!Node=} stayWithin | |
| 792 * @return {?Node} | |
| 793 */ | |
| 794 Node.prototype.traversePreviousNode = function(stayWithin) | |
| 795 { | |
| 796 if (stayWithin && this === stayWithin) | |
| 797 return null; | |
| 798 var node = this.previousSibling; | |
| 799 while (node && node.lastChild) | |
| 800 node = node.lastChild; | |
| 801 if (node) | |
| 802 return node; | |
| 803 return this.parentNodeOrShadowHost(); | |
| 804 } | |
| 805 | |
| 806 /** | |
| 807 * @param {*} text | |
| 808 * @param {string=} placeholder | |
| 809 * @return {boolean} true if was truncated | |
| 810 */ | |
| 811 Node.prototype.setTextContentTruncatedIfNeeded = function(text, placeholder) | |
| 812 { | |
| 813 // Huge texts in the UI reduce rendering performance drastically. | |
| 814 // Moreover, Blink/WebKit uses <unsigned short> internally for storing text
content | |
| 815 // length, so texts longer than 65535 are inherently displayed incorrectly. | |
| 816 const maxTextContentLength = 65535; | |
| 817 | |
| 818 if (typeof text === "string" && text.length > maxTextContentLength) { | |
| 819 this.textContent = typeof placeholder === "string" ? placeholder : text.
trimEnd(maxTextContentLength); | |
| 820 return true; | |
| 821 } | |
| 822 | |
| 823 this.textContent = text; | |
| 824 return false; | |
| 825 } | |
| 826 | |
| 827 /** | |
| 828 * @return {?Node} | |
| 829 */ | |
| 830 Event.prototype.deepElementFromPoint = function() | |
| 831 { | |
| 832 // 1. climb to the component root. | |
| 833 var node = this.target; | |
| 834 while (node && node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE && node.nodeTyp
e !== Node.DOCUMENT_NODE) | |
| 835 node = node.parentNode; | |
| 836 | |
| 837 if (!node) | |
| 838 return null; | |
| 839 | |
| 840 // 2. Find deepest node by coordinates. | |
| 841 node = node.elementFromPoint(this.pageX, this.pageY); | |
| 842 while (node && node.shadowRoot) | |
| 843 node = node.shadowRoot.elementFromPoint(this.pageX, this.pageY); | |
| 844 return node; | |
| 845 } | |
| 846 | |
| 847 /** | |
| 848 * @param {number} x | |
| 849 * @param {number} y | |
| 850 * @return {?Node} | |
| 851 */ | |
| 852 Document.prototype.deepElementFromPoint = function(x, y) | |
| 853 { | |
| 854 var node = this.elementFromPoint(x, y); | |
| 855 while (node && node.shadowRoot) | |
| 856 node = node.shadowRoot.elementFromPoint(x, y); | |
| 857 return node; | |
| 858 } | |
| 859 | |
| 860 /** | |
| 861 * @return {boolean} | |
| 862 */ | |
| 863 function isEnterKey(event) { | |
| 864 // Check if in IME. | |
| 865 return event.keyCode !== 229 && event.keyIdentifier === "Enter"; | |
| 866 } | |
| 867 | |
| 868 function consumeEvent(e) | |
| 869 { | |
| 870 e.consume(); | |
| 871 } | |
| OLD | NEW |