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

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

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

Powered by Google App Engine
This is Rietveld 408576698