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

Side by Side Diff: sky/framework/inspector/dom-agent.sky

Issue 687673003: Add beginnings of an inspector backend for Sky (Closed) Base URL: git@github.com:domokit/mojo.git@master
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
« no previous file with comments | « sky/framework/inspector/console-agent.sky ('k') | sky/framework/inspector/inspector.sky » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 <script>
2 function DOMAgent(delegate) {
3 this.enabled = false;
4 this.delegate_ = delegate;
5 this.nextNodeId_ = 1;
6 this.nodeToId_ = new Map();
7 this.idToNode_ = new Map();
8 }
9
10 DOMAgent.prototype.getIdForNode_ = function(node) {
11 if (this.nodeToId_.has(node))
12 return this.nodeToId_.get(node);
13 var id = this.nextNodeId_++;
14 this.nodeToId_.set(node, id);
15 this.idToNode_.set(id, node);
16 return id;
17 };
18
19 DOMAgent.prototype.serializeChildren_ = function(node) {
20 var children = [];
21 for (var child = node.firstChild; child; child = child.nextSibling) {
22 var record = this.serializeNode_(child);
23 if (record)
24 children.push(record);
25 }
26 return children;
27 };
28
29 DOMAgent.prototype.serializeAttributes_ = function(element) {
30 var attributes = [];
31 var attrs = element.attributes;
32 for (var i = 0; i < attrs.length; ++i) {
33 var attr = attrs[i];
34 attributes.push(attr.name);
35 attributes.push(attr.value);
36 }
37 return attributes;
38 };
39
40 DOMAgent.prototype.serializeNode_ = function(node) {
41 var id = this.getIdForNode_(node);
42
43 var record = {
44 nodeId: id,
45 };
46
47 var isContainer = false;
48
49 if (node instanceof Element) {
50 isContainer = true;
51 record.nodeType = 1;
52 record.nodeName = node.tagName;
53 record.localName = node.tagName;
54 record.nodeValue = "";
55 record.attributes = this.serializeAttributes_(node);
56 } else if (node instanceof Text) {
57 record.nodeType = 3;
58 record.nodeName = "#text";
59 var nodeValue = node.data;
60 if (!nodeValue.trim())
61 return null;
62 record.nodeValue = nodeValue;
63 } else if (node instanceof Comment) {
64 record.nodeType = 8;
65 record.nodeName = "#comment";
66 record.nodeValue = node.data;
67 } else if (node instanceof Document) {
68 isContainer = true;
69 record.nodeType = 9;
70 record.nodeName = "#document";
71 record.localName = "";
72 record.nodeValue = "";
73 record.documentURL = node.URL;
74 record.baseURL = node.baseURI;
75 } else if (node instanceof DocumentFragment) {
76 isContainer = true;
77 record.nodeType = 11;
78 record.nodeName = "#document-fragment";
79 record.localName = "";
80 record.nodeValue = "";
81 } else {
82 console.log("Unknown node type");
83 return null;
84 }
85
86 if (isContainer) {
87 var children = this.serializeChildren_(node);
88 if (children.length) {
89 record.childNodeCount = children.length;
90 record.children = children;
91 }
92 }
93
94 return record;
95 };
96
97 DOMAgent.prototype.enable = function() {
98 this.enabled = true;
99 this.observer_ = new MutationObserver(this.mutationCallback_.bind(this));
100 this.observer_.observe(document, {
101 childList: true,
102 attributes: true,
103 characterData: true,
104 subtree : true,
105 });
106 };
107
108 DOMAgent.prototype.getDocument = function() {
109 return {
110 root: this.serializeNode_(document),
111 };
112 };
113
114 DOMAgent.prototype.hideHighlight = function() {
115 };
116
117 DOMAgent.prototype.highlightNode = function() {
118 };
119
120 DOMAgent.prototype.mutationCallback_ = function(mutationRecords) {
121 for (var i = 0; i < mutationRecords.length; ++i) {
122 var record = mutationRecords[i];
123 var type = record.type;
124 var target = record.target;
125 var nodeId = this.getIdForNode_(target);
126 if (type == "attributes") {
127 var attributeName = record.attributeName;
128 if (target.hasAttribute(attributeName)) {
129 this.delegate_.sendMessage("DOM.attributeModified", {
130 nodeId: nodeId,
131 name: attributeName,
132 value: target.getAttribute(attributeName),
133 });
134 } else {
135 this.delegate_.sendMessage("DOM.attributeRemoved", {
136 nodeId: nodeId,
137 name: attributeName,
138 });
139 }
140 } else if (type == "characterData") {
141 this.delegate_.sendMessage("DOM.characterDataModified", {
142 nodeId: nodeId,
143 characterData: target.data,
144 });
145 } else if (type == "childList") {
146 // FIXME: If this subtree isn't expanded, we only need to send across the
147 // {"method":"DOM.childNodeCountUpdated","params":"nodeId":648,"childNodeC ount":2}
148
149 Array.prototype.forEach.call(record.removedNodes, function(node) {
150 this.delegate_.sendMessage("DOM.childNodeRemoved", {
151 parentNodeId: nodeId,
152 nodeId: this.getIdForNode_(node),
153 });
154 }.bind(this));
155
156 Array.prototype.forEach.call(record.addedNodes, function(node) {
157 var previousNodeId = node.previousSibling ? this.getIdForNode_(node.prev iousSibling) : 0;
158 this.delegate_.sendMessage("DOM.childNodeInserted", {
159 parentNodeId: nodeId,
160 previousNodeId: previousNodeId,
161 node: this.serializeNode_(node),
162 });
163 }.bind(this));
164 }
165 }
166 };
167
168 this.exports = DOMAgent;
169 </script>
OLDNEW
« no previous file with comments | « sky/framework/inspector/console-agent.sky ('k') | sky/framework/inspector/inspector.sky » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698