This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
DOMAgent.js
337 lines (306 loc) · 11.2 KB
/
DOMAgent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* Copyright (c) 2012 - present Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/**
* DOMAgent constructs and maintains a tree of {DOMNode}s that represents the
* rendered DOM tree in the remote browser. Nodes can be accessed by id or
* location (source offset). To update the DOM tree in response to a change of
* the source document (replace [from,to] with text) call
* `applyChange(from, to, text)`.
*
* The DOMAgent triggers `getDocument` once it has loaded
* the document.
*/
define(function DOMAgent(require, exports, module) {
"use strict";
var Inspector = require("LiveDevelopment/Inspector/Inspector"),
EventDispatcher = require("utils/EventDispatcher"),
EditAgent = require("LiveDevelopment/Agents/EditAgent"),
DOMNode = require("LiveDevelopment/Agents/DOMNode"),
DOMHelpers = require("LiveDevelopment/Agents/DOMHelpers");
var _load; // {$.Deferred} load promise
var _idToNode; // {nodeId -> node}
var _pendingRequests; // {integer} number of pending requests before initial loading is complete
/** Get the last node before the given location
* @param {integer} location
*/
function nodeBeforeLocation(location) {
var node;
exports.root.each(function each(n) {
if (!n.location || location < n.location) {
return true;
}
if (!node || node.location < n.location) {
node = n;
}
});
return node;
}
/** Get the element node that encloses the given location
* @param {location}
*/
function allNodesAtLocation(location) {
var nodes = [];
exports.root.each(function each(n) {
if (n.type === DOMNode.TYPE_ELEMENT && n.isAtLocation(location)) {
nodes.push(n);
}
});
return nodes;
}
/** Get the node at the given location
* @param {location}
*/
function nodeAtLocation(location) {
return exports.root.find(function each(n) {
return n.isAtLocation(location, false);
});
}
/** Find the node for the given id
* @param {DOMNode} node
*/
function nodeWithId(nodeId) {
return _idToNode[nodeId];
}
/** Update the node index
* @param {DOMNode} node
*/
function removeNode(node) {
if (node.nodeId) {
delete _idToNode[node.nodeId];
}
}
/** Update the node index
* @param {DOMNode} node
*/
function addNode(node) {
if (node.nodeId) {
_idToNode[node.nodeId] = node;
}
}
/** Request the child nodes for a node
* @param {DOMNode} node
*/
function requestChildNodes(node) {
if (_pendingRequests >= 0) {
_pendingRequests++;
}
Inspector.DOM.requestChildNodes(node.nodeId);
}
/** Eliminate the query string from a URL
* @param {string} URL
*/
function _cleanURL(url) {
var index = url.search(/[#\?]/);
if (index >= 0) {
url = url.substr(0, index);
}
return url;
}
/** Map the DOM document to the source text
* @param {string} source
*/
function _mapDocumentToSource(source) {
var node = exports.root;
DOMHelpers.eachNode(source, function each(payload) {
if (!node) {
return true;
}
if (payload.closing) {
var parent = node.findParentForNextNodeMatchingPayload(payload);
if (!parent) {
return console.warn("Matching Parent not at " + payload.sourceOffset + " (" + payload.nodeName + ")");
}
parent.closeLocation = payload.sourceOffset;
parent.closeLength = payload.sourceLength;
} else {
var next = node.findNextNodeMatchingPayload(payload);
if (!next) {
return console.warn("Skipping Source Node at " + payload.sourceOffset);
}
node = next;
node.location = payload.sourceOffset;
node.length = payload.sourceLength;
if (payload.closed) {
node.closed = payload.closed;
}
}
});
}
/** Load the source document and match it with the DOM tree*/
function _onFinishedLoadingDOM() {
var request = new XMLHttpRequest();
request.open("GET", exports.url);
request.onload = function onLoad() {
if ((request.status >= 200 && request.status < 300) ||
request.status === 304 || request.status === 0) {
_mapDocumentToSource(request.response);
_load.resolve();
} else {
var msg = "Received status " + request.status + " from XMLHttpRequest while attempting to load source file at " + exports.url;
_load.reject(msg, { message: msg });
}
};
request.onerror = function onError() {
var msg = "Could not load source file at " + exports.url;
_load.reject(msg, { message: msg });
};
request.send(null);
}
// WebInspector Event: Page.loadEventFired
function _onLoadEventFired(event, res) {
// res = {timestamp}
Inspector.DOM.getDocument(function onGetDocument(res) {
exports.trigger("getDocument", res);
// res = {root}
_idToNode = {};
_pendingRequests = 0;
exports.root = new DOMNode(exports, res.root);
});
}
// WebInspector Event: Page.frameNavigated
function _onFrameNavigated(event, res) {
// res = {frame}
if (!res.frame.parentId) {
exports.url = _cleanURL(res.frame.url);
}
}
// WebInspector Event: DOM.documentUpdated
function _onDocumentUpdated(event, res) {
// res = {}
}
// WebInspector Event: DOM.setChildNodes
function _onSetChildNodes(event, res) {
// res = {parentId, nodes}
var node = nodeWithId(res.parentId);
node.setChildrenPayload(res.nodes);
if (_pendingRequests > 0 && --_pendingRequests === 0) {
_onFinishedLoadingDOM();
}
}
// WebInspector Event: DOM.childNodeCountUpdated
function _onChildNodeCountUpdated(event, res) {
// res = {nodeId, childNodeCount}
if (res.nodeId > 0) {
Inspector.DOM.requestChildNodes(res.nodeId);
}
}
// WebInspector Event: DOM.childNodeInserted
function _onChildNodeInserted(event, res) {
// res = {parentNodeId, previousNodeId, node}
if (res.node.nodeId > 0) {
var parent = nodeWithId(res.parentNodeId);
var previousNode = nodeWithId(res.previousNodeId);
var node = new DOMNode(exports, res.node);
parent.insertChildAfter(node, previousNode);
}
}
// WebInspector Event: DOM.childNodeRemoved
function _onChildNodeRemoved(event, res) {
// res = {parentNodeId, nodeId}
if (res.nodeId > 0) {
var node = nodeWithId(res.nodeId);
node.remove();
}
}
/** Apply a change
* @param {integer} start offset of the change
* @param {integer} end offset of the change
* @param {string} change text
*/
function applyChange(from, to, text) {
var delta = from - to + text.length;
var node = nodeAtLocation(from);
// insert a text node
if (!node) {
if (!(/^\s*$/).test(text)) {
console.warn("Inserting nodes not supported.");
node = nodeBeforeLocation(from);
}
} else if (node.type === 3) {
// update a text node
var value = node.value.substr(0, from - node.location);
value += text;
value += node.value.substr(to - node.location);
node.value = value;
if (!EditAgent.isEditing) {
// only update the DOM if the change was not caused by the edit agent
Inspector.DOM.setNodeValue(node.nodeId, node.value);
}
} else {
console.warn("Changing non-text nodes not supported.");
}
// adjust the location of all nodes after the change
if (node) {
node.length += delta;
exports.root.each(function each(n) {
if (n.location > node.location) {
n.location += delta;
}
if (n.closeLocation !== undefined && n.closeLocation > node.location) {
n.closeLocation += delta;
}
});
}
}
/** Enable the domain */
function enable() {
return Inspector.DOM.enable();
}
/** Disable the domain */
function disable() {
return Inspector.DOM.disable();
}
/** Initialize the agent */
function load() {
_load = new $.Deferred();
Inspector.Page
.on("frameNavigated.DOMAgent", _onFrameNavigated)
.on("loadEventFired.DOMAgent", _onLoadEventFired);
Inspector.DOM
.on("documentUpdated.DOMAgent", _onDocumentUpdated)
.on("setChildNodes.DOMAgent", _onSetChildNodes)
.on("childNodeCountUpdated.DOMAgent", _onChildNodeCountUpdated)
.on("childNodeInserted.DOMAgent", _onChildNodeInserted)
.on("childNodeRemoved.DOMAgent", _onChildNodeRemoved);
return _load.promise();
}
/** Clean up */
function unload() {
Inspector.Page.off(".DOMAgent");
Inspector.DOM.off(".DOMAgent");
}
EventDispatcher.makeEventDispatcher(exports);
// Export private functions
exports.enable = enable;
exports.disable = disable;
exports.nodeBeforeLocation = nodeBeforeLocation;
exports.allNodesAtLocation = allNodesAtLocation;
exports.nodeAtLocation = nodeAtLocation;
exports.nodeWithId = nodeWithId;
exports.removeNode = removeNode;
exports.addNode = addNode;
exports.requestChildNodes = requestChildNodes;
exports.applyChange = applyChange;
exports.load = load;
exports.unload = unload;
});