Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/platform/DOMExtension.js

Issue 2393763002: [DevTools] Cleanup DOMExtension.js. (Closed)
Patch Set: review comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007 Apple Inc. All rights reserved. 2 * Copyright (C) 2007 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 8 *
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 { 174 {
175 // This code works only for 0-width border. 175 // This code works only for 0-width border.
176 // The scrollTop, clientHeight and scrollHeight are computed in double value s internally. 176 // The scrollTop, clientHeight and scrollHeight are computed in double value s internally.
177 // However, they are exposed to javascript differently, each being either ro unded (via 177 // However, they are exposed to javascript differently, each being either ro unded (via
178 // round, ceil or floor functions) or left intouch. 178 // round, ceil or floor functions) or left intouch.
179 // This adds up a total error up to 2. 179 // This adds up a total error up to 2.
180 return Math.abs(this.scrollTop + this.clientHeight - this.scrollHeight) <= 2 ; 180 return Math.abs(this.scrollTop + this.clientHeight - this.scrollHeight) <= 2 ;
181 } 181 }
182 182
183 /** 183 /**
184 * @param {!Node} fromNode
185 * @param {!Node} toNode
186 */
187 function removeSubsequentNodes(fromNode, toNode)
188 {
189 for (var node = fromNode; node && node !== toNode;) {
190 var nodeToRemove = node;
191 node = node.nextSibling;
192 nodeToRemove.remove();
193 }
194 }
195
196 /**
197 * @param {!Event} event
198 * @return {boolean}
199 */
200 Element.prototype.containsEventPoint = function(event)
201 {
202 var box = this.getBoundingClientRect();
203 return box.left < event.x && event.x < box.right &&
204 box.top < event.y && event.y < box.bottom;
205 }
206
207 /**
208 * @param {!Array.<string>} nameArray 184 * @param {!Array.<string>} nameArray
209 * @return {?Node} 185 * @return {?Node}
210 */ 186 */
211 Node.prototype.enclosingNodeOrSelfWithNodeNameInArray = function(nameArray) 187 Node.prototype.enclosingNodeOrSelfWithNodeNameInArray = function(nameArray)
212 { 188 {
213 for (var node = this; node && node !== this.ownerDocument; node = node.paren tNodeOrShadowHost()) { 189 for (var node = this; node && node !== this.ownerDocument; node = node.paren tNodeOrShadowHost()) {
214 for (var i = 0; i < nameArray.length; ++i) { 190 for (var i = 0; i < nameArray.length; ++i) {
215 if (node.nodeName.toLowerCase() === nameArray[i].toLowerCase()) 191 if (node.nodeName.toLowerCase() === nameArray[i].toLowerCase())
216 return node; 192 return node;
217 } 193 }
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 } 307 }
332 308
333 /** 309 /**
334 * @return {!Window} 310 * @return {!Window}
335 */ 311 */
336 Node.prototype.window = function() 312 Node.prototype.window = function()
337 { 313 {
338 return this.ownerDocument.defaultView; 314 return this.ownerDocument.defaultView;
339 } 315 }
340 316
341 /**
342 * @param {string} query
343 * @return {?Node}
344 */
345 Element.prototype.query = function(query)
346 {
347 return this.ownerDocument.evaluate(query, this, null, XPathResult.FIRST_ORDE RED_NODE_TYPE, null).singleNodeValue;
348 }
349
350 Element.prototype.removeChildren = function() 317 Element.prototype.removeChildren = function()
351 { 318 {
352 if (this.firstChild) 319 if (this.firstChild)
353 this.textContent = ""; 320 this.textContent = "";
354 } 321 }
355 322
356 /** 323 /**
357 * @return {boolean}
358 */
359 Element.prototype.isInsertionCaretInside = function()
360 {
361 var selection = this.getComponentSelection();
362 // @see crbug.com/602541
363 var selectionRange = selection && selection.rangeCount ? selection.getRangeA t(0) : null;
364 if (!selectionRange || !selection.isCollapsed)
365 return false;
366 return selectionRange.startContainer.isSelfOrDescendant(this);
367 }
368
369 /**
370 * @param {string} tagName 324 * @param {string} tagName
371 * @param {string=} customElementType 325 * @param {string=} customElementType
372 * @return {!Element} 326 * @return {!Element}
373 * @suppressGlobalPropertiesCheck 327 * @suppressGlobalPropertiesCheck
374 */ 328 */
375 function createElement(tagName, customElementType) 329 function createElement(tagName, customElementType)
376 { 330 {
377 return document.createElement(tagName, customElementType || ""); 331 return document.createElement(tagName, customElementType || "");
378 } 332 }
379 333
380 /** 334 /**
381 * @param {string} type
382 * @param {boolean} bubbles
383 * @param {boolean} cancelable
384 * @return {!Event}
385 * @suppressGlobalPropertiesCheck
386 */
387 function createEvent(type, bubbles, cancelable)
388 {
389 var event = document.createEvent("Event");
390 event.initEvent(type, bubbles, cancelable);
391 return event;
392 }
393
394 /**
395 * @param {number|string} data 335 * @param {number|string} data
396 * @return {!Text} 336 * @return {!Text}
397 * @suppressGlobalPropertiesCheck 337 * @suppressGlobalPropertiesCheck
398 */ 338 */
399 function createTextNode(data) 339 function createTextNode(data)
400 { 340 {
401 return document.createTextNode(data); 341 return document.createTextNode(data);
402 } 342 }
403 343
404 /** 344 /**
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 /** 458 /**
519 * @return {!{left: number, top: number}} 459 * @return {!{left: number, top: number}}
520 */ 460 */
521 Element.prototype.totalOffset = function() 461 Element.prototype.totalOffset = function()
522 { 462 {
523 var rect = this.getBoundingClientRect(); 463 var rect = this.getBoundingClientRect();
524 return { left: rect.left, top: rect.top }; 464 return { left: rect.left, top: rect.top };
525 } 465 }
526 466
527 /** 467 /**
528 * @return {!{left: number, top: number}}
529 */
530 Element.prototype.scrollOffset = function()
531 {
532 var curLeft = 0;
533 var curTop = 0;
534 for (var element = this; element; element = element.scrollParent) {
535 curLeft += element.scrollLeft;
536 curTop += element.scrollTop;
537 }
538 return { left: curLeft, top: curTop };
539 }
540
541 /**
542 * @param {string} childType 468 * @param {string} childType
543 * @param {string=} className 469 * @param {string=} className
544 * @return {!Element} 470 * @return {!Element}
545 */ 471 */
546 Element.prototype.createSVGChild = function(childType, className) 472 Element.prototype.createSVGChild = function(childType, className)
547 { 473 {
548 var child = this.ownerDocument.createSVGElement(childType, className); 474 var child = this.ownerDocument.createSVGElement(childType, className);
549 this.appendChild(child); 475 this.appendChild(child);
550 return child; 476 return child;
551 } 477 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 /** 513 /**
588 * @param {?AnchorBox} anchorBox 514 * @param {?AnchorBox} anchorBox
589 * @return {boolean} 515 * @return {boolean}
590 */ 516 */
591 AnchorBox.prototype.equals = function(anchorBox) 517 AnchorBox.prototype.equals = function(anchorBox)
592 { 518 {
593 return !!anchorBox && this.x === anchorBox.x && this.y === anchorBox.y && th is.width === anchorBox.width && this.height === anchorBox.height; 519 return !!anchorBox && this.x === anchorBox.x && this.y === anchorBox.y && th is.width === anchorBox.width && this.height === anchorBox.height;
594 } 520 }
595 521
596 /** 522 /**
597 * @param {!Window} targetWindow
598 * @return {!AnchorBox}
599 */
600 Element.prototype.offsetRelativeToWindow = function(targetWindow)
601 {
602 var elementOffset = new AnchorBox();
603 var curElement = this;
604 var curWindow = this.ownerDocument.defaultView;
605 while (curWindow && curElement) {
606 elementOffset.x += curElement.totalOffsetLeft();
607 elementOffset.y += curElement.totalOffsetTop();
608 if (curWindow === targetWindow)
609 break;
610
611 curElement = curWindow.frameElement;
612 curWindow = curWindow.parent;
613 }
614
615 return elementOffset;
616 }
617
618 /**
619 * @param {!Window=} targetWindow 523 * @param {!Window=} targetWindow
620 * @return {!AnchorBox} 524 * @return {!AnchorBox}
621 */ 525 */
622 Element.prototype.boxInWindow = function(targetWindow) 526 Element.prototype.boxInWindow = function(targetWindow)
623 { 527 {
624 targetWindow = targetWindow || this.ownerDocument.defaultView; 528 targetWindow = targetWindow || this.ownerDocument.defaultView;
625 529
626 var anchorBox = this.offsetRelativeToWindow(window); 530 var anchorBox = new AnchorBox();
627 anchorBox.width = Math.min(this.offsetWidth, window.innerWidth - anchorBox.x ); 531 var curElement = this;
628 anchorBox.height = Math.min(this.offsetHeight, window.innerHeight - anchorBo x.y); 532 var curWindow = this.ownerDocument.defaultView;
533 while (curWindow && curElement) {
534 anchorBox.x += curElement.totalOffsetLeft();
535 anchorBox.y += curElement.totalOffsetTop();
536 if (curWindow === targetWindow)
537 break;
538 curElement = curWindow.frameElement;
539 curWindow = curWindow.parent;
540 }
629 541
542 anchorBox.width = Math.min(this.offsetWidth, targetWindow.innerWidth - ancho rBox.x);
543 anchorBox.height = Math.min(this.offsetHeight, targetWindow.innerHeight - an chorBox.y);
630 return anchorBox; 544 return anchorBox;
631 } 545 }
632 546
633 /** 547 /**
634 * @param {string} text
635 */
636 Element.prototype.setTextAndTitle = function(text)
637 {
638 this.textContent = text;
639 this.title = text;
640 }
641
642 /**
643 * @param {boolean=} preventDefault 548 * @param {boolean=} preventDefault
644 */ 549 */
645 Event.prototype.consume = function(preventDefault) 550 Event.prototype.consume = function(preventDefault)
646 { 551 {
647 this.stopImmediatePropagation(); 552 this.stopImmediatePropagation();
648 if (preventDefault) 553 if (preventDefault)
649 this.preventDefault(); 554 this.preventDefault();
650 this.handled = true; 555 this.handled = true;
651 } 556 }
652 557
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
691 node = node.previousSibling; 596 node = node.previousSibling;
692 leftOffset += node.textContent.length; 597 leftOffset += node.textContent.length;
693 } 598 }
694 node = node.parentNodeOrShadowHost(); 599 node = node.parentNodeOrShadowHost();
695 } 600 }
696 601
697 return leftOffset; 602 return leftOffset;
698 } 603 }
699 604
700 /** 605 /**
701 * @this {!HTMLImageElement} element
702 * @return {!Promise<!HTMLImageElement>}
703 */
704 HTMLImageElement.prototype.completePromise = function()
705 {
706 var element = this;
707 if (element.complete)
708 return Promise.resolve(element);
709 return new Promise(promiseBody);
710
711 /**
712 * @param {function(!HTMLImageElement)} resolve
713 */
714 function promiseBody(resolve)
715 {
716 element.addEventListener("load", oncomplete);
717 element.addEventListener("error", oncomplete);
718
719 function oncomplete()
720 {
721 resolve(element);
722 }
723 }
724 }
725
726 /**
727 * @param {...!Node} var_args 606 * @param {...!Node} var_args
728 */ 607 */
729 Node.prototype.appendChildren = function(var_args) 608 Node.prototype.appendChildren = function(var_args)
730 { 609 {
731 for (var i = 0, n = arguments.length; i < n; ++i) 610 for (var i = 0, n = arguments.length; i < n; ++i)
732 this.appendChild(arguments[i]); 611 this.appendChild(arguments[i]);
733 } 612 }
734 613
735 /** 614 /**
736 * @return {string} 615 * @return {string}
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
900 /** 779 /**
901 * @return {?Node} 780 * @return {?Node}
902 */ 781 */
903 Event.prototype.deepElementFromPoint = function() 782 Event.prototype.deepElementFromPoint = function()
904 { 783 {
905 var root = this.target && this.target.getComponentRoot(); 784 var root = this.target && this.target.getComponentRoot();
906 return root ? root.deepElementFromPoint(this.pageX, this.pageY) : null; 785 return root ? root.deepElementFromPoint(this.pageX, this.pageY) : null;
907 } 786 }
908 787
909 /** 788 /**
910 * @return {?Element}
911 */
912 Event.prototype.deepActiveElement = function()
913 {
914 var document = this.target && this.target.ownerDocument;
915 return document ? document.deepActiveElement() : null;
916 }
917
918 /**
919 * @param {number} x 789 * @param {number} x
920 * @param {number} y 790 * @param {number} y
921 * @return {?Node} 791 * @return {?Node}
922 */ 792 */
923 Document.prototype.deepElementFromPoint = function(x, y) 793 Document.prototype.deepElementFromPoint = function(x, y)
924 { 794 {
925 var node = this.elementFromPoint(x, y); 795 var node = this.elementFromPoint(x, y);
926 while (node && node.shadowRoot) 796 while (node && node.shadowRoot)
927 node = node.shadowRoot.elementFromPoint(x, y); 797 node = node.shadowRoot.elementFromPoint(x, y);
928 return node; 798 return node;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
975 845
976 /** 846 /**
977 * @param {!Event} event 847 * @param {!Event} event
978 * @return {boolean} 848 * @return {boolean}
979 */ 849 */
980 function isEscKey(event) 850 function isEscKey(event)
981 { 851 {
982 return event.keyCode === 27; 852 return event.keyCode === 27;
983 } 853 }
984 854
985 function consumeEvent(e)
986 {
987 e.consume();
988 }
989
990 /** 855 /**
991 * @param {function()} callback 856 * @param {function()} callback
992 * @suppressGlobalPropertiesCheck 857 * @suppressGlobalPropertiesCheck
993 */ 858 */
994 function runOnWindowLoad(callback) 859 function runOnWindowLoad(callback)
995 { 860 {
996 /** 861 /**
997 * @suppressGlobalPropertiesCheck 862 * @suppressGlobalPropertiesCheck
998 */ 863 */
999 function windowLoaded() 864 function windowLoaded()
1000 { 865 {
1001 window.removeEventListener("DOMContentLoaded", windowLoaded, false); 866 window.removeEventListener("DOMContentLoaded", windowLoaded, false);
1002 callback(); 867 callback();
1003 } 868 }
1004 869
1005 if (document.readyState === "complete" || document.readyState === "interacti ve") 870 if (document.readyState === "complete" || document.readyState === "interacti ve")
1006 callback(); 871 callback();
1007 else 872 else
1008 window.addEventListener("DOMContentLoaded", windowLoaded, false); 873 window.addEventListener("DOMContentLoaded", windowLoaded, false);
1009 } 874 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698