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

Side by Side Diff: third_party/WebKit/Source/platform/v8_inspector/InjectedScriptSource.js

Issue 1983423002: [DevTools] Move CommandLineAPI querySelector and querySelectorAll to native (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@pass-bind-remote-object
Patch Set: Created 4 years, 7 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) 2013 Google Inc. All rights reserved. 3 * Copyright (C) 2013 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 * @param {...} var_args 47 * @param {...} var_args
48 * @template T 48 * @template T
49 */ 49 */
50 function push(array, var_args) 50 function push(array, var_args)
51 { 51 {
52 for (var i = 1; i < arguments.length; ++i) 52 for (var i = 1; i < arguments.length; ++i)
53 array[array.length] = arguments[i]; 53 array[array.length] = arguments[i];
54 } 54 }
55 55
56 /** 56 /**
57 * @param {(!Arguments.<T>|!NodeList)} array
58 * @param {number=} index
59 * @return {!Array.<T>}
60 * @template T
61 */
62 function slice(array, index)
63 {
64 var result = [];
65 for (var i = index || 0, j = 0; i < array.length; ++i, ++j)
66 result[j] = array[i];
67 return result;
68 }
69
70 /**
71 * @param {*} obj 57 * @param {*} obj
72 * @return {string} 58 * @return {string}
73 * @suppress {uselessCode} 59 * @suppress {uselessCode}
74 */ 60 */
75 function toString(obj) 61 function toString(obj)
76 { 62 {
77 // We don't use String(obj) because String16 could be overridden. 63 // We don't use String(obj) because String16 could be overridden.
78 // Also the ("" + obj) expression may throw. 64 // Also the ("" + obj) expression may throw.
79 try { 65 try {
80 return "" + obj; 66 return "" + obj;
(...skipping 454 matching lines...) Expand 10 before | Expand all | Expand 10 after
535 }, 521 },
536 522
537 /** 523 /**
538 * @param {!Object} nativeCommandLineAPI 524 * @param {!Object} nativeCommandLineAPI
539 * @return {!Object} 525 * @return {!Object}
540 */ 526 */
541 installCommandLineAPI: function(nativeCommandLineAPI) 527 installCommandLineAPI: function(nativeCommandLineAPI)
542 { 528 {
543 // NOTE: This list contains only not native Command Line API methods. Fo r full list: V8Console. 529 // NOTE: This list contains only not native Command Line API methods. Fo r full list: V8Console.
544 // NOTE: Argument names of these methods will be printed in the console, so use pretty names! 530 // NOTE: Argument names of these methods will be printed in the console, so use pretty names!
545 var members = [ "$", "$$", "$x", "monitorEvents", "unmonitorEvents", "ge tEventListeners" ]; 531 var members = [ "monitorEvents", "unmonitorEvents", "getEventListeners" ];
546 for (var member of members) 532 for (var member of members)
547 nativeCommandLineAPI[member] = CommandLineAPIImpl[member]; 533 nativeCommandLineAPI[member] = CommandLineAPIImpl[member];
548 var functionToStringMap = new Map([ 534 var functionToStringMap = new Map([
549 ["$", "function $(selector, [startNode]) { [Command Line AP I] }"], 535 ["$", "function $(selector, [startNode]) { [Command Line AP I] }"],
550 ["$$", "function $$(selector, [startNode]) { [Command Line A PI] }"], 536 ["$$", "function $$(selector, [startNode]) { [Command Line A PI] }"],
551 ["$x", "function $x(xpath, [startNode]) { [Command Line API] }"], 537 ["$x", "function $x(xpath, [startNode]) { [Command Line API] }"],
552 ["dir", "function dir(value) { [Command Line API] }"], 538 ["dir", "function dir(value) { [Command Line API] }"],
553 ["dirxml", "function dirxml(value) { [Command Line API] }"], 539 ["dirxml", "function dirxml(value) { [Command Line API] }"],
554 ["keys", "function keys(object) { [Command Line API] }"], 540 ["keys", "function keys(object) { [Command Line API] }"],
555 ["values", "function values(object) { [Command Line API] }"], 541 ["values", "function values(object) { [Command Line API] }"],
556 ["inspect", "function inspect(object) { [Command Line API] }"], 542 ["inspect", "function inspect(object) { [Command Line API] }"],
557 ["copy", "function copy(value) { [Command Line API] }"], 543 ["copy", "function copy(value) { [Command Line API] }"],
558 ["clear", "function clear() { [Command Line API] }"], 544 ["clear", "function clear() { [Command Line API] }"],
559 ["debug", "function debug(function) { [Command Line API] }"], 545 ["debug", "function debug(function) { [Command Line API] }"],
560 ["undebug", "function undebug(function) { [Command Line API] }"], 546 ["undebug", "function undebug(function) { [Command Line API] }"],
561 ["monitor", "function monitor(function) { [Command Line API] }"], 547 ["monitor", "function monitor(function) { [Command Line API] }"],
562 ["unmonitor", "function unmonitor(function) { [Command Line API] }" ], 548 ["unmonitor", "function unmonitor(function) { [Command Line API] }" ],
563 ["table", "function table(data, [columns]) { [Command Line API] }"], 549 ["table", "function table(data, [columns]) { [Command Line API] }"],
564 ["profile", "function profile(title) { [Command Line API] }"], 550 ["profile", "function profile(title) { [Command Line API] }"],
565 ["profileEnd", "function profileEnd(title) { [Command Line API] }"], 551 ["profileEnd", "function profileEnd(title) { [Command Line API] }"],
566 ["monitorEvents", "function monitorEvents(object, [types]) { [Comm and Line API] }"], 552 ["monitorEvents", "function monitorEvents(object, [types]) { [Comm and Line API] }"],
567 ["unmonitorEvents", "function unmonitorEvents(object, [types]) { [Co mmand Line API] }"], 553 ["unmonitorEvents", "function unmonitorEvents(object, [types]) { [Co mmand Line API] }"],
568 ["getEventListeners", "function getEventListeners(node) { [Command L ine API] }"] 554 ["getEventListeners", "function getEventListeners(node) { [Command L ine API] }"]
569 ]); 555 ]);
570 for (let entry of functionToStringMap) 556 for (let func in nativeCommandLineAPI)
571 nativeCommandLineAPI[entry[0]].toString = (() => entry[1]); 557 nativeCommandLineAPI[func].toString = (() => functionToStringMap.get (func));
572 return nativeCommandLineAPI; 558 return nativeCommandLineAPI;
573 }, 559 },
574 560
575 /** 561 /**
576 * @param {*} object 562 * @param {*} object
577 * @return {boolean} 563 * @return {boolean}
578 */ 564 */
579 _isDefined: function(object) 565 _isDefined: function(object)
580 { 566 {
581 return !!object || this._isHTMLAllCollection(object); 567 return !!object || this._isHTMLAllCollection(object);
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
1084 } 1070 }
1085 return string.substr(0, maxLength) + "\u2026"; 1071 return string.substr(0, maxLength) + "\u2026";
1086 }, 1072 },
1087 1073
1088 __proto__: null 1074 __proto__: null
1089 } 1075 }
1090 1076
1091 var CommandLineAPIImpl = { __proto__: null } 1077 var CommandLineAPIImpl = { __proto__: null }
1092 1078
1093 /** 1079 /**
1094 * @param {string} selector
1095 * @param {!Node=} opt_startNode
1096 * @return {*}
1097 */
1098 CommandLineAPIImpl.$ = function (selector, opt_startNode)
1099 {
1100 if (CommandLineAPIImpl._canQuerySelectorOnNode(opt_startNode))
1101 return opt_startNode.querySelector(selector);
1102
1103 return inspectedGlobalObject.document.querySelector(selector);
1104 }
1105
1106 /**
1107 * @param {string} selector
1108 * @param {!Node=} opt_startNode
1109 * @return {*}
1110 */
1111 CommandLineAPIImpl.$$ = function (selector, opt_startNode)
1112 {
1113 if (CommandLineAPIImpl._canQuerySelectorOnNode(opt_startNode))
1114 return slice(opt_startNode.querySelectorAll(selector));
1115 return slice(inspectedGlobalObject.document.querySelectorAll(selector));
1116 }
1117
1118 /**
1119 * @param {!Node=} node
1120 * @return {boolean}
1121 */
1122 CommandLineAPIImpl._canQuerySelectorOnNode = function(node)
1123 {
1124 return !!node && InjectedScriptHost.subtype(node) === "node" && (node.nodeTy pe === Node.ELEMENT_NODE || node.nodeType === Node.DOCUMENT_NODE || node.nodeTyp e === Node.DOCUMENT_FRAGMENT_NODE);
1125 }
1126
1127 /**
1128 * @param {string} xpath
1129 * @param {!Node=} opt_startNode
1130 * @return {*}
1131 */
1132 CommandLineAPIImpl.$x = function(xpath, opt_startNode)
1133 {
1134 var doc = (opt_startNode && opt_startNode.ownerDocument) || inspectedGlobalO bject.document;
1135 var result = doc.evaluate(xpath, opt_startNode || doc, null, XPathResult.ANY _TYPE, null);
1136 switch (result.resultType) {
1137 case XPathResult.NUMBER_TYPE:
1138 return result.numberValue;
1139 case XPathResult.STRING_TYPE:
1140 return result.stringValue;
1141 case XPathResult.BOOLEAN_TYPE:
1142 return result.booleanValue;
1143 default:
1144 var nodes = [];
1145 var node;
1146 while (node = result.iterateNext())
1147 push(nodes, node);
1148 return nodes;
1149 }
1150 }
1151
1152 /**
1153 * @param {!Object} object 1080 * @param {!Object} object
1154 * @param {!Array.<string>|string=} opt_types 1081 * @param {!Array.<string>|string=} opt_types
1155 */ 1082 */
1156 CommandLineAPIImpl.monitorEvents = function(object, opt_types) 1083 CommandLineAPIImpl.monitorEvents = function(object, opt_types)
1157 { 1084 {
1158 if (!object || !object.addEventListener || !object.removeEventListener) 1085 if (!object || !object.addEventListener || !object.removeEventListener)
1159 return; 1086 return;
1160 var types = CommandLineAPIImpl._normalizeEventTypes(opt_types); 1087 var types = CommandLineAPIImpl._normalizeEventTypes(opt_types);
1161 for (var i = 0; i < types.length; ++i) { 1088 for (var i = 0; i < types.length; ++i) {
1162 object.removeEventListener(types[i], CommandLineAPIImpl._logEvent, false ); 1089 object.removeEventListener(types[i], CommandLineAPIImpl._logEvent, false );
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1257 /** 1184 /**
1258 * @param {!Event} event 1185 * @param {!Event} event
1259 */ 1186 */
1260 CommandLineAPIImpl._logEvent = function(event) 1187 CommandLineAPIImpl._logEvent = function(event)
1261 { 1188 {
1262 inspectedGlobalObject.console.log(event.type, event); 1189 inspectedGlobalObject.console.log(event.type, event);
1263 } 1190 }
1264 1191
1265 return injectedScript; 1192 return injectedScript;
1266 }) 1193 })
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698