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

Side by Side Diff: Source/devtools/front_end/ui/TextPrompt.js

Issue 727823002: [DevTools] Ban getSelection, requestAnimationFrame, cancelAnimationFrame global functions. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 1 month 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * Copyright (C) 2011 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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 clearTimeout(this._selectionTimeout); 232 clearTimeout(this._selectionTimeout);
233 233
234 this._removeSuggestionAids(); 234 this._removeSuggestionAids();
235 235
236 /** 236 /**
237 * @this {WebInspector.TextPrompt} 237 * @this {WebInspector.TextPrompt}
238 */ 238 */
239 function moveBackIfOutside() 239 function moveBackIfOutside()
240 { 240 {
241 delete this._selectionTimeout; 241 delete this._selectionTimeout;
242 if (!this.isCaretInsidePrompt() && window.getSelection().isCollapsed ) { 242 if (!this.isCaretInsidePrompt() && this._element.window().getSelecti on().isCollapsed) {
243 this.moveCaretToEndOfPrompt(); 243 this.moveCaretToEndOfPrompt();
244 this.autoCompleteSoon(); 244 this.autoCompleteSoon();
245 } 245 }
246 } 246 }
247 247
248 this._selectionTimeout = setTimeout(moveBackIfOutside.bind(this), 100); 248 this._selectionTimeout = setTimeout(moveBackIfOutside.bind(this), 100);
249 }, 249 },
250 250
251 /** 251 /**
252 * @param {boolean=} force 252 * @param {boolean=} force
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 this._completeTimeout = setTimeout(this.complete.bind(this, force), immediately ? 0 : this._autocompletionTimeout); 370 this._completeTimeout = setTimeout(this.complete.bind(this, force), immediately ? 0 : this._autocompletionTimeout);
371 }, 371 },
372 372
373 /** 373 /**
374 * @param {boolean=} force 374 * @param {boolean=} force
375 * @param {boolean=} reverse 375 * @param {boolean=} reverse
376 */ 376 */
377 complete: function(force, reverse) 377 complete: function(force, reverse)
378 { 378 {
379 this.clearAutoComplete(true); 379 this.clearAutoComplete(true);
380 var selection = window.getSelection(); 380 var selection = this._element.window().getSelection();
381 if (!selection.rangeCount) 381 if (!selection.rangeCount)
382 return; 382 return;
383 383
384 var selectionRange = selection.getRangeAt(0); 384 var selectionRange = selection.getRangeAt(0);
385 var shouldExit; 385 var shouldExit;
386 386
387 if (!force && !this.isCaretAtEndOfPrompt() && !this.isSuggestBoxVisible( )) 387 if (!force && !this.isCaretAtEndOfPrompt() && !this.isSuggestBoxVisible( ))
388 shouldExit = true; 388 shouldExit = true;
389 else if (!selection.isCollapsed) 389 else if (!selection.isCollapsed)
390 shouldExit = true; 390 shouldExit = true;
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
563 delete this.autoCompleteElement; 563 delete this.autoCompleteElement;
564 } 564 }
565 565
566 if (isIntermediateSuggestion) 566 if (isIntermediateSuggestion)
567 finalSelectionRange.setStart(completionTextNode, wordPrefixLength); 567 finalSelectionRange.setStart(completionTextNode, wordPrefixLength);
568 else 568 else
569 finalSelectionRange.setStart(completionTextNode, completionText.leng th); 569 finalSelectionRange.setStart(completionTextNode, completionText.leng th);
570 570
571 finalSelectionRange.setEnd(completionTextNode, completionText.length); 571 finalSelectionRange.setEnd(completionTextNode, completionText.length);
572 572
573 var selection = window.getSelection(); 573 var selection = this._element.window().getSelection();
574 selection.removeAllRanges(); 574 selection.removeAllRanges();
575 selection.addRange(finalSelectionRange); 575 selection.addRange(finalSelectionRange);
576 if (isIntermediateSuggestion) 576 if (isIntermediateSuggestion)
577 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemApp lied, { itemText: completionText }); 577 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemApp lied, { itemText: completionText });
578 }, 578 },
579 579
580 /** 580 /**
581 * @override 581 * @override
582 */ 582 */
583 acceptSuggestion: function() 583 acceptSuggestion: function()
(...skipping 12 matching lines...) Expand all
596 596
597 var text = this.autoCompleteElement.textContent; 597 var text = this.autoCompleteElement.textContent;
598 var textNode = createTextNode(text); 598 var textNode = createTextNode(text);
599 this.autoCompleteElement.parentNode.replaceChild(textNode, this.autoComp leteElement); 599 this.autoCompleteElement.parentNode.replaceChild(textNode, this.autoComp leteElement);
600 delete this.autoCompleteElement; 600 delete this.autoCompleteElement;
601 601
602 var finalSelectionRange = this._createRange(); 602 var finalSelectionRange = this._createRange();
603 finalSelectionRange.setStart(textNode, text.length); 603 finalSelectionRange.setStart(textNode, text.length);
604 finalSelectionRange.setEnd(textNode, text.length); 604 finalSelectionRange.setEnd(textNode, text.length);
605 605
606 var selection = window.getSelection(); 606 var selection = this._element.window().getSelection();
607 selection.removeAllRanges(); 607 selection.removeAllRanges();
608 selection.addRange(finalSelectionRange); 608 selection.addRange(finalSelectionRange);
609 609
610 if (!prefixAccepted) { 610 if (!prefixAccepted) {
611 this.hideSuggestBox(); 611 this.hideSuggestBox();
612 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemAcc epted); 612 this.dispatchEventToListeners(WebInspector.TextPrompt.Events.ItemAcc epted);
613 } else 613 } else
614 this.autoCompleteSoon(true); 614 this.autoCompleteSoon(true);
615 615
616 return true; 616 return true;
(...skipping 19 matching lines...) Expand all
636 isCaretInsidePrompt: function() 636 isCaretInsidePrompt: function()
637 { 637 {
638 return this._element.isInsertionCaretInside(); 638 return this._element.isInsertionCaretInside();
639 }, 639 },
640 640
641 /** 641 /**
642 * @return {boolean} 642 * @return {boolean}
643 */ 643 */
644 isCaretAtEndOfPrompt: function() 644 isCaretAtEndOfPrompt: function()
645 { 645 {
646 var selection = window.getSelection(); 646 var selection = this._element.window().getSelection();
647 if (!selection.rangeCount || !selection.isCollapsed) 647 if (!selection.rangeCount || !selection.isCollapsed)
648 return false; 648 return false;
649 649
650 var selectionRange = selection.getRangeAt(0); 650 var selectionRange = selection.getRangeAt(0);
651 var node = selectionRange.startContainer; 651 var node = selectionRange.startContainer;
652 if (!node.isSelfOrDescendant(this._element)) 652 if (!node.isSelfOrDescendant(this._element))
653 return false; 653 return false;
654 654
655 if (node.nodeType === Node.TEXT_NODE && selectionRange.startOffset < nod e.nodeValue.length) 655 if (node.nodeType === Node.TEXT_NODE && selectionRange.startOffset < nod e.nodeValue.length)
656 return false; 656 return false;
(...skipping 10 matching lines...) Expand all
667 } 667 }
668 668
669 return true; 669 return true;
670 }, 670 },
671 671
672 /** 672 /**
673 * @return {boolean} 673 * @return {boolean}
674 */ 674 */
675 isCaretOnFirstLine: function() 675 isCaretOnFirstLine: function()
676 { 676 {
677 var selection = window.getSelection(); 677 var selection = this._element.window().getSelection();
678 var focusNode = selection.focusNode; 678 var focusNode = selection.focusNode;
679 if (!focusNode || focusNode.nodeType !== Node.TEXT_NODE || focusNode.par entNode !== this._element) 679 if (!focusNode || focusNode.nodeType !== Node.TEXT_NODE || focusNode.par entNode !== this._element)
680 return true; 680 return true;
681 681
682 if (focusNode.textContent.substring(0, selection.focusOffset).indexOf("\ n") !== -1) 682 if (focusNode.textContent.substring(0, selection.focusOffset).indexOf("\ n") !== -1)
683 return false; 683 return false;
684 focusNode = focusNode.previousSibling; 684 focusNode = focusNode.previousSibling;
685 685
686 while (focusNode) { 686 while (focusNode) {
687 if (focusNode.nodeType !== Node.TEXT_NODE) 687 if (focusNode.nodeType !== Node.TEXT_NODE)
688 return true; 688 return true;
689 if (focusNode.textContent.indexOf("\n") !== -1) 689 if (focusNode.textContent.indexOf("\n") !== -1)
690 return false; 690 return false;
691 focusNode = focusNode.previousSibling; 691 focusNode = focusNode.previousSibling;
692 } 692 }
693 693
694 return true; 694 return true;
695 }, 695 },
696 696
697 /** 697 /**
698 * @return {boolean} 698 * @return {boolean}
699 */ 699 */
700 isCaretOnLastLine: function() 700 isCaretOnLastLine: function()
701 { 701 {
702 var selection = window.getSelection(); 702 var selection = this._element.window().getSelection();
703 var focusNode = selection.focusNode; 703 var focusNode = selection.focusNode;
704 if (!focusNode || focusNode.nodeType !== Node.TEXT_NODE || focusNode.par entNode !== this._element) 704 if (!focusNode || focusNode.nodeType !== Node.TEXT_NODE || focusNode.par entNode !== this._element)
705 return true; 705 return true;
706 706
707 if (focusNode.textContent.substring(selection.focusOffset).indexOf("\n") !== -1) 707 if (focusNode.textContent.substring(selection.focusOffset).indexOf("\n") !== -1)
708 return false; 708 return false;
709 focusNode = focusNode.nextSibling; 709 focusNode = focusNode.nextSibling;
710 710
711 while (focusNode) { 711 while (focusNode) {
712 if (focusNode.nodeType !== Node.TEXT_NODE) 712 if (focusNode.nodeType !== Node.TEXT_NODE)
713 return true; 713 return true;
714 if (focusNode.textContent.indexOf("\n") !== -1) 714 if (focusNode.textContent.indexOf("\n") !== -1)
715 return false; 715 return false;
716 focusNode = focusNode.nextSibling; 716 focusNode = focusNode.nextSibling;
717 } 717 }
718 718
719 return true; 719 return true;
720 }, 720 },
721 721
722 moveCaretToEndOfPrompt: function() 722 moveCaretToEndOfPrompt: function()
723 { 723 {
724 var selection = window.getSelection(); 724 var selection = this._element.window().getSelection();
725 var selectionRange = this._createRange(); 725 var selectionRange = this._createRange();
726 726
727 var offset = this._element.childNodes.length; 727 var offset = this._element.childNodes.length;
728 selectionRange.setStart(this._element, offset); 728 selectionRange.setStart(this._element, offset);
729 selectionRange.setEnd(this._element, offset); 729 selectionRange.setEnd(this._element, offset);
730 730
731 selection.removeAllRanges(); 731 selection.removeAllRanges();
732 selection.addRange(selectionRange); 732 selection.addRange(selectionRange);
733 }, 733 },
734 734
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 898
899 if (newText !== undefined) { 899 if (newText !== undefined) {
900 event.consume(true); 900 event.consume(true);
901 this.text = newText; 901 this.text = newText;
902 902
903 if (isPrevious) { 903 if (isPrevious) {
904 var firstNewlineIndex = this.text.indexOf("\n"); 904 var firstNewlineIndex = this.text.indexOf("\n");
905 if (firstNewlineIndex === -1) 905 if (firstNewlineIndex === -1)
906 this.moveCaretToEndOfPrompt(); 906 this.moveCaretToEndOfPrompt();
907 else { 907 else {
908 var selection = window.getSelection(); 908 var selection = this._element.window().getSelection();
909 var selectionRange = this._createRange(); 909 var selectionRange = this._createRange();
910 910
911 selectionRange.setStart(this._element.firstChild, firstNewli neIndex); 911 selectionRange.setStart(this._element.firstChild, firstNewli neIndex);
912 selectionRange.setEnd(this._element.firstChild, firstNewline Index); 912 selectionRange.setEnd(this._element.firstChild, firstNewline Index);
913 913
914 selection.removeAllRanges(); 914 selection.removeAllRanges();
915 selection.addRange(selectionRange); 915 selection.addRange(selectionRange);
916 } 916 }
917 } 917 }
918 918
919 return; 919 return;
920 } 920 }
921 921
922 WebInspector.TextPrompt.prototype.onKeyDown.apply(this, arguments); 922 WebInspector.TextPrompt.prototype.onKeyDown.apply(this, arguments);
923 }, 923 },
924 924
925 __proto__: WebInspector.TextPrompt.prototype 925 __proto__: WebInspector.TextPrompt.prototype
926 } 926 }
927 927
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698