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

Side by Side Diff: Source/devtools/front_end/sdk/DOMModel.js

Issue 1104163003: Devtools: [ElementsPanel] Add dom listeners in sidebars (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@move-force-state
Patch Set: Address offline comments Created 5 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) 2009, 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2009, 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2009 Joseph Pecoraro 3 * Copyright (C) 2009 Joseph Pecoraro
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 are 6 * modification, are permitted provided that the following conditions are
7 * met: 7 * met:
8 * 8 *
9 * * Redistributions of source code must retain the above copyright 9 * * 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 1058 matching lines...) Expand 10 before | Expand all | Expand 10 after
1069 this._defaultHighlighter = new WebInspector.DefaultDOMNodeHighlighter(this._ agent); 1069 this._defaultHighlighter = new WebInspector.DefaultDOMNodeHighlighter(this._ agent);
1070 this._highlighter = this._defaultHighlighter; 1070 this._highlighter = this._defaultHighlighter;
1071 1071
1072 this._agent.enable(); 1072 this._agent.enable();
1073 } 1073 }
1074 1074
1075 WebInspector.DOMModel.Events = { 1075 WebInspector.DOMModel.Events = {
1076 AttrModified: "AttrModified", 1076 AttrModified: "AttrModified",
1077 AttrRemoved: "AttrRemoved", 1077 AttrRemoved: "AttrRemoved",
1078 CharacterDataModified: "CharacterDataModified", 1078 CharacterDataModified: "CharacterDataModified",
1079 DOMMutatedThrottled: "DOMMutatedThrottled",
lushnikov 2015/05/26 18:24:30 my suggestion about renaming this into DOMMutatedT
sergeyv 2015/05/26 21:09:06 Done.
1079 NodeInserted: "NodeInserted", 1080 NodeInserted: "NodeInserted",
1080 NodeInspected: "NodeInspected", 1081 NodeInspected: "NodeInspected",
1081 NodeRemoved: "NodeRemoved", 1082 NodeRemoved: "NodeRemoved",
1082 DocumentUpdated: "DocumentUpdated", 1083 DocumentUpdated: "DocumentUpdated",
1083 ChildNodeCountUpdated: "ChildNodeCountUpdated", 1084 ChildNodeCountUpdated: "ChildNodeCountUpdated",
1084 UndoRedoRequested: "UndoRedoRequested", 1085 UndoRedoRequested: "UndoRedoRequested",
1085 UndoRedoCompleted: "UndoRedoCompleted", 1086 UndoRedoCompleted: "UndoRedoCompleted",
1086 DistributedNodesChanged: "DistributedNodesChanged", 1087 DistributedNodesChanged: "DistributedNodesChanged",
1087 ModelSuspended: "ModelSuspended", 1088 ModelSuspended: "ModelSuspended",
1088 InspectModeWillBeToggled: "InspectModeWillBeToggled" 1089 InspectModeWillBeToggled: "InspectModeWillBeToggled"
(...skipping 30 matching lines...) Expand all
1119 domModel.highlightDOMNode(0); 1120 domModel.highlightDOMNode(0);
1120 } 1121 }
1121 1122
1122 WebInspector.DOMModel.cancelSearch = function() 1123 WebInspector.DOMModel.cancelSearch = function()
1123 { 1124 {
1124 for (var domModel of WebInspector.DOMModel.instances()) 1125 for (var domModel of WebInspector.DOMModel.instances())
1125 domModel._cancelSearch(); 1126 domModel._cancelSearch();
1126 } 1127 }
1127 1128
1128 WebInspector.DOMModel.prototype = { 1129 WebInspector.DOMModel.prototype = {
1130
lushnikov 2015/05/26 18:24:30 stray line
sergeyv 2015/05/26 21:09:06 Done.
1131 _scheduleMutationEvent: function()
1132 {
1133 if (!this.hasEventListeners(WebInspector.DOMModel.Events.DOMMutatedThrot tled))
1134 return;
1135
1136 this._lastMutationId = (this._lastMutationId || 0) + 1;
1137 Promise.resolve().then(callObserve.bind(this, this._lastMutationId));
1138
1139 /**
1140 * @this {WebInspector.DOMModel}
1141 * @param {number} mutationId
1142 */
1143 function callObserve(mutationId)
1144 {
1145 if (!this.hasEventListeners(WebInspector.DOMModel.Events.DOMMutatedT hrottled) || this._lastMutationId !== mutationId)
1146 return;
1147
1148 this.dispatchEventToListeners(WebInspector.DOMModel.Events.DOMMutate dThrottled);
1149 }
1150 },
1151
1129 /** 1152 /**
1130 * @param {function(!WebInspector.DOMDocument)=} callback 1153 * @param {function(!WebInspector.DOMDocument)=} callback
1131 */ 1154 */
1132 requestDocument: function(callback) 1155 requestDocument: function(callback)
1133 { 1156 {
1134 if (this._document) { 1157 if (this._document) {
1135 if (callback) 1158 if (callback)
1136 callback(this._document); 1159 callback(this._document);
1137 return; 1160 return;
1138 } 1161 }
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
1279 * @param {string} value 1302 * @param {string} value
1280 */ 1303 */
1281 _attributeModified: function(nodeId, name, value) 1304 _attributeModified: function(nodeId, name, value)
1282 { 1305 {
1283 var node = this._idToDOMNode[nodeId]; 1306 var node = this._idToDOMNode[nodeId];
1284 if (!node) 1307 if (!node)
1285 return; 1308 return;
1286 1309
1287 node._setAttribute(name, value); 1310 node._setAttribute(name, value);
1288 this.dispatchEventToListeners(WebInspector.DOMModel.Events.AttrModified, { node: node, name: name }); 1311 this.dispatchEventToListeners(WebInspector.DOMModel.Events.AttrModified, { node: node, name: name });
1312 this._scheduleMutationEvent();
1289 }, 1313 },
1290 1314
1291 /** 1315 /**
1292 * @param {!DOMAgent.NodeId} nodeId 1316 * @param {!DOMAgent.NodeId} nodeId
1293 * @param {string} name 1317 * @param {string} name
1294 */ 1318 */
1295 _attributeRemoved: function(nodeId, name) 1319 _attributeRemoved: function(nodeId, name)
1296 { 1320 {
1297 var node = this._idToDOMNode[nodeId]; 1321 var node = this._idToDOMNode[nodeId];
1298 if (!node) 1322 if (!node)
1299 return; 1323 return;
1300 node._removeAttribute(name); 1324 node._removeAttribute(name);
1301 this.dispatchEventToListeners(WebInspector.DOMModel.Events.AttrRemoved, { node: node, name: name }); 1325 this.dispatchEventToListeners(WebInspector.DOMModel.Events.AttrRemoved, { node: node, name: name });
1326 this._scheduleMutationEvent();
1302 }, 1327 },
1303 1328
1304 /** 1329 /**
1305 * @param {!Array.<!DOMAgent.NodeId>} nodeIds 1330 * @param {!Array.<!DOMAgent.NodeId>} nodeIds
1306 */ 1331 */
1307 _inlineStyleInvalidated: function(nodeIds) 1332 _inlineStyleInvalidated: function(nodeIds)
1308 { 1333 {
1309 for (var i = 0; i < nodeIds.length; ++i) 1334 for (var i = 0; i < nodeIds.length; ++i)
1310 this._attributeLoadNodeIds[nodeIds[i]] = true; 1335 this._attributeLoadNodeIds[nodeIds[i]] = true;
1311 if ("_loadNodeAttributesTimeout" in this) 1336 if ("_loadNodeAttributesTimeout" in this)
(...skipping 10 matching lines...) Expand all
1322 * @param {!Array.<string>} attributes 1347 * @param {!Array.<string>} attributes
1323 */ 1348 */
1324 function callback(nodeId, error, attributes) 1349 function callback(nodeId, error, attributes)
1325 { 1350 {
1326 if (error) { 1351 if (error) {
1327 // We are calling _loadNodeAttributes asynchronously, it is ok i f node is not found. 1352 // We are calling _loadNodeAttributes asynchronously, it is ok i f node is not found.
1328 return; 1353 return;
1329 } 1354 }
1330 var node = this._idToDOMNode[nodeId]; 1355 var node = this._idToDOMNode[nodeId];
1331 if (node) { 1356 if (node) {
1332 if (node._setAttributesPayload(attributes)) 1357 if (node._setAttributesPayload(attributes)) {
1333 this.dispatchEventToListeners(WebInspector.DOMModel.Events.A ttrModified, { node: node, name: "style" }); 1358 this.dispatchEventToListeners(WebInspector.DOMModel.Events.A ttrModified, { node: node, name: "style" });
1359 this._scheduleMutationEvent();
1360 }
1334 } 1361 }
1335 } 1362 }
1336 1363
1337 delete this._loadNodeAttributesTimeout; 1364 delete this._loadNodeAttributesTimeout;
1338 1365
1339 for (var nodeId in this._attributeLoadNodeIds) { 1366 for (var nodeId in this._attributeLoadNodeIds) {
1340 var nodeIdAsNumber = parseInt(nodeId, 10); 1367 var nodeIdAsNumber = parseInt(nodeId, 10);
1341 this._agent.getAttributes(nodeIdAsNumber, callback.bind(this, nodeId AsNumber)); 1368 this._agent.getAttributes(nodeIdAsNumber, callback.bind(this, nodeId AsNumber));
1342 } 1369 }
1343 this._attributeLoadNodeIds = {}; 1370 this._attributeLoadNodeIds = {};
1344 }, 1371 },
1345 1372
1346 /** 1373 /**
1347 * @param {!DOMAgent.NodeId} nodeId 1374 * @param {!DOMAgent.NodeId} nodeId
1348 * @param {string} newValue 1375 * @param {string} newValue
1349 */ 1376 */
1350 _characterDataModified: function(nodeId, newValue) 1377 _characterDataModified: function(nodeId, newValue)
1351 { 1378 {
1352 var node = this._idToDOMNode[nodeId]; 1379 var node = this._idToDOMNode[nodeId];
1353 node._nodeValue = newValue; 1380 node._nodeValue = newValue;
1354 this.dispatchEventToListeners(WebInspector.DOMModel.Events.CharacterData Modified, node); 1381 this.dispatchEventToListeners(WebInspector.DOMModel.Events.CharacterData Modified, node);
1382 this._scheduleMutationEvent();
1355 }, 1383 },
1356 1384
1357 /** 1385 /**
1358 * @param {!DOMAgent.NodeId} nodeId 1386 * @param {!DOMAgent.NodeId} nodeId
1359 * @return {?WebInspector.DOMNode} 1387 * @return {?WebInspector.DOMNode}
1360 */ 1388 */
1361 nodeForId: function(nodeId) 1389 nodeForId: function(nodeId)
1362 { 1390 {
1363 return this._idToDOMNode[nodeId] || null; 1391 return this._idToDOMNode[nodeId] || null;
1364 }, 1392 },
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1409 1437
1410 /** 1438 /**
1411 * @param {!DOMAgent.NodeId} nodeId 1439 * @param {!DOMAgent.NodeId} nodeId
1412 * @param {number} newValue 1440 * @param {number} newValue
1413 */ 1441 */
1414 _childNodeCountUpdated: function(nodeId, newValue) 1442 _childNodeCountUpdated: function(nodeId, newValue)
1415 { 1443 {
1416 var node = this._idToDOMNode[nodeId]; 1444 var node = this._idToDOMNode[nodeId];
1417 node._childNodeCount = newValue; 1445 node._childNodeCount = newValue;
1418 this.dispatchEventToListeners(WebInspector.DOMModel.Events.ChildNodeCoun tUpdated, node); 1446 this.dispatchEventToListeners(WebInspector.DOMModel.Events.ChildNodeCoun tUpdated, node);
1447 this._scheduleMutationEvent();
1419 }, 1448 },
1420 1449
1421 /** 1450 /**
1422 * @param {!DOMAgent.NodeId} parentId 1451 * @param {!DOMAgent.NodeId} parentId
1423 * @param {!DOMAgent.NodeId} prevId 1452 * @param {!DOMAgent.NodeId} prevId
1424 * @param {!DOMAgent.Node} payload 1453 * @param {!DOMAgent.Node} payload
1425 */ 1454 */
1426 _childNodeInserted: function(parentId, prevId, payload) 1455 _childNodeInserted: function(parentId, prevId, payload)
1427 { 1456 {
1428 var parent = this._idToDOMNode[parentId]; 1457 var parent = this._idToDOMNode[parentId];
1429 var prev = this._idToDOMNode[prevId]; 1458 var prev = this._idToDOMNode[prevId];
1430 var node = parent._insertChild(prev, payload); 1459 var node = parent._insertChild(prev, payload);
1431 this._idToDOMNode[node.id] = node; 1460 this._idToDOMNode[node.id] = node;
1432 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeInserted, node); 1461 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeInserted, node);
1462 this._scheduleMutationEvent();
1433 }, 1463 },
1434 1464
1435 /** 1465 /**
1436 * @param {!DOMAgent.NodeId} parentId 1466 * @param {!DOMAgent.NodeId} parentId
1437 * @param {!DOMAgent.NodeId} nodeId 1467 * @param {!DOMAgent.NodeId} nodeId
1438 */ 1468 */
1439 _childNodeRemoved: function(parentId, nodeId) 1469 _childNodeRemoved: function(parentId, nodeId)
1440 { 1470 {
1441 var parent = this._idToDOMNode[parentId]; 1471 var parent = this._idToDOMNode[parentId];
1442 var node = this._idToDOMNode[nodeId]; 1472 var node = this._idToDOMNode[nodeId];
1443 parent._removeChild(node); 1473 parent._removeChild(node);
1444 this._unbind(node); 1474 this._unbind(node);
1445 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeRemoved, {node: node, parent: parent}); 1475 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeRemoved, {node: node, parent: parent});
1476 this._scheduleMutationEvent();
1446 }, 1477 },
1447 1478
1448 /** 1479 /**
1449 * @param {!DOMAgent.NodeId} hostId 1480 * @param {!DOMAgent.NodeId} hostId
1450 * @param {!DOMAgent.Node} root 1481 * @param {!DOMAgent.Node} root
1451 */ 1482 */
1452 _shadowRootPushed: function(hostId, root) 1483 _shadowRootPushed: function(hostId, root)
1453 { 1484 {
1454 var host = this._idToDOMNode[hostId]; 1485 var host = this._idToDOMNode[hostId];
1455 if (!host) 1486 if (!host)
1456 return; 1487 return;
1457 var node = new WebInspector.DOMNode(this, host.ownerDocument, true, root ); 1488 var node = new WebInspector.DOMNode(this, host.ownerDocument, true, root );
1458 node.parentNode = host; 1489 node.parentNode = host;
1459 this._idToDOMNode[node.id] = node; 1490 this._idToDOMNode[node.id] = node;
1460 host._shadowRoots.unshift(node); 1491 host._shadowRoots.unshift(node);
1461 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeInserted, node); 1492 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeInserted, node);
1493 this._scheduleMutationEvent();
1462 }, 1494 },
1463 1495
1464 /** 1496 /**
1465 * @param {!DOMAgent.NodeId} hostId 1497 * @param {!DOMAgent.NodeId} hostId
1466 * @param {!DOMAgent.NodeId} rootId 1498 * @param {!DOMAgent.NodeId} rootId
1467 */ 1499 */
1468 _shadowRootPopped: function(hostId, rootId) 1500 _shadowRootPopped: function(hostId, rootId)
1469 { 1501 {
1470 var host = this._idToDOMNode[hostId]; 1502 var host = this._idToDOMNode[hostId];
1471 if (!host) 1503 if (!host)
1472 return; 1504 return;
1473 var root = this._idToDOMNode[rootId]; 1505 var root = this._idToDOMNode[rootId];
1474 if (!root) 1506 if (!root)
1475 return; 1507 return;
1476 host._removeChild(root); 1508 host._removeChild(root);
1477 this._unbind(root); 1509 this._unbind(root);
1478 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeRemoved, {node: root, parent: host}); 1510 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeRemoved, {node: root, parent: host});
1511 this._scheduleMutationEvent();
1479 }, 1512 },
1480 1513
1481 /** 1514 /**
1482 * @param {!DOMAgent.NodeId} parentId 1515 * @param {!DOMAgent.NodeId} parentId
1483 * @param {!DOMAgent.Node} pseudoElement 1516 * @param {!DOMAgent.Node} pseudoElement
1484 */ 1517 */
1485 _pseudoElementAdded: function(parentId, pseudoElement) 1518 _pseudoElementAdded: function(parentId, pseudoElement)
1486 { 1519 {
1487 var parent = this._idToDOMNode[parentId]; 1520 var parent = this._idToDOMNode[parentId];
1488 if (!parent) 1521 if (!parent)
1489 return; 1522 return;
1490 var node = new WebInspector.DOMNode(this, parent.ownerDocument, false, p seudoElement); 1523 var node = new WebInspector.DOMNode(this, parent.ownerDocument, false, p seudoElement);
1491 node.parentNode = parent; 1524 node.parentNode = parent;
1492 this._idToDOMNode[node.id] = node; 1525 this._idToDOMNode[node.id] = node;
1493 console.assert(!parent._pseudoElements.get(node.pseudoType())); 1526 console.assert(!parent._pseudoElements.get(node.pseudoType()));
1494 parent._pseudoElements.set(node.pseudoType(), node); 1527 parent._pseudoElements.set(node.pseudoType(), node);
1495 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeInserted, node); 1528 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeInserted, node);
1529 this._scheduleMutationEvent();
1496 }, 1530 },
1497 1531
1498 /** 1532 /**
1499 * @param {!DOMAgent.NodeId} parentId 1533 * @param {!DOMAgent.NodeId} parentId
1500 * @param {!DOMAgent.NodeId} pseudoElementId 1534 * @param {!DOMAgent.NodeId} pseudoElementId
1501 */ 1535 */
1502 _pseudoElementRemoved: function(parentId, pseudoElementId) 1536 _pseudoElementRemoved: function(parentId, pseudoElementId)
1503 { 1537 {
1504 var parent = this._idToDOMNode[parentId]; 1538 var parent = this._idToDOMNode[parentId];
1505 if (!parent) 1539 if (!parent)
1506 return; 1540 return;
1507 var pseudoElement = this._idToDOMNode[pseudoElementId]; 1541 var pseudoElement = this._idToDOMNode[pseudoElementId];
1508 if (!pseudoElement) 1542 if (!pseudoElement)
1509 return; 1543 return;
1510 parent._removeChild(pseudoElement); 1544 parent._removeChild(pseudoElement);
1511 this._unbind(pseudoElement); 1545 this._unbind(pseudoElement);
1512 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeRemoved, {node: pseudoElement, parent: parent}); 1546 this.dispatchEventToListeners(WebInspector.DOMModel.Events.NodeRemoved, {node: pseudoElement, parent: parent});
1547 this._scheduleMutationEvent();
1513 }, 1548 },
1514 1549
1515 /** 1550 /**
1516 * @param {!DOMAgent.NodeId} insertionPointId 1551 * @param {!DOMAgent.NodeId} insertionPointId
1517 * @param {!Array.<!DOMAgent.BackendNode>} distributedNodes 1552 * @param {!Array.<!DOMAgent.BackendNode>} distributedNodes
1518 */ 1553 */
1519 _distributedNodesUpdated: function(insertionPointId, distributedNodes) 1554 _distributedNodesUpdated: function(insertionPointId, distributedNodes)
1520 { 1555 {
1521 var insertionPoint = this._idToDOMNode[insertionPointId]; 1556 var insertionPoint = this._idToDOMNode[insertionPointId];
1522 if (!insertionPoint) 1557 if (!insertionPoint)
1523 return; 1558 return;
1524 insertionPoint._setDistributedNodePayloads(distributedNodes); 1559 insertionPoint._setDistributedNodePayloads(distributedNodes);
1525 this.dispatchEventToListeners(WebInspector.DOMModel.Events.DistributedNo desChanged, insertionPoint); 1560 this.dispatchEventToListeners(WebInspector.DOMModel.Events.DistributedNo desChanged, insertionPoint);
1561 this._scheduleMutationEvent();
1526 }, 1562 },
1527 1563
1528 /** 1564 /**
1529 * @param {!WebInspector.DOMNode} node 1565 * @param {!WebInspector.DOMNode} node
1530 */ 1566 */
1531 _unbind: function(node) 1567 _unbind: function(node)
1532 { 1568 {
1533 delete this._idToDOMNode[node.id]; 1569 delete this._idToDOMNode[node.id];
1534 for (var i = 0; node._children && i < node._children.length; ++i) 1570 for (var i = 0; node._children && i < node._children.length; ++i)
1535 this._unbind(node._children[i]); 1571 this._unbind(node._children[i]);
(...skipping 592 matching lines...) Expand 10 before | Expand all | Expand 10 after
2128 } 2164 }
2129 2165
2130 /** 2166 /**
2131 * @param {!WebInspector.Target} target 2167 * @param {!WebInspector.Target} target
2132 * @return {?WebInspector.DOMModel} 2168 * @return {?WebInspector.DOMModel}
2133 */ 2169 */
2134 WebInspector.DOMModel.fromTarget = function(target) 2170 WebInspector.DOMModel.fromTarget = function(target)
2135 { 2171 {
2136 return /** @type {?WebInspector.DOMModel} */ (target.model(WebInspector.DOMM odel)); 2172 return /** @type {?WebInspector.DOMModel} */ (target.model(WebInspector.DOMM odel));
2137 } 2173 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698