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

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

Issue 1927703003: [DevTools] Remove bind from CommandLineAPI (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@remove-injected-script-host
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
« no previous file with comments | « no previous file | third_party/WebKit/Source/platform/v8_inspector/V8InjectedScriptHost.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 581 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 592
593 /** 593 /**
594 * @param {!Object} nativeCommandLineAPI 594 * @param {!Object} nativeCommandLineAPI
595 * @return {!Object} 595 * @return {!Object}
596 */ 596 */
597 installCommandLineAPI: function(nativeCommandLineAPI) 597 installCommandLineAPI: function(nativeCommandLineAPI)
598 { 598 {
599 // NOTE: This list contains only not native Command Line API methods. Fo r full list: V8Console. 599 // NOTE: This list contains only not native Command Line API methods. Fo r full list: V8Console.
600 // NOTE: Argument names of these methods will be printed in the console, so use pretty names! 600 // NOTE: Argument names of these methods will be printed in the console, so use pretty names!
601 var members = [ "$", "$$", "$x", "monitorEvents", "unmonitorEvents", "ge tEventListeners" ]; 601 var members = [ "$", "$$", "$x", "monitorEvents", "unmonitorEvents", "ge tEventListeners" ];
602 var commandLineAPIImpl = this._commandLineAPIImpl;
603 for (var member of members) 602 for (var member of members)
604 nativeCommandLineAPI[member] = bind(commandLineAPIImpl[member], comm andLineAPIImpl); 603 nativeCommandLineAPI[member] = CommandLineAPIImpl[member];
605 var functionToStringMap = new Map([ 604 var functionToStringMap = new Map([
606 ["$", "function $(selector, [startNode]) { [Command Line AP I] }"], 605 ["$", "function $(selector, [startNode]) { [Command Line AP I] }"],
607 ["$$", "function $$(selector, [startNode]) { [Command Line A PI] }"], 606 ["$$", "function $$(selector, [startNode]) { [Command Line A PI] }"],
608 ["$x", "function $x(xpath, [startNode]) { [Command Line API] }"], 607 ["$x", "function $x(xpath, [startNode]) { [Command Line API] }"],
609 ["dir", "function dir(value) { [Command Line API] }"], 608 ["dir", "function dir(value) { [Command Line API] }"],
610 ["dirxml", "function dirxml(value) { [Command Line API] }"], 609 ["dirxml", "function dirxml(value) { [Command Line API] }"],
611 ["keys", "function keys(object) { [Command Line API] }"], 610 ["keys", "function keys(object) { [Command Line API] }"],
612 ["values", "function values(object) { [Command Line API] }"], 611 ["values", "function values(object) { [Command Line API] }"],
613 ["inspect", "function inspect(object) { [Command Line API] }"], 612 ["inspect", "function inspect(object) { [Command Line API] }"],
614 ["copy", "function copy(value) { [Command Line API] }"], 613 ["copy", "function copy(value) { [Command Line API] }"],
(...skipping 526 matching lines...) Expand 10 before | Expand all | Expand 10 after
1141 var leftHalf = maxLength >> 1; 1140 var leftHalf = maxLength >> 1;
1142 var rightHalf = maxLength - leftHalf - 1; 1141 var rightHalf = maxLength - leftHalf - 1;
1143 return string.substr(0, leftHalf) + "\u2026" + string.substr(string. length - rightHalf, rightHalf); 1142 return string.substr(0, leftHalf) + "\u2026" + string.substr(string. length - rightHalf, rightHalf);
1144 } 1143 }
1145 return string.substr(0, maxLength) + "\u2026"; 1144 return string.substr(0, maxLength) + "\u2026";
1146 }, 1145 },
1147 1146
1148 __proto__: null 1147 __proto__: null
1149 } 1148 }
1150 1149
1150 var CommandLineAPIImpl = { __proto__: null }
1151
1151 /** 1152 /**
1152 * @constructor 1153 * @param {string} selector
1154 * @param {!Node=} opt_startNode
1155 * @return {*}
1153 */ 1156 */
1154 function CommandLineAPIImpl() 1157 CommandLineAPIImpl.$ = function (selector, opt_startNode)
1155 { 1158 {
1159 if (CommandLineAPIImpl._canQuerySelectorOnNode(opt_startNode))
1160 return opt_startNode.querySelector(selector);
1161
1162 return inspectedGlobalObject.document.querySelector(selector);
1156 } 1163 }
1157 1164
1158 CommandLineAPIImpl.prototype = { 1165 /**
1159 /** 1166 * @param {string} selector
1160 * @param {string} selector 1167 * @param {!Node=} opt_startNode
1161 * @param {!Node=} opt_startNode 1168 * @return {*}
1162 * @return {*} 1169 */
1163 */ 1170 CommandLineAPIImpl.$$ = function (selector, opt_startNode)
1164 $: function (selector, opt_startNode) 1171 {
1172 if (CommandLineAPIImpl._canQuerySelectorOnNode(opt_startNode))
1173 return slice(opt_startNode.querySelectorAll(selector));
1174 return slice(inspectedGlobalObject.document.querySelectorAll(selector));
1175 }
1176
1177 /**
1178 * @param {!Node=} node
1179 * @return {boolean}
1180 */
1181 CommandLineAPIImpl._canQuerySelectorOnNode = function(node)
1182 {
1183 return !!node && InjectedScriptHost.subtype(node) === "node" && (node.nodeTy pe === Node.ELEMENT_NODE || node.nodeType === Node.DOCUMENT_NODE || node.nodeTyp e === Node.DOCUMENT_FRAGMENT_NODE);
1184 }
1185
1186 /**
1187 * @param {string} xpath
1188 * @param {!Node=} opt_startNode
1189 * @return {*}
1190 */
1191 CommandLineAPIImpl.$x = function(xpath, opt_startNode)
1192 {
1193 var doc = (opt_startNode && opt_startNode.ownerDocument) || inspectedGlobalO bject.document;
1194 var result = doc.evaluate(xpath, opt_startNode || doc, null, XPathResult.ANY _TYPE, null);
1195 switch (result.resultType) {
1196 case XPathResult.NUMBER_TYPE:
1197 return result.numberValue;
1198 case XPathResult.STRING_TYPE:
1199 return result.stringValue;
1200 case XPathResult.BOOLEAN_TYPE:
1201 return result.booleanValue;
1202 default:
1203 var nodes = [];
1204 var node;
1205 while (node = result.iterateNext())
1206 push(nodes, node);
1207 return nodes;
1208 }
1209 }
1210
1211 /**
1212 * @param {!Object} object
1213 * @param {!Array.<string>|string=} opt_types
1214 */
1215 CommandLineAPIImpl.monitorEvents = function(object, opt_types)
1216 {
1217 if (!object || !object.addEventListener || !object.removeEventListener)
1218 return;
1219 var types = CommandLineAPIImpl._normalizeEventTypes(opt_types);
1220 for (var i = 0; i < types.length; ++i) {
1221 object.removeEventListener(types[i], CommandLineAPIImpl._logEvent, false );
1222 object.addEventListener(types[i], CommandLineAPIImpl._logEvent, false);
1223 }
1224 }
1225
1226 /**
1227 * @param {!Object} object
1228 * @param {!Array.<string>|string=} opt_types
1229 */
1230 CommandLineAPIImpl.unmonitorEvents = function(object, opt_types)
1231 {
1232 if (!object || !object.addEventListener || !object.removeEventListener)
1233 return;
1234 var types = CommandLineAPIImpl._normalizeEventTypes(opt_types);
1235 for (var i = 0; i < types.length; ++i)
1236 object.removeEventListener(types[i], CommandLineAPIImpl._logEvent, false );
1237 }
1238
1239 /**
1240 * @param {!Node} node
1241 * @return {!Object|undefined}
1242 */
1243 CommandLineAPIImpl.getEventListeners = function(node)
1244 {
1245 var result = nullifyObjectProto(InjectedScriptHost.getEventListeners(node));
1246 if (!result)
1247 return;
1248
1249 // TODO(dtapuska): Remove this one closure compiler is updated
1250 // to handle EventListenerOptions and passive event listeners
1251 // has shipped. Don't JSDoc these otherwise it will fail.
1252 // @param {boolean} capture
1253 // @param {boolean} passive
1254 // @return {boolean|undefined|{capture: (boolean|undefined), passive: boolea n}}
1255 function eventListenerOptions(capture, passive)
1165 { 1256 {
1166 if (this._canQuerySelectorOnNode(opt_startNode)) 1257 return {"capture": capture, "passive": passive};
1167 return opt_startNode.querySelector(selector); 1258 }
1168
1169 return inspectedGlobalObject.document.querySelector(selector);
1170 },
1171
1172 /**
1173 * @param {string} selector
1174 * @param {!Node=} opt_startNode
1175 * @return {*}
1176 */
1177 $$: function (selector, opt_startNode)
1178 {
1179 if (this._canQuerySelectorOnNode(opt_startNode))
1180 return slice(opt_startNode.querySelectorAll(selector));
1181 return slice(inspectedGlobalObject.document.querySelectorAll(selector));
1182 },
1183
1184 /**
1185 * @param {!Node=} node
1186 * @return {boolean}
1187 */
1188 _canQuerySelectorOnNode: function(node)
1189 {
1190 return !!node && InjectedScriptHost.subtype(node) === "node" && (node.no deType === Node.ELEMENT_NODE || node.nodeType === Node.DOCUMENT_NODE || node.nod eType === Node.DOCUMENT_FRAGMENT_NODE);
1191 },
1192
1193 /**
1194 * @param {string} xpath
1195 * @param {!Node=} opt_startNode
1196 * @return {*}
1197 */
1198 $x: function(xpath, opt_startNode)
1199 {
1200 var doc = (opt_startNode && opt_startNode.ownerDocument) || inspectedGlo balObject.document;
1201 var result = doc.evaluate(xpath, opt_startNode || doc, null, XPathResult .ANY_TYPE, null);
1202 switch (result.resultType) {
1203 case XPathResult.NUMBER_TYPE:
1204 return result.numberValue;
1205 case XPathResult.STRING_TYPE:
1206 return result.stringValue;
1207 case XPathResult.BOOLEAN_TYPE:
1208 return result.booleanValue;
1209 default:
1210 var nodes = [];
1211 var node;
1212 while (node = result.iterateNext())
1213 push(nodes, node);
1214 return nodes;
1215 }
1216 },
1217
1218 /**
1219 * @param {!Object} object
1220 * @param {!Array.<string>|string=} opt_types
1221 */
1222 monitorEvents: function(object, opt_types)
1223 {
1224 if (!object || !object.addEventListener || !object.removeEventListener)
1225 return;
1226 var types = this._normalizeEventTypes(opt_types);
1227 for (var i = 0; i < types.length; ++i) {
1228 object.removeEventListener(types[i], this._logEvent, false);
1229 object.addEventListener(types[i], this._logEvent, false);
1230 }
1231 },
1232
1233 /**
1234 * @param {!Object} object
1235 * @param {!Array.<string>|string=} opt_types
1236 */
1237 unmonitorEvents: function(object, opt_types)
1238 {
1239 if (!object || !object.addEventListener || !object.removeEventListener)
1240 return;
1241 var types = this._normalizeEventTypes(opt_types);
1242 for (var i = 0; i < types.length; ++i)
1243 object.removeEventListener(types[i], this._logEvent, false);
1244 },
1245 1259
1246 /** 1260 /**
1247 * @param {!Node} node 1261 * @param {!Node} node
1248 * @return {!Object|undefined} 1262 * @param {string} type
1263 * @param {function()} listener
1264 * @param {boolean} capture
1265 * @param {boolean} passive
1249 */ 1266 */
1250 getEventListeners: function(node) 1267 function removeEventListenerWrapper(node, type, listener, capture, passive)
1251 { 1268 {
1252 var result = nullifyObjectProto(InjectedScriptHost.getEventListeners(nod e)); 1269 node.removeEventListener(type, listener, eventListenerOptions(capture, p assive));
1253 if (!result) 1270 }
1254 return;
1255 1271
1256 // TODO(dtapuska): Remove this one closure compiler is updated 1272 /** @this {{type: string, listener: function(), useCapture: boolean, passive : boolean}} */
1257 // to handle EventListenerOptions and passive event listeners 1273 var removeFunc = function()
1258 // has shipped. Don't JSDoc these otherwise it will fail. 1274 {
1259 // @param {boolean} capture 1275 removeEventListenerWrapper(node, this.type, this.listener, this.useCaptu re, this.passive);
1260 // @param {boolean} passive 1276 }
1261 // @return {boolean|undefined|{capture: (boolean|undefined), passive: bo olean}} 1277 for (var type in result) {
1262 function eventListenerOptions(capture, passive) 1278 var listeners = result[type];
1263 { 1279 for (var i = 0, listener; listener = listeners[i]; ++i) {
1264 return {"capture": capture, "passive": passive}; 1280 listener["type"] = type;
1281 listener["remove"] = removeFunc;
1265 } 1282 }
1266
1267 /**
1268 * @param {!Node} node
1269 * @param {string} type
1270 * @param {function()} listener
1271 * @param {boolean} capture
1272 * @param {boolean} passive
1273 */
1274 function removeEventListenerWrapper(node, type, listener, capture, passi ve)
1275 {
1276 node.removeEventListener(type, listener, eventListenerOptions(captur e, passive));
1277 }
1278
1279 /** @this {{type: string, listener: function(), useCapture: boolean, pas sive: boolean}} */
1280 var removeFunc = function()
1281 {
1282 removeEventListenerWrapper(node, this.type, this.listener, this.useC apture, this.passive);
1283 }
1284 for (var type in result) {
1285 var listeners = result[type];
1286 for (var i = 0, listener; listener = listeners[i]; ++i) {
1287 listener["type"] = type;
1288 listener["remove"] = removeFunc;
1289 }
1290 }
1291 return result;
1292 },
1293
1294 /**
1295 * @param {!Array.<string>|string=} types
1296 * @return {!Array.<string>}
1297 */
1298 _normalizeEventTypes: function(types)
1299 {
1300 if (typeof types === "undefined")
1301 types = ["mouse", "key", "touch", "pointer", "control", "load", "unl oad", "abort", "error", "select", "input", "change", "submit", "reset", "focus", "blur", "resize", "scroll", "search", "devicemotion", "deviceorientation"];
1302 else if (typeof types === "string")
1303 types = [types];
1304
1305 var result = [];
1306 for (var i = 0; i < types.length; ++i) {
1307 if (types[i] === "mouse")
1308 push(result, "click", "dblclick", "mousedown", "mouseeenter", "m ouseleave", "mousemove", "mouseout", "mouseover", "mouseup", "mouseleave", "mous ewheel");
1309 else if (types[i] === "key")
1310 push(result, "keydown", "keyup", "keypress", "textInput");
1311 else if (types[i] === "touch")
1312 push(result, "touchstart", "touchmove", "touchend", "touchcancel ");
1313 else if (types[i] === "pointer")
1314 push(result, "pointerover", "pointerout", "pointerenter", "point erleave", "pointerdown", "pointerup", "pointermove", "pointercancel", "gotpointe rcapture", "lostpointercapture");
1315 else if (types[i] === "control")
1316 push(result, "resize", "scroll", "zoom", "focus", "blur", "selec t", "input", "change", "submit", "reset");
1317 else
1318 push(result, types[i]);
1319 }
1320 return result;
1321 },
1322
1323 /**
1324 * @param {!Event} event
1325 */
1326 _logEvent: function(event)
1327 {
1328 inspectedGlobalObject.console.log(event.type, event);
1329 } 1283 }
1284 return result;
1330 } 1285 }
1331 1286
1332 injectedScript._commandLineAPIImpl = new CommandLineAPIImpl(); 1287 /**
1288 * @param {!Array.<string>|string=} types
1289 * @return {!Array.<string>}
1290 */
1291 CommandLineAPIImpl._normalizeEventTypes = function(types)
1292 {
1293 if (typeof types === "undefined")
1294 types = ["mouse", "key", "touch", "pointer", "control", "load", "unload" , "abort", "error", "select", "input", "change", "submit", "reset", "focus", "bl ur", "resize", "scroll", "search", "devicemotion", "deviceorientation"];
1295 else if (typeof types === "string")
1296 types = [types];
1297
1298 var result = [];
1299 for (var i = 0; i < types.length; ++i) {
1300 if (types[i] === "mouse")
1301 push(result, "click", "dblclick", "mousedown", "mouseeenter", "mouse leave", "mousemove", "mouseout", "mouseover", "mouseup", "mouseleave", "mousewhe el");
1302 else if (types[i] === "key")
1303 push(result, "keydown", "keyup", "keypress", "textInput");
1304 else if (types[i] === "touch")
1305 push(result, "touchstart", "touchmove", "touchend", "touchcancel");
1306 else if (types[i] === "pointer")
1307 push(result, "pointerover", "pointerout", "pointerenter", "pointerle ave", "pointerdown", "pointerup", "pointermove", "pointercancel", "gotpointercap ture", "lostpointercapture");
1308 else if (types[i] === "control")
1309 push(result, "resize", "scroll", "zoom", "focus", "blur", "select", "input", "change", "submit", "reset");
1310 else
1311 push(result, types[i]);
1312 }
1313 return result;
1314 }
1315
1316 /**
1317 * @param {!Event} event
1318 */
1319 CommandLineAPIImpl._logEvent = function(event)
1320 {
1321 inspectedGlobalObject.console.log(event.type, event);
1322 }
1323
1333 return injectedScript; 1324 return injectedScript;
1334 }) 1325 })
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/platform/v8_inspector/V8InjectedScriptHost.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698